// Countdown.js
// ------------
// Provides a countdown clock for various raffle milestones. Shows the countdown
// messages in the page element with ID 'countdown'. 

var m_targetDate;
var m_countdownPrefix;
var m_countdownSuffix;
var m_intervalId;


// InitializeCountdown(): Initializes the countdown timer. 

function InitializeCountdown() {
    
    // Set the appropriate target dates and messages
    // (note - in Date.UTC(), month values are 0-based)
    var countdownMessages = new Array();
    var message;
    
    // ... Early Bird 1
    message = new Object(); 
    message.TargetDate = new Date( Date.UTC( 2010, 1, 13, 1, 0, 0, 0 ) );
    message.Prefix = '<b>To be included in <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 1</a>:</b> <br /> ';
    message.Suffix = ' left';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 1, 24, 20, 0, 0, 0 ) );
    message.Prefix = '<b>Time left until <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 1</a>:</b> <br /> ';
    message.Suffix = '';
    countdownMessages.push( message );
    
    // ... Early Bird 2
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 2, 13, 1, 0, 0, 0 ) );
    message.Prefix = '<b>To be included in <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 2</a>:</b> <br /> ';
    message.Suffix = ' left';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 2, 24, 19, 0, 0, 0 ) );
    message.Prefix = '<b>Time left until <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 2</a>:</b> <br />';
    message.Suffix = '';
    countdownMessages.push( message );
    
    // ... Early Bird 3
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 3, 10, 0, 0, 0, 0 ) );
    message.Prefix = '<b>To be included in <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 3</a>:</b> <br /> ';
    message.Suffix = ' left';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 3, 21, 19, 0, 0, 0 ) );
    message.Prefix = '<b>Time left until <a href="EarlyBird.aspx"' + 
        ' onmousedown="StopCountdown()">Early Bird Drawing 3</a>:</b> <br /> ';
    message.Suffix = '';
    countdownMessages.push( message );
    
    // ... Grand Prize
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 4, 15, 0, 0, 0, 0 ) );
    message.Prefix = '<b>To be included in <a href="Prizes.aspx"' + 
        ' onmousedown="StopCountdown()">Grand Prize Drawing</a>:</b> <br /> ';
    message.Suffix = ' left';
    countdownMessages.push( message );
    
    // ... Raffle completed
    message = new Object();
    message.TargetDate = null;
    message.Prefix = '<b>Raffle complete.</b><br /> ';
    message.Suffix = 'Thanks to all who participated.';
    countdownMessages.push( message );
    
    // Load the message matching the current date
    var currentDate = new Date();
    for ( var i = 0; i < countdownMessages.length; i++ ) {
    
        if ( ( countdownMessages[ i ].TargetDate == null ) || 
            ( currentDate < countdownMessages[ i ].TargetDate ) ) {
            
            m_targetDate = countdownMessages[ i ].TargetDate;
            m_countdownPrefix = countdownMessages[ i ].Prefix;
            m_countdownSuffix = countdownMessages[ i ].Suffix;
            break;
        }
    }
    
    // Show the counter
    UpdateCountdown();

    // Update the counter every twentieth of a second
    m_intervalId = setInterval("UpdateCountdown()", 50);
}


// UpdateCountdown(): Updates the countdown text with the current time remaining. 

function UpdateCountdown() {

    // Locate the countdown text
    var countdownText = $( '#countdown' );
    
    // Skip calculation if all dates have passed
    if (m_targetDate == null) {
        countdownText.html( m_countdownPrefix + m_countdownSuffix );
        return;
    }
    
    // Set time constants
    var MS_PER_DAY = (1000 * 60 * 60 * 24);
    var MS_PER_HOUR = (1000 * 60 * 60);
    var MS_PER_MINUTE = (1000 * 60);
    var MS_PER_SEC = 1000;
    var MS_PER_HUNDREDTH = 10;
    
    // Calculate the time left - update message when date passes
    var currentDate = new Date();
    var remainder = m_targetDate - currentDate;
    
    if (remainder < 0) {
        InitializeCountdown();
    }
    
    var daysLeft = Math.floor(remainder / MS_PER_DAY);
    remainder = remainder % MS_PER_DAY;
    
    var hoursLeft = Math.floor(remainder / MS_PER_HOUR);
    remainder = remainder % MS_PER_HOUR;
    
    var minutesLeft = Math.floor(remainder / MS_PER_MINUTE);
    remainder = remainder % MS_PER_MINUTE;
    
    var secondsLeft = Math.floor(remainder / MS_PER_SEC);
    remainder = remainder % MS_PER_SEC;
    
    var hundredthsLeft = Math.floor(remainder / MS_PER_HUNDREDTH);
    
    // Update the countdown message
    countdownText.html(  
        m_countdownPrefix + 
        daysLeft + " days, " +
        hoursLeft + " hours, " +
        minutesLeft + ":" + AddLeadingZero(secondsLeft) + ":" + 
        AddLeadingZero(hundredthsLeft) + " minutes" +
        m_countdownSuffix
    );
}


// StopCountdown(): Stops the countdown timer so links can be processed. 

function StopCountdown() {
    clearInterval(m_intervalId);
}


// AddLeadingZero(): Adds a leading zero to the given number if it's less than 10. 

function AddLeadingZero(number) {
    if (number < 10) { 
        return ("0" + number.toString());
    } 
    else {
        return (number);
    }
}


// MAIN SCRIPT

// Initialize the countdown on page load
$( document ).ready( InitializeCountdown );
