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) {
    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) { alert("error"); }
            }
            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";
	who.style.zIndex="50";
	var nextObj=(who.nextSibling.tagName)?who.nextSibling:who.nextSibling.nextSibling;
	var preObj=(who.previousSibling.tagName)?who.previousSibling:who.previousSibling.previousSibling;
    if (nextObj.className.indexOf("sep_active_left") > -1)
	{
		if (document.getElementById(who.id + "_list").className.indexOf("_left") > -1)
			document.getElementById(who.id + "_list").style.left = -5 + "px";
		else 
			document.getElementById(who.id + "_list").style.right = -5 + "px";
	}
	else if (nextObj.className.indexOf("sep_active_right") > -1)
	{
		nextObj.className=nextObj.className+" separator_active_over";
		if (document.getElementById(who.id + "_list").className.indexOf("_left") > -1)
				document.getElementById(who.id + "_list").style.left = -7 + "px";					
	}
	else
		nextObj.className=nextObj.className+" separator_over";
	
	
    if (preObj.className.indexOf("sep_active_right") > -1)
	{
		if (document.getElementById(who.id + "_list").className.indexOf("_left") == -1)
			document.getElementById(who.id + "_list").style.right = -5 + "px";	
		else 
			document.getElementById(who.id + "_list").style.left = -5 + "px";
	}
	else if (preObj.className.indexOf("sep_active_left") > -1)
	{
		preObj.className=preObj.className+" separator_active_over";
			if (document.getElementById(who.id + "_list").className.indexOf("_left") == -1)
				document.getElementById(who.id + "_list").style.right = -7 + "px";					
	}
	else
		preObj.className=preObj.className+" separator_over";
		
    currentlyOpen = who;
	//event.addListener(document, "mousemove", checkCloseMenu
	document.body.onmousemove=checkCloseMenu;	
}

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","");
	who.style.zIndex="100";	
	var nextObj=(who.nextSibling.tagName)?who.nextSibling:who.nextSibling.nextSibling;
	var preObj=(who.previousSibling.tagName)?who.previousSibling:who.previousSibling.previousSibling;
    if (nextObj.className.indexOf("sep_active") > -1)
	{
		nextObj.className=nextObj.className.replace(" separator_active_over","");
		document.getElementById(who.id + "_list").style.left = "";		
	}
	else
		nextObj.className=nextObj.className.replace(" separator_over","");
	
	if (preObj.className.indexOf("sep_active") > -1)
	{
		preObj.className=preObj.className.replace(" separator_active_over","");
		document.getElementById(who.id + "_list").style.right = "";
	}
	else
		preObj.className=preObj.className.replace(" separator_over","");
	
    currentlyOpen = null;
	//event.removeListener(document, "mousemove", checkCloseMenu);
	document.body.onmousemove="";
}

function checkCloseMenu(e, bel) {
    if (currentlyOpen == null) {
		//event.removeListener(document, "mousemove", checkCloseMenu);
		document.body.onmousemove="";		
        return;
    }
	var s = null;
	/*s = event.getTarget(e);*/
	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(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;
    }
}
