// Slideshow
var availableImages = [
	"/images/slideshow/7.jpg",
	"/images/slideshow/1.jpg",
	"/images/slideshow/2.jpg",
	"/images/slideshow/3.jpg",
	"/images/slideshow/4.jpg"
];

var availableLinks = [
	"news.php5#Babi-Plus",
	"products.php5?fibre_optic_superled",
	"products.php5?clamps_and_cutters",
	"products.php5?phototherapy",
	"products.php5?reusable"
];

var currentIndex = 0;
var rotationSpeed = 4000;
var fadeLength = 1000;
var timerInstance;
var moviePlaying = true;

function updateslideshow(imgId) {
	 fadeOut(100, function () {
	 	currentIndex = imgId;
	 	switchSlides();
	 });
}

function switchSlides() {
	 var slide = document.getElementById("slide");
	 var backSlide = document.getElementById("backSlide");
	 slide.src = null;
	 changeOpacity(100);
	 slide.src = backSlide.src;
	 backSlide.src = availableImages[(currentIndex + 1) % availableImages.length];
	 updateAdLink(currentIndex);
}

function fadeOut(currentOpacity, callback) {
	if (currentOpacity > 0) {
		changeOpacity(currentOpacity);
		setTimeout(fadeOut, fadeLength / 100, currentOpacity - 1, callback);
	} else {
		changeOpacity(0);
		callback();
	}
}

function changeOpacity(opacity) {
	 var realImage = document.getElementById("slide");
	 realImage.style.opacity = (opacity / 100);
	 realImage.style.filter = "alpha(opacity=" + opacity + ")";
	 realImage.style.MozOpacity = (opacity / 100);
	 realImage.style.KhtmlOpacity = (opacity / 100);
}

function updateAdLink(imgId) {
	 var obj = document.getElementById("adLink");
	 obj.href = availableLinks[imgId];
}

var firstRun = true;

function rotatePhoto() {
	if (firstRun!=true) {
		getNextPhoto();
	}
	timerInstance = setTimeout("rotatePhoto()", rotationSpeed);
	firstRun = false;
}

function getNextPhoto() {
	 updateslideshow((currentIndex + 1) % availableImages.length);
}

function stopTimer() {
	 clearTimeout(timerInstance);
}

function startTimer() {
	timerInstance = setTimeout("rotatePhoto()", rotationSpeed);
}
