﻿/************************************** Splendid *************************************
* Creation Date:    4th September 2008
* Created By:       Steve
* Edited By:	    Phil
* On:
* Description -----------------------------------------------------------------------
*       This file contains the functions for opening/closing a lightbox popup
*       Requires jQuery to be included...
*
*      Slideshow Object has the following method:
*          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
*		   showProject()				Move to the next/previous project without removing the fadeOut.
**************************************************************************************/

if (document.images) {
    preload_image = new Image(64, 21);
    preload_image.src = "img/lightbox/close_over.gif";
    preload_image = new Image(6, 12);
    preload_image.src = "img/lightbox/arrowLeft_over.gif";
    preload_image = new Image(6, 12);
    preload_image.src = "img/lightbox/arrowRight_over.gif";
}

// Just adds in a div to create the fade effect with when the page loads.
/*
$(document).ready(function() {
    $('body').append('<div id="pageFade"></div>');
});
*/
/****
* Function to do the lightbox popup open
****/
function openPopup(which, id) {

    var flash = 'silverlight';
    if (!CheckSilverlightInstalled()) flash = 'flash';

    myFade();
    
    // 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, { vidtype: flash, id: id }, function(popup) {
        if ($('.lbPopup').length > 0) {
            $('.lbPopup').remove();
        }
        $('body').append(popup);
        $('.lbPopup').css({
            visibility: 'hidden'
        });

        var myTop = $(window).scrollTop() + 50;
        $('.lbPopup').css({
            position: 'absolute',
            left: $('body').width() / 2 - ($('.lbPopup').width() / 2) + 'px',
            top: myTop.toString() + 'px' // $(which).scrollTop() - $('.lbPopup').height()
        }).slideDown('slow', function() {
            $('.lbPopup').css({
                visibility: 'visible'
            });
            $('.lbFooter').css({
                marginTop: $('.slideshowImages li:first').height()
            });
        }); //.fadeIn(1000);
    });
}

/****
* Function to close the lightbox popup
****/
function closePopup() {
    $('#pageFade').fadeOut(500, function() {
        $('#pageFade').css({
            height: '0px'
        }).unbind('click', closePopup);
    });
    $('.lbPopup').fadeOut(500, function() {
        $('.lbPopup').remove();
    });
}

/****
* Does the background fade to white for the popup to sit on
****/
function myFade() {
    var pageHeight = $(document).height();
    pageHeight += 10;

    // For the different browsers
    $('#pageFade').css({
        filter: 'alpha(opacity=0)',
        opacity: 0,
        MozOpacity: 0,
        height: pageHeight + 'px',
        display: 'block'
    }).animate({
        opacity: 0.85
    }, 500);

    // When the fade is clicked, remove it again
    $('#pageFade').bind('click', closePopup);
}

/****
 * Move to the next/previous project without removing the fadeOut.
 ****/
function showProject(url, id) {

    var flash = 'silverlight';
    if (!CheckSilverlightInstalled()) flash = 'flash';

    myFade();
    
    // 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(url, { vidtype: flash, id: id }, function(popup) {
        if ($('.lbPopup').length > 0) {
            $('.lbPopup').remove();
        }
        $('body').append($(popup));
        $('.lbPopup').css({
            visibility: 'hidden'
        });

        var myTop = $(window).scrollTop() + 50;
        $('.lbPopup').css({
            position: 'absolute',
            left: $('body').width() / 2 - ($('.lbPopup').width() / 2) + 'px',
            top: myTop.toString() + 'px', // $(which).scrollTop() - $('.lbPopup').height()
            visibility: 'visible',
            display: 'block'
        });
        try { Slideshow.currImage = 0; }
        catch (e) { }
    });
}

function quickURLEncode(str) {
    while (str.indexOf(' ') > -1) str = str.replace(' ', '%20');
    while (str.indexOf('/') > -1) str = str.replace('/', '%2F');
    return str;
}

function buildLbImageList(listDivID) {
    if ($(listDivID).children().length > 0) {
        $(listDivID).children().each(function(idx) {
            //$(this).children(idx).attr('href', '/lbImages.aspx?imgThis=' + quickURLEncode($(this).children(idx).attr('href')));
            $(this).children(idx).bind(
                    'click',
                    function() {
                        try { Slideshow.currImage = 0; }
                        catch (e) { }
                        openPopup(this);
                        return false;
                    }
                );
            }
        );
    }
}

function CheckSilverlightInstalled() {
    var isSilverlightInstalled = false;
    try {
        try {
            var slControl = new ActiveXObject('AgControl.AgControl')//IE
            isSilverlightInstalled = true;
        }
        catch (e) {
            if (navigator.plugins["Silverlight Plug-In"]) //FF&Safari
            {
                isSilverlightInstalled = true;
            }
        }
    }
    catch (e) { }
    return isSilverlightInstalled;
}

