/* 
 * jQuery Lightbox v1
 * by Brian Schneider
 * Thanks to Allan Siongco for the centering piece
 */

(function($){
    $.fn.lightbox = function(options)
    {
        var defaults = {
            modalBackground: "#000000",
			opacityBack: .5,
			lightboxWidth: 500,
			lightboxHeight: 500,
			fadeInSpeed: 500,
			fadeOutSpeed: 500,
            staticContent: null,
			ajaxFile: null,
            alternateBox: false,
            lightboxTop: null,
            lightboxLeft: null,
            lightboxRight: null,
            alternateWidth: null,
            alternateHeight: null
        };
        
        var options = $.extend(defaults, options);

        // Lightbox Container
        var popup =$('#lightboxContainer');


        return this.each(function()
        {
           //Create Lightbox container to hold content and modal background
           createLightboxContainer();
           createModalBackground();

           if(options.alternateBox !=false)
           {
               createAlternateLightbox();
           }

           //Center lightbox on load of page
           $(document).ready(function(){
               center();
               modalWidthHeight();
           })

           //Center lightbox on resize and scroll
           $(window).resize(center);
           $(window).scroll(center);
           
           //Reize Window
           $(window).resize(modalWidthHeight);
           $(window).scroll(modalWidthHeight);

           //Item that initiates lightbox call
			obj = $(this);

            //event that shows the lightbox
            obj.click(function(){ 
               showModal();
                var imageTop = $(this).offset().top;
                //Get width of object
                var imageWidth = $(this).offset().left;
                imageWidth = imageWidth - 80;
                imageTop = imageTop - 10;
                $('#alternateLightbox').css({'top':imageTop, 'left':imageWidth});

               if(options.ajaxFile !=null)
               {
                    insertAjax();
               }
               if(options.staticContent !=null)
               {
                    insertStatic();
               }
                   
			    //event that close's Lightbox
			    $('.close, .cancel, .noThanks').click(function(){
				    hideModal();
				    return false;
			    });
			    return false;               
            });
			

            
            $('#modalBackground').click(function(){
                hideModal();
            });
        });
		
		//Create lightbox container
		function createLightboxContainer()
		{
            if(!$('#lightboxContainer').length)
            {
                var lightbox = document.createElement('div');
                lightbox.setAttribute('id', 'lightboxContainer');
                var lightboxContent = document.createElement('div');
                lightboxContent.setAttribute('id', 'lightboxContent');
                lightbox.appendChild(lightboxContent);
                $(lightbox).css({'display': 'none', 'width':options.lightboxWidth + 'px', 'height':options.lightboxHeight + 'px'});
                $(".mainForm")[0].appendChild(lightbox);
                //document.body.appendChild(lightbox);
            }
        }

        //Creates an alternate box
		function createAlternateLightbox()
		{
            if(!$('#alternateLightbox').length)
            {
                var alternateLightbox = document.createElement('div');
                alternateLightbox.setAttribute('id', 'alternateLightbox');
                var alternateLightboxContent = document.createElement('div');
                alternateLightboxContent.setAttribute('id', 'alternateLightboxContent');
                alternateLightbox.appendChild(alternateLightboxContent);
                
                $(alternateLightbox).css({
                    'display': 'none', 
                    'width':options.alternateWidth + 'px', 
                    'height':options.alternateHeight + 'px'
                });
                
                document.body.appendChild(alternateLightbox);
            }
        }
		
        //Create Lightbox background
		function createModalBackground()
        {
            if(!$('#modalBackground').length)
            {
                var modalBack = document.createElement('div');
                var modalId = "modalBackground";
                modalBack.setAttribute("id", modalId);
                document.body.appendChild(modalBack);
                $(modalBack).css({"display":"none", "background-color": options.modalBackground, "opacity":options.opacityBack, "z-index": "50"});
            }
        }
        
        
        function modalWidthHeight()
        {
            var windowWidth = $(window).width();
            var windowHeight = $('body').height();

            $('#modalBackground').css({"width":windowWidth, "height":windowHeight});
        }
		
		//Show lightbox background
		function showModal()
		{
            $('#modalBackground').fadeIn(options.fadeInSpeed);
            $('select').css('visibility', 'hidden');
			if(options.alternateBox == false)
            {
                $("#lightboxContainer").slideDown(500, function(){$('#lightboxContent').fadeIn(500);});
            }
            else
            {
                $("#alternateLightbox").slideDown(500);
                $("#alternateLightboxContent").show();
            }
		}
		
		//Hide lightbox background
		function hideModal()
		{
            $('#modalBackground').fadeOut(options.fadeOutSpeed, function(){ $('select').css('visibility', 'visible');});
            $('#lightboxContent').fadeOut(500, function(){$('#lightboxContent').html("")});
            $("#lightboxContainer").slideUp(300);
            $('#alternateLightboxContent').fadeOut(500, function(){$('#alternateLightboxContent').html("")});
            $("#alternateLightbox").slideUp(300);  
		}

        //Insert ajax content
		function insertAjax()
		{
			if(options.alternateBox == false)
            {
                $.ajax(
                {
                    url: options.ajaxFile,
                    cache: false,
                    success: function(html){
                        $('#lightboxContent').html(html);
                    }
                });
            }
            else
            {
                $.ajax(
                {
                    url: options.ajaxFile,
                    cache: false,
                    success: function(html){
                        $('#alternateLightboxContent').html(html);
                    }
                });
            }
		}

        //Insert Static Content
        function insertStatic()
        {
            var staticC = $(options.staticContent);
            if(options.alternateBox == false)
            {
                $("#lightboxContent").html(staticC.html());                
            }
            else
            {
                $("#alternateLightboxContent").html(staticC.html());                
            }

        }

        //Center the lightbox container to the window
        function center(){
            var popup = $('#lightboxContainer');
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();
            var scrollLeft = $(window).scrollLeft();
            var scrollTop = $(window).scrollTop();
            var popupWidth = popup.width();
            var popupHeight = popup.height();
            var leftPos = (windowWidth - popupWidth) / 2 + scrollLeft;
            var topPos = (windowHeight - popupHeight) / 2 + scrollTop;
            popup.css({
                "left" : leftPos,
                "top" : topPos
            });
        }
    };
})(jQuery);