﻿var myMenuFrameObj_1 = null;
var myMenuFrameObj_2 = null;
var myTimeout1 = null; // skapar en javascript strдng som anropas efter antal sek
var myTimeout2 = null;

function createIFrame(obj, secondVerticalMenu){
    var bounds = Utils.getBounds(obj);// getBounds(obj);
    var myMenuFrame = document.createElement("iFrame");
    myMenuFrame.style.height = "400px";
    myMenuFrame.style.width = "200px";
    myMenuFrame.style.position = "absolute";
    if (secondVerticalMenu){
        var bounds1 = Utils.getBounds(myMenuFrameObj_1); // getBounds(myMenuFrameObj_1);
        myMenuFrame.style.left = bounds1.left+bounds1.width-10+"px";
        myMenuFrame.style.top = bounds1.top+bounds.top+"px";   
    } else {
        myMenuFrame.style.left = bounds.left+"px";
        myMenuFrame.style.top = bounds.top+obj.offsetHeight+"px";
    }
    myMenuFrame.border=0;
    myMenuFrame.frameBorder = 0;
    myMenuFrame.style.zIndex = "999999";
    myMenuFrame.scrolling = "no";
    myMenuFrame.style.visibility = "hidden";
    document.body.appendChild(myMenuFrame);
    myMenuFrame.contentWindow.document.open(); // här under sker de konstiga man dra in style som innerMenu och menu
    var cssHref = "/css/TabBar.css"; //  document.getElementById("innerMenu").getAttribute("src");
    var styleInclude = "<link id='innerMenu' rel='stylesheet' type='text/css' href='" + cssHref + "'></link>";
    if (!window.cssContent) {
        window.requestContent(cssHref, function(responseText) { window.cssContent = responseText; });
    } else {
        styleInclude = "<style>" + window.cssContent + "</style>";
    }

   
myMenuFrame.contentWindow.document.write("<head>"  +styleInclude+ "</head><body style='margin:0px;padding:0px;'><div class='dropdownMenu'>" + obj.childNodes[0].innerHTML + "</div></body>");
    myMenuFrame.contentWindow.document.close();
    myMenuFrame.style.visibility = "visible";
    myMenuFrame.style.height = myMenuFrame.contentWindow.document.body.childNodes[0].offsetHeight + 3 +"px";
    myMenuFrame.JS_S = JS_S;
    return myMenuFrame;
}

function move(obj, secondVerticalMenu){
    show(obj, secondVerticalMenu);
}


function show(obj, secondVerticalMenu) {
    if (obj.showed && 
		(
			(!secondVerticalMenu && myMenuFrameObj_1) 
			|| 
			(secondVerticalMenu && myMenuFrameObj_2) 
		))
        return;
    if (myMenuFrameObj_1 && !secondVerticalMenu){
        myMenuFrameObj_1.parentNode.removeChild(myMenuFrameObj_1);
        myMenuFrameObj_1 = null;
    }
    if (myMenuFrameObj_2){
        myMenuFrameObj_2.parentNode.removeChild(myMenuFrameObj_2);        
        myMenuFrameObj_2 = null;
    }
    obj.showed = true;
    if (secondVerticalMenu){
        obj.className 
        myMenuFrameObj_2 = createIFrame(obj, true);
        myMenuFrameObj_2.obj = obj;
        myMenuFrameObj_2.onmouseover = function () {if (myTimeout1)clearTimeout(myTimeout1);if(myTimeout2)clearTimeout(myTimeout2);}
        myMenuFrameObj_2.onmousemove = function () {if (myTimeout1)clearTimeout(myTimeout1);if(myTimeout2)clearTimeout(myTimeout2);}
        myMenuFrameObj_2.onmouseout = function () { hide(); hide(true);}
        //Find index            
        var temp = obj;var i=0;
        while (obj.parentNode.childNodes[i++] != temp && i < obj.parentNode.childNodes.length);
        i--;
        myMenuFrameObj_1.contentWindow.document.body.childNodes[0].childNodes[i].active = true;
    } else {
        myMenuFrameObj_1 = createIFrame(obj, false);
        myMenuFrameObj_1.obj = obj;
        myMenuFrameObj_1.contentWindow.show = show;
        myMenuFrameObj_1.contentWindow.hide = hide;
        myMenuFrameObj_1.contentWindow.move = move;
        myMenuFrameObj_1.onmouseover = function () {try{clearTimeout(myTimeout1);}catch(e){}}
        myMenuFrameObj_1.onmousemove = function () {try{clearTimeout(myTimeout1);}catch(e){}}
        myMenuFrameObj_1.onmouseout = function () { hide(); }
    }
}

function hide(secondVerticalMenu,hide){
    if (!hide){
        if (secondVerticalMenu)
        {
            if (myTimeout2)
                clearTimeout(myTimeout2);
            myTimeout2 = setTimeout("hide(true,true);",0);        
        }else
        {
            if (myTimeout1)
                clearTimeout(myTimeout1);
            myTimeout1 = setTimeout("hide(false,true);",500);                    
        }                      
        return ;
    }
    if (!secondVerticalMenu && myMenuFrameObj_1){
        myMenuFrameObj_1.parentNode.removeChild(myMenuFrameObj_1);
        myMenuFrameObj_1.obj.showed = false;
        myMenuFrameObj_1 = null;
    }
    if (myMenuFrameObj_2){
        myMenuFrameObj_2.parentNode.removeChild(myMenuFrameObj_2);
        myMenuFrameObj_2.obj.showed = false;
        myMenuFrameObj_2 = null;
    }
    if (secondVerticalMenu && myMenuFrameObj_1){
        for (var i=0; i < myMenuFrameObj_1.contentWindow.document.body.childNodes[0].childNodes.length; i++){
            if (myMenuFrameObj_1.contentWindow.document.body.childNodes[0].childNodes[i].active)
                myMenuFrameObj_1.contentWindow.document.body.childNodes[0].childNodes[i].className = '';
        }
    }
}
 // shows menu item details form
 function   oMenu_P(winEvent, menMPaID)
    {
        open("/admin/all/menMenuPage_MenuItem_Details.aspx?menMPaID=" + menMPaID +"",null,"menubar=no,toolbar=no,width=1200px,resizable=yes");
        if (winEvent)   winEvent.cancelBubble = true;//stop event
        return false;
    }

    // shows menu tree page
    function oMenu_L(winEvent, menMPaID)
    {
        open("/admin/all/menMenuPage_Menu_List.aspx?menMPaID=" + menMPaID  ,null,"menubar=no,toolbar=no,width=1200px,resizable=yes"); 
        if (winEvent)   winEvent.cancelBubble = true;//stop event
        return false;
    }


    window.requestContent = function (location, callBack) {
        try { r = new XMLHttpRequest(); } catch (e) { try { r = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { r = new ActiveXObject('Microsoft.XMLHTTP'); } }
        if (r) {
            if (callBack)
                r.onreadystatechange = function () { if (r.readyState == 4 && r.status == 200) { callBack(r.responseText); } }
            r.open('GET', location, typeof callBack != undefined);
            r.send(null);
            if (!callBack)
                return r.responseText;
        }
    }