// positioning functions
function absLeft(obj) {
	var pos = obj.offsetLeft;
	var newobj = obj.offsetParent;
	while(newobj.tagName.toLowerCase() != "body") {
		pos = pos + newobj.offsetLeft;
		newobj = newobj.offsetParent;
	}
	pos = pos + newobj.offsetLeft;
	return pos;
}

function absTop(obj) {
	var pos = obj.offsetTop;
	var newobj = obj.offsetParent;
	while(newobj.tagName.toLowerCase() != "body") {
	    pos = pos + newobj.offsetTop;
	    newobj = newobj.offsetParent;
	}
	pos = pos + newobj.offsetTop;
	return pos;
}


// Browser check
var ie, ffox, opera;
ie = (navigator.appName.indexOf("Microsoft") != -1);
ffox = (navigator.userAgent.indexOf("Firefox") != -1);
opera = (navigator.userAgent.indexOf("Opera") != -1);

// Browser width and height
var docw = document.body.offsetWidth;
var maxw = maxwidth();

// Menu engine script
var menuarr = new Array();
var subflag = false;
var blk = "";
var menuclass = "menuitem";
var menuhdrclass = "menuhdr";
var curclass;
var curs = "hand";
var clrimg = "pixel.gif";
var subarr = new Array();
var subcount = 0;
var suboverlap = 4;
var subobj;

if(ffox) {
	curs = "pointer";
}

if(ffox || ie || opera) {
	var mxobj, mobj;
	var menuw = 0;

	// build menus
	for(i = 0; i < menumatrix.length; i++) {
		blk = "";
		blk = blk + "<div class=\"menu\" id=\"" + menumatrix[i][0];
		blk = blk + "\" style=\"position: absolute;"; // z-index: 2;";
		if(menumatrix[i][1]==null) {
			blk = blk + " left: 0px; top: 0px;";
		} else {
			mxobj = document.getElementById(menumatrix[i][1]);
			if(menumatrix[i].length > 4) {
				blk = blk + " left: " + (absLeft(mxobj) + menuxoff +
					menumatrix[i][4]) + "px;";
			} else {
				blk = blk + " left: " + (absLeft(mxobj) + menuxoff) + "px;";
			}
			// blk = blk + " top: " + (absTop(mxobj) + menuyoff) + "px;";
			blk = blk + " top: " + (absTop(mxobj) + menuyoff + (ffox ? -22 : 0)) + "px;";
			mxobj.menuName = menumatrix[i][0];
		}
		if(menumatrix[i].length > 3) {
			menuw = menumatrix[i][3];
			if(menuw > 0) blk = blk + " width: " + menumatrix[i][3] + "px;";
		}
		blk = blk + " visibility: hidden;\"";
		blk = blk + " onmouseover=\"menuhold('" + menumatrix[i][0] + "')\"";
		blk = blk + " onmouseout=\"menuout('" + menumatrix[i][0] + "')\">\n";
		
		menuarr = eval(menumatrix[i][2]);
		
		// add menu items
		for(j = 0; j < menuarr.length; j++) {
			curclass = menuclass;
			if(menuarr[j].length > 3) { if(menuarr[j][3]) { curclass = menuhdrclass; } }
			if(menuarr[j].length > 4) { if(menuarr[j][4]) {
				blk = blk + "<div style=\"padding: 0px;\">" +
					"<img src=\"" + clrimg + "\" width=\"1\" height=\"1\"></div>\n";
			} }
			if(menuarr[j][2] != null && menuarr[j][2] != "") { subflag = true } else { subflag = false }

			blk = blk + "<div class=\"" + curclass + "\" id=\"i" + i + "j" + j + "\"" +
				" onmouseover=\"itemin(this, '" + menuarr[j][1] + "');";
			if(subflag) { blk = blk + " subin('" + menuarr[j][2] + "');" }
			blk = blk + "\" onmouseout=\"itemout(this, '" + curclass + "');";
			if(subflag) { blk = blk + " subout('" + menuarr[j][2] + "');" }
			blk = blk + "\" onmouseup=\"itemclick('" + menuarr[j][1] + "')\">";
			//if(subflag) { blk = blk + subimage + "&nbsp;"; }
			if(subflag) { blk = blk + "<div style=\"float: right;\">" + subimage + "</div>"; }
			else { blk = blk + "<div style=\"float: right;\">&nbsp;</div>"; }
			blk = blk + menuarr[j][0];
			blk = blk + "</div>\n";
			
			
			if(subflag) {
				subarr.push([menuarr[j][2], menumatrix[i][0], "i" + i + "j" + j]);
			}
		}
		if(menuendspace > 0) {
			blk = blk + "<div class=\"" + menuclass + "\" style=\"padding: 0px;\">" +
				"<img src=\"" + clrimg + "\" width=1 height=" + menuendspace + "></div>\n";
		}
		blk = blk + "</div>\n";

		document.write(blk);
		
		// adjust position if menu leaks beyond right border
		mobj = document.getElementById(menumatrix[i][0]);
		if(parseInt(mobj.style.left) + mobj.offsetWidth > maxw) {
			mobj.style.left = (maxw - mobj.offsetWidth) + "px";
		}
		

	}
	
	// position submenus
	for(i = 0; i < subarr.length; i++) {
		parobj = document.getElementById(subarr[i][1]);
		objx = parobj.offsetLeft;
		objy = parobj.offsetTop;
		parelem = document.getElementById(subarr[i][2]);
		elemy = parelem.offsetTop;

		subobj = document.getElementById(subarr[i][0]);
		subobj.parObject = parobj;
		subobj.origWidth = subobj.offsetWidth;
		
		if(subobj.offsetWidth + objx + parobj.offsetWidth > maxw) {
			subobj.style.left = (objx - subobj.offsetWidth + suboverlap) + "px";
		} else {
			subobj.style.left = (objx + parobj.offsetWidth - suboverlap) + "px";
		}
		subobj.style.top = (objy + elemy - 1) + "px";
	}

	// create menu shadows
	if(menushadow) {
		for(i = 0; i < menumatrix.length; i++) {
			mobj = document.getElementById(menumatrix[i][0]);
			blk = "";
			blk = blk + "<div class=\"menushadow\" id=\"s" + menumatrix[i][0] +
				"\" style=\"position: absolute;" +
				" left: " + (mobj.offsetLeft + shadowoffset) + "px;" +
				" top: " + (mobj.offsetTop + shadowoffset) + "px;" +
				" width: " + mobj.offsetWidth + "px; height: " + mobj.offsetHeight + "px;" +
				" visibility: hidden;\">&nbsp;</div>\n";
			document.write(blk);
		}
	}

	// add menu tabs
	if(menutab) {
		blk = "";
		blk = blk + "<div class=\"menu\" id=\"menutab\"";
		blk = blk + " style=\"position: absolute;";
		blk = blk + " visibility: hidden;\"";
		blk = blk + "><div class=\"" + menuclass + "\" id=\"menutabitem\"";
		blk = blk + ">&nbsp;</div>\n</div>";
		document.write(blk);
	}
}


// Roll-over functions

var timeoutid;
var currmenu = "";
var cursub = new Array(5);
var level = 0;


function menuin(obj) {
	if(!ie && !ffox && !opera) return;
	menuclear();
	currmenu = obj.menuName;
	menupop(currmenu, "visible");
	menucursor(currmenu, curs);
	if(menutab) { tabshow(obj); }
}
function menuout() {
	if(!ie && !ffox && !opera) return;
	timeoutid = setTimeout("allhide()", 500);
}
function menuhold(item) {
	clearTimeout(timeoutid);
	menupop(item, "visible");
}

function menupop(item, vis) {
	var obj = document.getElementById(item);
	if(menushadow) document.getElementById("s" + item).style.visibility = vis;
	obj.style.visibility = vis;
}

function menuclear() {
	if(currmenu != "") { 
		clearTimeout(timeoutid);
		menupop(currmenu, "hidden");
	}
	allhide();
}

function menucursor(item, curstype) {
	var obj = document.getElementById(item);
	obj.style.cursor = curstype;
}

function itemclick(newurl) {
	location.href = newurl;
}
function itemin(obj, objurl) {
	obj.className = obj.className + "over"; //"menuitemover";
	window.status = objurl;
}
function itemout(obj, cls) {
	obj.className = cls;
	window.status = "";
}

function allhide(exceptitem) {
	var obj;
	for(i = 0; i < menumatrix.length; i++) {
		obj = document.getElementById(menumatrix[i][0]);
		if(exceptitem != menumatrix[i][0]) {
			obj.style.visibility = "hidden";
			if(menushadow) {
				document.getElementById("s" + menumatrix[i][0]).style.visibility = "hidden";
			}
		}
	}
	if(menutab) { document.getElementById("menutab").style.visibility = "hidden"; }
}

function subin(item) {
	menupop(item, "visible");
	menucursor(item, curs);
}
function subout(item) {
	menupop(item, "hidden");
}


function tabshow(obj) {
	var tabobj = document.getElementById("menutab");
	var tiobj = document.getElementById("menutabitem");
	
	tabobj.style.left = (absLeft(obj) + menuxoff) + "px";
	tabobj.style.top = (absTop(obj) + menuyoff) + "px";
	//alert(obj.menuName + "\n" + obj.innerHTML + "\n" + obj.href + "\n" + tiobj.innerHTML);
	tiobj.innerHTML = obj.innerHTML;
	tabobj.onmouseover = function() { menuhold(obj.menuName); window.status = obj.href;  }
	tabobj.onmouseout = function() { menuout(obj.menuName) }
	tabobj.onmouseup = function() { itemclick(obj.href) }
	tabobj.style.visibility = "visible";
	tabobj.style.cursor = curs;
}

allhide();


// window resizing functions
function maxwidth() {
	return (menuxmax > 0) ? (menuxfloat ? ((docw - menuxmax) / 2 + menuxmax) : menuxmax) : docw;
}

function winresize() {
	docw = document.body.offsetWidth;
	maxw = maxwidth();
	
	for(i = 0; i < menumatrix.length; i++) {
		if(menumatrix[i][1]!=null) {
			miobj = document.getElementById(menumatrix[i][1]);
			muobj = document.getElementById(menumatrix[i][0]);
			muobj.style.left = (absLeft(miobj) + menuxoff) + "px";
			if(parseInt(muobj.style.left) + muobj.offsetWidth > maxw) {
				muobj.style.left = (maxw - muobj.offsetWidth) + "px";
			}
		}
	}		
			
	var smobj, pw, pl;
	
	for(i = 0; i < subarr.length; i++) {
		smobj = document.getElementById(subarr[i][0]);
		pw = smobj.parObject.offsetWidth;
		pl = parseFloat(smobj.parObject.style.left);
		if(maxw < (smobj.origWidth + pw + pl)) {
			smobj.style.left = (pl - smobj.origWidth + suboverlap) + "px";
		} else {
			smobj.style.left = (pl + pw - suboverlap) + "px";
		}
	} 
	
	if(menushadow) {
		for(i = 0; i < menumatrix.length; i++) {
			mobj = document.getElementById(menumatrix[i][0]);
			smobj = document.getElementById("s" + menumatrix[i][0]);
			smobj.style.left = (parseInt(mobj.style.left) + shadowoffset) + "px";
		}
	}
}
window.onresize = winresize;

// rollover image functions

function imgrollon(imgobj) {
	imgobj.origsrc = imgobj.src;
	imgobj.src = imgobj.getAttribute("rollsrc");
}
function imgrolloff(imgobj) {
	if(imgobj.origsrc) {
		imgobj.src = imgobj.origsrc;
	}
}