//create onDomReady Event
window.onDOMReady = function(func) {
	//W3C-compliant browser
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", func, false);
	} else {
		//IE
		document.onreadystatechange = function(){
			if (document.readyState == "interactive" || document.readyState == "complete") {
				func();
			}
		}
	}
}

var Animation = {
	config: [
		{
			id			: 0,
			url			: "JavaScript:Animation.restart()",
			//url			: "index.php?show=a762a5ce&page_id=1",
			image		: "media/images/main_1.png",
			show_link	: false,
			delay		: 10000
		},
		{
			id			: 1,
			url			: "index.php?show=a762a5ce&page_id=6",
			image		: "media/images/main_2.png",
			show_link	: true,
			delay		: 7000
		},
		{
			id			: 2,
			url			: "index.php?show=a762a5ce&page_id=7",
			image		: "media/images/main_3.png",
			show_link	: true,
			delay		: 7000
		},
		{
			id			: 3,
			url			: "index.php?show=a762a5ce&page_id=8",
			image		: "media/images/main_5.png",
			show_link	: true,
			delay		: 7000
		},
		{
			id			: 4,
			url			: "index.php?show=a762a5ce&page_id=9",
			image		: "media/images/main_4.png",
			show_link	: true,
			delay		: 7000
		}
	],
	current:				null,
	buffer:					new Array(),
	
	init:			function() {
		for (var i = 0; i < this.config.length; i++) {
			this.config[i].previous	= this.config[i == 0 ? this.config.length - 1 : i - 1];
			this.config[i].next		= this.config[i == this.config.length - 1 ? 1 : i + 1];
		}
		
		this.current = this.config[0];

		for (var i = 0; i < 2; i++) {
			this.buffer[i] = document.getElementById("animation_" + i);
		}
	},
	
	setOpacity:		function(element, opacity) {
		opacity = (opacity == 1 || opacity === '') ? '1' : (opacity < 0.00001) ? 0 : opacity;
		
		// IE
		element.style.filter = "alpha(opacity:" + (opacity * 100) + ")";
		// Safari<1.2, Konqueror
		element.style.KHTMLopacity = opacity;
		// Older Mozilla and Firefox
		element.style.Mozopacity = opacity;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		element.style.opacity = opacity;
	},
	
	fadeOut:		function(element, opacity, value, oncomplete) {
		opacity-= value;
		if (opacity <= 0.0)
			opacity = 0.0;
		this.setOpacity(element, opacity);
			
		if (opacity > 0.0) {
			var _this = this;
			this.fadetimeout = window.setTimeout(function() {
				Animation.fadeOut(element, opacity, value, oncomplete);
			}, 10);
		} else {
			oncomplete();
		}
	},
			
	animate:		function() {		
		var _this = this;

		this.timeout = window.setTimeout(function() {
			_this.animate();
		}, this.current.delay ? this.current.delay : 10000);

		this.current = this.current.next;
		document.getElementById("welcome_screen").className = "welcome_screen button_" + (this.current.id) + "_active";
		document.getElementById("show_link").style.display = this.current.show_link ? "block" : "none";
		document.getElementById("show_link").href = this.current.url;
		
		var front = 0;
		var back = 1;
		
		if (this.buffer[0].style.zIndex < this.buffer[1].style.zIndex) {
			front = 1;
			back = 0;
		}
		
		this.fadeOut(this.buffer[front], 1.0, 0.02, function() {
			_this.buffer[back].style.zIndex = 1;
			_this.buffer[front].style.zIndex = 0;
			Animation.setOpacity(_this.buffer[front], 1.0);
			_this.buffer[front].src = _this.current.next.image;
		});
	},
	
	start:			function() {
		this.timeout = window.setTimeout(function() {
			Animation.animate();
		}, this.current.delay ? this.current.delay : 10000);
	},
	
	show:			function(id) {			
		window.clearTimeout(this.timeout);
		window.clearTimeout(this.fadetimeout);
		
		var front = 0;
		var back = 1;
		
		if (this.buffer[0].style.zIndex < this.buffer[1].style.zIndex) {
			front = 1;
			back = 0;
		}

		var is_still_selected = false;
		for (var i = 0; i < this.config.length; i++) {
			if (this.current.id == id) {
				is_still_selected = i == 0 ? true : false;
				break;
			}
			
			this.current = this.current.next;
		}
		
		document.getElementById("welcome_screen").className = "welcome_screen button_" + (this.current.id) + "_active";
		document.getElementById("show_link").href = this.current.url;

		this.setOpacity(this.buffer[front], 1.0);
		this.buffer[back].src = this.current.image;
		
		var _this = this;
		this.fadeOut(this.buffer[front], 1.0, 0.1, function() {
			_this.buffer[back].style.zIndex = 1;
			_this.buffer[front].style.zIndex = 0;
			Animation.setOpacity(_this.buffer[front], 1.0);
			_this.buffer[front].src = _this.current.next.image;
		});
	},
	
	restart:		function() {
		window.clearTimeout(this.timeout);
		
		this.current = this.config[0];
		
		// Bild setzen
		var front = 0;
		var back = 1;
		
		if (this.buffer[0].style.zIndex < this.buffer[1].style.zIndex) {
			front = 1;
			back = 0;
		}
		this.buffer[front].src = this.current.image;
		
		// show_link und CSS setzen
		document.getElementById("welcome_screen").className = "welcome_screen button_" + (this.current.id) + "_active";
		document.getElementById("show_link").style.display = "none";

		// Animation starten
		this.timeout = window.setTimeout(function() {
			Animation.animate();
		}, this.current.delay ? this.current.delay : 10000);
	}
};

var Guestbook_Animation = {
	init:			function() {
		this.timeout = window.setInterval(function() {
			Effect.toggle('softbox_guestbook', 'appear', {
				duration: 0.5
			});
		}, 10000);
	}
};

var AJAXLoader = {
	load: 			function(url, element) {
		if (!element)
			return;
			
		new Ajax.Request(url, {
			asynchronous: 	false,
			method:			'post',
			onSuccess: 		function(xmlHttpRequest) {
				element.innerHTML = xmlHttpRequest.responseText;
			}
		});
	}
};