//declare
var submenu = new Array();
var tmr = new Array();
var adj = new Array();
var last_zIndex = 1000;
var lastOverId = '';

// EXAMPLE CONFIG

var menuWidth 		= 164;			//width of submenu in pixels
var widthFix		= 24;
var widthFixIE		= 16;
var paddingLeft		= 16;
var paddingTop		= 4;
var alignSubmenu 	= 'right';		//bottom or right side of the main button.
var useLastItemCSS	= true;			//generate last menu item too
									//if false, it will behave as a normal item
									//and the css menuItemLast class will not be used

//fisrt submenu tree
// END OF EXAMPLE CONFIG

//build or unhide submenu div...
function buildSubmenu(obj){
	lastOverId = obj.id;

	//get common part of div id
	menuPath = obj.id.split('_');

	//check if we have a submenu of the obj...
	if(submenu[obj.id]){
		//calc position of mouseover
		d = obj;
		if(d){
			if(is_ie) {
				L_pos = d.offsetLeft + d.offsetWidth - paddingLeft;
				T_pos = d.offsetTop + paddingTop;
			}
			else {
				L_pos = d.offsetLeft + d.offsetWidth - paddingLeft;
				T_pos = d.offsetTop + paddingTop;
			}
			
			while(d.offsetParent){
				d = d.offsetParent;
				L_pos+= d.offsetLeft;
				T_pos+= d.offsetTop;
			}
		}
	
		//patch first submenu to go right below the main buttons...
		if(obj.className.indexOf('menuItem') < 0 && alignSubmenu == 'bottom'){
			L_pos-= obj.offsetWidth;
			T_pos+= obj.offsetHeight;
		}
				
		//check if allready build...
		c = document.getElementById('div_' + obj.id);
		if(c){
			//unhide...
			c.style.visibility = 'visible';
			c.style.zIndex = last_zIndex++;
			c.style.top = T_pos + 'px';
			c.style.left = L_pos + 'px';
		}else{
			//build new div
			subObj = document.createElement('div');
			subObj.id = 'div_' + obj.id;
			subObj.className = 'submenu';
			subObj.style.position = 'absolute';
			subObj.style.zIndex = last_zIndex++;
			subObj.style.width = menuWidth;
			subObj.style.top = T_pos + 'px';
			subObj.style.left = L_pos + 'px';	

			//write div to the body...
			document.getElementsByTagName('body')[0].appendChild(subObj);

			//build html for submenu
			content = "";
			m = submenu[obj.id];
			for(i=0;i<m.length;i++){

				//determin target for onclick...
				//	(sorry, I should rewrite this routine...)
				if(!m[i][2] || m[i][2] == '_self'){
					act = "self.location.href='" + m[i][1] + "';";
				}else if(m[i][2] == '_top'){
					act = "top.location.href='" + m[i][1] + "';";
				}else if(m[i][2] == '_blank'){
                    act = "window.open('" + m[i][1] + "');";
				}else if(m[i][2] == 'js'){
                    act = m[i][1];
				}else{
					//get javascript target (not tested yet)
					//TODO: check how to do this cleanly
					act = "window.frames['"+m[i][2]+"'].location.href='" + m[i][1] + "';";
				}

				//make item
				// is_ie variabila globala, setata in sniffer.js
				if(ie6_old)
					content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this)\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +" class=\"menuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + "\" id=\"" + obj.id + "_" + i +"\" title=\"" + m[i][3] + "\" style=\"padding-left: 8px; width: 172px; \">" + m[i][0] + "</div>";				
				else
				{
					if(is_ie)
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this)\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +" class=\"menuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + "\" id=\"" + obj.id + "_" + i +"\" title=\"" + m[i][3] + "\" style=\"padding-left: 8px;\">" + m[i][0] + "</div>";
					else
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this)\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +" class=\"menuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + "\" id=\"" + obj.id + "_" + i +"\" title=\"" + m[i][3] + "\">" + m[i][0] + "</div>";
				}
				adj[i] = obj.id + "_" + i;
			}
			
			if(content != "")
			{
				if(ie6_old)
				{
					content += "<div class=\"menuBottom\" style=\"width: 172px; height: 4px;\"></div>";
				}
				else
				{
					if(is_ie)
						content += "<div class=\"menuBottom\" style=\"width: " + (subObj.offsetWidth - 6) + "px;\"></div>";
					else
						content += "<div class=\"menuBottom\"></div>";
				}
			}

			//insert new menu
			subObj.innerHTML = content;

			//make all just made div's the same width...
			var firstItemWidth = 0;
			for(i=0;i<adj.length;i++)
			{
				if(is_ie)
				{
					if(firstItemWidth == 0) firstItemWidth = subObj.clientWidth - widthFix;
					document.getElementById(adj[i]).style.width = subObj.offsetWidth - widthFixIE + 'px';
				}
				else
				{
					if(firstItemWidth == 0) firstItemWidth = subObj.clientWidth - widthFix;
					document.getElementById(adj[i]).style.width = firstItemWidth + 'px';
				}
			}			
		}
	}
	
	//unset mousout of parent menus and make sure they are visible...
	x = "div";
	for(i=0;i<menuPath.length;i++){
		x+= '_' + menuPath[i];
		if(document.getElementById(x)){
			if(tmr[x])
				window.clearTimeout(tmr[x]);
			document.getElementById(x).style.visibility = 'visible';
		}
	}

}

//hide a submebu div
function hideSubmenu(obj){
	//get common part of div id
	closePath = obj.id.split('_');

	//hide path
	x = "div";
	for(i=0;i<closePath.length;i++){
		x+= '_' + closePath[i];
		if(document.getElementById(x))
			tmr[x] = window.setTimeout("document.getElementById('"+x+"').style.visibility = 'hidden';",100);
		//The timeout above is needed for MSIE browsers... Or else the menu's will disapear on EVERY mousout!!!
		//Please get a normal browser like Firefox, Mozilla or Opera!!
	}
}

//add an menu item to the config array (called in the config lines)
function menuItem(txt,url,tar, tooltip){
	return new Array(txt,url,tar, tooltip);
}

//*** Patch for firefox bug with focus on mouseover...
//		This function should be called onMouseOver of every iFrame that's under the menu structure.
function iFramePatch(){
	if(!document.all && lastOverId)
		hideSubmenu(document.getElementById(lastOverId));
}

//*** Now a patch for MSIE lag of CSS2 compliance!!
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')==-1){
	document.onmouseover = function(){
		obj = event.srcElement;
		if(obj != null  && (obj.className == 'menuItemFirst' || obj.className == 'menuItem' || obj.className == 'menuItemLast'))
			obj.className+='Over';
	}
	document.onmouseout = function(){
		obj = event.srcElement;
		if(obj != null  && (obj.className == 'menuItemFirstOver' || obj.className == 'menuItemOver' || obj.className == 'menuItemLastOver'))
			obj.className = obj.className.substring(0,(obj.className.length - 4));
	}
}
//*** End of msie patch
