window.onload = initAll;

var imageArray = new Array("1.jpg", "2.jpg", "3.jpg");
var currNextImg = 1;
var imageInterval = 5000;
var artwork1, artwork2;
var artworkFadeIn, artworkBehind;
var ontop = 2;
var fadeInOpa = 0;

function initAll(){
	for(var i = 1; i <= 6; i++) {
		if(document.getElementById("menu" + i)){
			document.getElementById("menu" + i).onmouseover = linkActive;
			document.getElementById("menu" + i).onmouseout = linkPassive;	
		}
	}
	if(document.getElementById("artwork1") && document.getElementById("artwork2")) {
		artwork1 = document.getElementById("artwork1");
		artwork2 = document.getElementById("artwork2");
		imageTimer = setTimeout("nextImage()", imageInterval);
	}
}

function linkActive(evt) {
	var e = (evt) ? evt.target : window.event.srcElement;
	e.style.top = "-40px";
}

function linkPassive(evt) {
	var e = (evt) ? evt.target : window.event.srcElement;
	e.style.top = "0px";
}

function nextImage(){
	setArtworks();
	updateCurrNextImg();
	setTimeout("fadeImages()", 100);
}

function setArtworks() {
	artworkFadeIn = (ontop == 1) ? artwork1 : artwork2;
	artworkBehind = (ontop == 2) ? artwork1 : artwork2;
	artworkBehind.style.zIndex = 1;
	artworkFadeIn.style.zIndex = 2;
}

function updateCurrNextImg() {
	currNextImg = (currNextImg+1)%(imageArray.length);
}

function fadeImages() {
	fadeInOpa += 5;
	artworkFadeIn.style.opacity = (fadeInOpa/100);
	artworkFadeIn.style.filter = "alpha(opacity=" + fadeInOpa + ")";
	if(fadeInOpa < 100) {
		setTimeout("fadeImages()", 50);
	} else {
		fadeInOpa = 0;
		artworkBehind.style.opacity = 0;
		artworkBehind.style.filter = "alpha(opacity=0)";
		setTimeout("updateNextImgSource()", 100);
	}			
}

function updateNextImgSource() {
	artworkBehind.src = "images/rotation/" + imageArray[currNextImg];
	ontop = (ontop == 1) ? 2 : 1;
	artworkBehind.onload = function() {
		setTimeout("nextImage()", imageInterval);
	};
}

