﻿var flashFinalText = true;
var fadeDuration = 1000;
var waitDuration = 10000;

function updateCountdown(totalSeconds) 
{
    var time = totalSeconds; 

    var seconds = time % 60; 
    time = Math.floor(time / 60);

    var minutes = time % 60; 
    time = Math.floor(time / 60);

    var hours = time % 24; 
    time = Math.floor(time / 24);
    
    var days = 0;
    var weeks = 0;
    
    if (showWeeks)
    {
        days = time % 7;
        time = Math.floor(time / 7);
        weeks = time;

        $("#countdownWeeks").text(weeks);
    }
    else
    {
        days = time;
        $("#countdownWeeksContainer").hide();
    }
    
    
    $("#countdownDays").text(days); 
    $("#countdownHours").text(hours); 
    $("#countdownMinutes").text(minutes); 
    $("#countdownSeconds").text(seconds); 
    
    if (weeks == 0)
    {
        $("#countdownWeeksContainer").addClass("yellow");

        if (days == 0)
        {
            $("#countdownDaysContainer").addClass("yellow");
            
            if (hours == 0)
            {
                $("#countdownHoursContainer").addClass("yellow");
                
                if (minutes == 0) 
                {
                    $("#countdownMinutesContainer").addClass("yellow");
                }
            }
        }
    }

    $('#countdownTimer').show();
}


function timerCallback() 
{
    var now = new Date(); 
    var difference = Math.floor((countdownTargetDate - now) / 1000); 

    if (difference > 0)
    {
        updateCountdown(difference);
    
        window.setTimeout(timerCallback, 1000);
    }
    else
    {
        $('#countdownTimer').hide();
        $('#countdownComplete').show();
        
        if (flashFinalText)
        {
            flashCallback();
        }
    }
}

$(document).ready(function() 
{
    timerCallback();
});

var flashState = 0;

function flashCallback()
{
    switch (flashState)
    {   
        case 0:
        case 2:
            $('#countdownComplete').animate ( { opacity: 0.25 }, fadeDuration);
            flashState++;
            window.setTimeout(flashCallback, fadeDuration);
            break;
            
        case 1:
        case 3:
            $('#countdownComplete').animate ( { opacity: 1 }, fadeDuration);
            window.setTimeout(flashCallback, fadeDuration);
            flashState++;
            break;
            
        case 4:
            flashState = 0;
            window.setTimeout(flashCallback, waitDuration);
            break;
            
    }
}