﻿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 heSearchKeyPressed(e) {
    var characterCode
    if (e && e.which) {           // NN4 specific code
        e = e
        characterCode = e.which
    }
    else {
        e = event
        characterCode = e.keyCode // IE specific code
    }
    if (characterCode == 13) {
        searchGibraltar();
    }
    return false;
}

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 closeActiveMenu() {
    if (currentlyOpen != null) SetupAction(500, "close", currentlyOpen);
}

function doOpenMenu(who, e) {
    var elem = document.getElementById(who.id + "_list");

    //check that there are items
    if (elem.children[0].children.length == 0)
        return;

    elem.style.display = "block";
    who.getElementsByTagName("a")[0].className = who.getElementsByTagName("a")[0].className + " over";
    who.style.zIndex = "500";
    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_right") > -1) {
        if (document.getElementById(who.id + "_list").className.indexOf("drop_menu_left") > -1)
            document.getElementById(who.id + "_list").style.left = -3 + "px";
    }
    else if (nextObj.className.indexOf("sep_active_left") > -1) {
        nextObj.className = nextObj.className + " separator_active_over";
        if (elem.className.indexOf("drop_menu_left") > -1)
            elem.style.left = -7 + "px";
    }
    else
        nextObj.className = nextObj.className + " separator_over";


    if (preObj.className.indexOf("sep_active_left") > -1) {
        if (elem.className.indexOf("drop_menu_left") == -1)
            elem.style.right = -3 + "px";
    }
    else if (preObj.className.indexOf("sep_active_right") > -1) {
        preObj.className = preObj.className + " separator_active_over";
        if (elem.className.indexOf("drop_menu_left") == -1)
            elem.style.right = -7 + "px";
    }
    else
        preObj.className = preObj.className + " separator_over";

    currentlyOpen = who;
    //event.addListener(document, "mousemove", checkCloseMenu
    document.body.onmousemove = checkCloseMenu;
}

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 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 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;
    }
}

function gotoLogin() {
    var url = location.href;
    location.href = "https://registration.orange.co.il/Copa/Pages/Protected/ProtectedRedirect.aspx?original=" + url;
}

function gotoUpdateDetails() {
    var url = location.href;
    location.href = updateBaseURL + "?origin=" + url;
}

function searchGibraltar() {
    var param = document.getElementById("inpSearchGib");
    try {
        window.location = searchBaseURL + "?Query=" + escape(param.value);
    }
    catch (e) {
    }
}

function logoutFromOrange(gotoURL) {
    try {
        top.GetVG().logout();
    }
    catch (e) {
    }

    if (typeof gotoURL == 'string') {
        window.location = gotoURL;
    }
}