function bindTimers() {
    tlc.bindAll();
}

var timeLeftClass = new Class({
    initialize: function() {
        this.tickers = [];
    },
    
    bindAll: function() {
        $each($$('div[id^=timerTimeleft_]'), function(el, key) {
            var t = el.get("text").split(":"), mode = "";
            if (t.length > 1) {
                mode = "inline";
                t = t[1];
            }
            else t = t[0];
            
            el.timeleft = parseInt(t);
            if (!isNaN(el.timeleft)) {
                el.empty();
                el.countdown = this.countDown.periodical(1000, this, el);
                this.countDown(el);
                el.setStyle("display", mode);
                this.tickers.push(el);
            }
        }, this);
        // this.periodicalTimer = this.updateCount.periodical(1000, this);
    },
    
    strPad: function(str, len, padWith, left) {
        str = String(str);
        var l = str.length, add = "";
        if (l < len) {
            for (i=0; i<len-l; i++) add += padWith;
            if (left) return add + str;
            return str + add;
        }
        return str;
    },
    
    countDown: function(el) {
        if (el.timeleft <= 0) {
            $clear(el.countdown);
            document.location.reload();
            return;
        }
        el.timeleft -= 1;
        
        var sPerDay = 24 * 60 * 60, dlstr,
        e_daysLeft = el.timeleft / sPerDay,
        daysLeft = Math.floor(e_daysLeft),
        e_hrsLeft = (e_daysLeft - daysLeft)*24,
        hrsLeft = Math.floor(e_hrsLeft),
        e_minsLeft = (e_hrsLeft - hrsLeft)*60,
        minsLeft = Math.floor((e_hrsLeft -hrsLeft)*60),
        //secsLeft = Math.ceil((e_minsLeft - minsLeft)*60);
        secsLeft = el.timeleft - daysLeft * sPerDay - hrsLeft * 3600 - minsLeft * 60;
        secsLeft = secsLeft == 60 ? 0:secsLeft;
        hrsLeft = this.strPad(hrsLeft, 2, "0", true);
        minsLeft = this.strPad(minsLeft, 2, "0", true);
        secsLeft = this.strPad(secsLeft, 2, "0", true);
        
        dlstr = daysLeft > 0 ? (daysLeft > 1 ? daysLeft + " Tagen " : "einem Tag "):"";
        el.set("text", dlstr + hrsLeft + ":" + minsLeft + ":" + secsLeft);
    }
});

var tlc = new timeLeftClass();

window.addEvent(load_method, bindTimers);
