﻿var getOffset = true;

function CountdownTimer(holidayDate, element) {

    // get client datetime
    var now = new Date();

    // convert holiday time to javascript datetime
    holidayDate = new Date(holidayDate);

    // get countdown area
    var holidaySpan = $('#' + element);
    
    // if we have time left...
    if (holidayDate > now) {

        var currentYear = now.getFullYear();
        var holidayYear = holidayDate.getFullYear();

        var yearDiff = holidayDate - currentYear;
        
        var currentMonth = holidayDate.getMonth() + 1;
        var

        seconds = (holidayDate - now) / 1000;
        seconds = Math.floor(seconds);

        days = Math.floor(seconds / (60 * 60 * 24));
        seconds = seconds - (days * 24 * 60 * 60)

        hours = Math.floor(seconds / (60 * 60));
        seconds = seconds - (hours * 60 * 60)

        minutes = Math.floor(seconds / 60);
        seconds = seconds - (minutes * 60)

        seconds = Math.floor(seconds);

        days = days > 0 ? days + (days == 1 ? ' Day ' : ' Days ') : '';

        holidaySpan.html(days + hours + (hours == 1 ? ' Hour ' : ' Hours ') + minutes + (minutes == 1 ? ' Minute ' : ' Minutes ') + seconds + (seconds == 1 ? ' Second ' : ' Seconds '));

        setTimeout(function() { CountdownTimer(holidayDate, element) }, 1000);
    }
    else {

        holidaySpan.html("This holiday has already happened!");
    }
}
