﻿/************************************** 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 = '#popupLogin';
var topOffset = 0;
var fadeSpeed = 750;
var isLogin = false;

/****
* Function to do the lightbox popup open
****/
function openPopup(which) {
    myFade();
    isLogin = false;
    
    if (which.id == 'login') {
        isLogin = true;
    }
    
    // Go get the popup, set it up and fade it in
    // Can add a parameter to this to remove the html header and stuff...
    $.post(which.href, { js: 'true', title: document.title }, function(popup) {
        $(popupID).remove();
        $('body').append(popup);
        positionPopup();
    });
}

function positionPopup() {
//    if (isLogin) {
//        $(popupID).css({
//            top: '210px',
//            left: getPopupLeft(),
//            //left: '472px',
//            position: "absolute"
//        }).fadeIn(fadeSpeed);
//    } else {
        setTopOffset();
        $(popupID).css({
            left: getPopupLeft(),
            top: getPopupTop(),
            position: "absolute"
        }).fadeIn(fadeSpeed);

        $.log("just checking...");
}

/****
* Function to close the lightbox popup
****/
function closePopup() {
    $('#pageFade').fadeOut(fadeSpeed, function() {
        $('#pageFade').css({
            height: '0px'
        }).unbind('click');
    });
    $(window).unbind('resize');
    
    $(popupID).fadeOut(fadeSpeed, 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
    }, fadeSpeed);

    // When the fade is clicked, remove it again
    $('#pageFade').click(function(){
        closePopup();
    });

    $(window).resize(function() {
        positionPopup();
    });
}

/****
* 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;
    }
}

/****
 * Returns the left position of the popup
 ****/
function getPopupLeft() {
    return ( ($(window).width() / 2) - ($(popupID).width()/2) ) + 'px';
}

/****
 * Returns the top position of the popup
 ****/
function getPopupTop() {
    return ((($(window).height() / 2) + topOffset) - ($(popupID).height() / 2)) + 'px'
}