// configuration
var MoveDuration = 7;
var FadeDuration = 2;
var slidshowWidth = 750;
var slidshowHeight = 400;

// vars
var imageX, imageY, nextImage;
var loadIndex = 0;
var displayIndex = 0;
var slideShowIsRunning = false;
var images = new Array();

// methodes
function fadeOut(id) {
	var fadeOutAnim = new YAHOO.util.Anim(id);
	fadeOutAnim.attributes.opacity = { to: 0};
	fadeOutAnim.duration = FadeDuration;
//	fadeOutAnim.method = YAHOO.util.Easing.easeIn;
	fadeOutAnim.onStart.subscribe(showNextImage);
	fadeOutAnim.animate();
}

function fadeOutAtEnd() {
	var id = this.getEl().id;
	var startLag = (MoveDuration-FadeDuration) * 1000;
	setTimeout("fadeOut('"+id+"');", startLag);
}

function pan(id) {
	var moveAnim = new YAHOO.util.Anim(id);
	var image = document.getElementById(id);
	image.style.top = "";
	image.style.right = "";
	image.style.bottom = "";
	image.style.left = "";
	var randomNumber=Math.floor(Math.random()*4);
	var toX = -image.width + slidshowWidth;
	var toY = -image.height + slidshowHeight;
	var debugBox = document.getElementById("debugText");
	if (debugBox) {
		debugBox.innerHTML = "toX: "+toX+"; toY: "+toY;
	}
	
	if (randomNumber < 1) {
		image.className = "bottomright";
		moveAnim.attributes.right = { to: toX };
		moveAnim.attributes.bottom = { to: toY };
	} else if (randomNumber < 2) {
		image.className = "bottomleft";
		moveAnim.attributes.left = { to: toX };
		moveAnim.attributes.bottom = { to: toY };
	} else if (randomNumber < 3) {
		image.className = "topleft";
		moveAnim.attributes.left = { to: toX };
		moveAnim.attributes.top = { to: toY };
	} else {
		image.className = "topright";
		moveAnim.attributes.right = { to: toX };
		moveAnim.attributes.top = { to: toY };
	}
	moveAnim.duration = MoveDuration;
//	moveAnim.method = YAHOO.util.Easing.easeOut;
	moveAnim.onStart.subscribe(fadeOutAtEnd);
	moveAnim.animate();
}

function fadeIn(id) {
	var fadeInAnim = new YAHOO.util.Anim(id);
	fadeInAnim.attributes.opacity = { to: 1};
	fadeInAnim.duration = 2;
	//fadeInAnim.method = YAHOO.util.Easing.easeOut;
	fadeInAnim.animate();
}

function loadNextImage() {
	if (imagesData[loadIndex]) {
		var debugBox = document.getElementById("debugText");
		if (debugBox) {
//			debugBox.innerHTML = "loading image no. "+loadIndex;
		}
		var imageData = imagesData[loadIndex];
		var image = new Image(imageData['width'],imageData['height']);
		image.src = imageData['src'];
		image.alt = imageData['alt'];
		if (imageData['url']) {
			image.url = imageData['url'];
		}
		image.onclick = 
		image.id = "slide"+loadIndex;
		images[loadIndex] = image;
//		alert("loaded image "+image.src+" (pos "+loadIndex+")");
		loadIndex++;
		if (!slideShowIsRunning) {
			image.onload = startShowAndLoadNextImage;
		} else {
			image.onload = loadNextImage;
		}
	} else {
//		var debugBox = document.getElementById("debugText");
//		debugBox.innerHTML = "No more pix found at position "+loadIndex + " therefore no of images: "+imagesData.length;
	}
}

function startShowAndLoadNextImage() {
	slideShowIsRunning = true;
	loadNextImage();
	showNextImage();
}

function showNextImage() {
	var debugBox = document.getElementById("debugText");
	if (debugBox) {
//		debugBox.innerHTML = "Displaying picture "+ (displayIndex+1) +" of "+images.length + ": "+images[displayIndex];
	}
	var box = document.getElementById("slideShowLink");
	if (!document.getElementById("slide"+displayIndex)) {
		box.appendChild(images[displayIndex]);
	}
	pan(images[displayIndex].id);
	fadeIn(images[displayIndex].id);
	if (images[displayIndex].url) {
		box.href = images[displayIndex].url;
	} else {
		box.href = "#";
		if (debugBox) {
//			debugBox.innerHTML = debugBox.innerHTML + " (no link!)";
		}
	}
	var slideShowCaptionBox = document.getElementById("slideShowCaption");
	if (slideShowCaptionBox && images[displayIndex].alt) {
		slideShowCaptionBox.innerHTML = images[displayIndex].alt;
	}

	displayIndex++;
	if (!images[displayIndex]) {
		displayIndex = 0;
	}
}

function loadAllImages() {
	images = new Array();
	loadImage(loadIndex);
}

function runSlideShow() {
	loadNextImage();
}