// JavaScript Document

var menuwidth = '150px'			//default menu width
var menubgcolor = 'lightyellow'	//menu bgcolor
var disappeardelay = 250		//menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick = "yes"	//hide menu when user clicks within menu?

var ie4 = document.all
var ns6 = document.getElementById && !document.all

//if (ie4||ns6)
//	document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>');

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getposOffset(what, offsettype)
{
	var to_down = (navigator.appName.indexOf("Netscape") != -1) ? 0 : 1;
	var to_left = (navigator.appName.indexOf("Netscape") != -1) ? 1 : 0;
	
	var td_with_a = what.parentNode;
	var mm_table = td_with_a.offsetParent;
	
	var drop_down_left = findPosX(td_with_a) - to_left;
	
	var max_drop_down_right = findPosX(mm_table) + mm_table.offsetWidth;
	var cur_drop_down_right = drop_down_left + parseInt(dropmenuobj.offsetWidth);
	var right_diff = cur_drop_down_right - max_drop_down_right;
	
	if (right_diff > 0){
		drop_down_left = drop_down_left - right_diff + 2 - to_left;	// 2 because of borders
	}

	//var msg = 'right_diff: ' + right_diff;
	//log(msg, 1)
	
	return (offsettype == "left")
			? drop_down_left
			: findPosY(td_with_a) + td_with_a.offsetHeight + to_down;
}

function showhide(obj, e, visible, hidden, menuwidth)
{
	if (ie4||ns6)
		dropmenuobj.style.left=dropmenuobj.style.top="-500px"
	
	if (menuwidth != "" && typeof(menuwidth) != 'undefined')
	{
		dropmenuobj.widthobj = dropmenuobj.style;
		dropmenuobj.widthobj.width = menuwidth;
	}
	
	if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
		obj.visibility = visible;
	else if (e.type=="click")
		obj.visibility = hidden;
}

function iecompattest()
{
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

function clearbrowseredge(obj, whichedge)
{
	var edgeoffset = 0
	
	if (whichedge=="rightedge")
	{
		var windowedge = ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
		dropmenuobj.contentmeasure = dropmenuobj.offsetWidth
		
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
	}
	else {
		var topedge = ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset;
		var windowedge = ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
		dropmenuobj.contentmeasure = dropmenuobj.offsetHeight;
		
		if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
		{	//move up?
			edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight;
			
			if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
				edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
		}
	}
	return edgeoffset
}

function populatemenu(what)
{
	if (ie4||ns6){
		var s = what.join("");
		dropmenuobj.innerHTML = (s != '') ? '<div>'+s+'</div>' : '';
	}
}

// onMouseOver
function dropdownmenu(obj, e, menucontents, menuwidth)
{
	if (window.event)
		event.cancelBubble = true;
	else if (e.stopPropagation)
		e.stopPropagation();
	
	clearhidemenu();

	dropmenuobj = document.getElementById ? document.getElementById("dropmenudiv") : dropmenudiv;
	
	populatemenu(menucontents);
	
	if (ie4||ns6 && dropmenuobj.innerHTML != '')
	{
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth);
		dropmenuobj.x = getposOffset(obj, "left");
		dropmenuobj.y = getposOffset(obj, "top");
		dropmenuobj.style.left = dropmenuobj.x - clearbrowseredge(obj, "rightedge")+"px";
		dropmenuobj.style.top = dropmenuobj.y - clearbrowseredge(obj, "bottomedge")+/*obj.offsetHeight+*/"px";
	//	dropmenuobj.style.width = obj.parentNode.offsetWidth+4+'px';
		_show_hide_IFrame(true, dropmenuobj);
	//	_show_hide_shadow_IFrame(true, dropmenuobj);
	}
	return clickreturnvalue();
}

function clickreturnvalue()
{
	return (ie4||ns6) ? false : true;
}

function contains_ns6(a, b)
{
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	return false;
}

function dynamichide(e)
{
	if (ie4&&!dropmenuobj.contains(e.toElement))
		delayhidemenu();
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		delayhidemenu();
}

function hidemenu(e)
{
	if (typeof dropmenuobj != "undefined")
	{
		if (ie4||ns6) {
			dropmenuobj.style.visibility = "hidden";
			_show_hide_IFrame(false, dropmenuobj);
		//	_show_hide_shadow_IFrame(false, dropmenuobj);
		}
	}
}

// onMouseOut
function delayhidemenu()
{
	if (ie4||ns6)
		delayhide = setTimeout("hidemenu()", disappeardelay);
}

function clearhidemenu()
{
	if (typeof delayhide != "undefined")
		clearTimeout(delayhide);
}

function _show_hide_IFrame(state, DivRef)
{
	var browser = navigator.appName;
	var el = document.getElementById('place_for_iframe');
	if (state)
	{
		if (browser == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') == -1)
		{
			el.innerHTML = '<iframe id="DivShim" style="position: absolute;  width: 1px; height: 1px; z-index: 2;" frameborder="no" scrolling="no"></iframe>';
			var IfrRef = document.getElementById('DivShim');
			IfrRef.style.top = DivRef.style.top;
			IfrRef.style.left = DivRef.style.left;
			IfrRef.style.width = DivRef.offsetWidth;
			IfrRef.style.height = DivRef.offsetHeight;
		}
	}
	else {
		if (browser == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') == -1){
			el.innerHTML = '';
		}
	}
}

function _show_hide_shadow_IFrame(state, DivRef)
{
	var browser = navigator.appName;
	var el_1 = document.getElementById('place_for_shd_iframe_1');
	var el_2 = document.getElementById('place_for_shd_iframe_2');
	if (state)
	{
		//if (browser == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') == -1)
		//{
			el_1.innerHTML = '<iframe id="DivShimShaddowBottom" style="position: absolute;  width: 1px; height: 1px; z-index: 2; background-color:#cccccc;" frameborder="no" scrolling="no"></iframe>';
			var IfrRefBottom = document.getElementById('DivShimShaddowBottom');
			IfrRefBottom.style.top = parseInt(DivRef.style.top) + parseInt(DivRef.offsetHeight) + 'px';
			IfrRefBottom.style.left = parseInt(DivRef.style.left) + 3 + 'px';
			IfrRefBottom.style.width = parseInt(DivRef.offsetWidth) - 3 + 'px';
			IfrRefBottom.style.height = 2 + 'px';
			
			el_2.innerHTML = '<iframe id="DivShimShaddowRight" style="position: absolute;  width: 1px; height: 1px; z-index: 2; background-color:#cccccc;" frameborder="no" scrolling="no"></iframe>';
			var IfrRefRight = document.getElementById('DivShimShaddowRight');
			IfrRefRight.style.top = parseInt(DivRef.style.top) + 3 + 'px';
			IfrRefRight.style.left = parseInt(DivRef.style.left) + parseInt(DivRef.offsetWidth) + 'px';
			IfrRefRight.style.width = 2 + 'px';
			IfrRefRight.style.height = parseInt(DivRef.offsetHeight) - 1 + 'px';
		//}
	}
	else {
		//if (browser == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') == -1){
			el_1.innerHTML = '';
			el_2.innerHTML = '';
		//}
	}
}

function log(msg, clear)
{
	var log_div = document.getElementById('main_menu_log');
	if (clear == 1)
		log_div.innerHTML = '';
	log_div.innerHTML += ' ' + msg;
}

if (hidemenu_onclick == "yes")
	document.onclick = hidemenu;
