/* DSLightBox
 * @author Simon Pollard for Future
 * @date March 2011
 * @version 1.1
 */

/*
 *  Initial start up function
 */
$(document).ready(function() {
    $('.promote').attr("href", "javascript:promote();")
});

/*
 * Promote - lightbox style pop-up
 */
function promote()
{
    // Add cover and cover-content divs to the dom
    $("body").append("<div id='cover'></div><div id='cover-content'></div>");

    // Set the cover div to be transparent at first
    $("#cover").css("filter","alpha(opacity=0)");
     // Work out the height of the entire page
    doc_height =  (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
    // Now set the cover to be that height (so it works in IE)
    $("#cover").css("height",doc_height);

    // Find out the browser window height
    if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      winH = window.innerHeight;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      winH = document.body.offsetHeight;
     }
    }

    height = winH-100;
    width = 900;

    // Now fade in the cover
    $("#cover").animate({opacity: 0.92,duration: 200}, function(){
        // Set the dimensions of the content box to match the image
        $("#cover-content").css("height",+height);
        $("#cover-content").css("margin-top",+-(height/2));
        $("#cover-content").css("width",+width);
        $("#cover-content").css("margin-left",+-(width/2));
        // Check if we are showing an image or video
        $("#cover-content").load('includes/promote.php');
        // Now fade in the content box
        $('#cover-content').fadeIn('1000');
    });

    // Add a click function to the cover (so it hides when clicked)
    $("#cover").click(function () {
        hideBox();
    });
}

/*
 * Hide Box
 */
function hideBox()
{
    // Fade out cover-content div and remove from dom
    $("#cover-content").fadeOut("500", function(){
        $('#cover-content').remove();
    });

    // Fade out cover div and remove from dom
    $("#cover").fadeOut("500", function(){
        $('#cover').remove();
    });
}

/*
 * Window resize handler
 */
$(window).resize(function() {

    // Check to see if the pop-up is viewable
    if($("#cover-content").length != 0) {

        // Find out the new browser window height
        if (parseInt(navigator.appVersion)>3) {
         if (navigator.appName=="Netscape") {
          winH = window.innerHeight;
         }
         if (navigator.appName.indexOf("Microsoft")!=-1) {
          winH = document.body.offsetHeight;
         }
        }

        height = winH-100;

        // Change the height of our pop-up accordingly
        $("#cover-content").css("height",+height);
        $("#cover-content").css("margin-top",+-(height/2));
    }
});

