
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;
var resizeDivs = new Array();
var prevWidth = 0;
var totWidth = 0;
function CSI_InitRules()
	{
	// propagate the basic styles
	var ssCol = document.styleSheets;
	for (var s = 0; s < ssCol.length; s++)
		{
		if (ssCol[s].href && 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.length > ml && 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.length > ml && styleItem[ml].cssText != '')
						{
						addCSSRule (menuSS, currentSelector, styleItem[ml].cssText);
						addCSSRule (menuSS, currentSelector + " A", styleItem[ml].cssText);
						}
					currentSelector = currentSelector + ".over";
					if (styleItemOver.length > ml && 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()
	{
  CSI_InitRules();	
  // 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,10)
											+ parseInt(ul.currentStyle.borderRightWidth,10)
											+ parseInt(ul.currentStyle.paddingLeft,10)
											+ parseInt(ul.currentStyle.paddingRight,10)
											+ parseInt(ul.currentStyle.marginLeft,10)
											+ parseInt(ul.currentStyle.marginRight,10);
				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);
            //alert("totWidth = " + totWidth + "\naddWidth = " + addWidth + ",\nnewWidth = " + newWidth + ",\nnewTotWidth = " + newTotWidth);
						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;
						}
					}
				}
			}
		}
	/*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 resizeElements()
	{
	if (document.body.offsetWidth != prevWidth) 
		{
		for (var d = 0; d < resizeDivs.length; d++)
			{
			resizeMenu (resizeDivs[d]);
			d.className = "over";
			}
		prevWidth = document.body.offsetWidth;
		}
	}
function resizeMenu (div)
	{
	//alert("resizeMenu");
	//alert('menuWidth = ' + menuWidth);
	var maxRight = document.body.clientWidth - div.offsetLeft;
	var newTotWidth = 0;
	var resizeWidth = menuWidth; //Math.max(Math.min(document.body.clientWidth, 900), 675);
	div.parentNode.width = resizeWidth;
	div.style.width=resizeWidth + "px";
	var ul = findChildNode(div, "UL");
	var nbEL = ul.childNodes.length;
	var nbLI = 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);
	// 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 = div.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)
			{
			li.style.left = newTotWidth + "px";
			var addWidth = Math.ceil(totWidth/(nbLI-l++));
			var newWidth = li.offsetWidth + addWidth;
			li.style.width = newWidth + "px"; //adjust this LI width
			var nextUL = findChildNode(li, "UL");
			if (nextUL)
				{
				maxLeft = maxRight - nextUL.offsetWidth;
				nextUL.style.left = Math.min(maxLeft, newTotWidth + 10) + "px";
				var absX = getAboluteLeft(nextUL);
				var rightPos = absX + nextUL.offsetWidth;
				//alert("rightPos = " + rightPos
						//+ "\ndocument.body.offsetWidth = " + document.body.offsetWidth);
				if(rightPos > document.body.offsetWidth)
					{
					
					var absParentX = getAboluteLeft(li);
					//nextUL.style.left = rightPos - nextUL.offsetWidth + "px";
					//alert('nextUL.style.left = ' + nextUL.style.left);
					}
				}
			newTotWidth += trueWidth (li);
			totWidth -= addWidth;
			}
		}
	//alert('newTotWidth = ' + newTotWidth);
	}

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.length > ml && 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 = (IE ? window.event : e);
	var LI = this.parentNode;
	newOver = LI;
	event.cancelBubble = true;
	}
function CSI_hide(e)
	{
	var event = (IE ? window.event : e);
	if(!this.contains(event.toElement))
		{
		//if(lastOver)lastOver.className = "";
		newOver = null;
		}
	}
function clickLI (e)
	{
	var event;
  if(IE)
    {
    event = window.event;
    if (event.srcElement.tagName == "A")return
    }
  else
    {
    event = e;
    if (event.currentTarget.tagName == "A")return
    }
	// check if a link in that element.
	var aLink = findLink(this);
	if (aLink) 
    {
    window.open(aLink.href, aLink.target ? aLink.target : '_self');
    }
	event.cancelBubble = true;
  //return false;
	}
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,10)
			+ parseInt (el.currentStyle.paddingRight,10)
			+ parseInt (el.currentStyle.borderLeftWidth,10)
			+ parseInt (el.currentStyle.borderRightWidth,10);

	}
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";
