window.addEventListener ? window.addEventListener("load", so_init, false) : window.attachEvent("onload", so_init);

// set the starting image.			
var d = document, image_slide = new Array(), i = 0, NumOfImages = 0, pause = false;

// The time to wait before moving to the next image. Set to 3 seconds by default.
var wait = 3000;
var dur = 1.0;

function so_init() {
	// The array of div names which will hold the images.
	image_slide = d.getElementById("cont_img").getElementsByTagName("img");
	
	// The number of images in the array.
	NumOfImages = image_slide.length;
	
	$(image_slide[0]).appear({ duration: dur});
	startSlideShow();
}

// The Fade Function
function SwapImage(x,y) {
	$(image_slide[x]).appear({ duration: dur});
	$(image_slide[y]).fade({ duration: dur});
}

// the onload event handler that starts the fading.
function startSlideShow() {
	play = setInterval('playPause()', wait);
	d.getElementById('playpause').className = "pause";
	d.getElementById('playpause').innerHTML = "Pause";
}

function setPause() {
	if (pause != false) {
		pause = false;
		clearInterval(play);
		playPause();
		startSlideShow();
	}
	else {
		pause = true;
		d.getElementById('playpause').className = "play";
		d.getElementById('playpause').innerHTML = "Play";
	}
}

function playPause() {
	if (pause != true) {
		var imageShow, imageHide;
		imageShow = i + 1;
		imageHide = i;
			
		if (imageShow == NumOfImages) {
			SwapImage(0, imageHide);	
			i = 0;
			
			if (next_project <= last) d.location.href = "?id=" + id + "&project=" + next_project;
			else d.location.href = "?id=" + id + "&project=1";
		}
		else {
			SwapImage(imageShow, imageHide);			
			i ++;
		}
	}
	else {
		clearInterval(play);	
	}
}
/*
d.onkeydown = function keyCheck(event) {
	var keyID = (window.event) ? window.event.keyCode : event.keyCode;
	
	switch(keyID) {
		case 37: prevImg();
		break;
		case 39: nextImg();
		break;
   }
   return false;
}
*/

function nextImg() {
	pause = true;
	clearInterval(play);
	d.getElementById('playpause').className = "play";
	d.getElementById('playpause').innerHTML = "Play";
	
	var imageShow, imageHide;
	imageShow = i + 1;
	imageHide = i;
	
	if (imageShow == NumOfImages) {
		SwapImage(0, imageHide);
		i = 0;
			
		if (next_project <= last) d.location.href = "?id=" + id + "&project=" + next_project;
		else d.location.href = "?id=" + id + "&project=1";
	}
	else {
		SwapImage(imageShow, imageHide);
		i ++;
	}
}

function prevImg() {
	pause = true;
	clearInterval(play);
	d.getElementById('playpause').className = "play";
	d.getElementById('playpause').innerHTML = "Play";
	var imageShow, imageHide;
	imageShow = i - 1;
	imageHide = i;
	
	if (imageShow >= 1) {
		SwapImage(imageShow, imageHide);
		i --;
	}
	else {
		SwapImage(0, imageHide);
		i = 0;
			
		if (prev_project >= 1) d.location.href = "?id=" + id + "&project=" + prev_project;
		else d.location.href = "?id=" + id + "&project=" + last;

	}
}