var itemCount;
var itemTotalCount = images.length;
var itemID;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function initTimer() {
	firstItemID = document.getElementById(fileNameToFieldName(images[0]));
	lastItemID = document.getElementById(fileNameToFieldName(images[itemTotalCount - 1]));
		
	itemCount = 0;
	
	document.getElementById('loading').className = 'hide';
	
	firstItemID.className = 'show';
	
	stopTimer();
	startTimer();
}

function stopTimer() {
	if (timerRunning) clearTimeout(timerID);
	timerRunning = false;
}

function startTimer() {
	for (i = 0; i < itemTotalCount; i++) {
		itemID = document.getElementById(fileNameToFieldName(images[i]));
		itemID.className = (i != itemCount) ? 'hide' : 'show';
	}

	itemCount = (itemCount == itemTotalCount - 1) ? 0 : itemCount + 1;
	
	timerRunning = true;
	timerID = self.setTimeout("startTimer()", delay);
}

function fileNameToFieldName(string) { 

	var regex = /^[\w ?!@#$%^&*(){}|\/:;"'<>,.=_+\[\]\\-]+$/;
	if (regex.test(string)) {
		// [?!@#$%^&*(){}|/:;"'<>,.=_+\[\]\\-]
		string = string.replace(/[?!@#$%^*(){}|\/:;"'<>,.=_+\[\]\\-]/g, '')
		string = string.replace(/\s+ /g, ' ')
		string = string.replace(/ /g, '')
		string = string.toLowerCase();
	}
	
	return string;
}