var Countdown = new Class({
    Implements:[Options, Events],
    options:{
        endTime: false,
        onComplete: $empty
    },
    initialize: function(element, options){
        this.setOptions(options);
        this.element = element;
        this.countdown = null;
        this.days = 0;
        this.hours = 0;
        this.mins = 0;
        this.secs = 0;
        this.timeDiff = 0;
        this.element.set('text',
            this.days + ((this.days < 2) ? ' Tag ' : ' Tage ')
            + this.hours + ':' + this.mins + ':' + this.secs);
    },
    start: function(unixTime){
        this.endTime = Date.parse(this.options.endTime);
//        unixTime = unixTime * 1000;
//        this.currentTime = unixTime || $time();
        this.currentTime = $time();
        this.countdown = this.timer.periodical(1000, this);
    },
    calcDiff: function(timeDiff){
        var days, hours, mins, secs;
        timeDiff = timeDiff / 1000;
        days     = Math.floor(timeDiff / 86400);
        timeDiff = timeDiff - days * 86400;
        hours    = Math.floor(timeDiff / 3600);
        timeDiff = timeDiff - hours * 3600;
        mins     = Math.floor(timeDiff / 60);
        timeDiff = timeDiff - mins * 60;
        secs     = Math.floor(timeDiff);
        this.days  = days;
        this.hours = (hours < 10) ? '0' + hours : hours;
        this.mins  = (mins  < 10) ? '0' + mins  : mins;
        this.secs  = (secs  < 10) ? '0' + secs  : secs;
    },
    timer: function(){
//        this.currentTime = this.currentTime + 1000;
        this.currentTime = $time();
        var timeDiff = this.endTime - this.currentTime;
        this.calcDiff(timeDiff);
        this.element.set('text',
            this.days + ((this.days < 2) ? ' Tag ' : ' Tage ')
            + this.hours + ':' + this.mins + ':' + this.secs);
        if(timeDiff <= 0){
            $clear(this.countdown);
            this.fireEvent('complete');
        }
    }
});
