/*! jQuery Countdown Plugin

* Copyright Tom Ellis http://www.webmuse.co.uk

* Licensed under MIT License

* See http://www.webmuse.co.uk/license/

*/

(function($) {

   

	jQuery.fn.countdown = function( options ) {  

	

		var defaults = {

			date: (new Date()),

			updateTime: 1000,

			htmlTemplate: "<span id='dynamic' style='margin-right:6px; margin-left:0px; float:left; width:45px;  text-align:center;'>%{d}</span> <span class=\"small\" style=\"display:none;\">days</span> <span id='dynamic' style='margin-right:10px; width:40px; float:left; text-align:center;'>%{h}</span> <span class=\"small\" style=\"display:none;\">hours</span> <span id='dynamic' style='width:40px; margin-right:0; float:left; text-align:center;'>%{m}</span> <span class=\"small\" style=\"display:none;\">mins</span> <span style=\"display:none;\">%{s}</span> <span class=\"small\" style=\"display:none;\">sec</span>",

			minus: false

		};

	 

		   

		var opts = $.extend( {}, defaults, options ),

			cancel = false,

			template = opts.htmlTemplate;

	   

		return this.each(function() {

			var intval = window.setInterval(function(){

				

				var TodaysDate = new Date(),

					CountdownDate = new Date( opts.date ),

					msPerDay = 24 * 60 * 60 * 1000,

					timeLeft = (CountdownDate.getTime() - TodaysDate.getTime()),

					e_daysLeft = timeLeft / msPerDay,

					daysLeft = Math.floor(e_daysLeft),

					e_hrsLeft = (e_daysLeft - daysLeft)*24, //Gets remainder and * 24

					hrsLeft = Math.floor(e_hrsLeft),

					minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60),					

					e_minsleft = (e_hrsLeft - hrsLeft)*60, //Gets remainder and * 60

					secLeft = Math.floor((e_minsleft - minsLeft)*60),

					time = "";



				if ( TodaysDate <= CountdownDate || opts.minus ){

				   	

				   	time = template.replace(/%{d}/, daysLeft).replace(/%{h}/, hrsLeft).replace(/%{m}/, minsLeft).replace(/%{s}/, secLeft);

				} else {

		

					time = template.replace(/(%{d}|%{h}|%{m}|%{s})/g, "00");

					cancel = true;

				}

			   

				$("#time").html( time );

			   

				if ( cancel ){

					cancel = false;

					clearInterval( intval );

				}       		

			

			}, opts.updateTime);



		});

	};

       

})(jQuery);
