/* JS-Code für das Handling der Countdown Counter */

var _counter = { };

function initializeCounter(counterId, initCount, oncompletefunc) {
	if (counterId) {
		_counter[counterId] = { 
			c : initCount ? initCount : 30,
			timeout : null,
			oncomplete : oncompletefunc ? oncompletefunc : null
		};
		stepCounter(counterId);
	}
}

function stepCounter(counterId) {
	if (!_counter[counterId])
		return;
	
	var counter = _counter[counterId];
	if(counter.c == 0) {
		if (counter.oncomplete) {
			var oncomplete = new Function(counter.oncomplete);
			oncomplete();
		}
	} 
	else {
		counter.c--;
		updateCounterComponent(counterId);
		counter.timeout = window.setTimeout("stepCounter('" + counterId + "');", 1000);
	}
}

function clearCounter(counterId) {
	if (!_counter[counterId])
		return;
	
	var counter = _counter[counterId];
	counter.c = 0;
	if (counter.timeout)
		window.clearTimeout(counter.timeout);
	updateCounterComponent(counterId);
}

function updateCounterComponent(counterId) {
	if (!_counter[counterId])
		return;
	var counter = _counter[counterId];
	var component = document.getElementById(counterId);
	if (component && component.firstChild) {
		component.removeChild(component.firstChild);
		component.appendChild(document.createTextNode(counter.c));
	}
}
