﻿var modalWindow = {
    width: Number,
    height: Number,
    opacity: Number,
    modal: Boolean = true,
    iframe: Boolean = true,
    savebut: Boolean = true,
    close: function() {
        $(".modal-window").remove();
        $(".modal-overlay").fadeOut('slow');
        $(".modal-overlay").remove();
    },
    save: function() {
        if (this.iframe) {
            window.frames['modalPopFrame'].document.forms[0].submit();
        }
        else {
            document.forms[0].submit();
        }

    },
    open: function(source) {
        var contents;
        var modalChunk = "";
        modalChunk += "<div class=\"modal-overlay\"></div>";
        if (this.iframe)
            modalChunk += "<img src='/images/loading-animation.gif' class='modal-loading' style=\"margin-top:-5px; margin-left:-75px;\" />";

        var topmargin = "";
        //we don't want to adjust the margin top for ie6 browser
        if (!($.browser.msie && $.browser.version.substr(0, 1) == "6")) {
            topmargin = "margin-top:-" + (this.height / 2) + "px;";
        }
        modalChunk += "<div id=\"modalPop\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; " + topmargin + " margin-left:-" + (this.width / 2) + "px;\">";
        modalChunk += "</div>";
        /*modalChunk += "<div id=\"modalPop\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
        modalChunk += "</div>";	*/

        $("body").append(modalChunk);

        //$(".modal-overlay").css({ position: "absolute", height: "100%", width: "100%", top: 0, left: 0, right: 0, bottom: 0 });
        $(".modal-overlay").css({ position: "absolute", height: $(document).height(), width: $(document).width(), top: 0, left: 0, right: 0, bottom: 0 });
        $(".modal-overlay").css({ zIndex: 101, display: "none", opacity: this.opacity });
        if (!this.modal) {
            //APL 10/03/2008 added code for the save button
            if (this.savebut) 
            {
                $("#modalPop").append("<a class=\"save-window\" style=\"display: none;\"></a>");
                $(".save-window").click(function() { modalWindow.save(); });
            }

            $("#modalPop").append("<a class=\"close-window\" style=\"display: none;\"></a>");
            $(".close-window").click(function() { modalWindow.close(); });
        }

        if (this.iframe)
            contents = "<iframe name='modalPopFrame' id='modalPopFrame' width='" + $("#modalPop").width() + "' height='" + $("#modalPop").height() + "' frameborder='0' scrolling='auto' allowtransparency='true' src='" + source + "'></iframe>";
        else
            contents = source;
        if (this.iframe)
            $(".modal-overlay").fadeIn('slow',
		        function() {
            $("#modalPop").append("<iframe name='modalPopFrame' id='modalPopFrame' width='" + $("#modalPop").width() + "' height='" + $("#modalPop").height() + "' frameborder='0' scrolling='auto' allowtransparency='true' src='" + source + "'></iframe>");
            $("#modalPopFrame").load(
		                function() {
                $(".modal-loading").remove();
                $(".close-window").css({ zIndex: 103, display: "block" });
                $(".save-window").css({ zIndex: 103, display: "block" });
            }
		            );

        }
		    );
        else
            $(".modal-overlay").fadeIn('slow',
		        function() {
            $("#modalPop").append(source);
            $(".close-window").css({ zIndex: 103, display: "block" });
            $(".save-window").css({ zIndex: 103, display: "block" });
        }
		    );
    }
};

var lightbox = function(source, width, height, modal, iframe, savebut) {

    if (width > $(window).width())
        modalWindow.width = $(window).width() - 30;
    else
        modalWindow.width = width;

    if (height > $(window).height())
        modalWindow.height = $(window).height() - 65;
    else
        modalWindow.height = height;

    modalWindow.modal = modal;
    modalWindow.opacity = .7;
    if (iframe != null)
        modalWindow.iframe = iframe;
    else
        modalWindow.iframe = true;
    if (savebut != null)
        modalWindow.savebut = savebut;
    else
        modalWindow.savebut = true;

    var contents = source;
    if (!modalWindow.iframe)
        contents = $("#" + source).html();
    modalWindow.open(contents);
};

var loadingLightbox = function()
{
    modalWindow.width = 200;
    modalWindow.height = 100;
    modalWindow.opacity = .7;
    modalWindow.modal = true;
    modalWindow.iframe = false;
    modalWindow.savebut = true;
    modalWindow.open('<div id="loadingWindow"><img src="/images/icons/loading/loading-circle-large.gif" /> Loading...</div>');    
};

