var oldlink = null;
var oldsublink = null;
var hashlocationhoriz = 0;
var hashlocationvert = 0;
var hashinterval = null;
var registeredhash = null;

function movetopage(horiz,vert,linkclicked,sublinkclicked) {
	hashlocationhoriz = hashlocationhoriz + horiz;
	hashlocationvert = hashlocationvert + vert;
	YUI({combine: true, timeout: 10000}).use("anim", function(Y) {
		var node = Y.get('#expander');
		//alert(document.getElementById("expander").style.top);
		
		var anim = new Y.Anim({ 
		        node: '#expander', 
		        to: { 
		            left: (((parseInt(document.getElementById("expander").style.left)*-1)+(horiz*1065))*-1)
		        },
		        easing: Y.Easing.easeBoth,
		        duration: 0.5
		});
		
		var target = null;
		
		var onEnd = function() {
        	target.detach('end', onEnd);
        	this.setAttrs({
        	    to: { top: (((parseInt(document.getElementById("expander").style.top)*-1)+(vert*521))*-1) },
        	    easing: Y.Easing.easeBoth,
        	    duration: 0.5
        	});
        	this.run();
    	};
 
		if (horiz == 0 && vert != 0) {
			//just run the vertical process.
			anim.setAttrs({
        	    to: { top: (((parseInt(document.getElementById("expander").style.top)*-1)+(vert*521))*-1) },
        	    easing: Y.Easing.easeBoth,
        	    duration: 0.5
        	});
		} else {
			target = anim.on('end', onEnd);
		}
		
		anim.run();
	});
	
	if (linkclicked) {
		if (oldlink != null) {
			oldlink.style.color = "";
		}
		oldlink = document.getElementById(linkclicked);
		oldlink.style.color = "white";
	}
	
	if (sublinkclicked) {
		if (oldsublink != null) {
			oldsublink.style.color = "";
		}
		oldsublink = document.getElementById(sublinkclicked);
		oldsublink.style.color = "white";
	}
	generatehash();
}



function movedefinite(horiz,linkclicked,sublinkclicked) {
	hashlocationhoriz = horiz;
	hashlocationvert = 0;
	
	//var fadeOut = new YAHOO.util.Anim('expander', { left: { to: ((horiz*1065)*-1) }, top: { to: 0 }}, 0.75, YAHOO.util.Easing.backOut);
	//fadeOut.animate();
	
	YUI({combine: true, timeout: 10000}).use("anim", function(Y) {
		var anim = new Y.Anim({ 
		        node: '#expander', 
		        to: { 
		            left: ((horiz*1065)*-1),
		            top: 0
		        },
		        easing: Y.Easing.easeBoth,
		        duration: 0.5
		});
		
		anim.run();
	});
	
	if (oldlink != null) {
		oldlink.style.color = "";
	}
	oldlink = document.getElementById(linkclicked);
	oldlink.style.color = "white";
	
	if (sublinkclicked) {
		if (oldsublink != null) {
			oldsublink.style.color = "";
		}
		oldsublink = document.getElementById(sublinkclicked);
		oldsublink.style.color = "white";
	}
	generatehash();
}

function movedefinitecomplete(horiz,vert,linkclicked,sublinkclicked) {
	hashlocationhoriz = horiz;
	hashlocationvert = vert;
	YUI({combine: true, timeout: 10000}).use("anim", function(Y) {
		var anim = new Y.Anim({ 
		        node: '#expander', 
		        to: { 
		            left: ((horiz*1065)*-1),
		            top: ((vert*521)*-1)
		        },
		        easing: Y.Easing.easeBoth,
		        duration: 0.5
		});
		
		anim.run();
	});
	if (oldlink != null) {
		oldlink.style.color = "";
	}
	oldlink = document.getElementById(linkclicked);
	oldlink.style.color = "white";
	
	if (sublinkclicked) {
		if (oldsublink != null) {
			oldsublink.style.color = "";
		}
		oldsublink = document.getElementById(sublinkclicked);
		oldsublink.style.color = "white";
	}
	
	generatehash();
}

function generatehash() {
	window.location.hash = "#"+hashlocationhoriz+"-"+hashlocationvert+"-"+oldlink.id;
	registeredhash = "#"+hashlocationhoriz+"-"+hashlocationvert+"-"+oldlink.id;
	var lociframe = document.getElementById("locationiframe");
	if (!document.all) {
		checkhash(); //if we're in safari or firefox, start checking the hash every second
	} else {
		lociframe.src = "changelocation.php?hash="+hashlocationhoriz+"-"+hashlocationvert+"-"+oldlink.id+"&cachedefeat="+Math.floor(Math.random()*10001);
	}
}

function processhash(hashvalue) {
	//alert(hashvalue);
	//called on return of the changelocation.php file (used by IE);
	var location = hashvalue.split("-");
	var currentlocation = window.location.hash;
	currentlocation = currentlocation.replace("#","");
	if (hashvalue != currentlocation) {
		alert("got in here");
		movedefinitecomplete(location[0],location[1],location[2]);
		if (oldlink != null) {
			oldlink.style.color = "";
		}
		oldlink = document.getElementById(location[2]);
		oldlink.style.color = "white";
	}
}

function initprocesshash(hashvalue) {
	var location = hashvalue.split("-");
	
	movedefinitecomplete(location[0],location[1],location[2]);
	if (oldlink != null) {
		oldlink.style.color = "";
	}
	oldlink = document.getElementById(location[2]);
	oldlink.style.color = "white";
}

function checkhash() {
	//used by non-IE browsers to check if back button was pressed.
	if (hashinterval) {
		if (registeredhash != window.location.hash) {
			var currentlocation = window.location.hash;
			currentlocation = currentlocation.replace("#","");
			var location = currentlocation.split("-");
			movedefinitecomplete(location[0],location[1],location[2]);
		} 
	} else {
		hashinterval = setInterval("checkhash()",1000); //check the hash every second!
	}
}