/************************************** 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:
*           closePopup()                    // Function to close the lightbox popup
*           removePopup()                   // Remove the popup elements so new one can be put in when needed.
*           myFade()                        // Does the background fade to white for the popup to sit on
*          
**************************************************************************************/

/********************************** Global Variables *********************************/
var popupID = "attendanceWin";
var pageFadeID = "pageFade";
var fadeSpeed = 1000;
var topOffset = 0;

/********************************* Page Fade Functions *******************************/

function openPopup(which, isContainerId)
{
    //console.log('entered openPopup');
    
	myFade();
	removePopup();
	$('body').append('<div id="'+popupID+'" class="popup"></div>');
	
    if(isContainerId){
	    $('#' + popupID).append($(which).html()).css({
	        display: 'none',
	        position: "absolute"
	    }) 
    	
	    positionPopup();
    
    }
    else{
        // 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"
            });
    		
		    positionPopup();
    		
		    $('#'+popupID).fadeIn(fadeSpeed);
        });
    }
	
	$('#' + popupID).fadeIn(fadeSpeed);
}


/****
* Function to close the lightbox popup
****/
function closePopup()
{
    // Fade out the page div so page is visible as noraml again.
    $(".nockBack").fadeOut(fadeSpeed, function()
    {
        $(".nockBack").css({
            height: "0px"
        }).unbind("click");
        $('.nockBack').remove();
    
        $("#pageWrap select").show();
        $("#pageWrap input:checkbox").show();
        $("#pageWrap input:radio").show();  

		$(window).unbind("resize");
    });

    removePopup();
}

/****
* Remove the popup elements so new one can be put in when needed.
****/
function removePopup()
{
    $('#'+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);

	if($.browser.msie)
    {
        if($.browser.version.substr(0,1) == "6")
        {
            $("#pageWrap select").hide();
            $("#pageWrap input:checkbox").hide();
            $("#pageWrap input:radio").hide();
        }
    }

    // When the fade is clicked, remove it again
    $(".nockBack").click(function()
    {
        closePopup();
    });
	
	
	$(window).resize(function(){
	  try {
	  	$('.nockBack').css({
			'height': $(document).height(),
			'width': $(window).width()
		});
	  	positionPopup();
	  } catch(e){}
	});	
}
	
function positionPopup()
{
	setTopOffset();

	if ($('#' + popupID).height() >= $(window).height()) {
	    $('#' + popupID).css({
	        left: ($(window).width() / 2) - 206 + 'px',
	        top: '30px'
	    });
	} else {
	    topOffset = topOffset - 15;
	    $('#' + popupID).css({
	        left: ($(window).width() / 2) - 206 + 'px',
	        top: ((($(window).height() / 2) + topOffset) - ($('#' + popupID).height() / 2)) + 'px'
	    });
	}
	
}

/****
* Set the top offset of the page. This is to know how far the page has been scrolled by.
****/
function setTopOffset() {

        if (document.all) {
            if (!document.documentElement.scrollTop)
                topOffset = parseInt(document.body.scrollTop);
            else
                topOffset = parseInt(document.documentElement.scrollTop);
        } else {
            topOffset = parseInt(window.pageYOffset);
        }
}
