// JavaScript Document

function swapNavIn(depth,section,current,elOut,elIn){
	
	//find the element we're going to slide out
	myElOut = document.getElementById(elOut);
	
	//figure out what its current css left position is (removing px from string)
	curX = myElOut.style.left.substr(0,myElOut.style.left.length - 2);
	
	if(curX == "-380"){
		curX = "0";
		myElOut.style.left = "0";
	}
	
	//if the result exists convert to an integer and figure out where the element will travel to
	if(curX && curX != "0"){
		numX = parseInt(curX);
		distX = numX - 190;
	}
	
	//otherwise set the position it will travel to to a preset value
	else{
		distX = -190;
	}
	
	//do the move
	new Effect.Move(elOut, { x: distX, duration: 0.4, y: 0, mode: 'absolute'});
	
	//find the next level in the navigation, populate the div and move it in
	updateNavIn(depth,section,current,elOut,elIn);
}

function swapNavOut(depth,section,current,elOut,elIn){
	
	//find the element we're going to slide out
	myElOut = document.getElementById(elOut);
	
	//figure out what its current css left position is (removing px from string)
	curX = myElOut.style.left.substr(0,myElOut.style.left.length - 2);
	
	//if the result exists convert to an integer and figure out where the element will travel to
	if(curX != "0" && curX){
		numX = parseInt(curX);
		distX = numX + 190;
	}
	
	//otherwise set the position it will travel to to a preset value
	else if(curX == "0"){
		//curX = "380";
		//myElOut.style.left = "380px";
		distX = 190;
	}
	else
	{
		distX = 190;
	}
	
	//do the move
	new Effect.Move(elOut, { x: distX, duration: 0.4, y: 0, mode: 'absolute'});
	
	//find the next level in the navigation, populate the div and move it in
	updateNavOut(depth,section,current,elOut,elIn);
}

function updateNavIn(depth,section,current,elOut,elIn) {
	
	//nS is the element to travel in
	nS = document.getElementById(elIn);
	
	//nL is the element to travel out
	nL = document.getElementById(elOut);
	
	//figure out what its current css left position is (removing px from string)
	curX = nS.style.left.substr(0,nS.style.left.length - 2);
	
	//if the item has scrolled off screen, reposition
	if(curX == "-190"){
		nS.style.left = "190px";
		curX = "190";
	}
	
	if(curX == "-380"){
		nS.style.left = "0";
		curX = "0"
	}
	
	//if the result exists convert to an integer and figure out where the element will travel to
	if(curX){
		numX = parseInt(curX);
		distX = numX - 190;
	}

	//otherwise set the position it will travel to to a preset value
	else{
		distX = -190;
	}
	
	//find the target for the ajax nav update
	navFile = "";
	for(i=0;i<depth;i++){
		navFile += "../";
	}
	safeCurrent = current.replace(/&/,"and");
	safeSection = section.replace(/&/,"and");
	navFile += "lib/nav.php?menu="+safeSection+"&depth="+depth+"&elOut="+elOut+"&current="+safeCurrent;
	
	//populate the new nav with the details from the php script and move the element in when updated
	new Ajax.Updater(elIn, navFile, { onComplete:function(){ 
	new Effect.Move(elIn, { duration: 0.4, x: distX, mode: 'absolute', afterFinish: function (){ }});}
	});
}

function updateNavOut(depth,section,current,elOut,elIn) {
	
	//nS is the element to travel in
	nS = document.getElementById(elIn);
	
	//nL is the element to travel out
	nL = document.getElementById(elOut);
	
	//figure out what its current css left position is (removing px from string)
	curX = nS.style.left.substr(0,nS.style.left.length - 2);
	
	
	//if the result exists convert to an integer and figure out where the element will travel to
	if(curX && curX != "0"){
		numX = parseInt(curX);
		
		//if it's travelled off screen, reposition
		if(numX == 190){
			nS.style.left = "-190px";
			numX = -190;
		}
		distX = numX + 190;
	}
	else{
		nS.style.left = "-380px";
		distX = -190;
	}
	
	//find the target for the ajax nav update
	navFile = "";
	for(i=0;i<depth;i++){
		navFile += "../";
	}
	safeCurrent = current.replace(/&/,"and");
	safeSection = section.replace(/&/,"and");
	navFile += "lib/nav.php?menu="+safeSection+"&depth="+depth+"&elOut="+elOut+"&current="+safeCurrent;

	//populate the new nav with the details from the php script and move the element in when updated
	new Ajax.Updater(elIn, navFile, { onComplete:function(){ 
	new Effect.Move(elIn, { duration: 0.4, x: distX, mode: 'absolute', afterFinish: function (){  }});}
	});
}