// ==================================================================
function myXmlSlideShow(){
// ------------------------------------------------------------------
// les propriétés :
// ------------------------------------------------------------------
	this.width=200;	
	this.height=200;
	this.xml='none';
	this.id='myXmlSlideShow';
	this.divToReplace='none';
	this.duration=1000;
	this.slides=false;
	this.index=0; // l'index de l'image en cours
// ------------------------------------------------------------------
// les méthodes
// ------------------------------------------------------------------
	var getXhr = function( ){
		if(window.XMLHttpRequest){return new XMLHttpRequest();}
		else if(window.ActiveXObject){
			try{return new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e){return new ActiveXObject("Microsoft.XMLHTTP");}
		}else{
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour");
	    		return false;
		}
	}
// ------------------------------------------------------------------
	this.start=function(){
		if( this.xml=='none' || this.divToReplace=='none' ){
			return false;
		} // erreur .... on ne fait rien !
		
		// ensuite on charge le xml
		var index=this.index;
		var id=this.id;
		var width=this.width;
		var height=this.height;
		var duration=this.duration;
		var divToReplace=this.divToReplace;
		var req=getXhr();
		req.onreadystatechange=function( ){
			if(req.readyState == 4 || req.readyState=="complete"){ 
				var xmlDoc=req.responseXML.documentElement;	
				this.slides=xmlDoc.getElementsByTagName('image');
				if( this.slides.length<1 ){ return false; }
				var tabSlide=new Array();
				for(var i=0; i<this.slides.length; i++){
					tabSlide[i]=this.slides[i].getAttribute('src');
				}
				suite( index, id, width, height, duration, tabSlide, divToReplace );
			}
		}
		req.open("get", this.xml, true );
		req.setRequestHeader("Content-type", "text/html; charset:UTF-8");
		req.send( null );
	}	
// ------------------------------------------------------------------
	var suite = function( index, id, width, height, duration, tabSlide, divToReplace ){
		// créer le conteneur :
		var tank=document.createElement('div');
		tank.id=id;
		tank.style.width=width+"px";
		tank.style.height=height+"px";
		// supprime le texte alternatif et autres enfants ...
		var div=document.getElementById( divToReplace );
		div.innerHTML="";

		// on y met le 'tank'
		div.appendChild( tank );
//		div.parentNode.insertBefore( tank, div );
//		div.parentNode.removeChild( div );

		// départ de la boucle d'affichage
		loadImage( index, id, width, height, duration, tabSlide );
	}
// ------------------------------------------------------------------
	var loadImage=function( i, id, width, height, duration, slides ){
		var img=new Image();
		img.src=slides[i];
		img.id=id+"_slide_"+i;
		// suite verification du chargement de l'image
		// et fonctions suivantes
		checkImageLoaded( img, id, width, height, duration, slides );
	}
// ------------------------------------------------------------------
	var checkImageLoaded = function( img, id, width, height, duration, slides ){
		if( img.complete ){
			setImage( img, id, width, height, duration, slides );
		}else{
			setTimeout( function(){ checkImageLoaded( img, id, width, height, duration, slides ); } , 10 );
		}
	}
// ------------------------------------------------------------------
	var opacite = function( img, opa_ ){
		if(document.all && !window.opera){ 
			img.style.filter = "alpha(opacity=" + opa_ + ");" 
		}else{ 
			var Val = opa_/100; 
			img.style.setProperty( "-moz-opacity", Val, ""); 
			img.style.setProperty( "-khtml-opacity", Val, ""); 
			img.style.setProperty( "opacity", Val, ""); 
		} 
	}
// ------------------------------------------------------------------
	var setImage = function( img, id, width, height, duration, slides ){
		// verfier les dimensions
		var w=img.width;
		var h=img.height;
		var q=w/h;
		// si la largeur dépasse la fenetre
		if( w>width ){
			img.width=width;
			img.height=width*h/w;
		}
		var w2=img.width;
		var h2=img.height
		if( h2>height ){
			img.height=height;
			img.width=height*w2/h2;
		}
		// si moins large on met une marge à gauche
		if( img.width<width ){
			img.style.marginLeft=(width-img.width)/2+"px";
		}
		// opacite à zéro
		opacite( img, 0 );
		// on append 
		document.getElementById( id ).appendChild( img );
		// on fait le fade in
		fadeIn( 0, img, id, width, height, duration, slides );
	}
// ------------------------------------------------------------------
	var fadeIn=function( cur, img, id, width, height, duration, slides ){
		var opa = cur+3;
		opacite( img, opa );
		if( opa>=100 ){
			if( slides.length==1 ){ return false; }
			setTimeout( function(){fadeOut( opa, img, id, width, height, duration, slides );}, duration );
			// dans ce cas timer pour le fadeOut
		}else{
			setTimeout( function(){ fadeIn( opa, img, id, width, height, duration, slides );} , 15 );
		}
	}
// ------------------------------------------------------------------
	var fadeOut=function( cur, img, id, width, height, duration, slides ){
		var opa = cur-3;
		opacite( img, opa );
		if( opa<=0 ){
			// recupère l'id de l'image et passe à la suivante
			var imgId=img.id.split('_');
			var nextId=Number(Number(imgId[2])+1);
			if(nextId==slides.length){nextId=0;}
			loadImage( nextId, id, width, height, duration, slides );
			img.parentNode.removeChild(img);
		}else{
			setTimeout( function(){ fadeOut( opa, img, id, width, height, duration, slides );} , 15 );
		}
	}
// ------------------------------------------------------------------
// ------------------------------------------------------------------
}
// ==================================================================

