﻿/************************************** Splendid *************************************
* Creation Date:    4th September 2008
* Created By:       Steve
* Edited ----------------------------------------------------------------------------
*		By:	    On:
* Description -----------------------------------------------------------------------
*       This file contains the functions for opening/closing a lightbox popup
*       Requires jQuery to be included...
*
*      
*       openPopup()                 Function to do the lightbox popup open
*       closePopup()                Function to close the lightbox popup
*       myFade()                    Does the background fade to white for the popup to sit on
*       setTopOffset()              Set the top offset of the page. This is to know how far the page has been scrolled by.
**************************************************************************************/

var popupID = '#popup';
var topOffset = 0;

/****
* Function to do the lightbox popup open
****/
function openPopup(which) {
    myFade();
    
    if ($(popupID).length > 0) {
        // If it's already loaded, just fade it back in again...
        $(popupID).fadeIn(1000);
    } else {
        setTopOffset();
    
        // Go get the popup, set it up and fade it in
        // Can add a parameter to this to remove the html header and stuff...
        $.get(which.href, { js: 'true', title: document.title }, function(popup) {
            $('body').append($(popup));
            $(popupID).css({
                left: $(window).width() / 2 - ($(popupID).width() / 2) + 'px',
                top: ((($(window).height() / 2) + topOffset) - ($(popupID).height() / 2)) + 'px',
                position: "absolute"
            }).fadeIn(1000);
        });
    }
}


/****
* Function to close the lightbox popup
****/
function closePopup() {
    $('#pageFade').fadeOut(1000, function() {
        $('#pageFade').css({
            height: '0px'
        }).unbind('click', closePopup);
    });
    $(popupID).fadeOut(1000, function() {
        $(this).remove();
    });
}

/****
* Does the background fade to white for the popup to sit on
****/
function myFade() {
    var pageHeight = $(document).height();

    // For the different browsers
    $('#pageFade').css({
        filter: 'alpha(opacity=0)',
        opacity: 0,
        MozOpacity: 0,
        height: pageHeight + 'px',
        display: 'block'
    }).animate({
        opacity: 0.8
    }, 1000);

    // When the fade is clicked, remove it again
    $('#pageFade').bind('click', closePopup);
}

/****
* 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 = document.body.scrollTop;
        else
            topOffset = document.documentElement.scrollTop;
    }
    else {
        topOffset = window.pageYOffset;
    }
}