/************************************** Splendid *************************************
* Created By:		Steve Doggett
* Creation Date:	26th January 2009
* Edited ----------------------------------------------------------------------------
*       By:              On:  
* Description -----------------------------------------------------------------------
*       This file gives the lightbox fade and basic functionality
*       Uses the JQuery plugin.
*
*       Functions:
*           openLightbox()                  // Main function to trigger displaying the lightbox
*           closeLightbox()                 // Function to close the lightbox popup
*           removeLightbox()                // Remove the lightbox elements so new one can be put in when needed.
*           myFade()                        // Does the background fade to background colour with opacity for the lightbox to sit on
*           positionLightbox()              // Puts the lightbox in the middle of the current browser window
*          
**************************************************************************************/

/********************************** Global Variables *********************************/
var popupID = "apPopup";
var pageFadeID = "pageFade";
var fadeSpeed = 1000;

/********************************* Page Fade Functions *******************************/

function openLightbox(which)
{
	myFade();
	removeLightbox();
	$('body').append('<div id="'+popupID+'"></div>');
	
    // Go get the popup, set it up and fade it in
    // Can add a parameter to this to remove the html header and stuff...
    $('#'+popupID).load(which.href, function(popup) {
        // Position the popup in the centre of the browser window and fade it in
        $('#'+popupID).css({
			display: 'none',
            position: "absolute",
            zIndex: 500
        });
		
		positionLightbox();
		
		$('#'+popupID).fadeIn(fadeSpeed);
    });
}
/****
* Function to close the lightbox popup
****/
function closeLightbox()
{
    // Fade out the page div so page is visible as noraml again.
    $(".nockBack").fadeOut(fadeSpeed, function()
    {
        $(".nockBack").css({
            height: "0px"
        }).unbind("click");
        $('.nockBack').remove();

        // This function is in main-navigation.js and it shows dropdowns, checkboxes and radio buttons in ie6.
        checkForDodgyIE6Elements("visible");

		$(window).unbind("resize");
    });

    removeLightbox();
}

/****
* Remove the lightbox elements so new one can be put in when needed.
****/
function removeLightbox()
{
    $('#'+popupID).fadeOut(fadeSpeed, function()
    {
        $('#'+popupID).remove();
    });
}

/****
* Does the background fade to transparent black for the popup to sit on
****/
function myFade()
{
    var pageHeight = $(document).height();
	$('body').append('<div class="nockBack" style="opacity:0;"></div>');

	
	$('.nockBack').css({
	   'height': $(document).height(),
	   'z-index': '5',
	   'width': $(window).width()
	}).animate({
	   'opacity': 0.7
	}, fadeSpeed);

    // This function is in main-navigation.js and it hides dropdowns, checkboxes and radio buttons in ie6.
	checkForDodgyIE6Elements("hidden");

    // When the fade is clicked, remove it again
    $(".nockBack").click(function()
    {
        closeLightbox();
    });
	
	
	$(window).resize(function(){
	  try {
	  	$('.nockBack').css({
			'height': $(document).height(),
			'width': $(window).width()
		});
	  	positionLightbox();
	  } catch(e){}
	});	
}
	
/****
 * Puts the lightbox in the middle of the current browser window	
 ****/
function positionLightbox()
{
	var topOffset = getTopOffset();

	if ($('#' + popupID).height() >= $(window).height()) {
	    topOffset = topOffset + 20;
	} else {
	    topOffset = topOffset - 15;
	}
	
	// Position the popup in the centre of the browser window and fade it in
	$('#'+popupID).css({
		left: ($(window).width() / 2) - ($('#'+popupID).width() / 2) + 'px',
		top: ((($(window).height() / 2) + topOffset) - ($('#'+popupID).height() / 2)) + 'px'
	});
}
