
var Slideshow = {

	objElement1 	: "",
	objElement2 	: "",

	transElement1	: 100,
	transElement2	: 0,

	// 0 - Element 1 zu Element 2
	// 1 - Element 2 zu Element 1
	direction		: 0,

	fadeSpeed		: 10,
	switchDelay		: 5000,

	lastId			: null,

	startFading : function () {

		// Element 2 holen
		$.ajax(
		{
			url: "/schuetzling.php",
			data: "last="+encodeURIComponent(this.lastId),
			type: "POST",
			success: this.respSchuetzling
		});

		// Element 2 aktivieren
		this.objElement2.style.display	= 'block';

		// Transition setzen
		this.setTransition();

		setTimeout("Slideshow.fade();", this.switchDelay);

	},

	fade : function (from, to) {

		this.setTransition();

		if(this.direction == 0)
		{
			// from element 1 to element 2
			this.transElement1	-= 1;
			this.transElement2	+= 1;

			if(this.transElement1>0)
			{
				setTimeout("Slideshow.fade();", this.fadeSpeed);

				return;
			}

			// direction ändern
			this.direction	= 1;

		} else if (this.direction == 1) {

			// from element 2 to element 1
			this.transElement1	+= 1;
			this.transElement2	-= 1;

			if(this.transElement2>0)
			{
				setTimeout("Slideshow.fade();", this.fadeSpeed);

				return;
			}

			// direction ändern
			this.direction	= 0;
		}

		// Ok, fade ist durch; Element  2 aktualisieren
		$.ajax(
		{
			url: "/schuetzling.php",
			data: "last="+encodeURIComponent(this.lastId),
			type: "POST",
			success: this.respSchuetzling
		});

		setTimeout("Slideshow.fade();", this.switchDelay);
	},

	setTransition : function () {

		// Gecko Engines
		this.objElement1.style.MozOpacity	= this.transElement1 / 100;
		this.objElement2.style.MozOpacity	= this.transElement2 / 100;

		// Opera
		this.objElement1.style.opacity		= this.transElement1 / 100;
		this.objElement2.style.opacity		= this.transElement2 / 100;

		// Internet Explorer 4 - 7
		this.objElement1.style.filter		= 'alpha(opacity='+this.transElement1+')';
		this.objElement2.style.filter		= 'alpha(opacity='+this.transElement2+')';
		// Internet Explorer ab 8
		this.objElement1.style.filter		= 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + this.transElement1 + ')';
		this.objElement2.style.filter		= 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + this.transElement2 + ')';

		// KHTML
		this.objElement1.style.KhtmlOpacity		= this.transElement1 / 100;
		this.objElement2.style.KhtmlOpacity		= this.transElement2 / 100;

		return;
	},

	respSchuetzling : function (response) {

		if(response!='')
		{
			var data	= eval("("+response+");");

			// letzte Id vermerken, dass diese nicht nochmal als nächstes
			// geholt wird
			Slideshow.lastId	= parseInt(data.id);

			// Daten schreiben
			if(Slideshow.direction == 0)
			{
				var obj	= Slideshow.objElement2;
			}

			if(Slideshow.direction == 1)
			{
				var obj	= Slideshow.objElement1;
			}

			$(obj).html('<div style="width:152px;"><img src="/gfx/bewohner/ausgewaehlte_schuetzlinge/'+data.bild+'" border="0" alt="'+data.name+'" /></div><div style="width:152px;"><b>'+data.name+'</b><br />'+data.kurztext+'</div>');

			return;
		}

		alert("Es trat leider ein Problem auf.");

		return;
	}
}

// ausführen, wenn document vollständig geladen wurde
$(document).ready(function()
{
	Slideshow.objElement1	= document.getElementById("schutzlingA");
	Slideshow.objElement2	= document.getElementById("schutzlingB");

	Slideshow.startFading();
});

