/***********************************************************************************************	
Copyright (c) 2005 - Alf Magne Kalleland post@dhtmlgoodies.com
Get this and other scripts at www.dhtmlgoodies.com
You can use this script freely as long as this copyright message is kept intact.
***********************************************************************************************/

var menuAlignment = 'left';	// Align menu to the left or right?		
var topMenuSpacer = 13; // Horizontal space(pixels) between the main menu items	
var activateSubOnClick = true; // if true-> Show sub menu items on click, if false, show submenu items onmouseover
var leftAlignSubItems = false; 	// left align sub items
var activeTabIndex = 0;	// Index of initial active tab	(0 = first tab) - If the value below is set to true, it will override this one.
var rememberActiveTabByCookie = true;	// Set it to true if you want to be able to save active tab as cookie

var activeMenuItem = false;	// Don't change this option. It should initially be false
var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
var Opera = navigator.userAgent.indexOf('Opera')>=0?true:false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE ([0-9]\.[0-9]).*/g,'$1')/1;
	
// Get the cookie value
function Get_Cookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for (i = 0; i < a_all_cookies.length; i++)
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split('=');		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if (cookie_name == check_name)
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if (a_temp_cookie.length > 1)
			{
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found)
	{
		return null;
	}
}

// Set a cookie
function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" +escape(value) +
    ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
    ( (path) ? ";path=" + path : "") + 
    ( (domain) ? ";domain=" + domain : "") +
    ( (secure) ? ";secure" : "");
}	

// Delete a cookie
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
    ( (path) ? ";path=" + path : "") +
    ( (domain) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// showHide is called when a user clicks on a root page hyperlink
function showHide()
{
	if(activeMenuItem){
	
	    // Set the previous active tab to inactive
		activeMenuItem.className = 'inactiveMenuItem'; 	
		
		// Get the id of the previous active tab
		var theId = activeMenuItem.id.replace(/[^0-9]/g,'');
		
		// Hide the previous actiave tab's submenu items
		document.getElementById('submenu_'+theId).style.display='none';			
	}
			
	// Set the new active tab to active status		
	activeMenuItem = this;		
	this.className = 'activeMenuItem';
	
	// Get the id of the active tabs
	var theId = this.id.replace(/[^0-9]/g,'');
	
	// Show the active tab's submenu items
	document.getElementById('submenu_'+theId).style.display='block';
			
	// If rememberActiveTabByCookie is set to true then save a cookie and assign it's value the id
	// of the active tab		
	if(rememberActiveTabByCookie){	  
	   Set_Cookie('dhtmlgoodies_tab_menu_tabIndex',(theId-1), null, '/');	 
	}     
}

function initMenu()
{
    var Menu = document.getElementById('TwoLevelMenu');
    Menu.style.display='';	

    // Get the 'mainMenu' div that surrounds the root pages
	var mainMenuObj = document.getElementById('mainMenu');
	
   	// Get all the hyperlinks contained in the 'mainMenu' div
	var menuItems = mainMenuObj.getElementsByTagName('A');
	
	if(document.all){
		mainMenuObj.style.visibility = 'hidden';
		document.getElementById('submenu').style.visibility='hidden';
	}	
	
	// If rememberActiveTabByCookie is true	
	if(rememberActiveTabByCookie){
	
	    // Get the value of the tab cookie.
		var cookieValue = Get_Cookie('dhtmlgoodies_tab_menu_tabIndex') + '';	
		cookieValue = cookieValue.replace(/[^0-9]/g,'');	
	
		// If the cookie value length is greater than zero and
		// the cookie value is less than the number of hyperlinks
		// in the mainMenu div then set the active tab to the cookie value
		if(cookieValue.length>0 && cookieValue<menuItems.length){
			activeTabIndex = cookieValue/1;
		}			
	}
	
	var currentLeftPos = 0;
	
	// Loop through each hyperlink in mainMenu div
	for(var no=0;no<menuItems.length;no++){	
		
		// If activateSubOnClick is true then assign the showHide function to the onclick event.
		// If activateSubOnClick is false then assign the showHide function to the onmouseover event.	
		if(activateSubOnClick)menuItems[no].onclick = showHide; else menuItems[no].onmouseover = showHide;		
		
		menuItems[no].id = 'mainMenuItem' + (no+1);
		
		// Put the menu on the left or right according to the value of menuAlignment
		if(menuAlignment=='left')
			menuItems[no].style.left = currentLeftPos + 'px';
		else
			menuItems[no].style.right = currentLeftPos + 'px';
			
		// Not sure what this is for	
		currentLeftPos = currentLeftPos + menuItems[no].offsetWidth + topMenuSpacer; 		
						
		if(no==activeTabIndex){
			menuItems[no].className='activeMenuItem';
			activeMenuItem = menuItems[no];						
		}
		else
		    menuItems[no].className='inactiveMenuItem';
		
		if(!document.all)menuItems[no].style.bottom = '-1px';
		if(MSIE && navigatorVersion < 6)menuItems[no].style.bottom = '-2px';		

	}		
	
	var mainMenuLinks = mainMenuObj.getElementsByTagName('A');
	
	var subCounter = 1;
	var parentWidth = mainMenuObj.offsetWidth;
	while(document.getElementById('submenu_' + subCounter)){
		var subItem = document.getElementById('submenu_' + subCounter);
		
		if(leftAlignSubItems){
			// No action
		}else{							
			var leftPos = mainMenuLinks[subCounter-1].offsetLeft;
			document.getElementById('submenu_'+subCounter).style.paddingLeft =  leftPos + 'px';
			subItem.style.position ='absolute';
			if(subItem.offsetWidth > parentWidth){
				leftPos = leftPos - Math.max(0,subItem.offsetWidth-parentWidth); 	
			}
			subItem.style.paddingLeft =  leftPos + 'px';
			subItem.style.position ='static';
				
			
		}
		if(subCounter==(activeTabIndex+1)){
			subItem.style.display='block';
		}else{
			subItem.style.display='none';
		}
		
		subCounter++;
	}
	if(document.all){
		mainMenuObj.style.visibility = 'visible';
		document.getElementById('submenu').style.visibility='visible';
	}
				
	document.getElementById('submenu').style.display='block';
	
}

// When the window loads run initMenu
window.onload = initMenu;

