/// <reference path="QvAjax.js" />
// Build 10.00.8935.7
var ModalType = { "popup": 0, "messagebox": 1 }

Qva.Modal = function(scriptPath) {
    if (typeof scriptPath !== 'string') scriptPath = null;
    this.ScriptPath = scriptPath || Qva.QvAjaxZfcPath + 'htc/';
    this.HideSelects = false;
    this.DefaultPage = "modal/loading.html";
    this.TabIndexes = [];
    this.PageBinders = [];
    this.PopupMasks = [];
    this.PopupCointainers = [];
    this.PopFrames = [];
    this.TabbableTags = ["A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME"];
    this.PopupIsShown = false;
    this.NumberOfShownWindows = 0;
    this.Type = [];
    Qva.Modal.instance = this;
}

Qva.Modal.prototype.ShowEx = function(pagebinder, url, width, height, type) {
    this.NumberOfShownWindows++;

    this.Type[this.NumberOfShownWindows] = type;

    this.PageBinders[this.NumberOfShownWindows] = pagebinder;
    this.Init();


    var closeBox = document.getElementById("popCloseBox" + this.NumberOfShownWindows);

    if (closeBox) {
        closeBox.style.display = "block";
    }

    this.PopupIsShown = true;

    this.DisableTabs();
    this.PopupMasks[this.NumberOfShownWindows].style.display = "block";
    this.PopupCointainers[this.NumberOfShownWindows].style.display = "block";



    if (width && height) this.SetSize(width, height);

    if (this.PageBinders[this.NumberOfShownWindows].Session) {
        // transfer session
        url += ((url.indexOf('?') == -1) ? '?' : '&') + 'session=' + escape(this.PageBinders[this.NumberOfShownWindows].Session);
    }
    // transfer ticket
    if (this.PageBinders[this.NumberOfShownWindows].Ticket) url = Qva.FixUrl(url, "ticket", this.PageBinders[this.NumberOfShownWindows].Ticket);
    // transfer host
    if (this.PageBinders[this.NumberOfShownWindows].Host) url = Qva.FixUrl(url, "host", this.PageBinders[this.NumberOfShownWindows].Host);
    if (this.PageBinders[this.NumberOfShownWindows].Unicorn) url = Qva.FixUrl(url, "unicorn", "3");
    //transfer view and remote
    if (this.PageBinders[this.NumberOfShownWindows].View != "") url = Qva.FixUrl(url, "view", this.PageBinders[this.NumberOfShownWindows].View);
    if (this.PageBinders[this.NumberOfShownWindows].Remote != "") url = Qva.FixUrl(url, "remote", this.PageBinders[this.NumberOfShownWindows].Remote);

    if (Qva.Benchmark) {
        this.Benchmark = new Qva.Benchmark();
    }
    url = Qva.AddProxyIfAny(url);
    // set the url
    this.PopFrames[this.NumberOfShownWindows].contentWindow.location.replace(url); // prevent browser history

    // for IE
    if (this.HideSelects == true) {
        this.HideSelectBoxes();
    }
}

Qva.Modal.prototype.Show = function(pagebinder, url, width, height) {
    this.ShowEx(pagebinder, url, width, height, ModalType.popup);
}

Qva.Modal.prototype.SetSize = function(width, height) {
    // calculate where to place the window on screen
    this.CenterWin(width, height);

    var titleBarHeight = parseInt(document.getElementById("popupTitleBar" + this.NumberOfShownWindows).offsetHeight, 10);

    this.PopupCointainers[this.NumberOfShownWindows].style.width = width + "px";
    this.PopupCointainers[this.NumberOfShownWindows].style.height = (height + titleBarHeight) + "px";
    
    this.SetMaskSize();
    
    // need to set the width of the iframe to the title bar width because of the dropshadow
    // some oddness was occuring and causing the frame to poke outside the border in IE6
    this.PopFrames[this.NumberOfShownWindows].style.width = parseInt(document.getElementById("popupTitleBar" + this.NumberOfShownWindows).offsetWidth, 10) + "px";
    this.PopFrames[this.NumberOfShownWindows].style.height = (height) + "px";
}

Qva.Modal.prototype.Init = function() {

    if (this.PopupMasks[this.NumberOfShownWindows] != null) return;
    theBody = document.getElementsByTagName('BODY')[0];

    var popmask = document.createElement('div');
    popmask.style.zIndex = 200 + 3 * this.NumberOfShownWindows;
    popmask.className = 'popupMask';
    popmask.style.position = 'absolute';

    var popcont = document.createElement('div');
    popcont.style.zIndex = 201 + 3 * this.NumberOfShownWindows;
    popcont.className = 'popupContainer';
    popcont.style.position = 'absolute';

    var imageText = ' ';



    if (this.Type[this.NumberOfShownWindows] != ModalType.popuponlyok)
        imageText = '<img src="' + Qva.AddProxyIfAny(this.ScriptPath + 'modal/close.gif') + '" id="popCloseBox' + this.NumberOfShownWindows + '" />';

    popcont.innerHTML = '' +
        '<div>' +
            '<div class="popupTitleBar" id="popupTitleBar' + this.NumberOfShownWindows + '">' +
                '<div class="popupTitle" id="popupTitle' + this.NumberOfShownWindows + '" style="width:90%"></div>' +
                '<div class="popupControls" id="popupControls' + this.NumberOfShownWindows + '" style="width:15px">' + imageText +
                '</div>' +
            '</div>' +
            '<iframe src="' + Qva.AddProxyIfAny(this.ScriptPath + this.DefaultPage) + '" style="width:100%;height:100%;background-color:#E9E9E9;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame' + this.NumberOfShownWindows + '" name="popupFrame" width="100%" height="100%"></iframe>' +
        '</div>';

    theBody.appendChild(popmask);
    theBody.appendChild(popcont);

    this.PopupCloseBox = document.getElementById("popCloseBox" + this.NumberOfShownWindows);
    this.PopupMasks[this.NumberOfShownWindows] = popmask;
    this.PopupCointainers[this.NumberOfShownWindows] = popcont;

    this.PopFrames[this.NumberOfShownWindows] = document.getElementById("popupFrame" + this.NumberOfShownWindows);

    var popupTitleBar = document.getElementById("popupTitleBar" + this.NumberOfShownWindows);
    popupTitleBar.style.zIndex = 202 + 3 * this.NumberOfShownWindows;

    popupTitleBar.onmousedown = function(event) {
        if (!event) event = window.event;

        var offsets = Qva.GetOffsets(event, popcont);

        function EndMove(event) {
            popupTitleBar.onmousemove = null;
            popupTitleBar.onmouseup = null;
            popupTitleBar.onmouseout = null;
            return false;
        }
        function MouseMove(event) {
            if (!event) event = window.event;
            var mousePos = { 'x': event.clientX + Qva.GetScrollLeft(),
                'y': event.clientY + Qva.GetScrollTop()
            };
            popcont.style.left = (mousePos.x - offsets.offsetX) + "px";
            popcont.style.top = (mousePos.y - offsets.offsetY) + "px";
            return false;
        }
        popupTitleBar.onmousemove = MouseMove;
        popupTitleBar.onmouseup = EndMove;
        popupTitleBar.onmouseout = EndMove;
        return false;
    };

    if (this.PopupCloseBox) {

        this.PopupCloseBox.onmousedown = function(event) {
            Qva.Modal.instance.Close();
        }
    }

    // check to see if this is IE version 6 or lower. hide select boxes if so
    // maybe they'll fix this in version 7?
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        this.HideSelects = true;
    }
}

// For IE.  Go through predefined tags and disable tabbing into them.
Qva.Modal.prototype.DisableTabs = function() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < this.TabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                this.TabIndexes[i] = tagElements[k].tabIndex;
                tagElements[k].tabIndex="-1";
                i++;
            }
        }
    }
}

// For IE. Restore tab-indexes.
Qva.Modal.prototype.RestoreTabs = function() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < this.TabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                tagElements[k].tabIndex = this.TabIndexes[i];
                tagElements[k].tabEnabled = true;
                i++;
            }
        }
    }
}

Qva.Modal.prototype.CenterWin = function(width, height) {
    if (this.PopupIsShown) {
        if (width == null || isNaN(width)) {
            width = this.PopupCointainers[this.NumberOfShownWindows].offsetWidth;
        }
        if (height == null) {
            height = this.PopupCointainers[this.NumberOfShownWindows].offsetHeight;
        }
        
        var theBody = document.getElementsByTagName("BODY")[0];
        var scTop = parseInt(Qva.GetScrollTop(),10);
        var scLeft = parseInt(theBody.scrollLeft,10);
        
        this.SetMaskSize();

        var titleBarHeight = parseInt(document.getElementById("popupTitleBar" + this.NumberOfShownWindows).offsetHeight, 10);
        
        var fullHeight = Qva.GetViewportHeight();
        var fullWidth = Qva.GetViewportWidth();

        this.PopupCointainers[this.NumberOfShownWindows].style.top = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px";
        this.PopupCointainers[this.NumberOfShownWindows].style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
    }
}

Qva.Modal.prototype.SetMaskSize = function() {
    var theBody = document.getElementsByTagName("BODY")[0];
    
    var fullHeight = Qva.GetViewportHeight();
    var fullWidth = Qva.GetViewportWidth();
    
    // Determine what's bigger, scrollHeight or fullHeight / width
    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }
    
    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }

    //this.PopupMasks[this.NumberOfShownWindows].style.height = popHeight + "px";
    //this.PopupMasks[this.NumberOfShownWindows].style.width = popWidth + "px";
}

Qva.Modal.prototype.HideSelectBoxes = function() {
    for(var i = 0; i < document.forms.length; i++) {
        for(var e = 0; e < document.forms[i].length; e++){
            if(document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility="hidden";
            }
        }
    }
}

Qva.Modal.prototype.DisplaySelectBoxes = function() {
    for(var i = 0; i < document.forms.length; i++) {
        for(var e = 0; e < document.forms[i].length; e++){
            if(document.forms[i].elements[e].tagName == "SELECT") {
            document.forms[i].elements[e].style.visibility="visible";
            }
        }
    }
}

Qva.Modal.prototype.Hide = function() {
    var theBody = document.getElementsByTagName("BODY")[0];
    theBody.style.overflow = "";
    this.RestoreTabs();
    if (this.PopupMasks[this.NumberOfShownWindows] == null) {
        return;
    }
    this.PopupMasks[this.NumberOfShownWindows].style.display = "none";
    this.PopupCointainers[this.NumberOfShownWindows].style.display = "none";
    this.PopFrames[this.NumberOfShownWindows].contentWindow.location.replace(Qva.AddProxyIfAny(this.ScriptPath + this.DefaultPage));
    // display all select boxes
    if (this.HideSelects == true) {
        this.DisplaySelectBoxes();
    }

    this.NumberOfShownWindows--;
    this.PopupIsShown = this.NumberOfShownWindows > 0;
}

Qva.Modal.prototype.SetTitle = function (text) {
    try {
        document.getElementById("popupTitle" + this.NumberOfShownWindows).innerText = text;
    } catch(e) {
    }
}

Qva.Modal.prototype.Close = function() {
    if (this.Type[this.NumberOfShownWindows] != ModalType.popup) return;
    this.CloseEx();
}


Qva.Modal.prototype.CloseEx = function() {
    this.Hide();
    if (this.PageBinders[this.NumberOfShownWindows + 1]) {
        if (this.PageBinders[this.NumberOfShownWindows + 1].LabelClick) {
            this.PageBinders[this.NumberOfShownWindows + 1].Set('.Nothing', 'add', 'nothing', true);
        } else {
            this.PageBinders[this.NumberOfShownWindows + 1].Refresh();
        }
    }
}
