/*!
 * jQuery Countdown plugin v0.9
 * http://projects.littlewebthings.com/lwtCountdown/
 *
 * Copyright 2010, Vassilis Dourdounis
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
(function($){

	$.fn.countDown = function (options) {

		config = {};
		$.extend(config, options);
		//alert(config.targetDate.hour+" - "+config.targetDate.min+" - "+config.targetDate.sec+" - ");
		var targetTime = new Date();
		targetTime.setDate(config.targetDate.day);
		targetTime.setMonth(config.targetDate.month-1);
		targetTime.setFullYear(config.targetDate.year);
		targetTime.setHours(config.targetDate.hour);
		targetTime.setMinutes(config.targetDate.min);
		targetTime.setSeconds(config.targetDate.sec);
		
		var nowTime = new Date(parseInt(config.nowd));
		

		diffSecs = Math.floor((targetTime.valueOf()-nowTime.valueOf())/1000);
	

		//$('#' + this.attr('id') + ' .digit').html('<div class="top"></div><div class="bottom"></div>');
		this.doCountDown(diffSecs);

	}

	$.fn.doCountDown = function (diffSecs) {
		
		if (diffSecs <= 0)
		{
			diffSecs = 0;
		}
		
		secs = diffSecs % 60;
		mins = Math.floor(diffSecs/60)%60;
		days = Math.floor(diffSecs/60/60/24)%7;
		weeks = Math.floor(diffSecs/60/60/24/7);
		hourss = Math.floor(diffSecs/60/60)%24;
		hourss = hourss+(weeks*7+days)*24;

		this.dashChangeTo('seconds', secs);
		this.dashChangeTo('minutes', mins);
		this.dashChangeTo('hours', hourss);

		if (diffSecs > 0)
		{	
			e = this;
			setTimeout(function() { e.doCountDown(diffSecs-1) } , 1000);
		}
	}

	$.fn.dashChangeTo = function(dash, n) {
		//alert(dash);
		n=n+"";
		if (n.length==1) n="0"+n;
		$("#"+dash).html(n);
	}

})(jQuery);

