(function($) {
	$.fn.count_down_from = function(date) {
		var initial = date.get_sec_time();
		
		this.each(function() {
			var element = $(this);
			
			setInterval((function() {
				var e = element;
				var c = Date.get_sec_time();
				
				return function() {
					c = Date.get_sec_time();
					var distance = initial - c;
					if (distance < 0) distance = 0;
					
					var hours = Math.floor(distance / Date.SECONDS_IN_HOUR);
					var minutes = Math.floor((distance % Date.SECONDS_IN_HOUR) / Date.SECONDS_IN_MINUTE);
					var seconds = (distance % Date.SECONDS_IN_MINUTE);
					
					var zero_fill = function(n) { return n < 10 ? '0' + n : n };
					
					e.find(".countdown.hours").text(zero_fill(hours));
					e.find(".countdown.minutes").text(zero_fill(minutes));
					e.find(".countdown.seconds").text(zero_fill(seconds));
				};
			})(), 1000);
		});
	};
})(jQuery);

// date extensions
Date.get_sec_time = function() {
	var d = new Date();
	
	return d.get_sec_time();
};

Date.SECONDS_IN_MINUTE = 60;
Date.SECONDS_IN_HOUR = Date.SECONDS_IN_MINUTE * 60;

Date.prototype.get_sec_time = function() {
	return Math.floor(this.getTime() / 1000);
};

