/*
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
*/
var Countdown = new Class({
	options: {
    countplus:true,
		days: true,
		hours: true,
		minutes: true,
		seconds: true,
		target: false,
		formatDays: '<div class="item">%days%:<br /><span class="expl">days</span></div>',
		formatHours: '<div class="item">%hours%:<br /><span class="expl">hours</span></div>',
		formatMinutes: '<div class="item">%minutes%:<br /><span class="expl">mins</span></div>',
		formatSeconds: '<div class="item last">%seconds%<br /><span class="expl">secs</span>',
		message: 'Expired',
		onTick: Class.empty,
		onComplete: Class.empty
	},
	initialize: function(el, options){

		this.setOptions(options);
		this.el = el;
		this.now = Math.round($time() / 1000);
		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.options.countplus==false) {
			this.done();
			return;
		}
		this.tick();
	},
	tick: function() {
		var remaining = this.remaining;
		this.getTime();
		this.display(this.time);
		if (this.remaining <= 0 && this.options.countplus==false) {
		
			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);
				if (this.remaining <= 0) {
				  this.remaining = this.target - Math.round($time() / 1000);
				  this.remaining = this.remaining * (-1);
		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 ;
//		alert(this.el);

				} else {
		     
		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 ;

        }
		
		if(hours < 10){
			hours = '0'+hours;
		}
		if(minutes < 10){
			minutes = '0'+minutes;
		}
		if(seconds < 10){
			seconds = '0'+seconds;
		}
		
		this.time = {
			days:    days,
			hours:   hours,
			minutes: minutes,
			seconds: seconds
		};
	},
	display: function(){
		this.el.innerHTML = 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);
