/*
Script: Countdown.js
		Contains <Countdown>

License:
		MIT-style license.

Class: Countdown
		Creates a countdown. Returns the time left.

Arguments:
		element - the countdown container
		options - see options below

Options:
		days - display days left
		hours - display hours left
		minutes - display minutes left
		seconds - display seconds left
		formatDays, formatHours, formatMinutes, formatSeconds - how to display your countdown, %days%, %hours%, %minutes%, %seconds% will be replaced by numbers
		message - message to display when time is up

Events:
		onTick - a function to fire every second
		onComplete - a function to fire when time is up

ATTENTION: Modified by Marçal Juan Llaó (2008-01-15) for become an universal countdown. To setup it just add the option now with the server time.

*/

var Countdown = new Class({

	options: {
		days: true,
		hours: true,
		minutes: true,
		seconds: true,
		target: false,
		formatDays: '%days% days ',
		formatHours: '%hours% hours ',
		formatMinutes: '%minutes% mins ',
		formatSeconds: '%seconds% secs',
		message: 'Expired',
		onTick: Class.empty,
		onComplete: Class.empty,
		now: false
	},

	initialize: function(el, options){

		this.setOptions(options);

		this.el = el;

// 		this.now = Math.round($time() / 1000);
		
		this.now = ( this.options.now ) ? new Date(this.options.now).getTime() / 1000: Math.round($time() / 1000);
		if (this.now == 'NaN') return;

		this.bMiddleTick = false;

		this.target = new Date(el.innerHTML).getTime() / 1000;
		if (this.target == 'NaN') return;

		// can't proceed without a target time
		if (!this.target) return;

		this.remaining = this.target - this.now;

		// target has already passed
		if (this.remaining < 0) {
			this.done();
			return;
		}

		this.tick();

	},

	tick: function() {

		var remaining = this.remaining;

		if( this.bMiddleTick )
		{
			this.now += 1;
		}

		this.bMiddleTick = !this.bMiddleTick;

		this.getTime();

		this.display(this.time);

		if (this.remaining == 0) {
			this.done();
			return;
		}

		if (remaining != this.remaining) {
			this.fireEvent('onTick', this.time);
		}

		(function(){this.tick()}.bind(this)).delay(500);

	},

	getTime: function() {

		var day = 86400;
		var hour = 3600;
		var minute = 60;

// 		this.remaining = this.target - Math.round($time() / 1000);
		this.remaining = this.target - this.now;

		var days    = this.options.days    ? Math.floor(this.remaining/day) : 0 ;
		var hours   = this.options.hours   ? Math.floor((this.remaining - (days*day))/hour) : 0 ;
		var minutes = this.options.minutes ? Math.floor((this.remaining - (days*day) - (hours*hour))/minute) : 0 ;
		var seconds = this.options.seconds ? this.remaining - (days*day) - (hours*hour) - (minutes*minute) : 0 ;

		this.time = {
			days:    days,
			hours:   hours,
			minutes: minutes,
			seconds: seconds
		};

	},

	display: function(){

		this.el.setHTML(this.format());

	},

	format: function() {

		var str = '';

		if (this.time.days > 0) {
			str += this.options.formatDays.replace('%days%', this.time.days);
		}
		if (this.time.days > 0 || this.time.hours > 0) {
			str += this.options.formatHours.replace('%hours%', this.time.hours);
		}
		if (this.time.days > 0 || this.time.hours > 0 || this.time.minutes > 0) {
			str += this.options.formatMinutes.replace('%minutes%', this.time.minutes);
		}
		str += this.options.formatSeconds.replace('%seconds%', this.time.seconds);

		return str;

	},

	done: function() {

		this.el.setHTML(this.options.message);

		this.fireEvent('onComplete', this.options.message);

	}

});

Countdown.implement(new Options);
Countdown.implement(new Events);