// 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, 9, 2, 0, 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 = ' ';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 9, 15, 0, 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, 9, 30, 0, 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 = ' ';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2010, 10, 12, 1, 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( 2011, 0, 8, 1, 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 = ' ';
    countdownMessages.push( message );
    
    message = new Object();
    message.TargetDate = new Date( Date.UTC( 2011, 0, 21, 1, 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( 2011, 1, 5, 7, 59, 0, 0 ) );
    message.Prefix = '<b>TO BE INCLUDED IN <a href="Prizes.aspx"' + 
        ' onmousedown="StopCountdown()">GRAND PRIZE DRAWING</a>:</b> <br /> ';
    message.Suffix = ' ';
    countdownMessages.push( message );
    
    // ... Raffle completed
    message = new Object();
    message.TargetDate = null;
    message.Prefix = '<b>TICKET SALES NOW CLOSED.</b><br /> ';
    message.Suffix = 'Grand Prize Drawing on July 10th';
    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 &nbsp; " +
        hoursLeft + " hrs &nbsp; " +
        minutesLeft + " mins &nbsp; " + 
        AddLeadingZero(secondsLeft) + "." + 
        AddLeadingZero(hundredthsLeft) + " secs" +
        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 );
