BOTS4.countdown = {

    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,

    interval: undefined,

    init: function (seconds) {
        if (seconds < 0) {
            $('countdown').hide();
            return;
        }

        this.days = Math.floor(seconds / (60 * 60 * 24));
        this.hours = Math.floor(seconds % (60 * 60 * 24) / (60 * 60));
        this.minutes = Math.floor(seconds % (60 * 60) / 60);
        this.seconds = seconds % 60;

        this.createBox('days', this.days);
        this.createBox('hours', this.hours);
        this.createBox('minutes', this.minutes);
        this.createBox('seconds', this.seconds);

        this.interval = setInterval(function () {
            this.updateAll();
        }.bind(this), 1000);
    },

    createBox: function (name, value) {
        var html = '';

        html += '<div class="countdown-box">';
        html += '<div id="countdown-' + name + '">';
        html += value;
        html += '</div>';
        html += name
        html += '</div>';

        $('countdown-inner').insert(html);
    },

    updateAll: function () {
        if (this.seconds === 0 && this.minutes === 0 && this.hours === 0 && this.days === 0) {
            clearInterval(this.interval);
            $('countdown').setStyle({color: 'yellow'});
            $('countdown').innerHTML = '<br /><br />I remembered to put something here this time!<br /><br />Now what are you waiting for...GO PLAY!!!';
            return;
        }

        if (true) {
            if (this.seconds === 0) {
                this.seconds = 60;
            }
            this.updateBox('seconds');
        }

        if (this.seconds === 59) {
            if (this.minutes === 0) {
                this.minutes = 60;
            }
            this.updateBox('minutes');
        }

        if (this.seconds === 59 && this.minutes === 59) {
            if (this.hours === 0) {
                this.hours = 24;
            }
            this.updateBox('hours');
        }

        if (this.seconds === 59 && this.minutes === 59 && this.hours === 23) {
            this.updateBox('days');
        }
    },

    updateBox: function (name) {
        $('countdown-' + name).innerHTML = --this[name];
    }

};

