﻿$(document).ready(function($) {
     if ($(".popup").length > 0) {
        SetupPopups();
     }
});

function windimension() {

    var windowDimensionArray = new Array();

    if (window.innerHeight !== undefined) {
        windowDimensionArray[0] = window.innerWidth
        windowDimensionArray[1] = window.innerHeight
    }
    else { // IE varieties
        var D = (document.body.clientWidth) ? document.body : document.documentElement;
        windowDimensionArray[0] = D.clientWidth;
        windowDimensionArray[1] = D.clientHeight;
    }
    return windowDimensionArray;
}

function SetupPopups(rel) {
    
    var popupClone = $("div.popup");
    $('<div id="popupOverlay"/>').prependTo($("body"));
    
    //popupClone.appendTo($("body"));
    
    for (var ii = 0; ii < popupClone.length; ii++) {
        var clone = $(popupClone[ii]);
        clone.wrap('<div class="popupContainer"/>');        
    }

    $("a.popup").click(function() {

        // Setup the overlay
        // var height = $(document).height();
        // var width = $(document).width();

        var diemnsionArray = windimension();

        var width = diemnsionArray[0];
        var height = diemnsionArray[1];

        $("#homeIntro #courseMap,#homeIntro .transparentPanel").css("opacity", 0.6);

        $('#popupOverlay').css({
            opacity: 0.6,
            height: height,
            width: width,
            display: "block"
        })

        if (navigator.appVersion.indexOf("MSIE 6") > -1) {
            //IF IE6
            var margin = (width - 555) / 2;
            margin = margin + "px";
            //alert(margin);

            $('.popupContainer').css({
                margin: "0px " + margin + " 0px " + margin + "!important"
            })
        }
        
        var popupId = "#" + $(this).attr("rel");
        return ShowPopup(popupId);

    });

    SetupPopupClose();
}

function ShowPopup(popupId) {
    
    var popup = $(popupId).show(); // The main container
    var height = $(popupId + " .popupContent").height(); // The contents width (Set through CSS)
    var width = $(popupId + " .popupContent").width() + 5; // The contents width (Set through CSS) + 10 is there to generate space for the shadow

    // Animate the container to be the width first then height of the content inside
    popup.animate({ width: width, height: height }, 1200, function() {
    $(popupId + " .popupContent").fadeIn("fast");
    thisMovie('videoPlayer').playPauseVideo();
    });

    return false;
}

function SetupPopupClose() {
    $(".close").click(function() {   
        thisMovie('videoPlayer').stopVideo();
        $("div.popupContent").fadeOut("slow", function() {
        $("div.popup").animate({ height: 0, width: 0 }, 1200, function() { $("#popupOverlay").hide(); $("#homeIntro #courseMap,#homeIntro .transparentPanel").css("opacity", 1); });
        });
        return false;
    });
}

// Will take a fieldset and aslong as it has focus it submits the correct form
function FireDefaultButton(event, target) {

    //event.srcElement doesn't work in FF so we check whether
    //it or event.target exists, using whichever is returned
    var element = event.target || event.srcElement;

    if (event.keyCode == 13 &&
        !(element &&
        element.tagName.toLowerCase() == "textarea")) {
        var defaultButton = document.getElementById(target);

        if (defaultButton && typeof defaultButton.click != "undefined") {
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation) {
                event.stopPropagation();
            }
            return false;
        }
    }    
    return true;
}
