/*
Filename: zonal.js
Author: Andreas Lindahl, Holger Berg
Date: January 16, 2003
Last edit: May 14, 2003 (Holger Berg)
Description:
Functions used by the zonal NavigationPlaceholderControl.
*/

if (vcc.ie && document.styleSheets) {
	document.write("<style>#zonal ul.treeMenuChild {display:none;}</style>");
}

vcc.zonal = new Object();

//Zonal init function. Called by onload event.
vcc.zonal.init = function(){
	var uls = document.getElementsByTagName("ul");
	if (vcc.ie && document.styleSheets) {
		document.styleSheets[document.styleSheets.length-1].addRule("#zonal ul.treeMenuChild","display:block");
	}
	for(var i = 0; i<uls.length; i++){
		var elmUl = uls[i];
		var cn = " " + elmUl.className + " ";
		if(cn.indexOf(" treeMenu ")>-1){
			var subUls = elmUl.getElementsByTagName("ul")
			for(var j = 0; j<subUls.length; j++){
				var s = subUls[j];
				s.parentNode.onclick = vcc.zonal.expandOrCollaps;
				var sCn = " " + s.className + " ";
				vcc.zonal.checkChildClasses(s)
				if(vcc.opera) {
					if (s.previousSibling.className != "expanded") s.style.display = "none";
				} else if (sCn.indexOf(" expand ") == -1 && s.parentNode.className != "expanded" && !((vcc.w3c || vcc.mac) && s.previousSibling.className == "expanded")){
					s.style.display = "none";
				}
			}
		}		
	}
}

// Called when a user clicks a link in zonal- or topNavigationPlaceholderControl. Sets the background-image attribute of the classes defined in zonal.css to 'none'.
// This is because otherwise when the user mouses over the zonal after clicking a link, the browser (ie on Windows) appears to stop loading the link that the user clicked.
vcc.zonal.disableBgImages = function() {
	/* This function is temporarily disabled
	if (vcc.win) {
		var objLastCSS = document.styleSheets[document.styleSheets.length - 1];
		if (objLastCSS.cssText && !objLastCSS.readOnly) {
			objLastCSS.cssText += "#zonal ul.treeMenuChild li.active a, #zonal ul.treeMenuChild a:hover, #zonal ul.treeMenuChild a, #zonal li.active a, #zonal li.expanded a, #zonal li.expandable a, #zonal li a:hover, #zonal li.expandable a:hover, #zonal li.expanded a:hover { background-image: none; }";			
		}
	}*/
}

//Called when the user clicks on a link, with subitems, in the zonal NavigationPlaceholderControl
vcc.zonal.expandOrCollaps = function(e){
	if(typeof(e) != "undefined"){ // Mozilla
		e.stopPropagation()
		var target = (e.target.nodeName == "A") ? e.target : e.target.parentNode;
	} else if(typeof(event) != "undefined"){ //IE
		event.cancelBubble = true
		var target = event.srcElement
	}
	vcc.zonal.switchChildDisplay(target);
	target.blur(); // To remove the highlighting of the menuitem in Mozilla/Netscape
	if(target.parentNode.className == "expandable") target.parentNode.className = "expanded";
	else if(target.parentNode.className == "expanded") target.parentNode.className = "expandable";
}

//Called from the expandOrCollaps function. Displays or hides the subitem.
vcc.zonal.switchChildDisplay = function(elm){
	var elmChildUL;
	if (vcc.w3c || vcc.mac) {
		elmChildUL = elm.parentNode.nextSibling;
	} else {
		if (vcc.ie && !vcc.opera) {
			//IE: Loop through all nodes until we find the UL element.
			for (var currentNode = elm.parentNode.firstChild; currentNode != null; currentNode = currentNode.nextSibling) {
				if (currentNode.nodeName == "UL") {
					elmChildUL = currentNode;
					break;
				}
			}
		} else if (vcc.opera) {
			elmChildUL = elm.parentNode.nextSibling;
		} else {
			elmChildUL = null;
		}
	}
	if (elmChildUL && elmChildUL.style && elmChildUL.className.toLowerCase().indexOf("child") > -1) {
		if (elmChildUL.style.display == "none")
			elmChildUL.style.display = "block";
		else
			elmChildUL.style.display = "none";
	}
}

vcc.zonal.checkChildClasses = function(s){
	for(var k = 0; k < s.childNodes.length; k++){
		var sChild = s.childNodes[k];
		if(sChild.tagName && sChild.tagName.toLowerCase() == "li"){
			var cnChild = " " + sChild.className + " ";
			if(cnChild.indexOf(" active ")) return true;
		}
	}
}

//Add the init function to the onload event
vcc.addEvent(window, "load", "vcc.zonal.init");