// JavaScript Document

var _dropMenus = new Array();
var _fadeSpeed = 20;
var _fadeAnimator;
var isIE = (window.navigator.appName.toLowerCase().indexOf("explorer")>-1);

function registerTopNav(blockobj,callerobj,regname) {
	// Input check
	
	var obj, obj2;
	
	if (typeof blockobj == "string")
	{
		obj = document.getElementById(blockobj);
	}
	else if (typeof blockobj != "undefined" && blockobj != null)
	{
		obj = blockobj;
	}
	else
		return false;
	if(typeof callerobj == "string")
	{
		obj2 = document.getElementById(callerobj);
	}
	else if (typeof callerobj != "undefined" && callerobj != null)
	{
		obj2 = callerobj;
	}
	else
		return false;
	
	if (typeof regname != "string")
	{
		return false;
	}
	
	if(obj == null || obj2 == null)
		return;
		
	// Creating menu object
	obj.setAttribute("myregname",regname);
	if (isIE) {
		// Set opacity
		obj.style.filter = "Alpha(opacity=0)";
	} else {
		obj.style.opacity = 0;
	}
	obj2.setAttribute("myregname",regname);
	var tempobj = new Object();
	tempobj = {
		_myName: regname,
		_myObject: obj,
		_callerMenu: obj2,
		_alpha: 0,
		_state: "idle"
	};

	_dropMenus.push(tempobj);

}

function _getMenuObjectByName(name) {
	for (var i=0;i<_dropMenus.length;i++) {
		if (_dropMenus[i]._myName == name) return _dropMenus[i];
	}
	return null;
}

function InitTopNav()
{
	var arrMenuItems = new Array();
	var divsArray = document.getElementsByTagName("div");
	for(var i = 0; i < divsArray.length; i++)
	{
		if(divsArray[i].id.indexOf("menuRow") != -1)
			arrMenuItems[arrMenuItems.length] = divsArray[i].id.substr(7, divsArray[i].id.length - 7);
		if(divsArray[i].id.indexOf("menuContainer") != -1)
			arrMenuItems[arrMenuItems.length] = divsArray[i].id.substr(13, divsArray[i].id.length - 13);
	}
	

	if(arrMenuItems == null || arrMenuItems.length == 0)
	{		
			return;
	}
	for(var i = 0; i < arrMenuItems.length; i++)
		registerTopNav("menuContainer" + arrMenuItems[i], "menuRow" + arrMenuItems[i], arrMenuItems[i]);
		
	_fadeAnimator = window.setInterval(faderCore,50);
	document.body.onmousemove = topmenuhandler;

}

function faderCore() {
	for (var i=0;i<_dropMenus.length;i++) {
		var thismenu = _dropMenus[i];
		
		switch (thismenu._state.toLowerCase()) {
			case "idle": /* Do nothing */break;
			case "show":
				if (thismenu._alpha > 100) {
					thismenu._alpha = 100;
					thismenu._state = "idle";
				} else {
					if (thismenu._alpha == 0) thismenu._myObject.style.display = "block";
					// Calc location
					var locx = _getRealX(thismenu._callerMenu) - (thismenu._myObject.offsetWidth - thismenu._callerMenu.offsetWidth);
					var locy = _getRealY(thismenu._callerMenu) + thismenu._callerMenu.offsetHeight;
					thismenu._myObject.style.top = locy + "px";
					thismenu._myObject.style.left = locx + "px";
													
					// Set alpha
					if (isIE) {
						thismenu._myObject.style.filter = "Alpha(opacity="+thismenu._alpha+")";
					} else {
						thismenu._myObject.style.opacity = (thismenu._alpha / 100) + "";
					}
					// Step alpha
					thismenu._alpha += _fadeSpeed;
					
					thismenu._myObject.style.visibility = "visible";
				}
				break;
			case "hide":
				
				if (thismenu._alpha < 0) {
					thismenu._alpha = 0;
					thismenu._state = "idle";
					thismenu._myObject.style.visibility = "hidden";
					} else {
					// Calc location
					var locx = _getRealX(thismenu._callerMenu) - (thismenu._myObject.offsetWidth - thismenu._callerMenu.offsetWidth);
					var locy = _getRealY(thismenu._callerMenu) + thismenu._callerMenu.offsetHeight;
					thismenu._myObject.style.top = locy + "px";
					thismenu._myObject.style.left = locx + "px";
					// Set alpha
					if (isIE) {
						thismenu._myObject.style.filter = "Alpha(opacity="+thismenu._alpha+")";
					} else {
						thismenu._myObject.style.opacity = thismenu._alpha / 100;
					}
					// Step alpha
					thismenu._alpha -= _fadeSpeed;
				}
				break;
		}
	}
}

/* Handlers and helpers */

function _getEventSource(ev) {
	if (typeof ev != "undefined" && ev != null) {
		return ev.target;
	} else {
		return event.srcElement;
	}
}

function _isChildOf(which,ofwho) {
	var checking = which;
	var iters = 50;
	while (iters>0) {
		if (checking == document.body && ofwho != document.body) return false;
		if (checking.offsetParent == ofwho) return true;
		checking = checking.offsetParent;
		iters--;
	}
	return false;
}

function _isPartOfNav(whichobj) {
	for (var i=0;i<_dropMenus.length;i++) {
		var thismenu = _dropMenus[i];
		if (_isChildOf(whichobj,thismenu._myObject) == true || _isChildOf(whichobj,thismenu._callerMenu) == true) {
			// Is part of nav
			return _dropMenus[i];
		}
	}
	return false;
}

function _doHideAllMenus() {
	for (var i=0;i<_dropMenus.length;i++) _dropMenus[i]._state = "hide";
}

function _doHideAllMenusExcept(which)
{

	for (var i=0;i<_dropMenus.length;i++)
		{
		 _dropMenus[i]._state = (_dropMenus[i]== which)? "show" : "hide" ;
		}
}

function _getRealX(obj) {
	var thisobj = obj;
	var pos = obj.offsetLeft;
	while (1>0) {
		if (thisobj == document.body) break;
		thisobj = thisobj.offsetParent;
		pos += thisobj.offsetLeft;
	}
	return pos;
}

function _getRealY(obj) {
	var thisobj = obj;
	var pos = obj.offsetTop;
	while (1>0) {
		if (thisobj == document.body) break;
		thisobj = thisobj.offsetParent;
		pos += thisobj.offsetTop;
	}
	return pos;
}

function topmenuhandler(ev) {
	var who = _getEventSource(ev);
	var partof = _isPartOfNav(who);

	if (partof == false) {
		_doHideAllMenus();
	} else {
		_doHideAllMenusExcept(partof);
	}
}
