var lustrumCounter = Class.create({
    target_time: 1244491500, // 8 juni 2009 22:05
    updater: undefined,

    initialize: function() {
        if(!$('lustrum_countdown')) {
            return;
        }

        // Update the countdown string
        this.countdownUpdate();

        // Update the countdown string each second
        this.updater = new PeriodicalExecuter(this.countdownUpdate.bind(this), 0.25);
    },

    countdownUpdate: function() {
        var time_current = time = new Date();
        var time_string = this.getDuration(this.target_time - time_current.getTime() / 1000);
        $('lustrum_countdown').update(time_string);
    },

    getDuration: function(s, l){
        if(l == undefined) {
            l = 0;
        }

        var tot = 0;
        var r = 0;
        var t = '';
        var p = 0;

        if(s > 86400) { // Days
            tot = Math.floor(s / 86400);
            r = s % 86400;
            if(tot > 1) {
                p = 'en ';
            } else {
                p = ' ';
            }

            if(tot > 0) {
                t = tot + ' dag' + p;
            }
        } else if(s > 3600) { // Hours
            tot = Math.floor(s / 3600);
            r = s % 3600;
            t = tot + ' uur ';
        } else if(s > 60) { // Minutes
            tot = Math.floor(s / 60);
            r = s % 60;
            if(tot > 1) {
                p = 'minuten ';
            } else {
                p = 'minuut ';
            }
            if(tot > 0) {
                t = tot + ' ' + p;
            }
        } else { // Seconds
            s = Math.floor(s);
            if(s != 1) {
                p = 'n ';
            } else {
                p = ' ';
            }

            if(s > 0) {
                t = 'en ' + s + ' seconde' + p;
            }
        }

        if(r > 0) {
            t = t + this.getDuration(r, l+1);
        }

        return t;
    }
});

var countdown = new lustrumCounter();