var currentlyOpen = null;
var targetRef = null;
var targetAction = "";
var targetFocus = null;
var evnt=null
var actionTimer = -1;

function openMenu(who, optFocus,e) {
    var timeout = (currentlyOpen == null) ? 500 : 100;
    SetupAction(timeout, "open", who, optFocus);
}

function closeActiveMenu() {
	if (currentlyOpen != null) SetupAction(500, "close", currentlyOpen);
}

function SetupAction(time, action, ref, optFocus) {
    targetAction = action;
    targetRef = ref;
    targetFocus = optFocus;
    actionTimer = setTimeout(Action, time);
}


function CancelActions() {
    if (actionTimer != -1) {
        clearTimeout(actionTimer);
        actionTimer = -1;
    }
    //console.log("Canceling action: " + targetAction + ":" + targetRef);
    targetAction = "";
    targetRef = null;
    targetFocus = null;
}

function Action() {
    switch (targetAction) {
        case "open":
            if (currentlyOpen != null) {
                closeMenu(currentlyOpen);
            }
            // Open the target
            doOpenMenu(targetRef);
            if (targetFocus != null && typeof targetFocus != "undefined") {
                try { targetFocus.focus(); } catch (e) { }
            }
            break;
        case "close":
            // Close the target
            closeMenu(currentlyOpen);
            break;
    }
    CancelActions();
}

function doOpenMenu(who,e) {
    document.getElementById(who.id + "_list").style.display = "block";
    who.getElementsByTagName("a")[0].className = who.getElementsByTagName("a")[0].className + " over";
    if (who.nextSibling.tagName) //for IE
    {
        if (who.nextSibling.className.indexOf("bottom_nav_left") > -1)
            who.nextSibling.className = who.nextSibling.className.replace("bottom_nav_left", "bottom_nav_left_hover");
        else
            who.nextSibling.className = who.nextSibling.className + " separator_over";
        who.previousSibling.className = who.previousSibling.className + " separator_over";
    }
    else //for Mozilla
    {
        if (who.nextSibling.nextSibling.className.indexOf("bottom_nav_left") > -1)
            who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className.replace("bottom_nav_left", "bottom_nav_left_hover");
        else
            who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className + " separator_over";
        who.previousSibling.previousSibling.className = who.previousSibling.previousSibling.className + " separator_over";
    }
    currentlyOpen = who;
	
	//event.addListener(document, "mousemove", checkCloseMenu);	
	jQuery(document).bind('mousemove', function(e) { checkCloseMenu(e); });

}

function closeMenu(who) {
    //console.log("Close menu: " + who.id);
   	document.getElementById(who.id + "_list").style.display = "none";
    who.getElementsByTagName("a")[0].className = who.getElementsByTagName("a")[0].className.replace(" over", "");
    if (who.nextSibling.tagName) //for IE
    {
        if (who.nextSibling.className.indexOf("bottom_nav_left") > -1)
            who.nextSibling.className = who.nextSibling.className.replace("bottom_nav_left_hover", "bottom_nav_left");
        else
            who.nextSibling.className = who.nextSibling.className.replace(" separator_over", "");
        who.previousSibling.className = who.previousSibling.className.replace(" separator_over", "");
    }
    else //for Mozilla
    {
        if (who.nextSibling.nextSibling.className.indexOf("bottom_nav_left") > -1)
            who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className.replace("bottom_nav_left_hover", "bottom_nav_left");
        else
            who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className.replace(" separator_over", "");
        who.previousSibling.previousSibling.className = who.previousSibling.previousSibling.className.replace(" separator_over", "");
    }
    currentlyOpen = null;
	
	//event.removeListener(document, "mousemove", checkCloseMenu);
	jQuery(document).unbind('mousemove');
}

function checkCloseMenu(e, bel) {
    if (currentlyOpen == null) {
		//event.removeListener(document, "mousemove", checkCloseMenu);
		jQuery(document).unbind('mousemove');
        return;
    }
	var s = null;
	
	//s = event.getTarget(e);		
	var s = e.target;
    if (IsChildOf(s, currentlyOpen)) {
        // Cancel any timeouts that might be running
        CancelActions();
    }
	else if (IsChildOf(s, document.getElementById('headerParent')))
	{
		return;
	}
    else {
        SetupAction(500, "close", currentlyOpen);
    }
}


function IsChildOf(who, pt) {
    try {
        if (pt == document.body) return true;
        if (who == pt) return true;
        var t = who.parentNode;
        while (1 == 1) {
            if (t == document.body) return false;
            if (t == pt) return true;
            t = t.parentNode;
        }
    }
    catch (e) {
        return false;
    }
}


