
var obTimer = null;
var index = 1;
var iTimer = 5000;
var b_isFading = false;
var b_userClicked = false;
var g_curOpac = 0;

function setOpacity(ob, opac) {
	g_curOpac = opac;
	ob.style.opacity = opac / 100;
	ob.style.MozOpacity = opac / 100;
	ob.style.KhtmlOpacity = opac / 100;
	ob.style.filter = "alpha(opacity=" + opac + ")";
}

function do_fadeIn(str_image, duration) {

	var iTime = duration / 10;
	var curOpac = 0;
	var ob_image = document.getElementById(str_image);

	if (g_curOpac < 100) {
		g_curOpac += 10;
		setOpacity(ob_image, g_curOpac);
		setTimeout("do_fadeIn('" + str_image + "', " + duration + ")", duration);
	}
	else {
		fadeFinished();
	}
}

function fadeIn(ob_div_out, ob_div_in, ob_image, str_image, duration) {
	b_isFading = true;
	setOpacity(ob_div_in, 0);
	ob_image.src = str_image;
	do_fadeIn(ob_div_in.id, duration);
}

function fadeFinished() {

	//preload next image.
	if (index < arr_images.length) {
		var obImage = new Image();
		obImage.src = arr_images[index];
	}

	var ob_div_out = document.getElementById("div_img_out");
	var ob_image   = document.images["photo"];
	ob_div_out.style.backgroundImage = "url(" + ob_image.src + ")";

	b_isFading = false;
	if (b_userClicked) {
		nextImage();
		b_userClicked = false;
		return;
	}

	obTimer = setTimeout(nextImage, iTimer);
}

function nextImage() {

	if (index >= arr_images.length)
		index = 0;

	if (obTimer != null) {
		clearTimeout(obTimer);
		obTimer = null;
	}

	if (b_isFading) {
		b_userClicked = true;
		return;
	}

	fadeIn(document.getElementById("div_img_out"), document.getElementById("div_img_in"), document.images["photo"], arr_images[index], 75);
	++index;
}

var arr_images = new Array();

window.onload = function() {
	if (arr_images.length != 0)
		obTimer = setTimeout(nextImage, iTimer);
}

//preload next image
if (arr_images.length > 1) {
	var obImage = new Image();
	obImage.src = arr_images[1];
}

