var currentlyOpen = null;
var targetRef = null;
var targetAction = "";
var targetFocus = null;
var evnt=null
var actionTimer = -1;
var oldMouseMove = null;

function openMenu(who, optFocus,e) {
    var timeout = (currentlyOpen == null) ? 200 : 100;
    SetupAction(timeout, "open", who, optFocus);
}

function closeActiveMenu() {
	if (currentlyOpen != null) SetupAction(200, "close", currentlyOpen);
}

function SetupAction(time, action, ref, optFocus) {
    if (actionTimer != -1) {
    	clearTimeout(actionTimer);
        actionTimer = -1;
    }
    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
    {
        who.nextSibling.className = who.nextSibling.className + " separator_over";
        who.previousSibling.className = who.previousSibling.className + " separator_over";
    }
    else //for Mozilla
    {
        who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className + " separator_over";
        who.previousSibling.previousSibling.className = who.previousSibling.previousSibling.className + " separator_over";
    }
    currentlyOpen = who;
	if (document.onmousemove != null) {
		oldMouseMove = document.onmousemove;
	}
	document.onmousemove = checkCloseMenu;
}

function closeMenu(who) {
    //console.log("Close menu: " + who.id);
   	document.getElementById(who.id + "_list").style.display = "none";
    //activeMenu.getElementsByTagName("div")[0].style.display="none";
    who.getElementsByTagName("a")[0].className = who.getElementsByTagName("a")[0].className.replace(" over", "");
    if (who.nextSibling.tagName) //for IE
    {
        who.nextSibling.className = who.nextSibling.className.replace(" separator_over", "");
        who.previousSibling.className = who.previousSibling.className.replace(" separator_over", "");
    }
    else //for Mozilla
    {
        who.nextSibling.nextSibling.className = who.nextSibling.nextSibling.className.replace(" separator_over", "");
        who.previousSibling.previousSibling.className = who.previousSibling.previousSibling.className.replace(" separator_over", "");
    }
    currentlyOpen = null;
	if (oldMouseMove != null) {
		document.onmousemove = oldMouseMove;
	} else {
		document.onmousemove = null;
	}
}

function checkCloseMenu(e, bel) {
    if (currentlyOpen == null) {
		document.onmousemove=""
        return;
    }
	var s = null;
	if (e) s = e.target; else s=event.srcElement;
	//console.log(s);
    if (IsChildOf(s, currentlyOpen)) {
        // Cancel any timeouts that might be running
        CancelActions();
    }
	else if (IsChildOf(s, document.getElementById('headerParent')))
	{
		return;
	}
    else {
        SetupAction(200, "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;
    }
}


