/**************************************************
 *
 * Countdown to next TW.
 *
 * @author Neck - http://www.eikylon.net
 * @version 0.2.0
 * @licence available under the Creative Commons Attribution 3.0 Unported - http://creativecommons.org/licenses/by/3.0/
 * WITHOUT ANY WARRANTY, use it "at your own risks"
 * @created november 2008
 *
 **************************************************/
ek_countdown = {
	/**
	* Launch countdown for the right spans.
	*
	*/
	initialize: function() {
		// look for spans
		var s = $$('#brd-announcement span.ek_countdown');
		if(!s) { return; }
	
		// create a countdown for each
		for(var i=0; i<s.length; ++i) {
			new this.countdown(s[i], this.lang);
		}
	},

	/**
	* Countdown class, take a span and replace it with a countdown.
	* 		
	*/
	countdown: Class.create({
		/**
		* Get the target date and options then start the countdown.
		*
		* @param DOMnode the span to use
		* @param objet translated strings
		*/
		initialize: function(cdSpan, lang) {
			this.cdSpan = cdSpan;
			this.lang = lang;

			// get the options
			this.options = {
				refresh:   'm',
				txtWhile:  '#{cd}',
				txtAfter:  ''
			};
			var options = this.cdSpan.getAttribute('title');
			if(options) {
				this.cdSpan.setAttribute('title', '');
				if(new RegExp('^([^:]+:[^\\|]+\\|?)+$').test(options)) {
					options = options.split('|');
					for(var i=0; i<options.length; ++i) {
						var o = options[i];
						this.options[o.substr(0,o.indexOf(':'))] = o.substr(o.indexOf(':')+1);
					}
				}
			}

			// target date
			if(this.options.date) {
				this.dTarget = new Date(this.options.date);
			} else {
				this.cdSpanTxt = this.cdSpan.firstChild.nodeValue;
				this.dTarget = new Date(this.cdSpanTxt);
			}
			if(!this.dTarget) { return; }
			this.dTarget = this.dTarget.getTime();

			this.pe = new PeriodicalExecuter(this.update.bind(this), ((this.options.refresh === 's') ? 1 : 60));
			this.update();
		},

		/**
		* Update span content.
		* 		
		*/
		update: function() {
			var remain = this.dTarget - new Date().getTime();

			// end of countdown
			if(remain <= 0) {
				if(this.options.txtAfter !== '') {
					this.cdSpan.update(this.options.txtAfter);
				} else {
					this.cdSpan.update(this.cdSpanTxt);
				}
				this.pe.stop();
				return;
			}

			// create the new display
			this.cdSpan.update(this.options.txtWhile.interpolate({cd: [
				this.split(remain, 86400000),
				this.split(remain, 3600000, 24),
				this.split(remain, 60000, 60),
				this.split(remain, 1000, 60)
			].inject('', this.display, this)}));
		},

		/**
		* Split an ammount of miliseconds(ms) in an unit
		*
		* @param int total of ms
		* @param int number of milisecond to consider per unit
		* @param int max you can have of an unit, infinite if omited
		* @return int number of unit
		*/
		split: function(msTotal, unitMs, unitMax) {
			var unitTotal = Math.floor(msTotal/unitMs);
			if(unitMax) { unitTotal = unitTotal % unitMax; }
			return unitTotal;
		},

		/**
		* Create the countdown text from an array of values.
		*
		* @param string accumulator
		* @param int number of unit
		* @param int index of current unit
		* @return string accumulated text	
		*/
		display: function(txtCd, unit, i) {
			var u = ['day', 'hour', 'min', 'sec'];

			if(this.options.refresh !== 's' && i === 3) { return txtCd; }
			if(txtCd.blank() && unit === 0) { return txtCd; }
			if(!txtCd.blank()) {
				txtCd += (unit < 10) ? ' 0' : ' ';
			}
			return txtCd+unit+this.lang[u[i]+((unit > 1)?'s':'')];
		}
	})
};

// launch all that once DOM is loaded
Event.observe(window, 'load', function() {
	ek_countdown.initialize();
});
