/**
 * @author Bob
 */
var CROWCODER;
if(!CROWCODER) CROWCODER = {};

/**
* @classDescription Emits a countdown timer to a webpage.
* @author Robert Crowley, www.crowcoder.com 2008
* @version 1.0
* @constructor
* @param {date} p_target_dt The Date/Time that the coundown
* is counting down to. THIS MUST BE UNIVERSAL TIME so
* display will be correct for any time zone
* @param {string} p_elem The HTML element id to emit the
* countdown display to.
* @param {string} p_msg The message to display in place of the
* countdown once time has run out.
*/

CROWCODER.JSCountdown = function(p_target_dt, p_elem, p_msg)
{
    // The number of milliseconds in one day
    var ONE_DAY = 86400000; 
    // ms in one hour
    var ONE_HR = 3600000;
    // ms in one min
    var ONE_MIN = 60000;
    // ms in one second
    var ONE_SEC = 1000;
    
    //Set the target datetime
    var target = ToLocalTime();
    var displayElem = document.getElementById(p_elem);
    var countdown_msg = p_msg; 
    var event_started = false; 
    var getCountdown;
    var show_countdown;
    var _interval;
    //Methods
    /**
    * @method Updates the countdown display once per second.
    * @param {string} p_elem the id of the HTML element to emit the countdown display to
    * @param {date} p_target The date to countdown to
    * @param {string} p_msg The text to display in place of the countdown when time runs down
    */
    
    show_countdown = function()
    { 
        //Call this function each second to give an animated appearance to the countdown
        if(! this.event_started)
        {
            //Resets the time with each function call
            //var today = new Date();
            //var tdate = new Date(target);
            //Write the return of start_countdown to the chosen html element
            displayElem.innerHTML = getCountdown();
        }
        else
        {
            clearInterval(_interval);
        }
    }
    
    this.start = function ()
    {
        _interval = setInterval(show_countdown, 1000);
    } 
    
    /**
    * @method _get_countdown Returns HTML markup tagged for css formatting that
    * displays the countdown as 'xxdays:xxhrs:xxmins:xxsec'
    * @param {date} p_target The date/time the event to count down to starts
    * @param {date} p_today The date/time right now
    * @param {string} p_msg The text to display when time has hit zero
    */
    getCountdown = function()
    {
        //the HTML to emit with the countdown display
        var retval;        
        var p_today = new Date();
        // Convert both dates to milliseconds
        var target_ms = target.getTime();
        var now_ms = p_today.getTime();
        //The number of milliseconds between now and the target date/time
        var date_diff = target_ms - now_ms;
        
        //Stop timer when time ticks down
        if(date_diff > 0)
        { 
            // Number of days between now and target datetime is
            var difference_days = Math.abs(target_ms - now_ms);
            var num_days = Math.floor(difference_days/ONE_DAY);
            //We will subtract days_ms from date_diff to get the hrs, mins, secs left
            var days_ms = num_days * ONE_DAY;
            var difference_hrs = Math.abs(days_ms - date_diff);
            var num_hrs = Math.floor(difference_hrs/ONE_HR);
            var hrs_ms = num_hrs * ONE_HR;
            var difference_mins = Math.abs(((hrs_ms + days_ms)) - date_diff);
            var num_mins = Math.floor(difference_mins/ONE_MIN);
            var mins_ms = num_mins * ONE_MIN;
            var difference_secs = Math.abs((days_ms + hrs_ms + mins_ms) - date_diff);
            var num_secs = Math.floor(difference_secs/ONE_SEC);
            
            retval = '<span class="days">' + num_days.toString() + '</span>' +
                '<span class="time_delim"> days: </span>' +
                '<span class="hrs">' + num_hrs.toString() + '</span>' +
                '<span class="time_delim"> hrs: </span>' +
                '<span class="mins">' + num_mins.toString() + '</span>' +
                '<span class="time_delim"> min: </span>' +
                '<span class="secs">' + num_secs.toString() + '</span>' +
                '<span class="time_delim">sec</span>';
        }
        else
        {
            event_started = true;
            retval = countdown_msg;
        }
        return retval;
    }
    
    function ToLocalTime()
    {
        var rightow = new Date();
        var rightNowUTC = p_target_dt;
        var timediff = rightow.getTimezoneOffset();
        
        rightNowUTC.setMinutes(rightNowUTC.getMinutes() - timediff);
        
        return rightNowUTC;
    }
}

