/**
* CSShiarchMenu v0.10
*	written by: me[AT]daantje[DOT]nl
*	last update: Tue Jan 23 17:36:30 CET 2007
*
*	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, donate a coin or two
*		http://www.dantje.nl Use my pay-pal donation button.
*/

//declare
var submenu = new Array();
var cssHMtmr = new Array();
var cssHMadj = new Array();
var last_zIndex = 10000;
var lastOverId = '';

var CSSHdocStarted = true;


// 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 CSSuseArrows	= true;			//Use arrows when there are sub's
var cssHMadjustFirst		= false;		//overlap the first submenu too?
var cssHMadjustTop		= 0;			//move the submenu's some pixels. Positive number (10) for down, negative (-10) for up.
var cssHMadjustLeft		= 0;			//move the submenu's some pixels. Positive number (10) for right, negative (-10) for left.

/*
//fisrt submenu tree
submenu['menu1'] = new Array();
submenu['menu1'][0] = menuItem('my own website','http://www.daantje.nl','_top');
submenu['menu1'][1] = menuItem('other sites');
	submenu['menu1_1'] = new Array();
	submenu['menu1_1'][0] = menuItem('google','http://www.google.nl','demoFrame');
	submenu['menu1_1'][1] = menuItem('/.','http://www.slashdot.org','demoFrame');
	submenu['menu1_1'][2] = menuItem('Open in _BLANK:');
		submenu['menu1_1_2'] = new Array();
		submenu['menu1_1_2'][0] = menuItem('check test','http://www.google.nl','_blank');
		submenu['menu1_1_2'][1] = menuItem('test again','http://www.slashdot.org','_blank');
submenu['menu1'][2] = menuItem('This should be the last one');

//second submenu tree
submenu['menu2'] = new Array();
submenu['menu2'][0] = menuItem('project 1','http://www.daantje.nl','_self');
submenu['menu2'][1] = menuItem('project 2');

//side menu item 1
submenu['menu4'] = new Array();
submenu['menu4'][0] = menuItem('test 1','http://www.daantje.nl','_self');
submenu['menu4'][1] = menuItem('test 2');
	submenu['menu4_1'] = new Array();
	submenu['menu4_1'][0] = menuItem('test 1','http://www.daantje.nl','_self');
	submenu['menu4_1'][1] = menuItem('test 2','http://www.daantje.nl','_self');
	submenu['menu4_1'][2] = menuItem('test 3','http://www.daantje.nl','_self');
*/

// END OF EXAMPLE CONFIG


//build or unhide submenu div...
function buildSubmenu(obj,align,cssHMadjust){

	lastOverId = obj.id;

	//overrule default alignment of this submenu
	if(!align)
		align = alignSubmenu;

	//overrule default first item overlap
	if(cssHMadjust === true || cssHMadjust === false)
		cssHMadjustFirst = cssHMadjust;

	//get common part of div id
	menuPath = obj.id.split('_');

	//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(cssHMtmr[x])
				window.clearTimeout(cssHMtmr[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((cssHMadjustTop || cssHMadjustLeft) && (cssHMadjustFirst || (!cssHMadjustFirst && obj.className.indexOf('menuItem') >= 0))){
			L_pos+= cssHMadjustLeft;
			T_pos+= cssHMadjustTop;
		}

		//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);
			if(c){
				//unhide...
				c.style.visibility = 'visible';
				c.style.zIndex = last_zIndex++;

				//hard replace
				c.style.top = T_pos + "px";
				c.style.left = L_pos + "px";
			}else{
				//only when there are sub items!
				if(submenu[obj.id].length){

					//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.top = T_pos + "px";
					subObj.style.left = L_pos + "px";

					//patch max left to screen width
					//if(L_pos + )

					//write div to the body...
					document.getElementsByTagName('body')[0].appendChild(subObj);

					//build html for submenu
					var csshsubmenu = "";
					m = submenu[obj.id];
					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] + "';";
							}
						}

						//check if this menu has subs...
						subkey = obj.id + "_" + i;
						placeArrow = (submenu[subkey] && submenu[subkey].length && CSSuseArrows) ? true : false;

						//make item
						csshsubmenu+= "<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 +"\"><div class=menuItemArrow  id=\"arrow" + obj.id + "_" + i +"\" style=\"display:" + (placeArrow ? 'all' : 'none') + "\"></div>" + m[i][0] +"</div>";
						cssHMadj[i] = subkey;

					}

					//insert new menu
					subObj.innerHTML = csshsubmenu;
				}
			}
		}
	}
}

//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))
			cssHMtmr[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...
// FIXME: realy should rewrite this routines, they are almost the same... no time for it now...
document.onmouseover = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirst' || obj.className == 'menuItem' || obj.className == 'menuItemLast'){
		obj.className+='Over';
		//do arrow too
		arrow = document.getElementById('arrow'+obj.id);
		if(arrow)
			arrow.className = 'menuItemArrowOver';

		//do over on parents...
		x = "";
		overPath = obj.id.split('_');
		for(i=0;i<overPath.length - 1;i++){
			x+= overPath[i] + '_';
			o = document.getElementById(x.substring(0,x.length - 1));
			if(o && o.className.substring((o.className.length - 4)) != 'Over'){
				o.className+='Over';
				arrow = document.getElementById('arrow'+o.id);
				if(arrow)
					arrow.className = 'menuItemArrowOver';
			}
		}
	}
}
document.onmouseout = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirstOver' || obj.className == 'menuItemOver' || obj.className == 'menuItemLastOver'){
		obj.className = obj.className.substring(0,(obj.className.length - 4));
		//do arrow too
		arrow = document.getElementById('arrow'+obj.id);
		if(arrow)
			arrow.className = 'menuItemArrow';

		//mouseout parent too
		x = "";
		outPath = obj.id.split('_');
		for(i=0;i<outPath.length - 1;i++){
			x+= outPath[i] + '_';
			o = document.getElementById(x.substring(0,x.length - 1));
			if(o && o.className.substring((o.className.length - 4)) == 'Over'){
				o.className = o.className.substring(0,(o.className.length - 4));
				arrow = document.getElementById('arrow'+o.id);
				if(arrow)
					arrow.className = 'menuItemArrow';
			}
		}
	}
}

//*** 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));
}

