/**
* CSShiarchMenu v0.6
*	written by: me[AT]daantje[DOT]nl
*	last update: Sat Nov 26 13:08:45 CET 2005
*
*	Documentation:
*		Build this small script cause all the gpl-ed scripts I found where too big
*		and had too many options I never going to use, or the config for the menu
*		was not easy to set with a PHP routine.
*
*	License:
*		LGPL. (http://www.gnu.org/copyleft/lesser.txt)
*
*	Dontate:
*		Please, when you like my script, click a view banners on
*		http://www.dantje.nl Or use my pay-pal donation button.
*/
//alert(typeof(toString(window.location)));
	
//declare
var submenu = new Array();
var tmr = new Array();
var adj = new Array();
var last_zIndex = 10000;
var lastOverId = '';
var innerHeight = 768;


// EXAMPLE CONFIG
var alignSubmenu 	= 'bottom';		//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
var adjustFirst		= true;			//overlap the first submenu too?
var adjustTop		= 0;			//move the submenu's some pixels. Positive number (10) for down, negative (-10) for up.
var adjustLeft		= 0;			//move the submenu's some pixels. Positive number (10) for right, negative (-10) for left.

//build or unhide submenu div...
function buildSubmenu(obj,align,color,m_width,m_top_adjust){
	lastOverId = obj.id;

	//overrule default alignment of this submenu
	if(!align)
		align = alignSubmenu;

	//get common part of div id
	menuPath = obj.id.split('_');
	
	if(menuPath.length == 2)
		sTemp = "width: 180px";
	else
		sTemp = "width:"+m_width+"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';
		}
	}

	//check if we have a submenu of the obj...
	if(submenu[obj.id]){
		//calc position of mouseover
		d = obj;
		if(d){
			L_pos = d.offsetLeft + d.offsetWidth;
			T_pos = d.offsetTop;
			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 && align == 'bottom'){
			L_pos-= obj.offsetWidth;
			T_pos+= obj.offsetHeight;
		}

		//move the submenu (overlap?)
		if((adjustTop || adjustLeft) && (adjustFirst || (!adjustFirst && obj.className.indexOf('menuItem') >= 0))){
			L_pos+= adjustLeft;
			T_pos+= adjustTop;
		}
		
		if(m_top_adjust)
		  T_pos += m_top_adjust;
		
		//Only parse when position is higher than 0,0... Else we dont have the right position!
		if(L_pos >= 0 && T_pos >0){
			//check if allready build...
			c = document.getElementById('div_' + obj.id);
			var myMenuH = (submenu[obj.id].length > 0  ? submenu[obj.id].length * (screen.availheight ? 28 : 22): 0 );
			var myScreenH = (screen.availheight ? screen.availheight : innerHeight);
			var myTopPos = 0;
			var ooTopPos = 0;
			
			myTopPos = menuFitPos(T_pos, T_pos, myMenuH, myScreenH);

			
			/*Postition adjustment for menu*/
			//if(obj.id == 'menu2' ||obj.id == 'menu3' || obj.id == 'menu4' || obj.id == 'menu5') {
			
//			if(obj.id == 'menu1'){
//				L_pos = 0;
//			}
			
			if(obj.id.length==5) {
				myTopPos += -3;
				L_pos += 3;
			}
			
			if(obj.id == 'menu1' || obj.id == 'menu3'){
				L_pos = 1;
			}else if(obj.id == 'menu4' || obj.id == 'menu9'){
				L_pos += -1;
			}else if(obj.id == 'menu2'){
				L_pos += -2;
			}
			
			//if(obj.id == 'menu2_0' ||obj.id == 'menu3_0' || obj.id == 'menu4_0' || obj.id == 'menu5_0') {
			if(obj.id.substring(obj.id.length-2,obj.id.length) == '_0') {
				myTopPos += 1;
			} 
			/*End of Postition adjustment for menu*/
			
			if(c){
				//unhide...
				c.style.visibility = 'visible';
				c.style.zIndex = last_zIndex++;

				//hard replace
				c.style.top = myTopPos+ "px";
				c.style.left = (L_pos - 1) + "px";
				
			} else {
				
				//build new div
				subObj = document.createElement('div');
				subObj.id = 'div_' + obj.id;
				subObj.className = 'submenu';
				subObj.style.position = 'absolute'; // default: absolute
				subObj.style.zIndex = last_zIndex++;
				//subObj.style.top =  myTopPos +"px"; 
				subObj.style.top =  myTopPos + "px";
				subObj.style.left = (L_pos - 1) + "px";

				//write div to the body...
				document.getElementsByTagName('body')[0].appendChild(subObj);

				//build html for submenu
				content = "";
				m = submenu[obj.id];
				var mmWidth=mmCnt=0;
				for(i=0;i<m.length;i++){

					//determin target for onclick...
					//	(sorry, I should rewrite this routine...)
					if(m[i][1]){
						if(m[i][1].substring(0,11) == 'javascript:'){
							act = m[i][1].substring(11);
						}else{
							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] == '_parent')
								act = "parent.location.href='" + m[i][1] + "';";
							else if(m[i][2] == '_blank')
								act = "window.open('" + m[i][1] + "');";
							else
								act = "window.frames['"+m[i][2]+"'].location.href='" + m[i][1] + "';";
						}
					}

					if(color=="green")
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this,'bottom','green')\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=greenmenuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\" style='" + sTemp + "'>" + m[i][0] + "</div>";
					else if(color=="brown")
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this,'bottom','brown')\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=brownmenuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\" style='" + sTemp + "'>" + m[i][0] + "</div>";
					else if(color=="blue")
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this,'bottom','blue')\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=bluemenuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\" style='" + sTemp + "'>" + m[i][0] + "</div>";
					else if(color=="red")
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this,'bottom','red')\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=redmenuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\" style='" + sTemp + "'>" + m[i][0] + "</div>";
					else
						content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this,'bottom','normal')\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=menuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\" style='" + sTemp + "'>" + m[i][0] + "</div>";
					adj[i] = obj.id + "_" + i;
				}

				//insert new menu
				subObj.innerHTML = content; 
//				subObj.innerHTML = content + '<span style=\'font: bold blue; background: #CCCCCC;\'>OT:'+ T_pos+' T:'+subObj.style.top+ ' Srh:'+myScreenH + ' H:' + myMenuH + 'SubH:(' + document.body.scrollTop+') ['+obj.id+'/'+ooTopPos+'/'+ooTopP2+'/'+myTopPos+']</span>';
				
			}
		}
	}
}

//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';",500);
		//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){
	return new Array(txt,url,tar);
}

//change div's on mouse over and out...
document.onmouseover = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirst' || obj.className == 'menuItem' || obj.className == 'menuItemLast' ||
	   obj.className == 'greenmenuItemFirst' || obj.className == 'greenmenuItem' || obj.className == 'greenmenuItemLast' ||
	   obj.className == 'brownmenuItemFirst' || obj.className == 'brownmenuItem' || obj.className == 'brownmenuItemLast' ||
	   obj.className == 'bluemenuItemFirst' || obj.className == 'bluemenuItem' || obj.className == 'bluemenuItemLast' ||
	   obj.className == 'redmenuItemFirst' || obj.className == 'redmenuItem' || obj.className == 'redmenuItemLast')
		obj.className+='Over';
}
document.onmouseout = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirstOver' || obj.className == 'menuItemOver' || obj.className == 'menuItemLastOver' ||
	   obj.className == 'greenmenuItemFirstOver' || obj.className == 'greenmenuItemOver' || obj.className == 'greenmenuItemLastOver' ||
	   obj.className == 'brownmenuItemFirstOver' || obj.className == 'brownmenuItemOver' || obj.className == 'brownmenuItemLastOver' ||
	   obj.className == 'bluemenuItemFirstOver' || obj.className == 'bluemenuItemOver' || obj.className == 'bluemenuItemLastOver' ||
	   obj.className == 'redmenuItemFirstOver' || obj.className == 'redmenuItemOver' || obj.className == 'redmenuItemLastOver')
		obj.className = obj.className.substring(0,(obj.className.length - 4));
}

//*** 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));
}
function menuFitPos(oTop, nTop, mHeight, scrSize) {
	var retTop = oTop;
	if((oTop + mHeight) <= (document.body.scrollTop + scrSize - 5)) {
		return oTop-(screen.availheight ? 10 : 2);
	} else {
		
		retTop = (document.body.scrollTop + scrSize - mHeight);
		//return retTop;
		if(retTop <= document.body.scrollTop) {
			if(document.body.scrollTop < oTop) {
				return document.body.scrollTop + 38;
			} else {
				return oTop;
			}
		} else {
			return retTop;
		}	
		
	}		
}

