// JavaScript Document
var ImgLayers;

window.addEvent("domready", function() {

//function LoadImg() {
	ImgLayers = $("slide").getChildren();
	var count = ImgLayers.length,
		container = new Element("div", {styles: {
			position: "relative"			
		}}).injectBefore(ImgLayers[0]).adopt(ImgLayers);
	if(count < 1) {
		return;	
	}
	
	time_first = (time/2);
		
//		console.log(container);
	var maxW = 0,
		maxH = 0;
		
	ImgLayers.each(function(imgItem) {
		var size = imgItem.getSize();
//		maxW = Math.max(maxW, size.x);
//		maxH = Math.max(maxH, size.y);
		maxW = size.x;
		maxH = size.y;
		
		imgItem.set({styles: {
			position: "absolute",
			left: 0,
			top: 0,
			display: "block",
			opacity: 0
		}});												   
	});
	
	ImgLayers[0].setStyle("opacity", '1');
//	new Fx.Style(ImgLayers[0], 'opacity').set(1);
	
	if(maxW == 0 && maxH == 0) {
		maxW = 660;
		maxH = 420;
	}
/*	
	container.setStyles({
		width: maxW,
		height: maxH
	});
*/	
	if(show == 'extended') {
		number = 0;
		window.setTimeout(initImgRand.pass(number), time_first);
	}
	else if(show == 'normal') {
		window.setTimeout("initImg()", time_first);
	}
});

i = 1;
function initImg() {
	var count = ImgLayers.length;
	
	imgBefore = i-1;
	if(i == count) { i = 0; }
	img = i;
	
/*
	ImgLayers[imgBefore].get("tween", {property: "opacity", duration: 5000}).cancel().start(0).chain(function() {
		this.setStyle("display","none");																											 
	}.bind(ImgLayers[imgBefore]));
*/
		
//	ImgLayers[img].setStyle('display', 'block').get("tween", {property: "opacity", duration: 5000}).cancel().start(1);
//console.log(fade_time);

	var myEffectsOut 	= new Fx.Style(ImgLayers[imgBefore], 'opacity', {duration: fade_time});
	var myEffectsIn 	= new Fx.Style(ImgLayers[img], 'opacity', {duration: fade_time});

//	if(imgBefore >= 0) {
	//ImgLayers[imgBefore].get("tween", {property: "opacity", duration: fade_time}).cancel().start(0);	
//	new Fx.Style(ImgLayers[imgBefore], 'opacity').start(1, 0);
	myEffectsOut.start(1, 0);
//	}
	
//	ImgLayers[img].get("tween", {property: "opacity", duration: fade_time}).cancel().start(1);
//	new Fx.Style(ImgLayers[img], 'opacity').start(0, 1);
	myEffectsIn.start(0, 1);
	
//	console.log(count);
//	console.log(i);
	i++;
	
	window.setTimeout("initImg()", time);
}

function initImgRand(before) {
	var count = ImgLayers.length;
	count = count-1;
	
	i = zufallsZahl(0, count);
	
	if(i == before) { 
		if(before < count) {
			i = before+1; 
		}
		else if(before >= count && before >= 1) {
			i = before-1;	
		}
		else if(before == 0) {
			i = 1;	
		}
		else if(i == count) {
			i = count-1;	
		}
	}

	imgBefore = before;
	img = i;
	
//	ImgLayers[imgBefore].get("tween", {property: "opacity", duration: fade_time}).cancel().start(0);
	var myEffectsOut 	= new Fx.Style(ImgLayers[imgBefore], 'opacity', {duration: fade_time});
	var myEffectsIn 	= new Fx.Style(ImgLayers[img], 'opacity', {duration: fade_time});

//	new Fx.Style(ImgLayers[imgBefore], 'opacity').start(1, 0);
	myEffectsOut.start(1, 0);
	
//	ImgLayers[img].get("tween", {property: "opacity", duration: fade_time}).cancel().start(1);
//	new Fx.Style(ImgLayers[img], 'opacity').start(0, 1);
	myEffectsIn.start(0, 1);
	
//	window.setTimeout("initImgRand(" + img + ")", time);
	window.setTimeout(initImgRand.pass(img), time);
}

function zufallsZahl(from, to) {
      from = parseInt(from, 10) || 0;
      to = parseInt(to, 10) || 100;
      return Math.round((Math.random() * (to - from) ) + from);
}


