/*************************************************************************************************

	(C) www.dhtmlgoodies.com, October 2005
	
	Update log:
	Version 1.1 	December, 1st 2005: Critical update for the new Firefox 1.5 browser
	Version 1.2: 	December, 21th 2005 : Mouseover effect when mouse moves outside of a submenu items text
	
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
**********************************************************************************************/	
		
	var dynamicmenu_menuObj;	// Reference to the menu div
	var currentZIndex = 1000;

	var visibleMenus = new Array();
	var activeMenuItem = false;
	var timeBeforeAutoHide = 1200; // Microseconds from mouse leaves menu to auto hide.
	
	var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
	var navigatorVersion = navigator.appVersion.replace(/.*?MSIE ([0-9]\.[0-9]).*/g,'$1')/1;
	
	function getTopPos(inputObj)
	{
		
	  var returnValue = inputObj.offsetTop;
	  if(inputObj.tagName=='LI' && inputObj.parentNode.className=='menuBlock1'){
	  	var aTag = inputObj.getElementsByTagName('A')[0];
	  	if(aTag)returnValue += aTag.parentNode.offsetHeight;

	  }	  
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;

	  return returnValue;
	}
	
	
	function getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	  return returnValue;
	}
	
	
	
	function showHideSub( thisObj ) {
		
		// seame x ja y paika
				
		var attr = thisObj.parentNode.getAttribute('currentDepth');
		if(navigator.userAgent.indexOf('Opera')>=0){
			attr = thisObj.parentNode.currentDepth;
		}
		
		thisObj.className = 'currentDepth' + attr + 'over';
		
		if(activeMenuItem && activeMenuItem!=thisObj){
			activeMenuItem.className=activeMenuItem.className.replace(/over/,'');
		}
		activeMenuItem = thisObj;
	
		var numericIdThis = thisObj.id.replace(/[^0-9]/g,'');
		var exceptionArray = new Array();
		// Showing sub item of this LI
		var sub = document.getElementById('subOf' + numericIdThis);
		
		if(sub){
			
			// START POSITSIONEERIMINE
			
			var offsetWidth = thisObj.offsetWidth;
			var numEnd = getNumericEnd(thisObj.className);
			
			// alert( numEnd );
			
			var topPos = getTopPos(thisObj); 
			var leftPos = getLeftPos(thisObj)/1 ; 
			
			if( numEnd > 1 ){
				leftPos+= offsetWidth;
			}
			
			//if( MSIE ) topPos+=4; // HACK ...
			topPos+=3;
			
			
			//alert( topPos + ', ' + leftPos );
			
			sub.style.position = 'absolute';
			sub.style.left = leftPos + 'px';
			sub.style.top = topPos + 'px';
			
			// END POSITSIONEERIMINE
			
			visibleMenus.push(sub);
			sub.style.display='';
			sub.parentNode.className = sub.parentNode.className + 'over';
			exceptionArray[sub.id] = true;
		}	
		
		// Showing parent items of this one
		
		var parent = thisObj.parentNode;
		while(parent && parent.id && parent.tagName=='UL'){
			visibleMenus.push(parent);
			exceptionArray[parent.id] = true;
			parent.style.display='';
			
			var li = document.getElementById('dynamicmenu_listItem' + parent.id.replace(/[^0-9]/g,''));
			if(li.className.indexOf('over')<0)li.className = li.className + 'over';
			parent = li.parentNode;
			
		}		
		hideMenuItems(exceptionArray);
	}
	

	function hideMenuItems(exceptionArray)
	{
		/*
		Hiding visible menu items
		*/
		var newVisibleMenuArray = new Array();
		for(var no=0;no<visibleMenus.length;no++){
			if(visibleMenus[no].className!='menuBlock1' && visibleMenus[no].id){
				if(!exceptionArray[visibleMenus[no].id]){
					var el = visibleMenus[no].getElementsByTagName('A')[0];
					visibleMenus[no].style.display = 'none';
					var li = document.getElementById('dynamicmenu_listItem' + visibleMenus[no].id.replace(/[^0-9]/g,''));
					if(li.className.indexOf('over')>0)li.className = li.className.replace(/over/,'');
				}else{				
					newVisibleMenuArray.push(visibleMenus[no]);
				}
			}
		}		
		visibleMenus = newVisibleMenuArray;		
	}
	

	var menuActive = true;
	var hideTimer = 0;
	function mouseOverMenu()
	{
		menuActive = true;		
	}
	
	function mouseOutMenu()
	{
		menuActive = false;
		timerAutoHide();	
	}
	
	function timerAutoHide()
	{
		if(menuActive){
			hideTimer = 0;
			return;
		}
		
		if(hideTimer<timeBeforeAutoHide){
			hideTimer+=100;
			setTimeout('timerAutoHide()',99);
		}else{
			hideTimer = 0;
			autohideMenuItems();	
		}
	}
	

	function autohideMenuItems()
	{
		if(!menuActive){
			hideMenuItems(new Array());	
			if(activeMenuItem)activeMenuItem.className=activeMenuItem.className.replace(/over/,'');		
		}
	}
	
	function getNumericEnd( str ){
		return str.replace(/[^0-9]/g,'');
		
	}