/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(initializeDocument());

function initializeDocument()
{
    $("a.externalLink").click(function(e){e.preventDefault();handleExternalLinks(this);});
    $("#disclaimerAgree").click(function(e){e.preventDefault();confirmDisclaimer();})
    $("#disclaimerLeave").click(function(e){e.preventDefault();leaveSite();})
    $("a#disclaimerTag").click(function(e){e.preventDefault();showDisclaimer();});
}

function handleExternalLinks(src)
{
    var w = window.open(src.href, "newWin");
    w.focus();
}

function showDisclaimer()
{
    $("#disclaimer").dialog({
        dialogClass: "disclaimerClass",
        title: "Disclaimer",
        closeText: "",
        resizable: true,
        height: 350,
        width: 300,
        maxHeight: 400,
        maxWidth: 400,
        modal: true
    });
}

function confirmDisclaimer()
{
    $("#disclaimer").dialog("destroy");
    $.ajax({
       url: "disclaimerFunctions.php",
       data: "confirm=1",
       type: "GET",
       error: function(jqXHR, textStatus, errorThrown){handleError(jqXHR, textStatus, errorThrown);}
 });
}

function leaveSite()
{
    $("#disclaimer").dialog("destroy");
    $.ajax({
       url: "disclaimerFunctions.php",
       data: "confirm=0",
       type: "GET",
       success: function(){top.location.href = "http://www.google.com/";},
       error: function(jqXHR, textStatus, errorThrown){handleError(jqXHR, textStatus, errorThrown);}
    });
}

function handleError(jq, ts, et)
{
    alert(ts + "\n" + jq + "\n" + et);
}

function countDown(goalTime, ctlPrefix)
{
    var today = new Date();
    var gt = Date.parse(goalTime);
    var diff = gt - today;
    var days = 0;
    var hours = 0;
    var mins = 0;
    var secs = 0;

    var secDiv = 1000;
    var minDiv = 60 * secDiv;
    var hourDiv = 60 * minDiv;
    var dayDiv = 24 * hourDiv;

    if( diff > dayDiv ) {
        days = Math.floor(diff / dayDiv);
        diff -= (days * dayDiv);
    }

    if( diff > hourDiv ) {
        hours = Math.floor(diff / hourDiv);
        diff -= ( hours * hourDiv );
    }

    if( diff > minDiv ) {
        mins = Math.floor(diff / minDiv);
        diff -= ( mins * minDiv );
    }

    if( diff > secDiv ) {
        secs = Math.floor(diff /  secDiv);
    }

    var dayTxt = "" + days;
    var hrTxt = "" + hours;
    var minTxt = "" + mins;
    var secTxt = "" + secs;

    if(dayTxt.length < 2) dayTxt = "0" + dayTxt;
    if(hrTxt.length < 2) hrTxt = "0" + hrTxt;
    if(minTxt.length < 2) minTxt = "0" + minTxt;
    if(secTxt.length < 2) secTxt = "0" + secTxt;

    $("#" + ctlPrefix + "CountdownDays").html(dayTxt);
    $("#" + ctlPrefix + "CountdownHours").html(hrTxt);
    $("#" + ctlPrefix + "CountdownMinutes").html(minTxt);
    $("#" + ctlPrefix + "CountdownSeconds").html(secTxt);
}
