var Countdown = new Class({
    options: {
        countplus: true,
        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
    },
    initialize: function(el, options){
        this.setOptions(options);
        this.el = el;
        this.now = Math.round($time() / 1000);

        this.target = new Date(el.get('html')).getTime() / 1000;
        if (this.target == 'NaN') 
            return;
        if (!this.target) 
            return;
        this.remaining = this.target - this.now;
        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
        }
        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(days <10 ) days = '0'+days;
		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.setHTML(this.format())
		this.el.set('html', 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.el.set('html', this.options.message);
        this.fireEvent('onComplete', this.options.message)
    }
});
Countdown.implement(new Options);
Countdown.implement(new Events);
