(function($){
	/**
	 * Countdown plugin
	 *
	 * Count down to a date
	 *
	 * @author	Bart Deslagmulder
	 * @date	2011-06-21
	 * @version	0.1, Bart Deslagmulder		Base functionality
	 */
	$.widget("bde.countdown",
	{
		options: {
			  clearInterval: function() {
				var data = $(this).data('countdown');

				if ('undefined' !== typeof data.options && 'undefined' !== typeof data.options.timer) {
					clearInterval(data.options.timer);
				}
			  }
			,  complete: function() {
			}
			,  secondsLeft: null
			, targetDate: {
				  year: 2015
				, month: 1
				, day: 1
				, hour: 00
				, min: 00
				, sec: 00
				, full: ''
			}
			, timer: null
		}


		//
		// CONSTRUCTOR / DESTRUCTOR ////////////////////////////////////////////////
		//

		, _create: function() {
			this._countdown();
		}

		, destroy: function() {
			// call the base destroy function
			$.Widget.prototype.destroy.call(this);
		}

		//
		// PRIVATE METHODS /////////////////////////////////////////////////////////
		//

		, _countdown: function() {

			// check if config is okay (valid targetDate needed)
//			this._checkConfig();

			this._saveState();

			// start countdown
			this._initCountdown();

		}

		, _formatDigit: function(digit) {
			digit = '' + digit;
			return 1 === digit.length ? '0'+digit: digit;
		}

		, _initCountdown: function() {

			// set countdown
			this.options.secondsLeft = this._calcDiffInSeconds();

			// start countdown
			this._startCountdown();

		}

		, _checkSecondsLeft: function() {
			if (0 > this.options.secondsLeft) {
				this._trigger('clearInterval');
				this._trigger('complete');
				return false;
			}

			return true;
		}

		, _calcDiffInSeconds: function() {
			var now = new Date()
				, target = new Date(this.options.targetDate.month + '/' + this.options.targetDate.day + '/' + this.options.targetDate.year + ' ' + this.options.targetDate.hour + ':' + this.options.targetDate.min + ':' + this.options.targetDate.sec);

			this.options.targetDate.full = '' + target;

			return Math.floor((target.valueOf()-now.valueOf())/1000);
		}


		, _saveState: function() {
			// save reference in DOM
			this.element.data('countdown', this.options);
		}

		, _setCounter: function() {
			secs = this.options.secondsLeft % 60;
			mins = Math.floor(this.options.secondsLeft/60)%60;
			hours = Math.floor(this.options.secondsLeft/60/60)%24;
			days = Math.floor(this.options.secondsLeft/60/60/24);
			weeks = Math.floor(this.options.secondsLeft/60/60/24/7);

			this.element
				.find('.countdown-days').text(this._formatDigit(days)).end()
				.find('.countdown-hours').text(this._formatDigit(hours)).end()
				.find('.countdown-mins').text(this._formatDigit(mins)).end()
				.find('.countdown-secs').text(this._formatDigit(secs)).end()
				.show();
		}


		, _setInterval: function() {
			var _self = this;
			
			this.options.timer = setInterval(function() {

				_self.options.secondsLeft = parseInt(_self.options.secondsLeft-1, 10);

				if (_self._checkSecondsLeft()) {
					_self._setCounter();
				}

			}, 1000);

			var $countdownUntil = this.element.find('div.countdown-until');

			if (1 === $countdownUntil.length) {
				$countdownUntil.text(this.options.targetDate.full);
			}
		}

		, _startCountdown: function() {
			// check if we still need to count down, or we need launch "onComplete"
			if (this._checkSecondsLeft()) {
				this._setInterval();
			}
		}

	});
})(jQuery);

