﻿//JQuery modal popup
if (!jQuery) {
    throw ('jQuery has not been loaded!');
}

//original
var modalWindow = {
    parent: "body",
    windowId: null,
    url: null,
    width: null,
    height: null,
    shown: false,
    skin: {
        loading: '<div id="modal-loading" >' +
						'<img src="../images/ajax-loading.gif" alt="loading" />' +
						'<span>Loading or <a href="javascript: modalWindow.deactivate();">Cancel</a></span>' +
					 '</div>',
        main: '<div id="modal-container" >' +
				        '<div id="modal-title-bar" >' +
					        '<span id="modal-title"></span>' +
						    '<a id="modal-close-link" >close</a>' +
				        '</div>' +
				        '<div id="modal-contents" >' +
				        '</div>' +
				  '</div>',
        overlay: "<div id=\"modal-overlay\"></div>"
    },
    close: function() {
        IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) &&
                      (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1)
        if (IE6) {
            jQuery("select").css("visibility", "visible");
        }
    
        jQuery("#modal-contents").remove();
        setTimeout(function() { jQuery('#modal-overlay').remove(); }, 0);
        jQuery("#modal").remove();

        this.shown = false;
    },
    open: function() {
        if (this.shown)
            return;
        else
            this.shown = true;

        try {
            var modal = document.createElement('div');
            modal.setAttribute('id', 'modal');
            modal.innerHTML = this.skin.overlay + this.skin.main;

            jQuery(this.parent).append(modal);
            jQuery("#modal-contents").html = this.skin.loading;
            jQuery("#modal-overlay").click(function() { modalWindow.close(); });
            jQuery("#modal-close-link").click(function() { modalWindow.close(); });

            IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) &&
                  (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1)
            if (IE6) {
                jQuery("select").css("visibility", "hidden");
            }

            var curWidth = this.width;
            var curHeight = this.height;
            jQuery("#modal-container").css("margin", "-" + curHeight / 2 + "px auto auto -" + curWidth / 2 + "px");
            jQuery("#modal-container").animate({ width: curWidth + "px", height: curHeight + "px" }, "slow");


            jQuery.get(this.url, function(data) {
                try {
                    data = data.replace("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">", "");
                    data = data.replace("<html xmlns=\"http://www.w3.org/1999/xhtml\">", "");
                    data = data.replace("</html>", "");
                    data = data.replace("<head>", "");
                    data = data.replace("</head>", "");
                    data = data.replace("<body>", "");
                    data = data.replace("</body>", "");

                    jQuery("#modal-contents").html(data);
                }
                catch (err) {
                    jQuery("#modal-contents").html("Error encountered when loading page (" + err.description + ")");
                }
            });
        }
        catch (Error) {
        }
    }
};

var openJQModal = function(url, width, height) {
    modalWindow.width = width;
    modalWindow.height = height;
    modalWindow.url = url;

    modalWindow.open();
};
