//
//Contains functions to show/hide/position global header tabs
//
//Created: 16/02/09

//Create Arry to store Timeout ids
var lTimerIDs=new Array();
var lOffTimerIDs=new Array();
var bShouldBeVisible=new Array();


function ShowNavTab(lpTabIndex){
	//Set timer to show menu after 0.2 secs
	bShouldBeVisible[lpTabIndex]=1;
	lTimerIDs[lpTabIndex]=setTimeout("DoShowNavTab("+lpTabIndex+")",200);
	

}

function DoShowNavTab(lpTabIndex){
		//Make sure it should be on
		if(bShouldBeVisible[lpTabIndex]!=1) return;


		//Get Elements (bottom navbar on header (menubar), Tab (each menu item), Tab Text (menu item text) and popup panel)
		var opMenuPopup=document.getElementById('mnupopup'+lpTabIndex);
		var opTab=document.getElementById('tab'+lpTabIndex);
		var opMenuBar=document.getElementById('menubar');
		
		//If panel isn't already visible make it visible
		if(opMenuPopup.style.display!='inline'){
			opMenuPopup.style.display='inline';
			//Highlight tabs
			opTab.className='global_header_nav_menus_text_ON';
			//work out where the menu bar is
			arrayleftTop = findPos(opMenuBar);
			//work out its left hand edge
			var lRightEdge=arrayleftTop[0]+opMenuBar.clientWidth;
			arrayleftTop = findPos(opTab);
			var lLeft = arrayleftTop[0];
			var lFudgeFactor=50;
			//if the right hand edge of the popup is over the right hand edge of the menu bar
			//shift it to the left by the same amount (plus a bit)
			if ((lLeft+opMenuPopup.clientWidth)>lRightEdge){
				lLeft=lLeft-(lLeft+opMenuPopup.clientWidth-lRightEdge)-lFudgeFactor;
			}
			//Store width of expanded div (IE6 hack)
			var lWidth=opMenuPopup.clientWidth;
			
			//Move the left hand side of popup to correct location
			opMenuPopup.style.left=lLeft+'px';
			
			//Restore width of expanded div (IE6 hack)
			opMenuPopup.style.width=lWidth+'px';
			
			//work out difference between width and offsetWidth
			var lBoxModelOffset=lWidth-(opMenuPopup.clientWidth-lWidth);
			
			//Restore it to its original width
			opMenuPopup.style.width=lBoxModelOffset+'px';
		}

		
	}
	function HideNavTab(lpTabIndex){
		
		//Set timer to hide menu after 0.2 secs
		lOffTimerIDs[lpTabIndex]=setTimeout("DoHideNavTab("+lpTabIndex+")",200);		
		bShouldBeVisible[lpTabIndex]=0;

	}
	function DoHideNavTab(lpTabIndex){
		//Only hide if it shouldn't be visible
		if (bShouldBeVisible[lpTabIndex]==0){
			document.getElementById('mnupopup'+lpTabIndex).style.display='none';
			document.getElementById('tab'+lpTabIndex).className='global_header_nav_menus_text';
		}
	}
	function ShowAccountTab(){
		var opMenuPopup=document.getElementById('mnuAccount');
		var opTab=document.getElementById('tabAccount');
		opMenuPopup.style.display='block';
		opTab.className='global_header_tab_account_container_ON';
		
		arrayleftTop = findPos(opTab);
			var lRightEdge=arrayleftTop[0]+opTab.clientWidth;
			
			var lLeft=lRightEdge-opMenuPopup.clientWidth;
			
			//Store width of expanded div (IE6 hack)
			var lWidth=opMenuPopup.clientWidth;
			var lPaddingOffset=20;
			//Move the left hand side of popup to correct location
			opMenuPopup.style.left=lLeft+'px';
			opMenuPopup.style.top=(arrayleftTop[1]+opTab.offsetHeight+lPaddingOffset)+'px';
	
			
			//Restore width of expanded div (IE6 hack)
			opMenuPopup.style.width=lWidth+'px';
			
			//work out difference between width and offsetWidth
			var lBoxModelOffset=lWidth-(opMenuPopup.clientWidth-lWidth);
			
			//Restore it to its original width
			opMenuPopup.style.width=lBoxModelOffset+'px';
		
	}
	function HideAccountTab(){
		document.getElementById('mnuAccount').style.display='none';
		document.getElementById('tabAccount').className='global_header_tab_account_container';
	}
// Returns an array with 2 elements for the left and right position of an object.

function findPos(obj) {

      var curleft = curtop = 0;

      if (obj.offsetParent) {

      do {

            curleft += obj.offsetLeft;

            curtop += obj.offsetTop;

      } while (obj = obj.offsetParent);

      }

      return [curleft,curtop];

}
