
//GeneralJavaScriptLeftNav starts

var targetBlock;
var contentHeight;
var intervalId;
var speed=0;
var currentOpenedMenu;
var openFlag;
var lefNavNumber ;
var leftNavName = [];
var subNavItem = [];
var subHeight = 24;
var clickAble = true;

var intervalID;
var tempIndex;
var tempSubName;
var tempcurrentName;

var subTotHeight;

var tempCurrentSubIndexNUmber;
var tempCurrentChildren;

function getHeight(elem) {    
    var tmpElem = elem;
    if(tmpElem.currentStyle) return tmpElem.offsetHeight;       
    else return parseInt(document.defaultView.getComputedStyle(tmpElem, "").getPropertyValue("height"))+8;                       
}

function getHeight_Sub(theSub) {    
		var subChildren = get_element(theSub).getElementsByTagName("a");
		var numberOfChildren = subChildren.length;
		//alert(numberOfChildren);
		var height = 0;
		for(var i=0;i<numberOfChildren;i++){
			height += getHeight(subChildren[i]);
			//alert(height);
		}               
		return height;
}



function initLeftNav(){
	if(!get_element("leftNavContainer")){
		return;
	}
	//alert(readCookie("currentOpenedMenu"));
	
	var tempItems = get_element("leftNavContainer").getElementsByTagName("dt");
	lefNavNumber = tempItems.length;
	
	for(var k=1;k<lefNavNumber+1;k++){leftNavName.push("leftNav"+k);}
	for(var i=0;i<lefNavNumber;i++){
		/*get_element(leftNavName[i]).onmouseover=function(){
			this.style.cursor ="hand";
			};*/
		if(leftNavName[i]+"_sub" == readCookie("currentOpenedMenu")){
				
				var theOpenedSub = readCookie("currentOpenedMenu");
				var theOpenedSubIndexNumber = readCookie("currentOpenedItem");
				handleRollover(theOpenedSub,theOpenedSubIndexNumber);
				//alert(theOpenedSub);
				//alert(getHeight_Sub(theOpenedSub));
				get_element(leftNavName[i]+"_sub").style.height = getHeight_Sub(theOpenedSub)+"px";
				get_element(leftNavName[i]+"_sub").style.visibility = "visible";
				get_element(leftNavName[i]+"_sub").style.borderBottom = "1px solid #FFFFFF";
				get_element(leftNavName[i]).opened = true;
				get_element(leftNavName[i]).parentNode.style.backgroundImage = "url(/wps/wcm/images/sideNavBg_on.gif?MOD=AJPERES)";
				get_element(leftNavName[i]).style.backgroundImage = "url(/wps/wcm/images/sideNavBg_on.gif?MOD=AJPERES)";
				get_element(leftNavName[i]).style.color = "white";
				currentOpenedMenu = theOpenedSub;
			}		
				get_element(leftNavName[i]).onclick=function(){
				
					removeRolloverForNav();
					if(clickAble){
						var theSub = this.getAttribute("id")+"_sub";
						//alert(theSub);
						document.cookie = "currentOpenedMenu="+theSub+"; path=/";
						document.cookie = "currentOpenedItem="+null+"; path=/";
						//alert(theSub);
						var subChildren = get_element(theSub).getElementsByTagName("a");
						var numberOfChildren = subChildren.length;
						
						subTotHeight = 0;
						for(var i=0;i<numberOfChildren;i++){
							
							subTotHeight += getHeight(subChildren[i]);
						}
					
						if(!this.opened){
								if(currentOpenedMenu !=null && currentOpenedMenu != theSub){
									//alert(subTotHeight);
									slidingUp(currentOpenedMenu,subTotHeight,theSub,numberOfChildren);							
								}
								else{
									slidingDown(theSub,subTotHeight);
								}
								this.parentNode.style.backgroundImage = "url(/wps/wcm/images/sideNavBg_on.gif?MOD=AJPERES)";
								this.style.backgroundImage = "url(/wps/wcm/images/sideNavBg_on.gif?MOD=AJPERES)";
								this.style.color = "white";
								this.opened = true;
								
						}
						/*else{
							slidingUp(theSub,numberOfChildren*subHeight,theSub,numberOfChildren);
							this.parentNode.style.backgroundImage = "url(/wps/wcm/images/sideNavBg.gif?MOD=AJPERES)";
							this.style.backgroundImage = "url(/wps/wcm/images/sideNavBg.gif?MOD=AJPERES)";
							this.style.color = "#333333";
							this.opened = false;
						}*/
					}
					//return true;
				};
	}
	
	}

function removeRolloverForNav(){
		for(var n=0;n<leftNavName.length;n++){
			get_element(leftNavName[n]).parentNode.style.backgroundImage = "url(/wps/wcm/images/sideNavBg.gif?MOD=AJPERES)";
			get_element(leftNavName[n]).style.backgroundImage = "url(/wps/wcm/images/sideNavBg.gif?MOD=AJPERES)";
			get_element(leftNavName[n]).style.color = "#333333";
			}
	}
	
function handleRollover(name,subIndex){
	var children = [];
	if (get_element(name).children){
		   children = get_element(name).children;
		}else{
			children = get_element(name).getElementsByTagName('*');					  
		}	
	for(var m=0;m<children.length;m++){		
		children[m].theIndex = m;
		children[m].subItems = children;
		children[m].onmouseover = handleMouseOver;
		children[m].onmouseout = handleMouseOut;
		children[m].onclick = handleMouseRelease;
		if(m == parseInt(subIndex))children[m].style.color = "#515151";
	}
	}

function handleMouseRelease(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)targ = targ.parentNode;
	document.cookie = "currentOpenedItem="+targ.theIndex+"; path=/";
	var children = targ.subItems;
	for(var i=0;i<children.length;i++){
		if(i == targ.theIndex)targ.style.color = "#515151";
		else children[i].style.color = "#4f8623"
	}
	
	}


function handleMouseOver(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)targ = targ.parentNode;
	targ.style.textDecoration = "underline";
	}

function handleMouseOut(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)targ = targ.parentNode;
	targ.style.textDecoration = "none";
	}
	

function doSlidingUp(){    
	clickAble=false;
	contentHeight -= 8;  
	if(contentHeight > 0)get_element(currentOpenedMenu).style.height = contentHeight+"px";	
    else{
        clearInterval(intervalId);
		get_element(currentOpenedMenu).style.height = 0+"px";
        targetBlock.style.visibility = "hidden";
        speed = 0;
        contentHeight = 0;
		clickAble=true;		
		//alert(tempSubName);
		if(currentOpenedMenu != tempSubName)slidingDown(tempSubName,subTotHeight);
		else currentOpenedMenu = null;
    }
	
}

function doSlidingDown(){
	clickAble=false;
    speed+=8;
    targetBlock.style.height = speed+"px";
    if(speed >= contentHeight){
        clearInterval(intervalId);
		//alert("complete");
		targetBlock.style.height = contentHeight+"px";
        speed = 0;
		contentHeight = 0;		
		currentOpenedMenu = tempcurrentName;
		//clickAble=true;
		//document.cookie = "currentOpenedMenu="+currentOpenedMenu+"; path=/";
    }
}

function slidingDown(targetName,targetHeight){
		//alert(targetHeight);
		tempcurrentName = targetName;
        targetBlock = get_element(targetName); 
        contentHeight = targetHeight; 
        if(targetBlock.style.visibility != "visible"){
        targetBlock.style.visibility = "visible";
		targetBlock.style.borderBottom = "1px solid #FFFFFF";
		intervalId = setInterval(doSlidingDown,10);	  
   }
}

function slidingUp(targetName,originalHeight,nextOne,thisIndex){
    get_element(targetName.substr(0,8)).opened = false;
	targetBlock = get_element(targetName);
    contentHeight = originalHeight;
	tempIndex = thisIndex;
	tempSubName = nextOne;
   if(targetBlock.style.visibility == "visible"){
	intervalId = setInterval(doSlidingUp,10);
    }   
}



get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };




function readCookie(name){
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(name+"=");
	if (pos != -1) {
		var start = pos +18;                 
		var end = allcookies.indexOf(";", start); 
		if (end == -1) end = allcookies.length;
		var value = allcookies.substring(start, end);  
	}
	return value;
}

