﻿Type.registerNamespace('GlobeInternational.Controls.Constructors');

GlobeInternational.Controls.Constructors.CountdownTimer = function(EndDate) {
    var self = this;
    this.dateEnd = new Date(EndDate);
    this.countdownTimerBox = document.getElementById('countdownTimerBox');
    
    this.init = function() {
        this.timer();
    }
    
    this.setDay = function(v) { document.getElementById('countdownTimerDays').innerHTML = v; }
    this.setHour = function(v) { document.getElementById('countdownTimerHours').innerHTML = v; }
    this.setMinute = function(v) { document.getElementById('countdownTimerMinutes').innerHTML = v; }
    this.setSecond = function(v) { document.getElementById('countdownTimerSeconds').innerHTML = v; }
    
    this.getSeconds = function() {
        var diff = new Date(this.dateEnd - (new Date()));
        return Math.round(diff.valueOf() / 1000);
    }
    
    this.addZeros = function(value) {
        return ((value+'').length < 2 ? '0' : '') + value;
    }
    
    this.timer = function() {
        var s = self.getSeconds();

        if(s > 0) 
        {
            self.setDay(self.addZeros(Math.floor(s/86400)));
            self.setHour(self.addZeros(Math.floor(s/3600)%24));
            self.setMinute(self.addZeros(Math.floor(s/60)%60));
            self.setSecond(self.addZeros(s%60));
            
            setTimeout(self.timer, 999);
        }
        else 
        {
            countdownTimerBox.innerHTML = '<div class="countdown_complete">The Copenhagen Event is in progress</div>';
        }
    }
    this.init();
}

Element.observe(window, 'load', function() { new GlobeInternational.Controls.Constructors.CountdownTimer(CountdownTimer_EndDate); });
