/*
 * menuDropdown.js - implements an dropdown menu based on a HTML list
 * Modified from source by: Dave Lindquist (http://www.gazingus.org)
 */

var currentMenu = null;
var myTimer = null;
var timerOn = false;

if (!document.getElementById)
    document.getElementById = function() { 
    	return null; 
    }

// Click anywhere outside the menu to make it go away
/*document.onmousedown = function() {
	if (currentMenu)
		currentMenu.style.visibility = "hidden";
}*/

// Sniff browser. Some need the dropdown menu offset by a few pixels
function needsShift() {
	var agt=navigator.userAgent.toLowerCase();	
	//var is_major = parseInt(navigator.appVersion);
    //var is_minor = parseFloat(navigator.appVersion);
    var is_safari = (agt.indexOf("safari") != -1);
	var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_gecko = (agt.indexOf('gecko') != -1);
	var is_opera = (agt.indexOf("opera") != -1);
	if(is_ie || is_safari || is_opera)
		return true;
}

/* ************************************************************* */
function killMenu(amenu) {
	var menu = document.getElementById(amenu);
	menu.style.visibility = "hidden";
	stopTime();
}

/* ************************************************************* */
function stopTime() {
	if (myTimer) {
		 clearTimeout(myTimer);
		 myTimer = null;
		 timerOn = false;
	}
}

/* ************************************************************* */
function initialiseMenu(ID) {
    var menu = document.getElementById('menu' + ID);
    var root = document.getElementById('menu' + ID + 'Root');

    if (menu == null || root == null) return;

    root.onmouseover = function() {
        if (currentMenu)
            currentMenu.style.visibility = "hidden";
        this.showMenu();
        stopTime();    
        return false;
    }
  
    menu.onmouseover = function() {
        if (currentMenu)
            currentMenu.style.visibility = "hidden";
        this.showMenu();
    }

	root.onfocus = function() {
		this.onmouseover();
	}
	root.onblur = function() {
		this.onmouseout();
	}
	
    root.showMenu = function() { 
    	offset=0;
    	if(needsShift()) // For IE: move submenu 10px to the right to line up properly
    		offset=10;
        menu.style.left = (this.offsetLeft + offset) + "px";
        menu.style.top = this.offsetTop + this.offsetHeight + "px";
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
    
    // ---------------
    
	menu.showMenu = function() {
		//FormOff();
		menu.style.visibility = "visible";
		currentMenu = menu;
		stopTime();
	}

	menu.hideMenu = function()  {
		if (!timerOn) {
			myTimer = setTimeout("killMenu('menu" + ID + "');", 250);
			timerOn = true;
		}
	}

	menu.onmouseout = function(event) {
		this.hideMenu();
	}

	root.onmouseout = function() {
		menu.hideMenu();
	}
}
/* ************************************************************* */