/* 
   Banners Countdown script 
*/
var jsCountdown = function(){
	
        var self = this;

        this.time_left = 10; //number of seconds for countdown
	this.output_element = $$('.cntd');
	this.keep_counting = 1;
	this.no_time_left_message = 'Completed!';
         
	this.countdown = function() {
		if(this.time_left < 2) {
			this.keep_counting = 0;
		}
 
		this.time_left = this.time_left - 1;
	};
 
	this.add_leading_zero = function(n) {
		if(n.toString().length < 2) {
			return '0' + n;
		} else {
			return n;
		}
	};
 
	this.format_output = function() {
		var daystr, days, hours, minutes, seconds;
		seconds = this.time_left % 60;
		minutes = Math.floor(this.time_left / 60) % 60;
		hours   = Math.floor(this.time_left / 3600);
                
                daystr = '';
                if(hours >= 24) {
                    days = Math.floor(hours/24);
                    daystr = (days > 1)? days + " " + days_string : days + " " + day_string;
                    hours = hours - days*24;
                }
                    
		seconds = this.add_leading_zero( seconds );
		minutes = this.add_leading_zero( minutes );
		hours = this.add_leading_zero( hours );
 
		return daystr + ' ' + hours + ':' + minutes + ':' + seconds;
	};
 
	this.show_time_left = function() {
                this.time_string = this.format_output();
              
		this.output_element.innerHTML = this.time_string;//time_left;
	};
 
	this.no_time_left = function() {
		this.output_element.innerHTML = this.no_time_left_message;
	};

        this.count = function () {
                this.countdown();
                this.show_time_left();
        };
        
        this.timer = function () {
                this.count();

                if(this.keep_counting) {
                        setTimeout(function() { self.timer(); }, 1000)
                        //setTimeout(this.timer, 1000);
                } else {
                        this.no_time_left();
                }
        };
        
        this.init = function (element) {

                this.time_left = element.lang;
                
                this.output_element = element;
                this.timer();
        };

};
