var graphdata=new Array();
var graphloaded=false;
var graphSteps=new Array();
var graphLegend=new Array();
var historydata=new Array();
var monthdata=new Array();
defineGraph(0,0,"Months","Clicks",1500,400,1000,'0x999999',14,40,10);
function shorten() {
	url=encodeUpload(objid("thelink").value);
	loadingObject("result","Shrinking "+objid("thelink").value);
	sendPostRequest('/index.php','cmd=shorten&url='+url,receivedSectionContent);
}
function getToCopy() {
	return objid('shrunken').value;
}
function mylinks(srcObj,page,order) {
	loadingObject("mylinks","loading...");
	//srcObj.parentNode.innerHTML="Loading page...";
	sendPostRequest('/index.php','cmd=mylinks&page='+page+'&sort='+order,receivedSectionContent);
}
function getLinkDetails(lid) {
	loadingObject("result","loading...");
	//objid("result").innerHTML="Loading...";
	sendPostRequest('/index.php','cmd=getLinkDetails&linkid='+lid,receivedSectionContent);
}
function loadingObject(id,msg) {
	des=objid(id);
	w=des.clientWidth;
	h=des.clientHeight;
	if (h<50) h=50;
	ins="<table cellspacing=0 cellpadding=0><tr><td style='width:"+w+"px;height:"+h+"px'><img src='/skin/loading.gif' /><br />"+msg+"</td></tr></table>";
	des.innerHTML=ins;
}
function embedGraph(id) {
	if (objid(id+'output').style.display=="none") {
		objid(id+'output').style.display="block";
		var flashvars = { };
		var params = { };
			params.wmode="transparent";
			params.menu = "false";
			params.allowScriptAccess = "sameDomain";
			params.swliveconnect = "true";
		var attributes = {id:"graph",name:"graph"};
		swfobject.embedSWF("/skin/graph.swf",id,"500","400","9.0.0","expressInstall.swf", flashvars, params, attributes);
	}
}
function loadGraphData() {
	if (graphdata.length>1) {
		graphloaded=true;
		return graphdata;
	}else{
		graphloaded=true;
		return new Array();
	}
}
function showNewGraph() {
	if (graphloaded) {
		thegraph = getFlashMovie('graph');
		thegraph.setGraph(graphdata);
	}
}
function defineGraph(gtwist,gelevation,gxlegend,gylegend,gwidth,gdepth,gheight,gaxiscolour,gfontsize,glegendoffset,gyaxissteps) {
	graphdata[0]=[gtwist,gelevation,gxlegend,gylegend,false,gwidth,gdepth,gheight,gaxiscolour,gfontsize,glegendoffset,gyaxissteps];
}
function defineGraphData(gdataindex,gtitle,gcolour,gnegcolour,gdatapairs) {
	if (gdataindex==null) {
		graphdata.push([gtitle,gcolour,gnegcolour,gdatapairs]);
	}else{
		graphdata[gdataindex]=[gtitle,gcolour,gnegcolour,gdatapairs];
	}
}
function removeGraphData(gdataindex) {
	graphdata.splice(gdataindex,1);
}
function drawGraphButtons() {
	ins="";
	if (historydata["year"].length>0 && historydata["month"].length>0) {
		ins="Display the past: ";
		ins+="<span class='textLink' onclick='selectGraphData(historydata[\"year\"],"+graphSteps[0]+",\""+graphLegend[0]+"\")'>Year</span>";
		ins+=" / ";
		ins+="<span class='textLink' onclick='selectGraphData(historydata[\"month\"],"+graphSteps[1]+",\""+graphLegend[1]+"\")'>Month</span>";
		objid("graphcomment").innerHTML=ins;
	}
	if (historydata["year"].length>0) {
		selectGraphData(historydata["year"],graphSteps[0],graphLegend[0]);
	}else if (historydata["month"].length>0) {
		selectGraphData(historydata["month"],graphSteps[1],graphLegend[1]);
	}else{
		objid("graphoutput").style.display='none';
	}
}
function selectGraphData(ar,st,leg) {
	defineGraphData(1,"History","0x44bb44","0xbb4444",ar);
	graphdata[0][11]=st;
	graphdata[0][2]=leg;
	showNewGraph();
}
function shrunk(reply) {
	if (reply["error"]) {
		alert(reply["error"]);
	}else if (reply["url"]) {
		alert(reply["longurl"]+" has been shrunk to "+reply["url"]+" with title '"+reply["title"]+"'");
	}
}
function logoff(src) {
	src.innerHTML="<img src='/skin/loading.gif' />";
	var vars="cmd=logout";
	sendPostRequest("/index.php",vars,logoutReceived);
}
function logoutReceived(reply) {
	window.location="/";
}
var buttonType='/ext/shrink32.gif';
function updateThis() {
	url="";
	if (objid('manualURL').checked) {
		err="";
		url=objid('theURL').value;
		if (url=="http://" || url=="") err="<span class='alerted'>You must define the URL if you are to use the manual setting</span>";
		url=escape(url);
		op="<a href='http://l2l.at/?url="+url+"' title='Shrink this URL with L2L.at'>";
		if (buttonType=="hyperlink") {
			op+="Shrink this!";
			p="<span class='textLink' title='Shrink this URL with L2L.at'>Shrink this!</span>";
		}else{
			op+="<img src='http://l2l.at"+buttonType+"' border=0 />";
			p="<img src='http://l2l.at"+buttonType+"' style='cursor:pointer' title='Shrink this URL with L2L.at' />";
		}
		if (err!="") p+="<br />"+err;
		op+="</a>";
	}else{
		op="<script type='text/javascript'>document.write(\"<a href='http://l2l.at/?url=\"+escape(window.location)+\"' target='_blank' title='Shrink this URL with L2L.at'>";
		if (buttonType=="hyperlink") {
			op+="Shrink this!";
			p="<span class='textLink' title='Shrink this URL with L2L.at'>Shrink this!</span>";
		}else{
			op+="<img src='http://l2l.at"+buttonType+"' border=0 />";
			p="<img src='http://l2l.at"+buttonType+"' style='cursor:pointer' title='Shrink this URL with L2L.at' />";
		}
		op+="</a>\");</script>";
	}
	objid("theHTML").value=op;
	objid("preview").innerHTML=p;
}
