/*************
 * Written by Erik Rasmussen (erikwordpresswidgets -at- gmail)
 * http://www.erik-rasmussen.com/blog/2007/04/08/wordpress-countdown-widget/
 **/
var BoxedDuration = Class.create();
BoxedDuration.prototype =
{
	units:
	{
		day: 'day',
		days: 'days',
		d: 'd',
		hour: 'hour',
		hours: 'hours',
		minute: 'minute',
		minutes: 'minutes',
		second: 'second',
		seconds: 'seconds'
	},

	initialize: function(milliseconds, abbreviated)
	{
		this.milliseconds = milliseconds;
		this.abbreviated = abbreviated;
	},

	toArray: function()
	{
		var days = Math.floor(this.milliseconds / 86400000);
		var remainder = this.milliseconds - days * 86400000;
		var hours = Math.floor(remainder / 3600000);
		remainder -= hours * 3600000;
		var minutes = Math.floor(remainder / 60000);
		remainder -= minutes * 60000;
		var seconds = Math.floor(remainder / 1000);
		remainder -= seconds * 1000;

		return new Array(days, hours, minutes, seconds);
		var buffer = [];
		if (days > 0)
		{
			buffer.push(days);
			//if (this.abbreviated)
			//	buffer.push(this.units.d);
			//else
			//{
			buffer.push(' ');
			buffer.push(days > 1 ? this.units.days : this.units.day);
			//}
		}
		if (this.abbreviated)
		{
			if (buffer.length > 0)
				buffer.push(' ');
			//if (hours < 10)
			//	buffer.push('0');
			buffer.push(hours);
			buffer.push(' hr ');
			//if (minutes < 10)
		        //	buffer.push('0');
			buffer.push(minutes);
                        buffer.push(' min ');
			//if (seconds < 10)
			//	buffer.push('0');
			buffer.push(seconds);
			buffer.push(' sec');
		}
		else
		{
			var out = function(value, singularUnit, pluralUnit)
			{
				if (value > 0)
				{
					if (buffer.length > 0)
						buffer.push(', ');
					buffer.push(value);
					buffer.push(' ');
					if (value > 1)
						buffer.push(pluralUnit);
					else
						buffer.push(singularUnit);
				}
			}
			out(hours, this.units.hour, this.units.hours);
			out(minutes, this.units.minute, this.units.minutes);
			out(seconds, this.units.second, this.units.seconds);
		}
		return buffer.join('');
	}
}
BoxedDuration.countdown = function(seconds, day_elem, hrs_elem, min_elem, sec_elem)
{
        var elements = new Array($(day_elem), $(hrs_elem), $(min_elem), $(sec_elem));
	var date = new Date().getTime() + (1000 * seconds);
	var refresh = function()
	{
		var milliseconds = date - new Date().getTime();
		if (milliseconds > 0) {
		    var duration = new BoxedDuration(Math.abs(milliseconds));
                    var curr_value = duration.toArray();
		    for (var i = 0; i < curr_value.length; i++) {
			elements[i].value = curr_value[i];
		    }
		    window.setTimeout(refresh, 1000);
		}
		else {
                    for (var i = 0; i < elements.length; i++) {
                        elements[i].value = '0';
                    }
		}
	}
	refresh();
}
