/**
 * jQuery's Countdown Plugin
 *
 * display a countdown effect at given seconds, check out the following website for further information:
 * http://heartstringz.net/blog/posts/show/jquery-countdown-plugin
 *
 * @author Felix Ding
 * Modified by Jeff S.
 * @version 0.1
 * @copyright Copyright(c) 2008. Felix Ding
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @date 2008-03-09
 * @lastmodified 2008-03-09 17:48    		 
 * @todo error & exceptions handling
*/
var CountdownCounter = 0;

jQuery.fn.countdown = function(options) {
	/**
	 * app init
	*/	
	if(!options) options = '()';
	if(jQuery(this).length == 0) return false;
	var obj = this;	
	
	if (options.counter != CountdownCounter) {return;}

	/**
	 * break out and execute callback (if any)
	 */
	if(options.seconds < 0 || options.seconds == 'undefined')
	{
		if(options.callback) {eval(options.callback)};
		return null;
	}
	
	if (options.seconds > 60) {
	
		options.minutes = Math.floor(options.seconds / 60);
		options.seconds = options.seconds % 60;
	
	}
	
	if (options.minutes > 60) {
	
		options.hours = Math.floor(options.minutes / 60);
		options.minutes = options.minutes % 60;
		
	}
	
	if (options.hours > 24) {
	
		options.days = Math.floor(options.hours / 24);
		options.hours = options.hours % 24;
		
	}

	/**
	 * recursive countdown
	 */
	window.setTimeout(
		function() {
			jQuery(obj).html((options.days ? options.days + " day(s) + " : '') + (options.hours ? PadNumber(options.hours) + ":" : "") + ((options.minutes || options.hours || options.days) ? PadNumber(options.minutes) + ":" : "") + PadNumber(options.seconds));
			--options.seconds;
			if (options.seconds < 0 && (options.minutes > 0 || options.hours > 0 || options.days > 0)) {options.seconds = 59; options.minutes--;}
			if (options.minutes < 0 && (options.hours > 0 || options.days > 0)) {options.minutes = 59; options.hours--;}
			if (options.hours < 0 && options.days > 0) {options.hours = 23; options.days--;}
            //console.log(options);
			jQuery(obj).countdown(options);
		}
		, 1000
	);	

	/**
     * return null
     */
    return this;
}