// JavaScript Document

// config
arrayFadeIn = new Array();
var imgPath = "bilder/slide/";
var anzahlBilder = 60;
var anzahlTd = 20;
//$("table.slideTable img:nth-child(1)").fadeIn('slow');
	
$(window).load(function()
	{
			// Bilder der Tabelle ausblenden
			$('table.slideTable tr td img').hide();
			
			// bestimmen in welcher zufallsreihenfolge die bilder aufgerufen werden (beim start)
			var durchlaeufe = 0;
			while (durchlaeufe<anzahlTd)
			{
				var zufallszahl = Math.round(Math.random() * anzahlTd)+1; // Zufallszahl erzeugen
				// Falls Zahl doppelt erscheint oder grer als max. Zahl wird solange eine neue Zufallszahl gebildet bis es passt
				while (in_array(arrayFadeIn,zufallszahl) || zufallszahl>anzahlTd) zufallszahl = Math.round(Math.random() * anzahlTd)+1;
				// Korrekte Zufallszahl ins Array einfgen
				arrayFadeIn.push(zufallszahl);
				durchlaeufe++;
			}
	
			// Ladebalken Sequenz
			var loaderInit = $('div.loader').css('width');
			if ( loaderInit == '1px' )
			{
				$('div.loader').animate({ width: '383px' }, 3000, function() { $('div.loaderBg').fadeOut(1000,
					function() {
						//console.log(arrayFadeIn);
						fadeinTds(0); 
					}
					)
				 });
			}
			

 		
			
		} // ende function
); // ende document.ready

var angezeigteBilder = ['1','2','3','23','55','5','6','22','8','9','10','39','12','13','14','25','26','45','60','17'];


function fadeinTds(i)
{

	$('.td'+arrayFadeIn[i]+' img').fadeIn(500); 
	
	setTimeout(function()
	{ 
		i++;
		if (i<arrayFadeIn.length-1)  fadeinTds(i);  // nchstes Bild einblenden
		else $('.td'+arrayFadeIn[i]+' img').fadeIn(500, function() { fadeoutAll(); }); // alle eingeblendet => alle gleichzeitig ausblenden
	}, 200);
	
} // ende fadeinTds



function fadeoutAll()
{
	/*setTimeout(function()
	{
		$('table.slideTable tr td img').fadeOut(800);
	}, 3000);*/
	
	setTimeout(function()
	{
		$('table.slideTable tr.tr1 td img').fadeOut(800);
		setTimeout(function() {    $('table.slideTable tr.tr2 td img').fadeOut(800);    }, 400);
		setTimeout(function() {    $('table.slideTable tr.tr3 td img').fadeOut(800);    }, 800);
		setTimeout(function() {    $('table.slideTable tr.tr4 td img').fadeOut(800);    }, 1200);
	}, 3000);
	
	setTimeout(function()
	{
		// nachdem alle Bilder ausgefadet sind ...
		zufallsBilder();
	}, 5400);
	
} // ende fadeoutAll


function zufallsBilder()
{
	// Reihenfolge TDs neu mischen
	arrayFadeIn = new Array();
	// bestimmen in welcher zufallsreihenfolge die bilder aufgerufen werden
	var durchlaeufe = 0;
	//console.log(durchlaeufe+" + "+anzahlTd);
	while (durchlaeufe<anzahlTd)
	{
		var zufallszahl = Math.round(Math.random() * anzahlTd)+1; // Zufallszahl erzeugen
		// Falls Zahl doppelt erscheint oder grer als max. Zahl wird solange eine neue Zufallszahl gebildet bis es passt
		while (in_array(arrayFadeIn,zufallszahl) || zufallszahl>anzahlTd) zufallszahl = Math.round(Math.random() * anzahlTd)+1;
		// Korrekte Zufallszahl ins Array einfgen
		arrayFadeIn.push(zufallszahl);
		durchlaeufe++;
	}
	//console.log("zufallsreihenfolge = "+durchlaeufe);


	arrayBilder = new Array();
	
	// alle Bilder der Tabelle werden neu vergeben
	var counter = 1;
	while (counter<=anzahlTd) // fr alle TDs
	{
		zufallsBild = Math.round(Math.random() * anzahlBilder)+1;
		while (zufallsBild>anzahlBilder || in_array(angezeigteBilder,zufallsBild))
		{
			zufallsBild = Math.round(Math.random() * anzahlBilder)+1;
		}
		arrayBilder.push(zufallsBild); // Kontrollarray fr nchsten Durchlauf
		angezeigteBilder.push(zufallsBild); // Array zur Kontrolle ob doppelte Bilder benutzt werden
		if (zufallsBild<10) zufallsBild = '0'+zufallsBild;
		$('.td'+counter+' img').attr('src',imgPath+zufallsBild+'.jpg'); // neues Bild wird der TD zugewiesen
		counter++;
	}
	angezeigteBilder = arrayBilder;
	//console.log(angezeigteBilder);
	fadeinTds(0);
	
} // ende zufallsBilder


function in_array(a,p){
 for (x=0;x<a.length;x++)
  if (a[x] == p) return true
 return false
}
