/*
$ /js/slideshow.js | 2007/12/05 13:28 | 2007/12/05 14:32 $
*/

var current = 1;
var next = 2;

var slideshow_counter = 0;
var slideshow_speed = 10;
var slideshow_wait = 5000; // 5000
var slideshow_wait_click = 6500; // 6500

var slideshow_clicked = 0;
var button_changed = 0;


/* init */
function init() {
/*	// Umgelagert direkt ins Modul (Ladezeit)
	for (var i = 1; i <= 5; i++) {
		if (document.getElementById("slideshow_" + i)) {
			window["button_" + i] = document.getElementById("button_" + i);

			if (i == 1)
				window["button_" + i].className = "button_1_active";;
			window["button_" + i].style.display = "block";
		}
	}
*/

	for (var i = 1; i <= 5; i++) {
		if (document.getElementById("slideshow_" + i)) {
			window["slideshow_" + i] = document.getElementById("slideshow_" + i);

			if (i == 1) {
				window["slideshow_" + i].style.display = "block";
				window["slideshow_" + i].xOpacity = 1;
			}
			else {
				window["slideshow_" + i].style.display = "none";
				window["slideshow_" + i].xOpacity = 0;
			}

			slideshow_counter++;
		}
	}

	if (slideshow_counter > 1)
		timeout_object = setTimeout(fade, slideshow_wait - slideshow_wait * 0.8);
}


/* fade */
function fade() {
	object_next = document.getElementById("slideshow_" + next);;

	for (var i = 1; i <= slideshow_counter; i++) {
		actual_object = document.getElementById("slideshow_" + i);

		if (i == next) {
			if (actual_object.xOpacity < 1)
				actual_object.xOpacity += 0.05;
			actual_object.style.display = "block";
		}
		else {
			if (actual_object.xOpacity > 0)
				actual_object.xOpacity -= 0.05;
		}

		setOpacity(actual_object);
	}


	if (object_next.xOpacity >= 0.3 && button_changed == 0 && slideshow_counter > 2) {
		button_changed = 1;

		for (var i = 1; i <= slideshow_counter; i++) {
			actual_button = document.getElementById("button_" + i);

			if (i == next)
				actual_button.className = "button_" + i + "_active";
			else
				actual_button.className = "button_" + i;
		}
	}


	if (object_next.xOpacity >= 1) {
		button_changed = 0;

		if (slideshow_clicked == 1) {
			slideshow_wait_actual = slideshow_wait_click;
			slideshow_clicked = 0;
		}
		else {
			slideshow_wait_actual = slideshow_wait;
		}

		for (var i = 1; i <= slideshow_counter; i++) {
			actual_object = document.getElementById("slideshow_" + i);

			if (i == next) {
				actual_object.xOpacity = 1;
			}
			else {
				actual_object.xOpacity = 0;
				actual_object.style.display = "none";
			}
		}


		if (current >= slideshow_counter)
			current = 1;
		else
			current++;

		if (next >= slideshow_counter)
			next = 1;
		else
			next = current + 1;


		timeout_object = setTimeout(fade, slideshow_wait_actual);
	}
	else {
		timeout_object = setTimeout(fade, slideshow_speed);
	}
}


/* selected slideshow element */
function select_slideshow_element(element_number) {
	if (timeout_object)
		clearTimeout(timeout_object);

	next = element_number;
	if (next == 1)
		current = slideshow_counter;
	else
		current = next - 1;

	slideshow_clicked = 1;

	for (var i = 1; i <= slideshow_counter; i++) {
		actual_button = document.getElementById("button_" + i);

		if (i == next)
			actual_button.className = "button_" + i + "_active";
		else
			actual_button.className = "button_" + i;
	}

	timeout_object = setTimeout(fade, 1);
}


/* set element opacity */
function setOpacity(e) {
	if (e.xOpacity > 1) {
		e.xOpacity = 1;
		return;
	}

	e.style.opacity = e.xOpacity;
	e.style.MozOpacity = e.xOpacity;
	e.style.KhtmlOpacity = e.xOpacity;
	e.style.filter = "alpha(opacity="+(e.xOpacity*100)+")";
}



/* attach to onload event */
if (window.addEventListener)
	window.addEventListener("load", init, false);
else
	window.attachEvent("onload", init);
