var lastOver = newOver = null;
var panels = new Array();
var traceOn = false;
var numEl = 0;
var stylePanel = new Array();
var styleItem = new Array();
var styleItemOver = new Array();
var menuSS;
var currentSelector = "DIV.CSI_menubar"
var maxLevel = 0;
var animHandle;
function CSI_InitRules()
	{
	// propagate the basic styles
	var ssCol = document.styleSheets;
	for (var s = 0; s < ssCol.length; s++)
		{
		if (ssCol[s].href.search(/CSI_/i) >= 0) 
			{
			menuSS = ssCol[s];
			var rCol = getCSSRules(menuSS);
			//alert(rCol);
			for (var r = 0; r < rCol.length; r++)
				{
				switch(rCol[r].selectorText.toLowerCase())
					{
					case "csi_panel":
					stylePanel[0] = rCol[r].style; break;
					case "csi_subpanel":
					stylePanel[1] = rCol[r].style; break;
					case "csi_item":
					styleItem[0] = rCol[r].style; break;
					case "csi_subitem":
					styleItem[1] = rCol[r].style; break;
					case "csi_itemover":
					styleItemOver[0] = rCol[r].style; break;
					case "csi_subitemover":
					styleItemOver[1] = rCol[r].style; break;
					}
				}
			}
		}
	
	// initialise all menus
	var divs = document.getElementsByTagName("div");
	for (var d = 0; d<divs.length; d++)
		{
		if (divs[d].className.substr(0,8).toLowerCase() == "csi_menu") 
			{
			CSI_initMenuRule (divs[d], 1);
			}
		}
	/*alert(menuSS.rules.length);
	for (i = 0; i < menuSS.rules.length; i++)
		{
		alert(menuSS.rules[i].selectorText);
		alert(menuSS.rules[i].style.cssText);
		}*/
	window.setInterval("CSI_animate()", 100);
	//document.attachEvent('onmouseover', CSI_hide);
	}
function CSI_initMenuRule (m, level)
	{
	var el = m.childNodes;
	for (var l = 0; l<el.length; l++)
		{
		if(el[l].tagName && el[l].nodeType == 1)
			{
			var tag = el[l].tagName.toUpperCase();
			if (tag == "UL") 
				{
				if (level > maxLevel)
					{
					maxLevel = level;
					var lev = (level-1)/2;
					var ml = Math.min(1, lev);
					//alert("Level = " + lev + ", ml = " + ml + " : " + el[l].innerText);
					currentSelector = currentSelector + " UL";
					if (stylePanel[ml].cssText != '')
						addCSSRule (menuSS, currentSelector, stylePanel[ml].cssText);
					}
				CSI_initMenuRule (el[l], level+1);
				}
			else if (tag == "LI") 
				{
				if (level > maxLevel)
					{
					maxLevel = level;
					var lev = Math.floor(level/2) - 1;
					var ml = Math.min(1, lev);
					//alert("Level = " + lev + ", ml = " + ml + " : " + el[l].innerText);
					currentSelector = currentSelector + " LI";
					if (styleItem[ml].cssText != '')
						{
						addCSSRule (menuSS, currentSelector, styleItem[ml].cssText);
						addCSSRule (menuSS, currentSelector + " A", styleItem[ml].cssText);
						}
					currentSelector = currentSelector + ".over";
					if (styleItemOver[ml].cssText != '')
						{
						addCSSRule (menuSS, currentSelector, styleItemOver[ml].cssText);
						addCSSRule (menuSS, currentSelector + " A", styleItemOver[ml].cssText);
						}
					}
				CSI_initMenuRule (el[l], level+1);
				}
			}
		}
	}

function CSI_Init()
	{
	// initialise all menues
	var divs = document.getElementsByTagName("div");
	for (var d = 0; d<divs.length; d++)
		{
		if (divs[d].className.substr(0,8).toLowerCase() == "csi_menu") 
			{
			var next = panels.length;
			CSI_initMenu (divs[d], false, 1);
			panels[next].base = true;
			if (divs[d].className.toLowerCase() == "csi_menubar") 
				{
				var ul = findChildNode(divs[d], "UL");
				var nbEL = ul.childNodes.length;
				var nbLI = 0;
				var totWidth = 0;
				totWidth = parseInt(ul.currentStyle.borderLeftWidth)
											+ parseInt(ul.currentStyle.borderRightWidth)
											+ parseInt(ul.currentStyle.paddingLeft)
											+ parseInt(ul.currentStyle.paddingRight)
											+ parseInt(ul.currentStyle.marginLeft)
											+ parseInt(ul.currentStyle.marginRight);
				var newTotWidth = 0;
				// Loop on all LIs in the level 1 UL menu (UL LI)
				// in order to balance level 1 panels width
				for (var el = 0; el < nbEL; el++) 
					{
					var child = ul.childNodes[el];
					if (child.nodeType == 1) 
						{
						totWidth += trueWidth (child);
						nbLI++;
						}
					}
				totWidth = divs[d].offsetWidth - totWidth;
				var l = 0;
				// Loop again on all LIs in the level 1 UL menu (UL LI)
				for (el = 0; el < nbEL; el++) 
					{
					var li = ul.childNodes[el];
					if(li.nodeType == 1)
						{
						var addWidth = Math.ceil(totWidth/(nbLI-l++));
						var newWidth = li.offsetWidth + addWidth;
						li.style.width = newWidth + "px"; //adjust this LI width
						newTotWidth += trueWidth (li);
						var nextUL = findChildNode(li, "UL");
						if (nextUL)
							{
							nextUL.style.left = li.offsetLeft + 10 + "px";
							var absX = getAboluteLeft(nextUL);
							if(absX + nextUL.offsetWidth > document.body.offsetWidth)
								{
								var absParentX = getAboluteLeft(nextUL.parentNode);
								nextUL.style.left = nextUL.offsetLeft
									+ nextUL.parentNode.offsetWidth
									- nextUL.offsetWidth 
									+ absParentX*2
									- absX*2
									+ "px";
								}
							}
						totWidth -= addWidth;
						li.style.visibility="visible";

						}
					}
				}
			}
		}
	/*alert(menuSS.rules.length);
	for (i = 0; i < menuSS.rules.length; i++)
		{
		alert(menuSS.rules[i].selectorText);
		alert(menuSS.rules[i].style.cssText);
		}*/
	window.setInterval("CSI_animate()", 100);
	//document.attachEvent('onmouseover', CSI_hide);
	}
function CSI_initMenu (m, addParent, level)
	{
	var el = m.childNodes;
	for (var l = 0; l<el.length; l++)
		{
		if(el[l].tagName && el[l].nodeType == 1)
			{
			var tag = el[l].tagName.toUpperCase();
			if (tag == "UL") 
				{
				if (IE)
					{
					// create an IFRAME to workaround the IE SELECT-z-index bug
					var iframe = document.createElement('iframe');
					iframe.frameBorder=0;
	        iframe.style.display='block';
	        iframe.style.position='absolute';
					iframe.style.visibility='hidden';
					el[l].parentNode.insertBefore(iframe, el[l++]);
					el[l].selectHider = iframe;
					}
				if (level > maxLevel)
					{
					maxLevel = level;
					var lev = (level-1)/2;
					var ml = Math.min(1, lev);
					//alert("Level = " + lev + ", ml = " + ml + " : " + el[l].innerText);
					currentSelector = currentSelector + " UL";
					if (stylePanel[ml].cssText != '')
						addCSSRule (menuSS, currentSelector, stylePanel[ml].cssText);
					}
				panels.push(el[l]);
				if(addParent) 
					{
					m.subPanel = el[l];
					el[l].parent = m;
					el[l].style.left = el[l].parentNode.offsetLeft + el[l].offsetLeft;
					var absX = getAboluteLeft(el[l]);
					if(absX + el[l].offsetWidth > document.body.offsetWidth)
						{
						var absParentX = getAboluteLeft(el[l].parentNode);
						el[l].style.left = el[l].offsetLeft
							+ el[l].parentNode.offsetWidth
							- el[l].offsetWidth 
							+ absParentX*2
							- absX*2
							+ "px";
						}
					el[l].style.top = el[l].parentNode.offsetTop + el[l].offsetTop;
					el[l].style.zIndex = level;
					el[l].level = level;
					}
				el[l].onmouseout = CSI_hide;
				CSI_initMenu (el[l], false, level+1);
				}
			else if (tag == "LI") 
				{
				if (level > maxLevel)
					{
					maxLevel = level;
					var lev = level/2 - 1;
					var ml = Math.min(1, lev);
					//alert("Level = " + lev + ", ml = " + ml + " : " + el[l].innerText);
					}
				var html = el[l].innerHTML;
				var UL = html.search (/<UL/i);
				if (UL < 0)
					el[l].innerHTML = '<DIV>'
						+ html + '</DIV>';
				else
					el[l].innerHTML = '<DIV>'
						+ html.substr (0, UL) + '</DIV>' + html.substr (UL);
				el[l].childNodes[0].onmouseover = CSI_show;
				el[l].childNodes[0].style.width=el[l].offsetWidth + "px";
				el[l].numEl = numEl++;
				el[l].onclick = clickLI;
				//el[l].attachEvent('onmouseout', CSI_hide);
				CSI_initMenu (el[l],true, level+1);
				}
			}
		}
	}
function CSI_show(e)
	{
	var event = (document.all ? window.event : e);
	var LI = this.parentNode;
	//alert(event.toElement.numEl + ", " + this.numEl + ", " + this.contains(event.toElement));
	//alert("this = " + LI.numEl + " : " + this.tagName);
	//if(this == event.toElement)
		{
		//alert(this.innerText)
		//window.status = this.numEl;
		//if(lastOver && lastOver != LI)lastOver.className = "";
		newOver = LI;
	event.cancelBubble = true;
		}
	}
function CSI_hide(e)
	{
	var event = (document.all ? window.event : e);
	if(!this.contains(event.toElement))
		{
		//if(lastOver)lastOver.className = "";
		newOver = null;
		}
	}
function clickLI (e)
	{
	var event = (document.all ? window.event : e);
	// check if a link in that element.
	var aLink = findLink(this);
	if (aLink) aLink.click();
	event.cancelBubble = true;
	}
function findLink(el)
	{
	for (var i = 0; i < el.childNodes.length; i++)
		{
		var child = el.childNodes[i];
		if (child.tagName && child.nodeType == 1)
			{
			var tag = child.tagName.toUpperCase();
			if (tag == "A") return child;
			if (tag == "UL" || tag == "LI") return null;
			return findLink(child);
			}
		}
	return null;
	}
function CSI_animate()
	{
	if (CSI_locked) return;
	var p;
	for (p = 0; p<panels.length; p++)panels[p].state = panels[p].base;
	if (newOver) 
		{
		if(lastOver && newOver != lastOver)lastOver.className = "";
		lastOver = newOver;
		//newOver = null;
		if (lastOver.className != "over") lastOver.className = "over";
		p = lastOver.parentNode;
		// Find active ULs
		while(p && p.tagName.toUpperCase() == "UL")
			{
			p.state = true;
			p = p.parentNode;
			if (p) p = p.parentNode;
			}
		if(lastOver.subPanel) lastOver.subPanel.state = true;
		}
	// set new hidden
	for (p = 0; p<panels.length; p++) 
		{
		if (!panels[p].state && panels[p].style.visibility != "hidden")
			{
			panels[p].style.visibility = "hidden";
			if (IE)
				{
				var iframe = panels[p].selectHider;
				iframe.style.visibility='hidden';
				}
			}
		}
	// set new classes
	for (p = 0; p<panels.length; p++) 
		{
		if (panels[p].parent)
			{
			if (panels[p].state)
				{
				if (panels[p].parent.className != "over")	
						{
						panels[p].parent.className = "over";
						//alert("over");
						}
				}
				else if (panels[p].parent.className != "")
						{
						panels[p].parent.className = "";
						//alert("not over");
						}
			}
		}
	// set new visible
	for (p = 0; p<panels.length; p++) 
		{
		if (panels[p].state && panels[p].style.visibility != "visible") 
			{
			panels[p].style.visibility = "visible";
			if (IE)
				{
				var iframe = panels[p].selectHider;
				iframe.style.top=panels[p].offsetTop;
        iframe.style.left=panels[p].offsetLeft;
        iframe.style.width=panels[p].offsetWidth;
        iframe.style.height=panels[p].offsetHeight;
				iframe.style.visibility='visible';
				}
			}
		}
	}
function getCSSRules (styleSheet)
	{
	if (styleSheet.rules)	return menuSS.rules;
	else if (styleSheet.cssRules)	return styleSheet.cssRules;
	}
function addCSSRule(styleSheet, selectorText, declarations)
	{
	if (styleSheet.insertRule) 
		{
		styleSheet.insertRule(selectorText + ' { ' + declarations + ' }',
			styleSheet.cssRules.length);
		}
	else if (styleSheet.addRule) 
		{
		styleSheet.addRule(selectorText, declarations);
		}
	}
function trueWidth(el)
	{
	if (IE)
		return el.offsetWidth 
	else
		return el.offsetWidth 
			+ parseInt (el.currentStyle.paddingLeft)
			+ parseInt (el.currentStyle.paddingRight)
			+ parseInt (el.currentStyle.borderLeftWidth)
			+ parseInt (el.currentStyle.borderRightWidth);

	}
function trace(mess)
	{
	if (traceOn) traceWin.document.write(mess + "<BR>");
	}
	function getAboluteLeft(el)
	{
	var l = el.offsetLeft;
	while (el.offsetParent)
		{
		el = el.offsetParent;
		l += el.offsetLeft;
		}
	return l;
	}
function CSI_lock(el)
	{
	CSI_locked = true;
	el.onblur = CSI_unlock;
	}
function CSI_unlock()
	{
	CSI_locked = false;
	}
var CSI_locked = false;
if(window.addEventListener) // Mozilla, Netscape, Firefox
	window.addEventListener('load', CSI_Init, false);
else  // IE
	window.attachEvent('onload', CSI_Init);
if (traceOn) 
	{
	traceWin =
	 open("javascript:void(0)","testPage","width=500,height=700,scrollbars");
	traceWin.focus();
	}
function findChildNode(el, tagName)
	{
	var tag = tagName.toUpperCase();
	for (var i=0; i < el.childNodes.length; i++)
		{
		var child = el.childNodes[i];
		if (child.nodeType == 1 && child.tagName.toUpperCase() == tag) return child;
		}
	return null;
	}

var IE = /MSIE/.test(navigator.userAgent);
var MOZ = !IE && navigator.product == "Gecko";
if (MOZ)
	{
	HTMLDocument.prototype.attachEvent = 
	HTMLElement.prototype.attachEvent = function (sType, fHandler) {
	   var shortTypeName = sType.replace(/on/, "");
		 if (shortTypeName == "mouseenter")shortTypeName = "mouseover";
		 else if (shortTypeName == "mouseleave")shortTypeName = "mouseout";
	   fHandler._ieEmuEventHandler = function (e) {
	      window.event = e;
	      return fHandler();
	   };
	   this.addEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
		};
		Event.prototype.__defineGetter__("srcElement", function() { 
		return (this.target.nodeType == Node.ELEMENT_NODE) ? this.target : this.target.parentNode; 
		}); 
		Event.prototype.__defineGetter__("fromElement",function() { 
		return (this.type == "mouseover") ? this.relatedTarget : (this.type == "mouseout") ? this.srcElement : null; 
		}); 
		Event.prototype.__defineGetter__("toElement", function() { 
		return (this.type == "mouseout") ? this.relatedTarget : (this.type == "mouseover") ? this.srcElement : null; 
		}); 
		Event.prototype.__defineSetter__("cancelBubble", function (value) { 
		// this can't be undone! 
		if (value) this.stopPropagation(); 
		}); 
	// mimic microsoft's "currentStyle" 
	HTMLElement.prototype.__defineGetter__("currentStyle", function() { 
	return getComputedStyle(this, null); 
	}); 
// support "parentElement" 
HTMLElement.prototype.__defineGetter__("parentElement", function() {
return (this.parentNode == this.ownerDocument) ? null : this.parentNode; 
}); 
// support the "contains" method 
HTMLElement.prototype.contains = function contains(element) { 
return Boolean(element == this || (element && this.contains(element.parentElement))); 
}; 
// support "innerText" 
HTMLElement.prototype.__defineGetter__("innerText", function() { 
return this.textContent; 
}); 
// mozilla already supports the read-only "cancelBubble" 
// so we only need to define the setter 
Event.prototype.__defineSetter__("cancelBubble", function (value) { 
// this can't be undone! 
if (value) this.stopPropagation(); 
}); 

}
CSI_InitRules();
