/**
 * DigitalWorks Lightbox for JQuery & JQuery UI
 **/

var callbackOnCloseLightbox = null;
var isLightboxOpened = false;
var currentElement = null;
/**
 * CenterElement
 * This function centers an element on the screen.
 *
 * Args:
 * element - An element to center on the screen.
 **/
function CenterElement(element)
{
    $(element).position({
      my: "center center",
      at: "center center",
      of: $(window)
  });
}

/**
 * OpenLightbox 
 * This function centers an element on the screen.
 *
 * Args:
 * title - The title for the lightbox
 * contents - The contents for the body of the lightbox
 * buttonText - null to hide the button or text to a button that closes the lightbox
 * callbackFunction - null or a callback to call on lightbox close.
 * reloadCallback - null or a callback to call on lightbox reload (hide and show)
 **/
 
function OpenLightbox(element, callbackFunction, reloadCallback)
{
	if(isLightboxOpened)
	{
		CloseLightbox();
		$("#ShadowLightbox").fadeTo(500,0.3);
		$(element).fadeTo(500,0, function(){
			ShowLightbox(element, callbackFunction);
			if(reloadCallback != null){
				reloadCallback();
			}
		});
	}
	else
	{
		ShowLightbox(element,callbackFunction);
	}
}

/**
 * CloseLightbox 
 * This function closes the lightbox
 **/
function CloseLightbox()
{
    //$(window).unbind();
    $("#ShadowLightbox").fadeTo(500,0, function(){
        $('#ShadowLightbox').hide();
    });
	if(currentElement != null)
	{
		$(currentElement).fadeTo(500,0, function(){
			$(currentElement).hide();
			currentElement = null;
			if(callbackOnCloseLightbox != null)
			{
				isLightboxOpened = false;
				callbackOnCloseLightbox();
			}
		});
	}
}




/**
 * YOU SHOULD NOT CALL THIS FUNCTION DIRECTLY - CALL OpenLightbox INSTEAD!
 
 * ShowLightbox 
 * This function shows the lightbox
 *
 * Args:
 * title - The title for the lightbox
 * contents - The contents for the body of the lightbox
 * buttonText - null to hide the button or text to a button that closes the lightbox
 * callbackFunction - null or a callback to call on lightbox close.
 **/
function ShowLightbox(element, callbackFunction)
{
    // Reset the lightbox
    callbackOnCloseLightbox = null;

    if(callbackFunction != null)
        callbackOnCloseLightbox = callbackFunction;

    $("#ShadowLightbox").fadeTo(500,0.6);
    $(element).fadeTo(0,1);
    CenterElement(element);
	
	isLightboxOpened = true;
	
	currentElement = element;
	
    $('#ShadowLightbox').css({height:$(document).height(),width:$(document).width()});

    $(window).bind('resize', function() {
        CenterElement(element);
		$('#ShadowLightbox').css({height:$(window).height(),width:$(window).width()});
		CenterElement($('#ShadowLightbox'));
    });
    $(window).bind('scroll', function() {
		if($(element).css("display") != "none")
	        CenterElement(element);
		CenterElement($('#ShadowLightbox'));
    });
}
