/**
 *  JavaScript Source file, Slash
 */

function PixalCounter (settings){
	this.time = settings.startTime;
	this.targetSpan = settings.targetSpan;
	this.timeOverMessage = settings.timeOverMessage;
	this.timeOverSpan = settings.timeOverSpan;
	this.autoReloadURL = settings.autoReloadURL;
	this.status = 1;
}

PixalCounter.prototype.beat = function(){
	this.time = this.time - 1;
	document.getElementById(this.targetSpan).innerHTML = this.formatTime();
	if (this.time <= 0){
		document.getElementById(this.timeOverSpan).innerHTML = this.timeOverMessage;
		document.getElementById(this.targetSpan).innerHTML = '';
		if (this.autoReloadURL){
			window.location = this.autoReloadURL;
		}
	}
}

PixalCounter.prototype.formatTime = function(){
	var hours = Math.floor(this.time/3600);
	var minutes = Math.floor(this.time/60)%60;
	var seconds = this.time%60;
	
	if (seconds < 10)
		seconds = '0'+seconds;
	if (hours > 0){
		if (minutes < 10)
			minutes = '0'+minutes;
		return hours + ":" + minutes+":"+seconds;
	} else {
		return minutes+":"+seconds;
	}
	
}

PixalCounter.prototype.isActive = function(){
	return this.time>0;
}

var timers = new Array();

/**
@deprecated use initTimers instead
*/
function initForm(){
	initTimers();
}

function initTimers(){
	window.setInterval("fireTimers()",1000);
}

function fireTimers(){
	for (var i = 0; i < timers.length; i++){
		if (timers[i].isActive())
			timers[i].beat();
	}
}

function initClock(hours, minutes, seconds){
	document.clockDate = new Date();
	document.clockDate.setHours(hours);
	document.clockDate.setMinutes(minutes);
	document.clockDate.setSeconds(seconds);
		
	clockTick();
	window.setInterval("clockTick()", 1000);
}

function initClock(){
	document.clockDate = new Date();
	clockTick();
	window.setInterval("clockTick()", 1000);
}


function clockTick(){
	
	document.clockDate.setTime(document.clockDate.getTime()+1000);
	var yeDate = document.clockDate; 
	var minutes = yeDate.getMinutes();
	if (minutes < 10)
		minutes = "0"+minutes;
	var seconds = yeDate.getSeconds();
	if (seconds < 10)
		seconds = "0"+seconds;
	document.getElementById("clockSpan").innerHTML = ""+yeDate.getHours()+":"+minutes+":"+seconds;
}
