/* 
	Alimak Hek Menu Script
	JavaScript by Elin Tjerngren
	(c) 2005-2006, Artopod
	Last update: 2006-04-04 13:34:14
*/

var defaultId; // set in dropmenu highlight, defined in createMenu
var curId;

var menuEl=new Array();
var menuElNames=new Array();

// object for managing menu
function menuElement(descr,dropObj) {
	this.descr=descr;

	// images for tabs - paths in globals
	this.pictHiLight=defaultLoc+pictLoc+descr+"_1.gif";
	this.pictLow=defaultLoc+pictLoc+descr+"_0.gif";
	// preload imgs	
	this.preload1=new Image();
	this.preload1.src=this.pictHiLight;
	this.preload2=new Image();
	this.preload2.src=this.pictLow;
	// objects for easier handling in the script
	this.dropObj=dropObj; // M+descr 
	this.tabObj=document.getElementById('S'+descr);

	if (this.tabObj.firstChild)
		this.imgObj=this.tabObj.firstChild;

	// events
	this.tabObj.onmouseover=new Function("showMen('"+descr+"')");
	this.tabObj.onmouseout=hideMen;

	this.dropObj.onmouseout=hideMen;
	this.dropObj.onmouseover=new Function("mouseOverSub('"+descr+"')");

	// for timeout on mouseout
	this.hideTimer=0;

	/* methods */
	this.show=show;
	this.hide=hide;
	
	this.showing=false;
	
}

// create objects from classnames
function createMenu() {
	if (document.getElementById && document.createElement) {
		// create jsobjects of all menu elements
		var divs=document.body.getElementsByTagName('div');
		for (i=0;i<divs.length;i++) {
			if (divs[i].className.indexOf("dropmenu")!=-1 && divs[i].id.match(/^M/)) {
				var descriptive=divs[i].id.substring(1);
				menuEl[descriptive]=new menuElement(descriptive,divs[i]);
				menuElNames[menuElNames.length]=descriptive;
				if (divs[i].className.indexOf("highlight")!=-1) {
					defaultId=descriptive;
				}
			} 
		}
	}
}

// 
function resetMenu() {
	if (defaultId) {
		showMen(defaultId);
	}
}

/* show - method for object, and more functions */
function show() {
	this.dropObj.style.display='block'; 
	this.imgObj.src=this.pictHiLight;
	if (this.descr!=defaultId) { 
		if (defaultId) {
			menuEl[defaultId].hide();
		}
	}
	this.showing=true;
}

// show specific menu - hides others (called from events set in object)
function showMen(el) {
	// clear animation and switch picture for others----
	for (i=0;i<menuElNames.length;i++) {
		objname=menuElNames[i];
		if (objname!=el && objname!=defaultId) { 
			menuEl[objname].hide();
		}
	}
	// show this menu
	menuEl[el].show();
}



// method hide - and more funtions for hiding
function hide() {
	if (this.descr!=defaultId) {
		this.imgObj.src=this.pictLow;
	}
	if (navigator.userAgent.indexOf("Opera") != -1) // important because of opera 
		menuEl[this.descr].dropObj.style.display="none !important";
	else 
		menuEl[this.descr].dropObj.style.display="none";
	this.showing=false;

}
// hide specific menu (called from events set in object)
function hideMen(e) {
	var curId=this.id;
	// be sure that this isn't the default menu....
	if (curId.substring(1)!=defaultId) {
	
		var dropO,tabO;
		var X,Y,cancel,feather,objDest,objFrom;
	
		var menuJSObj=menuEl[curId.substring(1)];
		
		var dropO=menuJSObj.dropObj;
		var menO=document.getElementById("menu");
		var tabO=menuJSObj.tabObj;
		var imgObj=tabO.firstChild;
		/*while (imgObj.firstChild && imgObj.tagName.toLowerCase!="img") {
			imgObj=imgObj.firstChild;
		}*/
	
		if (curId.match(/^M/)) {
			type="drop";
			var pageOffsetXmin=dropO.offsetLeft;
			var pageOffsetXmax=pageOffsetXmin+dropO.offsetWidth;
			var pageOffsetYmin=tabO.offsetTop;
			var pageOffsetYmax=pageOffsetYmin+imgObj.offsetHeight+dropO.offsetHeight;
			feather=5;
		} else {
			type="tab";
			var pageOffsetXmin=tabO.offsetLeft;
			var pageOffsetXmax=pageOffsetXmin+imgObj.offsetWidth; // img in div
			var pageOffsetYmin=tabO.offsetTop;
			var pageOffsetYmax=dropO.offsetTop+dropO.offsetHeight; // img in div
			feather=0;
		}
		// make it relative to centered page 
		// note: Win IE 5.01 doesn't recognize offsetLeft on position relative elements, therefor buggy on this one 
		// also buggy on Opera 7..
		pageOffsetXmin+=document.getElementById("page").offsetLeft;
		pageOffsetXmax+=document.getElementById("page").offsetLeft;
		//pageOffsetYmin+=document.getElementById("page").offsetTop;
		//pageOffsetYmax+=document.getElementById("page").offsetTop;
		// make it relative to the menu from the top
		pageOffsetYmin+=document.getElementById("menu").offsetTop;
		pageOffsetYmax+=document.getElementById("menu").offsetTop;
			
		if (moz || navigator.userAgent.indexOf("Opera")!=-1) {
			var X=e.clientX;
			var Y=e.clientY;
			if (e.toElement)
				objDest=e.toElement;
		} else if (ie) {
			var X=window.event.clientX;//window.event.x;//+document.body.scrollLeft;//
			var Y=window.event.clientY-3;//window.event.y;//
			objDest=window.event.toElement;
			objFrom=window.event.fromElement;
			// destination should not be inside object, and this (the layer) should no be the destination
			if (type=="drop") {
				if ((dropO.contains(objFrom))&&(!dropO.contains(objDest))) cancel=false; 
				else if ((dropO.contains(objDest))||(objDest==dropO)) cancel=true;
				else if (dropO.contains(obj)) cancel=true;
			} else {
				if (dropO==objDest) cancel=false; 
			}
		} 
		
		if (objDest) {
			// test if it's inside area - tabs or dropdown
			if (objDest.id=='S'+curId.substring(1)) cancel=true;
			if (objDest.id=='M'+curId.substring(1)) cancel=true;
		}
		
		// "if not already set to cancel and not in the specified area for the menu then do..."
		if (!(cancel) && 
			!(X<=pageOffsetXmax+feather &&
			X>pageOffsetXmin-feather &&
			Y<=pageOffsetYmax &&
			Y>pageOffsetYmin-feather)
			) {
				if (type=="drop") {//|| Y>pageOffsetYmin-feather
					menuEl[curId.substring(1)].hideTimer=setTimeout("doTheHiding('"+curId+"')",1000);
				} else if (type=="tab") {
					resetMenu();
					menuEl[curId.substring(1)].hide();
				}
				menuEl[curId.substring(1)].showing=false;
		}
	}
}

// hides and resets menu:
function doTheHiding(curId) {
	menuEl[curId.substring(1)].hide();
	resetMenu();
}

// called from event on object - resets timer onmouseout
function mouseOverSub(el) {
	if (menuEl[el].hideTimer>0) {
		clearTimeout(menuEl[el].hideTimer);
		menuEl[el].hideTimer=0;
	}
}

