/**
* Count down interval script
* Eric Tompkins, North Wind Media
* 12/5/2007
*
* Required jQuery library version 1.2 or greater
*
* Countdowns down to a certain date, and then the day after reaching that date counts down to
* the next date a certain number of days away
*
* Options:
* month: The target date numerical month
* day: The target date numerical day
* year: The target date numerical year
* hour: The target date hour (24 hour format)
* minute: The target date minute - defaults to '00'
* seconds: The target date seconds - defaults to '00'
* timezoneOffset: The UTC timezone offset - defaults to '-0500'
* displayText: A string respresenting how the countdown date/time will be displayed
*                {D} = Day
*                {H} = Hour
*                {M} = Minute
*                {S} = Seconds
*                {TD} = Target Day of the month
*                {TNM} = Target Numerical Month
*                {TFM} = Target Full text Month
*                {TY} = Target Year
*                {TH} = Target Hour
*                {TM} = Target Minute
*                {TS} = Target Second
* message: The message to be displayed when the countdown is over
* startMonth: The numerical month that the original countdown starts on
* startDay: the numerical day that the original countdown starts on
* startYear: the numerical day that the original countdown starts on
* dayInterval: the number of days between each countdown date
* countdownEnd: What to do when the countdown ends. 'restart' is the default and will restart the countdown
*               for the next interval date. 'message' will display the message text
* displayMessageTime: If countdownEnd is set to 'message' this will determine how many seconds the message will be displayed
*                     before the countdown starts again. If set to 0 then the message will display indefinitely.
*
* Example:
* Count down to every 2nd Tuesday starting with December 18, 2007
* var options = {
*    startMonth: 12,
*    startDay: 4,
*    startYear: 2007,
*    hour: 17,
*    displayText: '<p>Target Date: {TFM} {TD}, {TY} </p><p>Countdown: {D} Day(s), {H} Hour(s), {M} Minute(s).</p>',
*    message: 'Time is up!'
* }
* $('#myElementId').countdownInterval(options);
*/
(function($) {
    //If the NC scope is not availalable, add it
    $.nc = $.nc || {};
    $.fn.countdownInterval = function(options) {
        return this.each(function() {
            new $.nc.countdownInterval(this,options);
        });
    };

    $.nc.countdownInterval = function(el,options) {
        this.element; this.timeDiff; this.targetDate; this.timer;
        this.options = {
            //Options for countdown function
            month: '12',
            day: '25',
            year: '2007',
            hour: '1',
            minute: '00',
            seconds: '00',
            timezoneOffset: '-0500',
            displayText: '{D} Days, {H} Hours, {M} Minutes, {S} Seconds.',
            message: 'All done',
            daylightSavings: 'yes',
            //Options for countdownInterval
            startMonth: '12',
            startDay: '4',
            startYear: '2007',
            dayInterval: 14,
            countdownEnd: 'restart',
            displayMessageTime: 86400
        };
        this.options = jQuery.extend(this.options, options);
        this.daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        this.element = $(el);
        this.timeDiff = 0;
        var o = this.options;
        var self = this;
        this.display();
    };
    $.extend($.nc.countdownInterval.prototype, {
        display: function() {
            this.getCountdownDate();
            this.element.countdown({
                month: this.options.month,
                day: this.options.day,
                year: this.options.year,
                hour: this.options.hour,
                minute: this.options.minute,
                seconds: this.options.seconds,
                displayText: this.options.displayText,
                message: this.options.message,
                daylightSavings: this.options.daylightSavings
            });
        },
        getCountdownDate: function() {
            //Set the starting date
            var startDate = this.options.startMonth + '/' + this.options.startDay + '/' + this.options.startYear + ' 0:00:00';
            if (this.options.timezoneOffset.length > 0) {
                startDate += ' UTC' + this.options.timezoneOffset;
            }
            var start = new Date(startDate);
            //Get today's date
            var nowDate = new Date();
            var nowString = (nowDate.getMonth() + 1) + '/' + nowDate.getDate() + '/' + nowDate.getFullYear() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds();
            if (this.options.timezoneOffset.length > 0) {
                nowString += ' UTC' + this.options.timezoneOffset;
            }
            var now = new Date(nowString);
            //Get the difference between the two days
            var timeDiff = Math.floor((now.getTime() - start.getTime()) / 1000);
            var dayDifference = Math.floor(timeDiff/86400);
            //If the day difference divided by the day interval = 0 then the current day is current countdown date
            var interval = parseInt(this.options.dayInterval);
            var modulus = dayDifference%interval;
            if (modulus == 0) {
                //The countdown is on the current date, now see if it has passed the hour:minute:second yet
                var todayDateString = (now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear() + ' ' + this.options.hour + ':' + this.options.minute + ':' + this.options.seconds;
                if (this.options.timezoneOffset.length > 0) {
                    todayDateString += ' UTC' + this.options.timezoneOffset;
                }
                var today = new Date(todayDateString);
                var todayDiff = Math.floor((today.getTime() - now.getTime()) / 1000);
                if (todayDiff > 0) {
                    //The current time is still less than the countdown time. Continue with the current countdown
                    this.setCountdownDate(now.getMonth(), now.getDate(), now.getFullYear());
                }
                else {
                    //The current time is greater than the countdown time.
                    if (this.options.countdownEnd == 'restart') {
                        //Don't display the message. Restart the countdown
                        this.setCountdownDate(now.getMonth(), now.getDate() - modulus + interval, now.getFullYear());
                    }
                    else {
                        //Display the message if the display message time allows it
                        if (this.options.displayMessageTime > 0 && Math.abs(todayDiff) > parseInt(this.options.displayMessageTime)) {
                            //The amount of time that the message could be displayed is over. Restart the countdown
                            this.setCountdownDate(now.getMonth(), now.getDate() - modulus + interval, now.getFullYear());
                        }
                        else {
                            //Either the displayMessageTime is 0 (infinite) or the amount of time that the message could be displayed
                            //has not been met yet so display the message
                            //By continuing with the current countdown the message will be displayed by the countdown script
                            this.setCountdownDate(now.getMonth(), now.getDate(), now.getFullYear());
                        }
                    }
                }

            }
            else {
                this.setCountdownDate(now.getMonth(), now.getDate() - modulus + interval, now.getFullYear());
            }
        },
        getNumDaysInFeburary: function(year) {
            return 32 - new Date(year, 1, 32).getDate();
        },
        setCountdownDate: function(month, day, year) {
            this.options.month = month + 1;
            this.options.year = year;
            this.options.day = day;
        }
    });
})($);
/**
* Count down script
* Eric Tompkins, North Wind Media
* 12/5/2007
*
* Required jQuery library version 1.2 or greater
*
* Options:
* month: The target date numerical month
* day: The target date numerical day
* year: The target date numerical year
* hour: The target date hour (24 hour format)
* minute: The target date minute - defaults to '00'
* seconds: The target date seconds - defaults to '00'
* timezoneOffset: The UTC timezone offset - defaults to '-0500'
* displayText: A string respresenting how the countdown date/time will be displayed
*                {D} = Day
*                {H} = Hour
*                {M} = Minute
*                {S} = Seconds
*                {TD} = Target Day of the month
*                {TNM} = Target Numerical Month
*                {TFM} = Target Full text Month
*                {TY} = Target Year
*                {TH} = Target Hour
*                {TM} = Target Minute
*                {TS} = Target Second
* message: The message to be displayed when the countdown is over
*
* Example:
* Count down to 5:00 PM Christmas eve, 2007
* var options = {
*    month: 12,
*    day: 24,
*    year: 2007,
*    hour: 17,
*    minute: 0,
*    displayText: '{D} days, {H} hours, and {M} minutes till we open presents!',
*    message: 'Time to open presents!'
* }
* $('#myElementId').countdown(options);
*/
(function($) {
    //If the NC scope is not availalable, add it
    $.nc = $.nc || {};
    $.fn.countdown = function(options) {
        return this.each(function() {
            new $.nc.countdown(this,options);
        });
    };

    $.nc.countdown = function(el,options) {
        this.element; this.timeDiff; this.targetDate; this.timer;
        this.options = {
            month: '12',
            day: '25',
            year: '2007',
            hour: '1',
            minute: '00',
            seconds: '00',
            timezoneOffset: '-0500',
            displayText: '{D} Days, {H} Hours, {M} Minutes, {S} Seconds.',
            message: 'All done',
            daylightSavings: 'no'
        };
        this.options = jQuery.extend(this.options, options);
        this.fullMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        this.element = $(el);
        this.timeDiff = 0;
        var o = this.options;
        var self = this;
        var dateString = o.month + '/' + o.day + '/' + o.year + ' ' + o.hour + ':' + o.minute + ':' + o.seconds;
        if (o.timezoneOffset.length > 0) {
            dateString += ' UTC' + o.timezoneOffset;
        }
        this.targetDate = new Date(dateString);
        this.display();
        var i = 0;
        this.timer = setInterval(function() {
            self.display();
            i ++;
        }, 1000);
    };
    $.extend($.nc.countdown.prototype, {
        display: function() {
            this.calculateTime();
            if (this.timeDiff > 0) {
                var text =  this.options.displayText;
                text = text.replace(/{TD}/g, this.targetDate.getDate());
                text = text.replace(/{TNM}/g, this.targetDate.getMonth());
                text = text.replace(/{TFM}/g, this.fullMonths[this.targetDate.getMonth()]);
                text = text.replace(/{TY}/g, this.targetDate.getFullYear());
                text = text.replace(/{TH}/g, this.targetDate.getHours());
                text = text.replace(/{TM}/g, this.targetDate.getMinutes());
                text = text.replace(/{TS}/g, this.targetDate.getSeconds());
                text = text.replace(/{D}/g, this.getTime('days'));
                text = text.replace(/{H}/g, this.getTime('hours'));
                text = text.replace(/{M}/g, this.getTime('minutes'));
                text = text.replace(/{S}/g, this.getTime('seconds'));
                this.element.html(text);
            }
            else {
                //                var text =  this.options.displayText;
                //                text = text.replace(/{TD}/g, this.targetDate.getDate());
                //                text = text.replace(/{TNM}/g, this.targetDate.getMonth());
                //                text = text.replace(/{TFM}/g, this.fullMonths[this.targetDate.getMonth()]);
                //                text = text.replace(/{TY}/g, this.targetDate.getFullYear());
                //                text = text.replace(/{TH}/g, this.targetDate.getHours());
                //                text = text.replace(/{TM}/g, this.targetDate.getMinutes());
                //                text = text.replace(/{TS}/g, this.targetDate.getSeconds());
                //                text = text.replace(/{D}/g, this.getTime('days'));
                //                text = text.replace(/{H}/g, this.getTime('hours'));
                //                text = text.replace(/{M}/g, this.getTime('minutes'));
                //                text = text.replace(/{S}/g, this.getTime('seconds'));
                //                this.element.html(text);
                this.element.html(this.options.message);
                clearInterval(this.timer);
            }
        },
        calculateTime: function() {
            var now = new Date();
            var nowTime = now.getTime();
            if (this.testForDaylightSavings()) {
                nowTime += 3600000; // Subtract 1 hour in milliseconds
            }
            this.timeDiff = Math.floor((this.targetDate.getTime() - nowTime) / 1000);
        },
        getTime: function(timetype) {
            var timeLeft = this.timeDiff;
            //Credit goes to Scott Benjamin for the calculations
            //http://www.scott-benjamin.com/countdown/
            days = Math.floor(timeLeft/86400);
            timeLeft = timeLeft%86400;
            hours = Math.floor(timeLeft/3600);
            timeLeft = timeLeft%3600;
            mins = Math.floor(timeLeft/60);
            timeLeft = timeLeft%60;
            secs = Math.floor(timeLeft);
            switch (timetype) {
                case 'seconds': return secs; break;
                case 'minutes': return mins; break;
                case 'hours': return hours; break;
                case 'days': return days; break;
            }
        },
        testForDaylightSavings: function()
        {
            if (this.options.daylightSavings == 'no') {
                return false;
            }
            var rightNow = new Date();
            var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
            var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
            var temp = date1.toGMTString();
            var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
            var temp = date2.toGMTString();
            var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
            var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
            var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
            if (hoursDiffDaylightTime == hoursDiffStdTime) {
                // Daylight savings is not observed
                return false;
            } else {
                // Daylight savings is observed
                return true;
            }
        }
    });
})($);