/**
 * Menu-script for Campuz Mobile
 * @author   Mikael Berggren @ Wyoming
 */
function Menulayer() {

	var timer = null;
	var menuOffsetTop = 28; // The height of the menu bar
	var menuOffsetLeft = 0;
	var timeOut = 200; // Delay of showing/hiding the submenu (milisec)
	var activeMenuID = null;
	var activeImage = null;
	var shownIds = null;

	// Map functions
	this.init = _init;
	this.show = _show;
	this.hide = _hide;
	this.hideSubmenu = _hideSubmenu;

	function _init () {
		shownIds = new Array();
	}

	function _show ( id, e, isAct ) {
		// Hide all previous show submenus
		if (shownIds.length > 0)
			_hideAll();
		// Put this id in shown ids
		shownIds.push(id);

		// Check if this id is active (can only be one)
		if (isAct == '_act')
			activeImage = id;
		// If it's a new menu-tab, hide old
		if (activeMenuID != id && activeMenuID != null)
			_hideImg(activeMenuID);
		// Save id & show image
		activeMenuID = id;
		_showImg(id);

		var mnu = document.getElementById ? document.getElementById(id+'sub'): null;
		if (!mnu)
			return; // No submenu, no need to go further
		if ( mnu.onmouseout == null )
			mnu.onmouseout = _mouseoutCheck;
		if ( mnu.onmouseover == null )
			mnu.onmouseover = _clearTimer;
		_position(mnu,e,id);
	}

	// Called by onmouseout or this.mouseoutCheck
	function _hide () {
		_clearTimer();
		if (activeMenuID && document.getElementById)
			timer = setTimeout("menu.hideSubmenu('"+activeMenuID+"')", timeOut);
	}

	function _hideSubmenu ( sId ) {
		var oMenu = document.getElementById(sId + 'sub') ? document.getElementById(sId + 'sub') : null;
		if (oMenu != null)
			oMenu.style.display = 'none';
		_hideImg(sId);
	}

	function _position ( mnu, e, id ) {
		mnu.style.left = _getLeft(id) + menuOffsetLeft + 'px';
		mnu.style.top = _getTop(id) + menuOffsetTop + 'px';
		timer = setTimeout("document.getElementById('" + activeMenuID + "sub').style.display = 'block'", timeOut);
	}

	function _mouseoutCheck ( e ) {
		e = e? e: window.event;
		// is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
		var mnu = document.getElementById(activeMenuID);
		var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
		if ( mnu != toEl && !_contained(toEl, mnu) )
			_hide();
	}

	// returns true of oNode is contained by oCont (container)
	function _contained ( oNode, oCont ) {
		if (!oNode) return; // in case alt-tab away while hovering (prevent error)
		while ( oNode = oNode.parentNode )
			if ( oNode == oCont ) return true;
		return false;
	}

	function _clearTimer () {
		if (timer)
			clearTimeout(timer);
	}

	function _showImg ( sId ) {
		var oLink = document.getElementById(sId);
		imgSrc = oLink.firstChild.src;
		if (imgSrc.indexOf('_act.gif') == -1) {
			imgSrc = imgSrc.substring(0, imgSrc.indexOf('.gif')) + '_act.gif';
			oLink.firstChild.src = imgSrc;
		}
	}

	function _hideImg ( sId ) {
		var oLink = document.getElementById(sId);
		var imgSrc = oLink.firstChild.src;
		if (imgSrc.indexOf('_act.gif') > 0 && activeImage != sId)
			oLink.firstChild.src = imgSrc.substring(0, imgSrc.indexOf('_act.gif')) + '.gif';
	}

	function _hideAll () {
		for (var k in shownIds) {
			var oDiv = (document.getElementById(shownIds[k]+'sub')) ? document.getElementById(shownIds[k]+'sub') : null;
			if (oDiv != null)
				oDiv.style.display = 'none';
		}
		shownIds = new Array();
	}

	function _getLeft ( ref ) {
		var offsetObj = document.getElementById(ref);
		var objOffsetTop = objOffsetLeft = 0;
		while(offsetObj != document.body){

			if(offsetObj.id == 'sitecontainer' && offsetObj.offsetLeft == 0)
				objOffsetLeft += 50;
			else	
				objOffsetLeft += offsetObj.offsetLeft;

			offsetObj = offsetObj.offsetParent;
		}
		return objOffsetLeft;
	}

	function _getTop ( ref ) {
		var offsetObj = document.getElementById(ref);
		var objOffsetTop = objOffsetLeft = 0;
		while(offsetObj != document.body){

			if(offsetObj.id == 'sitecontainer' && offsetObj.offsetTop == 0)
				objOffsetTop += 22;
			else	
				objOffsetTop += offsetObj.offsetTop;

			offsetObj = offsetObj.offsetParent;
		}
		return objOffsetTop;
	}
}

/**
 * Initiate class
 */
var menu = new Menulayer();
menu.init();