﻿
var wndElement = new Array();

var stack = new Stack();
var zIndexvalue = 102;
var zIndexDefault = 1;
var zIndexFocus = 99;
var selectedWndID = 0;

//---------------------------------- wnd op--------------------------------------------------
function wndLoadIframe(src) {
   // debugger;
    var iframe = document.getElementById('wndIContent');
    if (iframe) {
        iframe.src = src;
     }
 }


function wndKillDialog(id) {

    if (id.id != null)
        id = id.id;
    var window = document.getElementById(id);

    if (window) {

        var panelID = wndElement[window.id]['panelID'];
        if (wndElement[id]['wndContentType'] == 'text') {
            //wndCleanUp(window.id);
            document.body.removeChild(window);
            window.innerHTML = null;
        }
        else {
            var pnl = document.getElementById(panelID);
            if (pnl) {
                var d = document.getElementById("dialogs");
                d.style.display = "none";
                d.appendChild(pnl);
            }

            //wndClear(id);
            try {
            document.getElementsByTagName("form")[0].removeChild(window);
            } catch (err) {
                document.getElementsByTagName("form")[0].parentElement.removeChild(window);
            }
            window.innerHTML = null;
        }
        window = null;

        wndModalCover(id, false);
        stack.pop(id);
        createTabs();

    }
}
function wndModalCover(id, status) {
    var theBody = document.body;
    var gPopupMask = document.getElementById('popupMask_' + id);
    if (!gPopupMask) {
        popmask = document.createElement('div');
        popmask.id = 'popupMask_' + id;
        theBody.appendChild(popmask);
    }
    var gPopupMask = document.getElementById('popupMask_' + id);
    var fullWidth = getViewportWidth();
    var fullHeight = getViewportHeight();


    // Determine what's bigger, scrollHeight or fullHeight / width
    var popWidth = 0;  //document.body.clientWidth > fullWidth ? theBody.clientWidth : fullWidth;
    var popHeight = 0; // document.body.clientHeight > fullWidth ? theBody.clientHeight : fullWidth;

    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }

    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }
    //debugger;
    gPopupMask.style.backgroundImage = "url(generic/images/maskBG.png)";
    gPopupMask.style.backgroundColor = "#333333";
    gPopupMask.style.filter = " alpha(opacity=40)";
    gPopupMask.style.position = "absolute";
    gPopupMask.style.top = "0px";
    gPopupMask.style.left = "0px";
    gPopupMask.style.border = "none";
    gPopupMask.style.height = popHeight + "px";
    gPopupMask.style.width = popWidth + "px";

    if (!status) {
        gPopupMask.style.display = "none";
        document.body.removeChild(gPopupMask);
    } else {
        gPopupMask.style.display = "inline";
        gPopupMask.style.zIndex = 0;
    }
}

function wndKillAllDialogs() {
    var t = stack.cards;
    if (t) {
        if (t.length > 0) {
            for (var i = 0; i < t.length; i++) {
                var elem = t[i].split('_');
                wndKillDialog(elem[0]);
            }
        }
    }
}

function wndDialog(id, content, wndProps) {
    try {
        //debugger;
        wndElement[id] = new Array();
        var left = wndProps['left'];
        var top = wndProps['top'];
        var width = wndProps['width'];
        var height = wndProps['height'];

        wndElement[id]['left'] = left;
        wndElement[id]['top'] = top;
        wndElement[id]['width'] = width;
        wndElement[id]['height'] = height;
        wndElement[id]['center'] = wndProps['center'];

        contentType = wndProps['contentType'];

        var data = decodeBase64(content);
        var wnd = data.split("#");

        var wind = document.createElement('div');
        wind.id = id;

        var conType = contentType.split("##");
        contentType = conType[0];
        var t = wnd[0];

        if (wndProps['moduleName'] == "sys.menu") {

            var path = wnd[1];
            var codeData =  wndIframeContentTmpl(id, path) + wndBottomTmpl();

            wind.innerHTML = codeData;
            document.body.appendChild(wind);

            var w = wndElement[id]['width'];
            var h = getViewportHeight() - 15;

            setSize(id, w, h);
            var xpos = wndElement[id]['left'];
            var ypos = wndElement[id]['top'];

            moveTo(id, xpos, ypos); return;
        }
        if (wndProps['moduleName'] == "sys.auth") {

            var path = wnd[1];
            var codeData = wndIframeTopTmpl(wind.id, t) + wndIframeContentTmpl(id, path) + wndBottomTmpl();

            wind.innerHTML = codeData;
            document.body.appendChild(wind);
            var m = document.getElementById(id + "_min");
            if (m)
                m.style.display = 'none';
            var w = 400; var h = 300; setSize(id, w, h); var xpos = "middle"; var ypos = "middle";

            moveTo(id, xpos, ypos); return;

            wndModalCover(id, true);

        }
        if (!wndIsOpen(t)) {
            if (contentType == 'div' && conType[1] == 'text') {
                var codeData = wndTopTmpl(wind.id, t) + wndContentTmpl(wind.id, wnd[1]) + wndBottomTmpl();
                wind.innerHTML = codeData;
                document.body.appendChild(wind);

                if (wndElement[id]['width'] == "0px")
                    wndElement[id]['width'] = document.getElementById("wndContent_" + id).offsetWidth;

                if (wndElement[id]['height'] == "0px")
                    wndElement[id]['height'] = wind.offsetHeight;

                var w = wndElement[id]['width'];
                var h = wndElement[id]['height'];

                setSize(id, w, h);

                var xpos = wndElement[id]['center'] ? "middle" : wndElement[id]['left'];  //getValue("left", wndProp) //Get x coord of window
                var ypos = wndElement[id]['center'] ? "middle" : wndElement[id]['top'];

                moveTo(id, xpos, ypos);

                wndElement[id]['window'] = wind;
                wndElement[id]['wndContentType'] = 'text';
                wndModalCover(id, true);
            }
            else
                if (contentType == 'div' && conType[1] == 'id') {

                var controlID = wnd[1];
                var content = document.getElementById(controlID);
                var codeData = wndTopTmpl(wind.id, t) + wndContentTmpl(wind.id, content.innerHTML) + wndBottomTmpl();

                content.style.display = "none";
                wind.innerHTML = codeData;

                document.getElementsByTagName("form")[0].appendChild(wind);
                document.body.appendChild(content);

                if (wndElement[id]['width'] == "0px")
                    wndElement[id]['width'] = content.offsetParent.offsetWidth ? content.offsetParent.offsetWidth : 520;
                if (wndElement[id]['height'] == "0px")
                    wndElement[id]['height'] = content.offsetParent.offsetHeight;  //"100%";

                var w = wndElement[id]['width'];
                var h = wndElement[id]['height'];

                setSize(id, w, h);

                var xpos = wndElement[id]['center'] ? "middle" : wndElement[id]['left'];
                var ypos = wndElement[id]['center'] ? "middle" : wndElement[id]['top'];

                moveTo(id, xpos, ypos);

                wndElement[id]['window'] = wind;
                wndElement[id]['wndContentType'] = 'id';
                wndElement[id]['wndContent'] = wndContentTmpl(id, content);
                wndElement[id]['window'] = wind;
                wndElement[id]['panelID'] = controlID;

                wndModalCover(id, true);

            }
            else
                if (contentType == 'iframe') {

                var path = wnd[1];
                var codeData = wndIframeTopTmpl(wind.id, t) + wndIframeContentTmpl(id, path) + wndBottomTmpl();
                //debugger;
                var w = wndElement[id]['width'] == "0px" ? getViewportWidth() - 235 : wndElement[id]['width'];
                var h = getViewportHeight() - 55;
                // alert(w + ' ' + h);
                wind.innerHTML = codeData;
                document.getElementsByTagName("form")[0].appendChild(wind);
                document.body.appendChild(wind);
                setSize(id, w, h);

                var xpos = wndElement[id]['left']; // wndElement[id]['center'] ? "middle" : wndElement[id]['left'];  //getValue("left", wndProp) //Get x coord of window
                var ypos = wndElement[id]['top']; //wndElement[id]['center'] ? "middle" : wndElement[id]['top'];

                moveTo(id, xpos, ypos);  // wndElement[id]['left'], wndElement[id]['top']);

                wndElement[id]['window'] = wind;
                wndElement[id]['wndContentType'] = 'text';
                // var x = document.getElementById('phContent').focus();
            }

            stack.push(id + '_' + t);

            var mainTbl = document.getElementById('mainTbl');
            if (mainTbl) {
                mainTbl.style.height = parseInt(getViewportHeight(), 10) - 17;
            }
            wind.style.zIndex = "100";
            // wind.style.border = " 1px solid #333333";
            wind.style.backgroundColor = "#ffffff";
            selectedWndID = id;
            createTabs();
            wndSelectTab(id);

        }
        else {
            wind.style.zIndex = "100";
            wndSelectTab(id);
        }

    } catch (err) {
        stack.pop(id);
        alert("wndDialog:" + err);
    }
}

function wndClear(id) {
    var wnd = document.getElementById(id);
    if (wnd != null) {
        wnd.onclose = null;
        wnd.onclick = null;
        //wnd.innerHTML = null;

    }
}




function createTabs() {
    var tabs = "";
    var t = stack.cards;
    if (t) {
        if (t.length > 0) {
            for (var i = 0; i < t.length; i++) {
                var elem = t[i].split('_');
                tabs += "<span class=\"sampleDiv\" onclick=\"wndSelectTab('" + elem[0] + "')\">" + elem[1] + "</>" + ' | ';
            }
        }
    }
    var phTab = document.getElementById('wndTabsPh');
    if (phTab) {
        phTab.innerHTML = tabs;
        //phTab.style.zIndex = 94;
        //phTab.style.display = 'inline';
    }

}
function wndSelectTab(id) {

    if (id.id != null)
        id = id.id;

    wndMaximize(id);
    wndFocus(id);
    selectedWndID = id;
    createTabs();

}
function wndDefaultZindex() {

    var t = stack.cards;
    if (t) {
        if (t.length > 0) {
            for (var i = 0; i < t.length; i++) {
                var elem = t[i].split('_');
                wndZIndex(elem[0], zIndexDefault);
            }
        }
    }
}
function wndZIndex(id, value) {
    if (id.id != null)
        id = id.id;
    var w = document.getElementById(id);
    if (w) { w.style.zIndex = value; }
}
function wndMinimize(id) {
    wndShowHide(id, false);
}
function wndMaximize(id) {
    wndShowHide(id, true);
}
function wndFocus(id) {
    wndDefaultZindex();
    wndZIndex(id, zIndexFocus);
}

function setSize(id, w, h) { //set window size (min is 200px wide by 50px tall)
    //debugger;
    var wnd = document.getElementById(id);  //wndElement[id]['window'];
    wnd.style.position = "absolute";
    wnd.style.width = Math.max(parseInt(w), 220) + "px";
    wnd.zIndex = zIndexvalue;
    wnd.style.height = Math.max(parseInt(h), 50) + "px";

}


function moveTo(id, x, y) { //move window. Position includes current viewpoint of document
    //Get current viewpoint numbers
    //debugger;
    var theBody = document.getElementsByTagName("BODY")[0];
    var scroll_top = parseInt(getScrollTop(), 10);
    var scroll_left = parseInt(theBody.scrollLeft, 10);
    var docwidth = getViewportWidth();
    var docheight = getViewportHeight();
    var wnd = document.getElementById(id);

    if (wnd) {
        var titleBarHeight = parseInt(wnd.offsetHeight, 10);

        var xpos = (x == "middle") ? scroll_left + (docwidth - wnd.offsetWidth) / 2 + "px" : scroll_left + parseInt(x) + "px"
        var ypos = (y == "middle") ? scroll_top + (docheight - (wnd.offsetHeight)) / 2 + "px" : scroll_top + parseInt(y) + "px"
        wnd.style.left = xpos;
        wnd.style.top = ypos;
        //wnd.style.zIndex = 130;
    }

    //alert('x=' + wnd.style.left + '--y' + wnd.style.top);
}
function wndShowHide(id, status) {
    if (id.id != null)
        id = id.id;
    var w = document.getElementById(id);
    if (w) {
        var value = (status) ? 'inline' : 'none';
        w.style.display = value;
    }
}
//addEvent(window, "resize", moveTo);
//addEvent(window, "scroll", moveTo);
//window.onscroll = moveTo;
function wndIsOpen(hook) {
    var t = stack.cards;
    if (t) {
        if (t.length > 0) {
            for (var i = 0; i < t.length; i++) {
                var elem = t[i].split('_');
                if (elem[1] == hook)
                    return true;
            }
        }
    }
    return false;
}

//-----------------------create template-----------------------------------------------------------
function wndTopTmpl(id, title) {
    //debugger;

    var imageFiles = new Array();
    imageFiles['close'] = '<img onclick="wndKillDialog(' + id + ')" src="http://www.bisericaemanuelsibiu.ro/generic/images/tools/icons/close.gif" title="Inchide" id="' + id + '_close">';
    //imageFiles['minimize'] = '<img src="/automarket/generic/images/icons/min.gif" title="Minimizeaza" id="' + id + '_min">';
    var handle = '<div class="drag-handle" valign="top" style="opacity: 1; width:100%;">' + title + '<div class="drag-controls">' + imageFiles['close'] + '</div></div>';
    return handle;
}

function wndBottomTmpl() {
    var imageFile = new Array();
    imageFile['statusarea'] = 'url(http://www.bisericaemanuelsibiu.ro/generic/images/tools/icons/resize.gif)';
    var statusArea = '<div class="drag-statusarea" style="display: block;"><div style="background: transparent ' + imageFile['statusarea'] + ' no-repeat scroll right top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" class="drag-resizearea"> </div></div>'
    return statusArea;
}
function wndContentTmpl(id, content) {
    var contentarea = '<div id="wndContent_' + id + '" class="drag-contentarea" style="overflow: hidden; width:100%; height:100%;display: block;">' + content + '</div>';
    return contentarea;
}
function wndIframeTopTmpl(id, title) {
    //debugger;

    var imageFiles = new Array();
    imageFiles['close'] = '<img onclick="wndKillDialog(' + id + ')" src="http://www.bisericaemanuelsibiu.ro/generic/images/tools/icons/close.gif" title="Inchide" id="' + id + '_close">';
    imageFiles['minimize'] = '<img onclick="wndMinimize(' + id + ')" src="http://www.bisericaemanuelsibiu.ro/generic/images/tools/icons/min.gif" title="Minimizeaza" id="' + id + '_min">';
    var handle = '<div class="drag-handle" valign="top" style="opacity: 1; width:100%;">' + title + '<div class="drag-controls">' + imageFiles['minimize'] + imageFiles['close'] + '</div></div>';
    return handle;
}
function wndIframeContentTmpl(id, path) {
    //<td id="wndTdContent_' + id + '"></td>
    var contentarea = '<iframe class="drag-contentarea" id="wndIContent_' + id + '" src="' + path + '" style="width:100%;height:100%;" scrolling="auto" frameborder="0" marginwidth="1" marginheight="1" width="100%" height="100%" ></iframe>'//allowTransparency="true"
    return contentarea;
}
function wndMenuTopTmpl() {
    //debugger;
    var imageFile = new Array();
    imageFile['menu'] = 'url(generic/images/icons/collapse.jpg)';
    var handle = '<div class="drag-menu" style="display: block;"><div style="background: transparent ' + imageFile['menu'] + ' no-repeat scroll right top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" class="drag-menu"> </div></div>';
    return handle;
}
//-----------------------------------------------------Creating Stack Object-------------------------------------------------------------------------------------
function Stack() {
    // Create an empty array of cards.
    this.cards = new Array();  //cards array inside stack object
    this.push = pushdata;      //Call pushdata function on push operation
    this.pop = popdata;      //Call popdata function on pop operation
    this.printStack = showStackData; //Call showStackData function on printstack operation
}

function pushdata(data) {
    stack.cards.push(data);
}

function popdata(data) {
    var hook = data.split('_');
    var t = stack.cards;
    if (t) {
        if (t.length > 0) {
            for (var i = 0; i < t.length; i++) {
                var elem = t[i].split('_');
                if (elem[0] == hook[0])
                    data = hook[0] + "_" + elem[1];
            }
        }
    }
    return stack.cards.pop(data);
}

function showStackData() {
    return stack.cards;
}
