﻿var modalWindow = {
	width:Number,
	height:Number,
	opacity:Number,
	modal:Boolean = true,
	iframe:Boolean = true,
	callback:function()
	{
	    
	},
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").fadeOut('slow');
		$(".modal-overlay").remove();
	},
	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;\" />";
		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: 101, display: "none", opacity: this.opacity });
		if (!this.modal)
        {
		    $("#modalPop").append("<a class=\"close-window\" style=\"display: none;\"></a>");
		    $(".close-window").click(function(){modalWindow.close();});
		}
		
		if (this.iframe)
		    contents = "<iframe 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 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" });
		                }
		            );
    		        
		        }
		    );
		else
		    $(".modal-overlay").fadeIn('slow',
		        function(){
		            $("#modalPop").append(source);
                    $(".close-window").css({ zIndex: 103, display: "block" });
		            modalWindow.callback();
		        }
		    );
		    
	}
};

var lightbox = function(source, width, height, modal, iframe, callback)   
{   
    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;
        
    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.open('<div id="loadingWindow"><img src="/images/icons/loading/loading-circle-large.gif" /> Loading...</div>');    
};

