var loaded = 0;

function nextRota() {
	if(pages.length > (current + 1)) {
		current++;
	} else {
		current = 1;
	}
	loadImage();
	startFade(-20, 'frontImg2', 80);
	setTimeout('updateFront()', 1000);
	setTimeout('nextRota()', 7500);
}

function loadImage() {
	setBg('frontImg1', '/img/rota/' + pages[current][0] + '.jpg')
	document.getElementById('frontLink').href = pages[current][3];
	
	if(pages.length > (current + 1)) {
		setSrc('preloadImage', '/img/rota/' + pages[(parseInt(current) + 1)][0] + '.jpg');
	} else if(current != 1) {
		setSrc('preloadImage', '/img/rota/' + pages[1][0] + '.jpg');
	}
}

function startFade(direction, identifier, speed) {
	el = document.getElementById(identifier);
	if(el) {
		if(!el.style.opacity) {
			el.style.opacity = 1;
		}
		fade(direction, identifier, el.style.opacity, speed);
	} else {
		alert("No element");
	}
}

function fade(direction, identifier, start, speed) {
	el = document.getElementById(identifier);
	value = ((start * 100) + direction);
	
	el.style.opacity = (value / 100);
	el.style.filter = 'alpha(opacity=' + value + ')';
		
	if((direction > 0 && start < 1) || (direction < 0 && start > 0)) {
		setTimeout('fade(' + direction + ', "' + identifier + '", ' + (value / 100) + ', ' + speed + ')', speed);
	}
}

function updateFront() {
	setBg('frontImg2', '/img/rota/' + pages[current][0] + '.jpg');
	startFade(20, 'frontImg2', 5);
	setInner('captionBottom', '<b>' + pages[current][1] + '</b> - ' + pages[current][2]);
}

function setSrc(id, value) {
	document.getElementById(id).src = value;
}

function setOpac(id, value) {
	el = document.getElementById(id);
	el.style.opacity = (value / 100);
	el.style.filter = 'alpha(opacity=' + value + ')';
}

function setInner(id, html) {
	document.getElementById(id).innerHTML = html;
}

function setBg(id, image) {
	document.getElementById(id).style.background = '#fff url(' + image + ') left top no-repeat';
}

function checkOpacity(identifier) {
	el = document.getElementById(identifier);
	if(el) {
		if(!el.opacity) {
			return 1;
		} else {
			return el.opacity;
		}
	} else {
		return false;
	}
}