var GL_parentMenuId = 0;
var GL_childMenu = 0;
var GL_menuBar;
var GL_unself;
var gl_isMenuClicked;


function GL_menuClicked(menuId) {
    if ( gl_isLoaded && gl_isMenuClicked ) {
        gl_isMenuClicked = false;
        return;
    }
    gl_isMenuClicked = true;
    doMenu(menuId, '-1', 1);
}

function GL_menuMouseOverCall(menuId) {
    if (gl_isLoaded && gl_isMenuClicked ) {
        doMenu(menuId, '-1', 1);
    }
}

function doMenu(parentId, childId, isHorizontal) {
  if ((DragDropper.currentDrag)) //don't display menu while dragging
		return;
  
  var parentLeft, parentTop, childLeft, childTop;
  if ( isHorizontal == 1 ) {  
    parentLeft = -2;
  	parentTop = 18;
  	childLeft = 20;
  	childTop = -10;  
  } else if ( isHorizontal == 2 ) { 
  	parentLeft = -220;
  	parentTop = 20;
  	childLeft = -372;
  	childTop = -1;  
  } else {
  	parentLeft = 15;
  	parentTop = -2;
  	childLeft = 15;
  	childTop = -2;
  } 
    
  if (GL_parentMenuId) {
  	hideMenu(GL_parentMenuId); 
  }
  GL_parentMenuId = parentId;
  var parentMenuObj = findObject("menu" + parentId);
  GL_setMenuLocation(parentId, parentMenuObj, parentLeft, parentTop);

  if (GL_childMenu) hideMenu(GL_childMenu); 
  if ( childId > 0 ) {
	  var childMenuObj = findObject("menu" + childId);
	  GL_childMenu=childId;
	  GL_setMenuLocation(childId, childMenuObj, childLeft, childTop, parentMenuObj);	  
	} 
}

function GL_setMenuLocation(id, menuObj, leftOffset, topOffset, parentMenuObj){
	var pos = getAnchorPosition("hiddenImage" + id);
	var left = (pos.x + leftOffset);
	var top = (pos.y + topOffset);
	menuObj.style.left = left + 'px';
	menuObj.style.top = top + 'px';
	menuObj.style.visibility = "visible";    
	menuObj.style.display = "block";
	
	var windowDim = GL_getWindowDim();
	if (left + menuObj.offsetWidth > windowDim.width){
		if (parentMenuObj){
			var newLeft = left - parentMenuObj.offsetWidth - menuObj.offsetWidth + leftOffset;
			if (newLeft >= 0){
				menuObj.style.left = newLeft + 'px';
			}
		}
		else{
			var overhang = left + menuObj.offsetWidth - windowDim.width;
			menuObj.style.left = (left - overhang) + 'px';
		}
	}

	if(document.all && !Core.isOpera()) {
		var parentUnderIFrame = findObject('GL_underIFrame' + id);
	    if ( !parentUnderIFrame ) {
	    	//under IFrame is a hack to fix drop down menu bug in IE6
	       writeUnderIFrame('GL_underIFrame' + id);
	       parentUnderIFrame = findObject('GL_underIFrame' + id);
	    } 
	    if ( !parentUnderIFrame ) {
	    	return;
	    }
	    parentUnderIFrame.style.width = menuObj.offsetWidth;
	    parentUnderIFrame.style.height = menuObj.offsetHeight;
	    parentUnderIFrame.style.top = menuObj.style.top;
	    parentUnderIFrame.style.left = menuObj.style.left;
	    parentUnderIFrame.style.zIndex = menuObj.style.zIndex - 1;
	    parentUnderIFrame.style.visibility = "visible";
	    parentUnderIFrame.style.display= "block";
	    menuObj.parentNode.style.zIndex = 2;
	}
}

function GL_getWindowDim(){
	var dimObj = {};
	if (window.innerHeight !== undefined){
		dimObj.width = window.innerWidth;
		dimObj.height = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		dimObj.width = document.documentElement.clientWidth;
		dimObj.height = document.documentElement.clientHeight;		
	}
	else if (document.body.clientHeight !== undefined){
		dimObj.width = document.body.clientWidth;
		dimObj.height = document.body.clientHeight;
	}
	return dimObj;
}

function hideImage (id) {
	findObject("hiddenImage" + id).src="image/transparent.gif";
}

function GL_swapImage (id, imgSrc) {
	findObject("hiddenImage" + id).src=imgSrc;
}

function hideMenu(menuDivID) {
	var menuDiv = findObject('menu' + menuDivID);
	menuDiv.style.visibility = "hidden";
	menuDiv.display = 'none';
	
	if(document.all) {
		var underIFrame = findObject('GL_underIFrame' + menuDivID);
		if ( underIFrame ) {
			underIFrame.style.visibility = "hidden";
			underIFrame.display = 'none';	
		}	
	}
}

function turnOff() {
    gl_isMenuClicked = false;

    if (GL_parentMenuId) {
        hideMenu(GL_parentMenuId);
        GL_parentMenuId = 0;
	    if (GL_menuBar){
	    	hideMenuRows(GL_menuBar);
	    	GL_menuBar = null;
	    }
    }    
    if (GL_childMenu) {
        hideMenu(GL_childMenu);
        childMenu = 0;
    }
}
   
function timer(offon)  {
	if (offon == 0)	{
    	GL_unself = setTimeout('turnOff()', 1200);
    }
	if (offon ==1)	{
		clearTimeout(GL_unself);
	}
}	

function empty() {
	turnOff();
}

function showMenuRows(menubar, event){
	if (menubar.scrollHeight > menubar.offsetHeight && !GL_menuBar){
		if (menubar.originalHeight === undefined)
			menubar.originalHeight = menubar.offsetHeight;
		menubar.style.left = (menubar.offsetLeft - 1) + 'px'; 
		menubar.style.top = (menubar.offsetTop - 1) + 'px'; 
		menubar.style.height = menubar.scrollHeight + 'px';
		menubar.style.background = '#FFF';
		Core.appendClassName(menubar, 'horizontalMenuHover');
		GL_menuBar = menubar;
		Core.addEventHandler(document.body, 'mouseover', hideMenuRowsMouseoverCallback);		
	}
}

function hideMenuRowsMouseoverCallback(event){
	var node = Core.getNode(event);
	if (node != GL_menuBar && !Core.isAncestor(node, GL_menuBar)){
		Core.removeEventHandler(document.body, 'mouseover', hideMenuRowsMouseoverCallback);		
		hideMenuRows(GL_menuBar, event);
	}
}

function hideMenuRows(menubar, event){
	if (gl_isMenuClicked)
		return;
	if (menubar.originalHeight !== undefined){
		menubar.style.height = menubar.originalHeight + 'px';
		Core.removeClassName(menubar, 'horizontalMenuHover');
		menubar.style.left = ''; 
		menubar.style.top = '';
		menubar.style.background = 'transparent';
		GL_menuBar = null;
	}
}

function tabMouseOver(tab, event){
	Core.appendClassName(tab.parentNode.parentNode, 'tabLeftCornerHover');
	Core.appendClassName(tab.parentNode, 'tabRightCornerHover');
}

function tabMouseOut(tab, event){
	Core.removeClassName(tab.parentNode.parentNode, 'tabLeftCornerHover');
	Core.removeClassName(tab.parentNode, 'tabRightCornerHover');
}