/**
 * SUBMODAL v1.6
 * By Subimage LLC
 * http://www.subimage.com
 * Up to date code can be found at http://submodal.googlecode.com
 */

// Popup code
function ToffPopUpModal() {
    var mySelf = this;
    this.gDefaultPage = ToffConstant.ROOT + "loading.html";
    this.gPopupMask = null;
    this.gPopupContainer = null;
    this.gPopFrame = null;
    this.gReturnFunc = null;
    this.gPopupIsShown = true;
    this.gHideSelects = false;
    this.gReturnVal = null;
    this.gTitleBar = null;
    this.gTitle = null;
    this.gMessageDiv = null;

    this.gTabIndexes = new Array();
    // Pre-defined list of tags we want to disable/enable tabbing into
    this.gTabbableTags = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");
    
    this.gHidableTags = new Array("OBJECT", "EMBED", "APPLET", "SELECT");

    // If using Mozilla or Firefox, use Tab-key trap.
    if (!document.all) {
        document.onkeypress = mySelf.KeyDownHandler;
    }

    // Initializes popup code on load.	
    this.Init = function () {
        // Add the HTML to the body
        theBody = document.getElementsByTagName('BODY')[0];
        popmask = document.createElement('div');
        popmask.id = 'popupMask';
        popcont = document.createElement('div');
        popcont.id = 'popupContainer';
        popcont.innerHTML = '' +
		'<div id="popupInner" class="borderedInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle"></div>' +
				'<div id="popupControls">' +
					'<a href="javascript:;" onclick="ToffPopup.Hide(false);" id="popCloseBox"></a>' +
				'</div>' +
			'</div>' +
			'<div id="popupMessageDiv" style="display:none"></div>' +
			'<iframe src="' + mySelf.gDefaultPage + '" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%" style="display:none"></iframe>' +
		'</div>';
        theBody.appendChild(popmask);
        theBody.appendChild(popcont);

        mySelf.gPopupMask = document.getElementById("popupMask");
        mySelf.gPopupContainer = document.getElementById("popupContainer");
        mySelf.gPopFrame = document.getElementById("popupFrame");
        mySelf.gTitleBar = document.getElementById("popupTitleBar");
        mySelf.gTitle = document.getElementById("popupTitle");
        mySelf.gMessageDiv = document.getElementById("popupMessageDiv");

        // 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) {
            mySelf.gHideSelects = true;
        }

        mySelf.AddEvent(window, "resize", mySelf.Center);
        mySelf.AddEvent(window, "scroll", mySelf.Center);
        window.onscroll = mySelf.Center;
    }

    this.Show = function(url, title, width, height) {
        mySelf.gPopFrame.style.display = "block";
        mySelf.gMessageDiv.style.display = "none";
        mySelf.Set(title, width, height, false, false, false, null);
        // set the url
        if(url) mySelf.gPopFrame.src = url;
    }

    this.ShowFrameNoBorder = function(width, height, url) {
        mySelf.gPopFrame.style.display = "block";
        mySelf.gMessageDiv.style.display = "none";
        mySelf.Set(null, width, height, false, false, false, null);
        // set the url
        if (url) mySelf.gPopFrame.src = url;
    }

    this.ShowFrame = function(width, height, showCloseBox, title, url) {
        mySelf.gPopFrame.style.display = "block";
        mySelf.gMessageDiv.style.display = "none";
        mySelf.Set(title, width, height, showCloseBox, true, true, null);
        // set the url
        if (url) mySelf.gPopFrame.src = url;
    }

    this.ShowLoading = function(url) {
        mySelf.Set(null, 250, 70, false, false, true, null);
        mySelf.gPopFrame.style.display = "none";
        mySelf.gMessageDiv.style.display = "block";
        mySelf.gMessageDiv.innerHTML = '<img src="' + ToffConstant.ROOT + 'images/loading.gif" width="60" height="60" alt="loading ..." style="float:left; padding-left:10px" /><span>Loading.....<br />Please wait.</span>';
        if (url) { mySelf.gPopFrame.src = url; }
    }
    
    this.ShowMessage = function(message, width, height) {
        mySelf.gPopFrame.style.display = "none";
        mySelf.gMessageDiv.style.display = "block";
        mySelf.gMessageDiv.innerHTML = message;
        mySelf.Set(null, width, height, false, false, true, null);
    }

    this.Set = function(title, width, height, showCloseBox, showTitle, showBorder, returnFunc) {
        document.getElementById("popCloseBox").style.display = (showCloseBox && showCloseBox != false ? "block" : "none");
        document.getElementById("popupInner").className = (showBorder && showBorder != false ? "borderedInner" : "noBorderInner");
        mySelf.gTitleBar.style.display = (showTitle && showTitle != false ? "block" : "none");
        mySelf.gTitleBar.style.visibility = (showTitle && showTitle != false ? "visible" : "hidden");

        mySelf.gTitle.innerHTML = (title ? title : "");

        mySelf.gPopupIsShown = true;
        mySelf.DisableTabIndexes();
        mySelf.gPopupMask.style.display = "block";
        mySelf.gPopupContainer.style.display = "block";
        // calculate where to place the window on screen
        mySelf.Center(width, height);

        var titleBarHeight = parseInt(mySelf.gTitleBar.offsetHeight, 10);

        mySelf.gPopupContainer.style.width = width + "px";
        mySelf.gPopupContainer.style.height = (height + (showTitle && showTitle != false ? titleBarHeight : 0)) + "px";

        mySelf.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
        mySelf.gPopFrame.style.width = (showTitle && showTitle != false ? parseInt(mySelf.gTitleBar.offsetWidth, 10) : width) + "px";
        mySelf.gPopFrame.style.height = (height) + "px";
        mySelf.gMessageDiv.style.height = (height) + "px";

        mySelf.gReturnFunc = returnFunc;

        mySelf.HideTags();
    }


    /**
    * @argument callReturnFunc - bool - determines if we call the return function specified
    * @argument returnVal - anything - return value 
    */
    this.Hide = function(callReturnFunc) {
        mySelf.gPopupIsShown = false;
        var theBody = document.getElementsByTagName("BODY")[0];
        theBody.style.overflow = "";
        mySelf.RestoreTabIndexes();
        if (mySelf.gPopupMask == null) return;

        mySelf.gPopFrame.style.display = "none";
        mySelf.gMessageDiv.style.display = "none";
        mySelf.gPopupMask.style.display = "none";
        mySelf.gPopupContainer.style.display = "none";
        if (callReturnFunc == true && mySelf.gReturnFunc != null) {
            mySelf.gReturnVal = window.frames["popupFrame"].returnVal;
            window.setTimeout('ToffPopup.gReturnFunc(ToffPopup.gReturnVal);', 1);
        }
        mySelf.gPopFrame.src = mySelf.gDefaultPage;

        mySelf.RestoreTags();
    }
    
    //
    this.Center = function (width, height) {
        if (mySelf.gPopupIsShown == true) {
            if (width == null || isNaN(width)) {
                width = mySelf.gPopupContainer.offsetWidth;
            }
            if (height == null) {
                height = mySelf.gPopupContainer.offsetHeight;
            }

            var theBody = document.getElementsByTagName("BODY")[0];
            var scTop = parseInt(mySelf.GetScrollTop(), 10);
            var scLeft = parseInt(theBody.scrollLeft, 10);

            mySelf.SetMaskSize();

            var titleBarHeight = parseInt(mySelf.gTitleBar.offsetHeight, 10);

            var fullHeight = mySelf.GetViewportHeight();
            var fullWidth = mySelf.GetViewportWidth();

            mySelf.gPopupContainer.style.top = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px";
            mySelf.gPopupContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
        }
    }

    // Sets the size of the popup mask.
    this.SetMaskSize = function () {
        var theBody = document.getElementsByTagName("BODY")[0];

        var fullHeight = mySelf.GetViewportHeight();
        var fullWidth = mySelf.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;
        }

        mySelf.gPopupMask.style.height = popHeight + "px";
        mySelf.gPopupMask.style.width = popWidth + "px";
    }

    // Tab key trap. if popup is shown and key was [TAB], suppress it.
    // @argument e - event - keyboard event that caused this function to be called.
    this.KeyDownHandler = function (e) {
        if (mySelf.gPopupIsShown && e.keyCode == 9) return false;
    }

    // For IE.  Go through predefined tags and disable tabbing into them.
    this.DisableTabIndexes = function () {
        if (document.all) {
            var i = 0;
            for (var j = 0; j < mySelf.gTabbableTags.length; j++) {
                var tagElements = document.getElementsByTagName(mySelf.gTabbableTags[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    mySelf.gTabIndexes[i] = tagElements[k].tabIndex;
                    tagElements[k].tabIndex = "-1";
                    i++;
                }
            }
        }
    }

    // For IE. Restore tab-indexes.
    this.RestoreTabIndexes = function () {
        if (document.all) {
            var i = 0;
            for (var j = 0; j < mySelf.gTabbableTags.length; j++) {
                var tagElements = document.getElementsByTagName(mySelf.gTabbableTags[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    tagElements[k].tabIndex = mySelf.gTabIndexes[i];
                    tagElements[k].tabEnabled = true;
                    i++;
                }
            }
        }
    }

    this.HideTags = function() {
        for (var i = 0; i < mySelf.gHidableTags.length; i++) {
            var tagElements = document.getElementsByTagName(mySelf.gHidableTags[j]);
            for (var j = 0; j < tagElements.length; j++) {
                tagElements[j].oldDisplay = tagElements[j].style.display ? tagElements[j].style.display : "";
                tagElements[j].style.display = "none";
            }
        }
    }

    this.RestoreTags = function () {
        for (var i = 0; i < mySelf.gHidableTags.length; i++) {
            var tagElements = document.getElementsByTagName(mySelf.gHidableTags[j]);
            for (var j = 0; j < tagElements.length; j++) {
                if(tagElements[j].oldDisplay)
                    tagElements[j].style.display = tagElements[j].oldDisplay;
            }
        }
    }
    /**
    * By Seth Banks (webmaster at subimage dot com)
    * http://www.subimage.com/
    *
    * Up to date code can be found at http://www.subimage.com/dhtml/
    */
    this.AddEvent = function (obj, evType, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(evType, fn, false);
            return true;
        } else if (obj.attachEvent) {
            var r = obj.attachEvent("on" + evType, fn);
            return r;
        } else {
            return false;
        }
    }
    this.RemoveEvent = function (obj, evType, fn, useCapture) {
        if (obj.removeEventListener) {
            obj.removeEventListener(evType, fn, useCapture);
            return true;
        } else if (obj.detachEvent) {
            var r = obj.detachEvent("on" + evType, fn);
            return r;
        } else {
            alert("Handler could not be removed");
        }
    }

    /**
    * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
    * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
    */
    this.GetViewportHeight = function () {
        if (window.innerHeight != window.undefined) return window.innerHeight;
        if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
        if (document.body) return document.body.clientHeight;

        return window.undefined;
    }
    this.GetViewportWidth = function () {
        var offset = 17;
        var width = null;
        if (window.innerWidth != window.undefined) return window.innerWidth;
        if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
        if (document.body) return document.body.clientWidth;
    }

    /**
    * Gets the real scroll top
    */
    this.GetScrollTop = function () {
        if (self.pageYOffset) // all except Explorer
        {
            return self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
        {
            return document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            return document.body.scrollTop;
        }
    }
    this.GetScrollLeft = function () {
        if (self.pageXOffset) // all except Explorer
        {
            return self.pageXOffset;
        }
        else if (document.documentElement && document.documentElement.scrollLeft)
        // Explorer 6 Strict
        {
            return document.documentElement.scrollLeft;
        }
        else if (document.body) // all other Explorers
        {
            return document.body.scrollLeft;
        }
    }
}

var ToffPopup = new ToffPopUpModal();
