//DiaShow Klasse
var d4pc_Misc_DiaShow = Class.create();
//Klasse implementieren
d4pc_Misc_DiaShow.prototype = {	

	initialize:function (id1,id2,dur){
		this.ID1=id1;
		this.ID2=id2;
		this.ActiveID=id2;
		this.FXDuration = dur;
		this.DiaShowPosition = 0;
	},
	
	toggleActiveID:function(itsme){
		if (itsme.ActiveID==itsme.ID1){
			itsme.ActiveID=itsme.ID2;
		}
		else{
			itsme.ActiveID=itsme.ID1;
		};
	},
	
	DiaFadeIn:function(id,dur){
		new Effect.Appear(id,{duration: dur});		
	},
	
	DiaFadeOut:function(id,dur){
		new Effect.Fade(id,{duration: dur, sync:true});
	},
	
	DiaFadeInOut:function(id_1, id_2,dur){
		new Effect.Parallel(
    		[ 
			 	new Effect.Fade(id_1,{sync: true}),							  
      		  	new Effect.Appear(id_2,{sync: true})
			],
			{duration: dur,
			delay:(dur)});
	},
	
	//onSuccess für DiaUpdate
	//Evaluiert die RPC-Antwort vom Server und wertet diese aus.
	//Die einzelnen Bilder der gewünschten DiaShow liegen danach im Array "Images".
	//Der Updater für die Darstellung wird erstellt (DiaShowUpdater) und gestartet.
	//Er führt periodisch die Funktion DiaShowUpdate aus.
	DiaUpdate_onSuccess:function(transport,json,itsme){
		//Evaluieren der RPC-Rückgabe.
		var rpc= eval ('('+transport.responseText+')');
		itsme.Images = eval ('('+rpc.result+')');
		//Mehr als ein Bild im Ergebins?
		if (itsme.Images.length>1){
			itsme.DiaShowUpdate(itsme);
			new PeriodicalExecuter(function(){itsme.DiaShowUpdate(itsme);},(itsme.FXDuration*2));
		};
	},
	
	DiaShowUpdate:function(itsme){
		
		//$(itsme.ActiveID).src=(itsme.Images[itsme.DiaShowPosition]);
		
		if (itsme.ActiveID == itsme.ID1){
			$(itsme.ActiveID).src=(itsme.Images[itsme.DiaShowPosition]);
			itsme.DiaFadeInOut(itsme.ID2, itsme.ID1,itsme.FXDuration);
		}
		else{
			$(itsme.ActiveID).src=(itsme.Images[itsme.DiaShowPosition]);
			itsme.DiaFadeInOut(itsme.ID1,itsme.ID2,itsme.FXDuration);
		};		
		
		if (itsme.DiaShowPosition<(itsme.Images.length-1)){
			itsme.DiaShowPosition += 1;
		}
		else{
			itsme.DiaShowPosition = 0;
		};
		
		itsme.toggleActiveID(itsme);		
		
	},
	
	runDiaShow:function (show,itsme){
		var r=Hash.toQueryString({RPC:'{"method":"getImages","DiaShow":"'+show+'","id":'+(new Date().getTime())+'}'});
		var request =Hash.toQueryString({command:r});
		new Ajax.Request("/system/classes/d4pc/Misc/DiaShow/api.php",
						{
							method:'get',
							parameters:request,
							onSuccess:function(transport,json){
								itsme.DiaUpdate_onSuccess(transport,json,itsme);
							}
						}
		);
	},
	
	preloadAllImages:function (show,itsme){
		var r=Hash.toQueryString({RPC:'{"method":"getImages","DiaShow":"'+show+'","id":'+(new Date().getTime())+'}'});
		var request =Hash.toQueryString({command:r});
		new Ajax.Request("/system/classes/d4pc/Misc/DiaShow/api.php",
						{
							method:'get',
							parameters:request,
							onSuccess:function(transport,json){itsme.preloadAllImages_onSuccess(transport,json,itsme);}
						}
		);
	},
	
	preloadAllImages_onSuccess:function(transport,json,itsme){
		//Evaluieren der RPC-Rückgabe.
		var rpc= eval ('('+transport.responseText+')');
		itsme.Images = eval ('('+rpc.result+')');
		//Mehr als ein Bild im Ergebins?
		//if (itsme.Images.length>1){
			itsme.loadImages(itsme.Images);
		//};
	},
	
	decode_utf8:function (utftext) {
             var plaintext = ""; var i=0; var c=c1=c2=0;
             // while-Schleife, weil einige Zeichen uebersprungen werden
             while(i<utftext.length)
                 {
                 c = utftext.charCodeAt(i);
                 if (c<128) {
                     plaintext += String.fromCharCode(c);
                     i++;}
                 else if((c>191) && (c<224)) {
                     c2 = utftext.charCodeAt(i+1);
                     plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
                     i+=2;}
                 else {
                     c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
                     plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
                     i+=3;}
                 }
             return plaintext;
    },
	
	loadImages:function(imgs){
		document.tempimage = Array();
		for (i=0;i<imgs.length;i++){			
			document.tempimage[i]=new Image();
			document.tempimage[i].src=imgs[i];
		};		
	}
};
