/*  lightbox 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

(function($){
		 
    $.fn.lightbox = function(options){
        /*
            set lightbox variables
        */
        var o = $.extend({}, $.fn.lightbox.defaults, options);
        var dialogContent, dialogBox, dialogVeil;
		var _popup = -1;
		var reRender = -1;
        /*
            create lightbox elements
        */
        var createVeil = function(){
			if($('#veil').length == 0) $('body').append('<div id="veil"></div>');
            var veil = $('#veil');
            return veil;
        };
        var createBox = function(){
            var box = document.createElement('div');
            $(box).attr('id', dialogContent.id + 'Box').addClass('wx-jq-light-box').css({width:o.dialogWidth + 'px'});
            return box;
        };
        var createHeader = function(){
			var header = '';
			if(o.topPane == true)
			{
				header =  '<div class="wx-jq-top-bar"><span class="wx-jq-title">'+o.dialogTitle+'</span><a href="#" class="wx-jq-close close-top-btn"></a></div>';
			}
			else
			{
				header = '<a href="#" class="wx-jq-close close-btn"></a>';
			}
			return header;
        };
		var createFooter = function(){
			var footer = "";
			if(o.bottomPane == true)
			{
				$(dialogContent).css('background','url("/includes/images/lightbox/lower-gradient.jpg") repeat-x scroll left bottom transparent')
			}
			return footer;
		}
		
        var createDialog = function(){
            var dialog = createBox();
            var header = createHeader();
			if(o.bottomPane)
			{
				createFooter();
			}
            $(dialog).append(header).append(dialogContent);
			if(o.bgColor)
			{
				$(dialog).css('background-color', o.bgColor);
			}			
			$(dialogContent).addClass('wx-jq-content');
			if($(dialogContent).find('.wx-jq-video').length)
			{
				$(dialogContent).addClass('wx-jq-video-css');
			}
			bindClick( $('.wx-jq-close'));
            return dialog;
        };
        /*
            hide and show select boxes to 
			overcome z-index problem. 
        */		
		var hideSelects = function(){
			$("select").hide();
		};
		var showSelects = function(){
			$("select").show();
		};		
        /*
            get viewport calculations
        */
        var getViewportSize = function(){
            return {
                width: $(window).width(),
                height: $(window).height()
            };
        };      
        var getDialogOffset = function(){
            return {
                width: $(dialogBox).outerWidth(),
                height: $(dialogBox).outerHeight()
            };
        };
        /*
            adjust dialog box to viewport
            by adding a negative margin
        */
        var adjustToViewport = function(){
            var viewportSize = getViewportSize();
            var dialogOffset = getDialogOffset();
			if(dialogBox != -1)
			{
				$(dialogBox).css({left:(viewportSize.width - dialogOffset.width)/2});	
				//if(dialogOffset.height > viewportSize.height)
				/*******Extra space for the button*****/
				if(dialogOffset.height + 20 > viewportSize.height)
				{
					if(reRender<0)
					{
						//$(dialogBox).css({top: $(window).scrollTop() + (viewportSize.height - dialogOffset.height)/2 + 70, position:'absolute'});
						$(dialogBox).css({top: $(window).scrollTop() + 55, position:'absolute'});
						reRender = 0;
					}
				}
				else
				{
					/*
						adjustment for IE
					*/
					if($.browser.msie && $.browser.version.substr(0,1)<7)
					{
						$(dialogBox).css({top: $(window).scrollTop() + (viewportSize.height - dialogOffset.height)/2});
					}
					else
					{
						$(dialogBox).css({top: (viewportSize.height - dialogOffset.height)/2, position:'fixed' });
					}
					reRender = -1;
				}
				
			}
			$('#veil').css('height', $(window).scrollTop() + viewportSize.height);
            return;
        };

        /*
            reveal dialog box and veil
            if dialog box already exists, then do not recreate it
            if a dialog is being toggled, then do not create veil
        */
        var renderDialogBox = function(){
            dialogBox = document.getElementById(dialogContent.id + 'Box');
            if( !dialogBox ){
                dialogBox = createDialog();
                $('body').append(dialogBox);
            }
            $(dialogBox).show();
            $(dialogContent).show();
            return;
        };
        var renderVeil = function(){
            if( !o.toggle ){
                dialogVeil = createVeil();
                $('body').append(dialogVeil);
                $(dialogVeil).fadeIn('fast');
            }
            return;
        };
        /*
            conceal dialog box and veil
        */
        var hideDialogBox = function() {
            var dialog = $(dialogContent).parent().not(':hidden');
            $(dialog).hide();
            return;
        };
        var removeVeil = function() {
            var veil = $('#veil').not(':hidden');
            $(veil).remove();
            return;
        };
        /*
            add "close" lightbox events
        */
        var bindClick = function($cta) {
            $cta.unbind('click.closeDialog').bind('click.closeDialog', function(ev){
                closeLightbox();
                return false;
            });
            return;
        };
        var addClickEvents = function(){
            bindClick( $(dialogVeil) );
            bindClick( $(dialogBox).find('.wx-jq-close') );
			$(document).keydown(function(e){
				if(!e)evt = window.event;
				if(e.keyCode == 27 && _popup != -1){
					closeLightbox();
				}
			});

            return;
        };
        /*
            open lightbox
        */
        var openLightbox = function(){
            renderVeil();
            renderDialogBox();
            adjustToViewport();
            addClickEvents();
			hideSelects();
            if(o.dialogOpen){
                o.dialogOpen();
            }
			_popup = 1;
            return;
        };
        /*
            close lightbox
        */
        var closeLightbox = function(){
			// for videos auto playing bug in iframes....
			$('iframe').each(function (){
			$(this).attr('src','');
			});
            hideDialogBox();
			showSelects();
            removeVeil();
            if(o.dialogClose){
                o.dialogClose();
            }
			if($.browser.msie)
			{
				$(dialogContent).find('.wx-jq-video').each(function()
					{
						var clone = $(this).children().clone(true);
						$(this).children().remove();
						$(this).append(clone);													
					});
			}
			_popup = -1;
            return;
        };
        /*
            initialize lightbox
        */
        var init = function(element){
            dialogContent = element;
            if( o.toggle ){
                hideDialogBox();
            }
            if( o.action === 'open' ){
                openLightbox();
            }
            else{
                closeLightbox();
            }
			if(dialogBox != -1)
			{
				$(window).bind('scroll', adjustToViewport);
				$(window).resize(adjustToViewport);
			}
			
            return;
        };
        return this.each(function(index, element){
            init(element);
        });
    };
    /*
        lightbox defaults
    */
    $.fn.lightbox.defaults = {
        action: 'open',
        dialogTitle: '',
		bgColor: '',
		topPane: false,
		bottomPane:false,
        dialogWidth: 410,
        toggle: null
    };
})(jQuery);

