/*
 * menu.js
 * JavaScript for menu interface
 */

var topMenuSpacer = 0;                  // 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 = true;           // left align sub items 
//var activeMenuItem = false;             // Don't change this option. It should initially be false
var activeMenuItem = new Object;        // Don't change this option. It should initially be false
var activeTabIndex = new Object;        // 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 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;

function showHide(me, parent)
{
	if (activeMenuItem[parent]) {
        
		activeMenuItem[parent].className = 'inactiveMenuItem';
		
		var theId = activeMenuItem[parent].id.replace(/[^0-9]/g,'');

        if ( document.getElementById(parent).getElementsByTagName('div').length > 1 ) {
    		document.getElementById( 'submenu_' + theId ).style.display = 'none';
        }

		var img = activeMenuItem[parent].getElementsByTagName('img');
		
		if ( img.length > 0 ) {
            img[0].style.display = 'none';
        }
	}
	
	var img = me.getElementsByTagName('img');
	
	if ( img.length > 0 ) {
        img[0].style.display = 'inline';
    } 
	
	activeMenuItem[parent] = me;
	
	me.className = 'activeMenuItem';
	
	var theId = me.id.replace(/[^0-9]/g,'');
	
    if ( document.getElementById(parent).getElementsByTagName('div').length > 1 ) {
	    document.getElementById( 'submenu_' + theId ).style.display = 'block';
    }

	if ( rememberActiveTabByCookie ) {
		setCookie( 'siteMenuTabIndex' + '-' + parent, 'index: ' + ( theId - 1 ), 100 );
	}
}

function initMenu(menuId)
{
	var mainMenuObj = document.getElementById(menuId);

    if ( ! mainMenuObj ) {
        return false;
    }

	var menuItems = mainMenuObj.getElementsByTagName('a');

	if (document.all) {
		mainMenuObj.style.visibility = 'hidden';
		document.getElementById('submenu').style.visibility = 'hidden';
	}
	
	if (rememberActiveTabByCookie) {
		var cookieValue = getCookie( 'siteMenuTabIndex' + '-' + mainMenuObj.parentNode.id ) + '';
		cookieValue = cookieValue.replace(/[^0-9]/g,'');
		
		if ( cookieValue.length > 0 && cookieValue < menuItems.length ) {
			activeTabIndex[menuId] = cookieValue/1; 
		} 
	}

    if ( ! menuItems ) {
        return false;
    }
	
	var currentLeftPos = 15;
	
	for ( var i = 0; i < menuItems.length; i++ ) {
		
		if (activateSubOnClick) {
            menuItems[i].onclick = function() { showHide(this, mainMenuObj.parentNode.id); };
        } else {
		    menuItems[i].onmouseover = function() { showHide(this, mainMenuObj.parentNode.id); };
        }
		
		menuItems[i].id = menuId + (i+1);
		menuItems[i].style.left = currentLeftPos + 'px';
		
		currentLeftPos = currentLeftPos + menuItems[i].offsetWidth + topMenuSpacer;
		
		var img = menuItems[i].getElementsByTagName('img');
		
		if ( img.length > 0 ) {
			
			img[0].style.display = 'none';
			
			if ( MSIE && !Opera ) {
				img[0].style.bottom = '-1px';
				img[0].style.right = '-1px';
			}
		}
		
		if ( i == activeTabIndex[menuId] ) {
			menuItems[i].className = 'activeMenuItem';
			activeMenuItem[ menuId + '-menu' ] = menuItems[i];
			var img = activeMenuItem[ menuId + '-menu' ].getElementsByTagName('img');
			
			if (img.length > 0 ) {
                img[0].style.display = 'inline';
            }
		} else {
            menuItems[i].className = 'inactiveMenuItem';
        }
		
		if ( ! document.all ) { 
            menuItems[i].style.bottom = '-1px';
        }
		
		if ( MSIE && navigatorVersion < 6 ) {
            menuItems[i].style.bottom = '-2px';
        }
	}

    if ( mainMenuObj.parentNode.getElementsByTagName('div').length > 1 ) {
        
        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[menuId] + 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';
}

/* window.onload = function() { initMenu('navigation'); activateSubOnClick = true; initMenu('page-navigation'); }; */
window.onload = function() { initMenu('navigation'); };
