/*
 * 7/30/2009
 * scripted by Chris LaFond
 * endurance@eot.com
 * 
 * This file encapsules the javascript functions used by the menu system
 */
 //global, keeps the currently expanded menus ID
var CurrentExpandedMenu = "";
var ContentDiv = document.getElementById('contentContainer');
var TopMenu = document.getElementById('navleft');
var firstSub = TopMenu.childNodes; //the children are the h3's
var http = createRequestObject();
var lastExpandedMenu  = '';
var ulBool = false;
/*
 * DEPRECATED! as of 12/05/09
 */
function expandSubMenu(subMenuName){
	//if(CurrentExpandedMenu == subMenuName)
		//return false;
	// Make sure all menus are collapsed
	
	if(document.getElementById(subMenuName).parentNode.id != "navleft"){
		/*
		 * Go through all its siblings, and set an ULs
		 */
		document.getElementById(subMenuName).className = 'visible';
		return false;
	}
	for(menuName=0; menuName < firstSub.length; menuName++){
		
			if(firstSub[menuName].tagName == "UL"){
					firstSub[menuName].style.display = "none"; // ul's
			}
	}	
	
	// expand the curent one, and set the variable
	if(document.getElementById(subMenuName) != null){
		document.getElementById(subMenuName).style.display = "block";
		
	}
	CurrentExpandedMenu = subMenuName;

}
/*
 * DEPRECATED! as of 12/05/09
 */
function expandCategoryMenu(subMenuName){
	
	if(CurrentExpandedMenu == subMenuName)
		return false;
	if(document.getElementById(subMenuName).parentNode.id != "navleft"){
		
		document.getElementById(subMenuName).className = 'visible';
		return false;
	}
	
	for(menuName=0; menuName < firstSub.length; menuName++){
		if(firstSub[menuName].tagName == "UL"){
				firstSub[menuName].style.display = "none"; // ul's

		}
	}
	document.getElementById(subMenuName).className = 'visible';
	lastExpandedMenu = subMenuName;


}


function showContent(pageName){
	//Fade Out
	/*for(i = 100; i >= 0; i--) {
		changeOpac(i);
	}*/
	http.open('get', 'get_Main_Content.php?name=' 
			+ pageName);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = showResponse; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
	/* Create the BreadCrumb */
	//CreateBreadCrumb(BreadCrumbPath);
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

function showResponse(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	
	if(http.readyState == 4){ //Finished loading the response
		// FADE OUT
		  //speed for each frame
	    var speed = 5 // millisec /100
	    var timer = 0;
	    //Fade Out
	    //for(i = 100; i >= 0; i--) {
	      //  setTimeout("changeOpac(" + i + ")",(timer * speed));
	       // timer++;
	    //}
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
			
		document.getElementById('contentContainer').innerHTML = response;
		SetThickBox();
		InvokeThick();
	}
	// Fade IN
 /*   for(i = 0; i <= 100; i++){
    	setTimeout("changeOpac(" + i + ")",(timer * speed));
    	timer++;
    }*/
}

//change the opacity for different browsers
function changeOpac(opacity) {
    var object = document.getElementById('contentContainer').style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function CreateBreadCrumb(BreadCrumbPath){
	var BreadContainer = document.getElementById('breadCrumbs');
	var BreadPaths = BreadCrumbPath.split("|");
	var htmlString = "<ul id='crumbs'><li><a href='#main.php'>Home</a></li>";
	var htmlName = "";
	var newCrumbPath = "";
	var goodPath = "";
	for(var x in BreadPaths){
		
		htmlName = BreadPaths[x].split(",");
		if(htmlName[1]){ //htmlName[1] is the name, not the path
				newCrumbPath += BreadPaths[x] + "|";
			goodPath = newCrumbPath.substring(0,newCrumbPath.length -1);
			
			htmlString += "<li><a href='#"+ goodPath +"'>"+ htmlName[1]+"</a></li>";
		}
	}
	htmlString += "</ul>";
	BreadContainer.innerHTML = htmlString;

}
//
function RecurseOpenMenu(objElement){
	if(objElement.id == "navleft")
		return false; //we have hit the top, so stop
	
	if(objElement.tagName == "UL")
		objElement.className = "visible";
	
	RecurseOpenMenu(objElement.parentNode);
}

function RecurseCloseChildren(objElement){
	for(var x = 0; x< objElement.childNodes.length; x++){
		var children = objElement.childNodes[x];
		if(children.tagName == "UL"){
			children.className = "hidden";
		}
		if(children.hasChildNodes)
			RecurseCloseChildren(children);
		else
			return false;
	}
}
