﻿var modalWindow = {
    width: Number,
    height: Number,
    opacity: Number,
    modal: Boolean = true,
    iframe: Boolean = true,
    savebut: Boolean = true,
    callback: function() {

    },
    close: function() {
        $(".modal-window").remove();
        $(".modal-overlay").fadeOut('slow');
        $(".modal-overlay").remove();
    },
    save: function() {
        if (this.iframe) {
            if (window.frames['modalPopFrame'].document) {
                window.frames['modalPopFrame'].document.forms[0].submit();
            }
            else {
                window.frames[0].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: $(document).height(), width: $(document).width(), top: 0, left: 0, right: 0, bottom: 0 });
        $(".modal-overlay").css({ zIndex: 1001, display: "none", opacity: this.opacity });
        if (!this.modal) {
            if (this.savebut) {
                $("#modalPop").append("<a class=\"save-window\" style=\"display: none;\"></a>");
                $(".save-window").click(function() { modalWindow.save(); });
            }

            if(this.closebut) {
                $("#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: 1003, display: "block" });
                $(".save-window").css({ zIndex: 1003, display: "block" });
            }
		            );

        }
		    );
        else
            $(".modal-overlay").fadeIn('slow',
		        function() {
            $("#modalPop").append(source);
            $(".close-window").css({ zIndex: 1003, display: "block" });
            $(".save-window").css({ zIndex: 1003, display: "block" });
            modalWindow.callback();
        }
		    );

    }
};

var lightbox = function(source, width, height, modal, overrideHeight, iframe, callback, savebut, closebut)   
{   
    if (width > $(window).width())
        modalWindow.width = $(window).width() - 30;
    else
        modalWindow.width = width;
        
    if (height > $(window).height() && !overrideHeight)
        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;
   
    if (closebut != null)
        modalWindow.closebut = closebut;
    else
        modalWindow.closebut = true;
            
    var contents = source;
    if (!modalWindow.iframe)
        contents = $("#" + source).html();
        
    if (callback != null)
        modalWindow.callback = callback;
    
    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.closebut = true;
    modalWindow.open('<div id="loadingWindow"><img src="/images/loading-circle-large.gif" /> Loading...</div>');    
};

