﻿var experimentEndDate = new Date(Date.UTC(2011, 9, 2, 23, 59, 59, 0));
var onearthPopupStatus = 0;

function checkOnEarthCount() {
    //$.cookies.del('onearth__20111002');
    var count = $.cookies.get('onearth__20111002');
    if (isNaN(count)||(count == null))
    {
        count = "1";
        var clientDate = new Date();
        var endDate = DateAdd("d", 1, clientDate);

        var newOptions = { expiresAt: endDate };

        $.cookies.set("onearth__20111002", count);
        
        if (clientDate < experimentEndDate) {
            loadOnEarthPopup();
        }
    }
}

function redirectUser()
{
    document.location.href = "http://onearthpractice.copperstrings.com";
}

function loadOnEarthPopup() 
{
    if (onearthPopupStatus == 0) 
    {
        $("#onearthbackground").css({
            "opacity": "0.7"
        });
        $("#onearthbackground").fadeIn("slow");
        $("#onearthbanner").fadeIn("slow");
        onearthPopupStatus = 1;
        centerOnEarthPopup();
    }
    
}

function disableOnEarthPopup() {
    if (onearthPopupStatus == 1) {
        $("#onearthbackground").fadeOut("slow");
        $("#onearthbanner").fadeOut("slow");
        onearthPopupStatus = 0;
    }
}

function centerOnEarthPopup() 
{
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = 582;//  $("#onearthbanner").height();
    var popupWidth = 450; //$("#onearthbanner").width();

    var LeftPosition = (windowWidth) ? (windowWidth - popupWidth) / 2 : 0;
    var TopPosition = (windowHeight) ? (windowHeight - popupHeight) / 2 : 0;

    $("#onearthbanner").css({
        "position": "absolute",
        "top": TopPosition,
        "left": LeftPosition
    });
    //only need force for IE6

    $("#onearthbackground").css({
        "height": windowHeight
    });
}

$(document).ready(function() 
{
    $(window).load(function() 
    {
        checkOnEarthCount();
    });

    $(window).scroll(function() 
    {
        centerOnEarthPopup();
    });

    $(window).resize(function() {
        centerOnEarthPopup();
    });

    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && onearthPopupStatus == 1) {
            disableOnEarthPopup();
        }
    });
});
