﻿(function ($) {

	"use strict";

	function zeroPad(num, count) {
		var numZeroPad = num.toString();
		while (numZeroPad.length < count) {
			numZeroPad = "0" + numZeroPad;
		}
		return numZeroPad;
	}

	$.fn.countDown = function () {

		this.each(function () {
			var eventDate, timer, obj;
			obj = $(this);
			eventDate = new Date(obj.find('.EventDate').val());
			function updateCountDown() {
				var now, difference, milliseconds, seconds, minutes, hours, days;
				now = new Date();
				difference = eventDate - now;

				if (difference > 0) {

					milliseconds = Math.floor(difference % 1000);
					difference = difference / 1000;

					seconds = Math.floor(difference % 60);
					difference = difference / 60;

					minutes = Math.floor(difference % 60);
					difference = difference / 60;

					hours = Math.floor(difference % 24);
					difference = difference / 24;

					days = Math.floor(difference);

					obj.find('.Day').html(days);
					obj.find('.Hour').html(zeroPad(hours, 2));
					obj.find('.Minute').html(zeroPad(minutes, 2));
					obj.find('.Second').html(zeroPad(seconds, 2));

				} else {
					clearInterval(timer);
					obj.find('.EventCountDown').hide();
				}
			}

			timer = setInterval(updateCountDown, 1000);
		});

	};
} (jQuery));
