var imgIndex = 0;
var repeatInterval = 3500;

function switchImg(option) {
	imgObj = document.getElementById('img0');
	imgNumObj = document.getElementById('imgNum');

	if(option == 1){
		if (imgIndex == 0) imgIndex = imgNames.length-1;
		else imgIndex-- ;
		imgObj.src = imgNames[imgIndex];
		imgNumObj.innerHTML = imgIndex + 1;
	}	else if(option == 2){
		if (imgIndex == imgNames.length-1) imgIndex = 0;
		else imgIndex++ ;
		imgObj.src = imgNames[imgIndex];
		imgNumObj.innerHTML = imgIndex + 1;
	}
	
	return false;
}


function startFade(mode) {
	 if((typeof document.getElementById('img0') != 'undefined') && (typeof document.getElementById('img1') != 'undefined') && (typeof imgList != 'undefined')){
		//debug("Anzahl: "+imgList[0].length);
	
		img0Obj = document.getElementById('img0');
		img1Obj = document.getElementById('img1');
		imgNumObj = document.getElementById('imgNum');
		
		img1Obj.style.display = "block";
		//wurde ein "mode" uebergeben? (1 prev ; 2 next)
		if (!isNaN(mode)) {
			//debug("ID ("+mode+") wurde uebergeben");
			window.clearTimeout(schleife);
			if(mode == 1){
				imgIndex = imgIndex - 2;
				if(imgIndex == -1)imgIndex = imgList.length - 1;
				if(imgIndex == -2)imgIndex = imgList.length - 2;
			}
			img0Obj.src = imgList[imgIndex];
			img1Obj.src = imgList[imgIndex];
			
			imgNumObj.innerHTML = imgIndex + 1;
		}
		fade(102);
	}
}

function fade(step) {
	// wenn nichts uebergeben wurde, dann step auf 0 setzen:
	step = step || 0;
		
	//debug("step ("+step+") wurde uebergeben");	

	if (step <= 100){
		// Bild-Deckkraft auf Null setzen und von 0% (step = 0) erhöhen auf 100% (step 100) - Achtung: 1. Zeile bremst iPad/iPod:
		img1Obj.style.opacity = step/100;
		img1Obj.style.filter = "alpha(opacity=" + step + ")"; // old IE
		img1Obj.style.zIndex = 2;
		

		// Text austauschen 
		if ((step == 50)){
			imgNumObj.innerHTML = imgIndex + 1;
		}

		step = step + 2;
		schleife = window.setTimeout(function () { fade(step); }, 40);
	}
	else {
		// Hintergrundbild austauschen (gleich wie Vordergrund)
		img0Obj.src = img1Obj.src;

		// Bild transparent machen:
		img1Obj.style.opacity = 0;
		img1Obj.style.filter = "alpha(opacity=0)"; // IE

		// naechstes Bild:
		imgIndex++;
		if(imgIndex >= imgList.length)imgIndex = 0;
		img1Obj.src = imgList[imgIndex];
		schleife = window.setTimeout('fade()', repeatInterval);
	}
}




function debug(m){
	if(typeof console != 'undefined') console.log(m);
}
