var stop = 0;
var neuesbild = "";

function WechselStop() {
	stop = 1;
}


function Wechsel(bild,pfad,bildname,datum) {
	stop=0;
	var a=0;
	var anfang = bild.style.opacity * 100;
	if(bild.src == document.URL+"#") {
		GetBild(pfad,bildname,"v",bild,datum,"vor");
	}
	if(neuesbild != "") {
		//alert("Fade start:"+a);
		fade(bild,anfang,"i");
		}
	else {
		a=a+1;
		window.setTimeout(function () { Wechsel(bild,pfad,bildname,datum); }, 100);
	}
	
	function fade(bild,step,modus) {
			if(stop == 0) {
			
				bild.style.opacity = step/100;
				bild.style.filter = "alpha(opacity=" + step + ")"; // IE
			
				switch(modus) {
					case "i":
						step = step + 1;
						if (step < 100) {
							window.setTimeout(function () { fade(bild,step,"i"); }, 1);
						}
						else {						
							var bild_aktuell = bild.parentNode.getElementsByTagName("img")[1].src.slice(bild.parentNode.getElementsByTagName("img")[1].src.search(/bild=.+&datum/)+5,bild.parentNode.getElementsByTagName("img")[1].src.search(/&datum/));
							GetBild(pfad,bild_aktuell,"v2",bild.parentNode.getElementsByTagName("img")[1],datum,"vor");
							window.setTimeout(function () { fade(bild,step,"o"); }, 300);
						}
					break;
					case "o":
						step = step - 1;
						if (step > 0) {
							window.setTimeout(function () { fade(bild,step,"o"); }, 1);
						}
						else {
							var bild_aktuell = bild.src.slice(bild.src.search(/bild=.+&datum/)+5,bild.src.search(/&datum/));
							GetBild(pfad,bild_aktuell,"v2",bild,datum,"vor");
							
							window.setTimeout(function () { fade(bild,step,"i"); }, 300);
						}
					break;
					default:
					break;
				}
			}
            

        }

}


function GetBild(pfad,bildname,richtung,src,datum,modus) {
	try{
	req = new XMLHttpRequest();
	}
	catch (e){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed){
				req = null;
			}
		}  
	}
	 if (req == null)
		alert("Error creating request object!");

	var url = 'fileadmin/gallery/galerie_wechsel.php?pfad='+pfad+'&bild='+bildname+'&modus='+richtung;
                  req.open("GET", url, true);

                //Beim abschliessen des request wird diese Funktion ausgeführt
                req.onreadystatechange = function () {handleTranslation(pfad,src,datum,modus);};
  
                  req.setRequestHeader("Content-Type",
                                      "application/x-www-form-urlencoded");
                req.send(null);
}


function handleTranslation(pfad,src,datum,modus){            
	switch(req.readyState) {
        case 4:
			if(req.status!=200) {
				alert("Fehler:"+req.status); 
			}else{    
				neuesbild = req.responseText;
				if(modus == "vor")
					src.src = "fileadmin/gallery/galerie_bild.php?pfad="+pfad+"&bild="+neuesbild+"&datum="+datum;
				if(modus == "full") {
					src.src = "fileadmin/gallery/"+pfad+"/"+neuesbild;
					document.getElementById("facebook").href = "http://www.pfaditrogen.ch/library/facebook/index.php?pfad="+pfad+"&bild="+neuesbild;
					document.getElementById("speichern").href = "http://www.eipi1.ch/pfaditrogen/fileadmin/gallery/"+pfad+"/"+neuesbild;
				}
			}
        break;
        default:
            return false;
        break;     
    }
}
