var mastertabvar= new Array();
var oSelectedMenu //company, clients, process, contact
var oSubMenuBar
var isPrivacyTerms

//Preload images -- doesn't work as well as just putting them, display none, in the body of a page.
var products = new Image();
products.src = "images/products_selected.jpg";
var segments = new Image();
segments.src = "images/segments_selected.jpg";
var order = new Image();
order.src = "images/order_selected.jpg";
var support = new Image();
support.src = "images/support_selected.jpg";
var contact = new Image();
contact.src = "images/contact_selected.jpg";
var about = new Image();
about.src = "images/about_selected.jpg";

function initialize(){
	var globalmenuid = "nav_global";
	//get collection of DIVs in nav_global
	var aLI = document.getElementById(globalmenuid).getElementsByTagName("div"); 
	var vID; //used to form the id of the submenus
	var numbsubmenus = aLI.length;
	var y = 0; //for the array index, below.
	var isRel, vRel
	for (var i=0; i<numbsubmenus; i++){
		isRel = (aLI[i].getAttribute("rel") == "undefined" || aLI[i].getAttribute("rel") == null) ? false : true;
		if (isRel == true){ //DIV's if it is indicated to have a submenu (indicated by including a rel attribute)
			vRel = aLI[i].getAttribute("rel")
			mastertabvar[y]=("td_" + vRel); //store ids of submenus of tab menu
			y = y + 1;
			//with each addtion to the mastertabvar object array, length gets longer.
			aLI[i].getElementsByTagName("a")[0].onmouseover = function () {showsubmenu(this.id);} // Assigne id of the anchor tag, which matches its rel attribute.
		} //end if rel
	} //end for
//NOTE: not indicating a submenu by leaving out the REL breaks the script because closeall() expects something.  Easier to simply comment out the content of the secondary-nav table.	
} //end function

function swapoutbackground(who, what){
	//Second param = 0 | 1 for default | selected.
	var vDiv = "dv_" + who;
	var oWho = document.getElementById(vDiv);
	var vWhat, vBackground, vSubdir
	if(oSelectedMenu){ //ASSUMES that only the index.htm is in the root dir.  ALL OTHERS assume one dir down.
		vSubdir = "../"; 
	}else{
		vSubdir = ""
	}
	if(what==0){
		vWhat = who + "_default.jpg"
	}else{
		vWhat = who + "_selected.jpg"
	}
	vBackground = eval('"url(' + vSubdir + 'images/' + vWhat + ') top left no-repeat\";');
	surface(vDiv);
	oWho.style.background = vBackground;
}
function SetZone(x){
//alert("SetZone");	
	//Set page zone selection
	vParentID = document.getElementById("wrapper").parentNode.getAttribute("rel"); // the BODY rel, which should always be the zone name.  The body CLASS = the subpage ID.
	oSubMenuBar = document.getElementById("space_between_global_and_subn");
	if(vParentID == "default"){ //homepage?
		oSelectedMenu = null;
		if(x){
			isPrivacyTerms = x; //For terms and privacy pages: show the blue bar.
			oSubMenuBar.style.visibility = "visible";
		}else{oSubMenuBar.style.visibility = "hidden";}
		return;
	}
	var submenuID = "td_" + vParentID; //Actually should be tbl_, but td_[parentid] = the ID of the associated table.
	var vZindex = "dv_" + vParentID;
	oSelectedMenu = document.getElementById(submenuID);
	oSubMenuBar.style.visibility = "visible";
	//Set SELECTED state of parent, global nav element
	swapoutbackground(vParentID, 1);
	oSelectedMenu.style.display = "block";
//	MouseOutParams(vParentID);
}

var iStart = 10;
function surface(dv) {
//	var dvLayer = "dv_" + dv;
	iStart++
  document.getElementById(dv).style.zIndex = iStart;
  //LATER: reset all the zindexs back to some beginning threshhold when a max has been reached.
}

function closeall(x){
//alert("closeall");	
	//Idea is to 1) closeall submenus and 2) re-show the one that corresponds to whose global nav item is being moused over.
	//All but index page have mouseout with value of 1 to indicate there is a sub-menu to re-display.
	var oParent
	var vID, oGlobalMenuItem, vMouseOutParams
	for (var i=0; i<mastertabvar.length;i++){ //removed ref to length of mastertabvar array.
		//Loop thru all global submenus.
		//hide submenu
		document.getElementById(mastertabvar[i]).style.display="none";
		if(isPrivacyTerms){
			oSubMenuBar.style.visibility = "visible";
		}else{oSubMenuBar.style.visibility = "hidden";}

		//Remove hover state from parent
		vID = mastertabvar[i].substr(3);
		//the RELs and IDs in the Parents must be correct, else the next line results in an error.	
		//Swap out background image. Second param = 0 | 1 for default | selected.
		swapoutbackground(vID, 0);
			//vID gets transformed to dv_parentid (dv_vID)
	}
	if(x){//If there is a selected menu, however, turn it back on.  Activated only on MouseOut of the phantom zones. In the mouseout segments of ~index pages have 1 for a argument, else blank.
		if(oSelectedMenu){
			vID = oSelectedMenu.id.substr(3);
			oSubMenuBar.style.visibility = "visible";
			oSelectedMenu.style.display = "block";
			swapoutbackground(vID, 1);
//			vMouseOutParams = oSelectedMenu.id.substr(3);
//			MouseOutParams(vMouseOutParams);
		}
	} 
}

function showsubmenu(parentid){
	//This was assigned to all the Global Navigation links.
	//parentid = id of anchor tag, which is the "root" id.
	var oParent = document.getElementById(parentid);
	var subid = "td_" + parentid;
	var divid = "dv_" + parentid; //For mouseover effect
	var oDiv = document.getElementById(divid);
	var dvbackground
	
	//hide any open menus
	closeall();
	//mouseover color
	swapoutbackground(parentid, 1); //will be transformed to: dv_parentid
	
	//show the one that was just moused over
	var oSubMenuToOpen = document.getElementById(subid);
	surface(divid); //changes the z-index of the GLOBAL, parent element to be the top-most.
	oSubMenuBar.style.visibility = "visible";
	oSubMenuToOpen.style.display = "block";
	// To accommodate one-off structure for extra long sub-navigation. 
//	MouseOutParams(parentid)
}
function MouseOutHovered(parentid){
	//id of anchor = parentID
	var vDiv = "dv_" + parentID
	swapoutbackground(parentid, 0)
}

var d = new Date(); //write in the footer for the copyright year.
//document.write(d.getFullYear());