/*=============================================================================
script :        anim_image.js
auteur :        Rémi Buisson
contenu :       Script Javascript : fonction permettant l'animation d'image : changer une image toutes les X ms
											
date creation : 15/09/2008

modifications :
=============================================================================*/
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function start_anim_image(nom_image, width_max, height_max, tab_url_lien_image, tab_src_image, temps_en_ms, repertoire){
// initialisation de l'animation pour l'image <nom_image>, <tab_url_lien_image> tableau des url, <tab_src_image> tableau des url image

	if (get_version() >= 3){
		
		var height_reduc = height_max - 50;
		var width_reduc = width_max - 70;
		
		document.getElementById('calk_image_'+nom_image).innerHTML = "";
		//document.write('<center><a href="'+tab_url_lien_image[0]+'" id="a_'+nom_image+'" ><img src="'+tab_src_image[0]+'" alt="Winnershorse" title="Winnershorse" name="'+nom_image+'" width="'+width_max+'" height="'+height_max+'"></a><center>');
		defil_image(nom_image, width_max, height_max, tab_url_lien_image, tab_src_image, 0, temps_en_ms, repertoire);
	}
	else{
		document.write('<a href="'+tab_url_lien_image[0]+'" target="_blank" title="Publicit&eacute;"><img src="'+tab_src_image[0]+'" alt=\"Winnershorse\" title=\"Winnershorse\" name=\"'+nom_image+'\" width=\"\" heigth=\"\"></a>')
	}
}
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function modif_image(nom_image,width_max, height_max, src_image, url_lien_image, repertoire){
// change l'image <nom_image> en lui donnant la valeur de <src_image>
// change le lien de l'image <nom_image> en lui donnant la valeur de <url_lien_image>


	var ligne_code = "";
	
	
	
	if(url_lien_image != "") { ligne_code += '<a href="'+url_lien_image+'" target="_blank" title="Publicit&eacute;">'; } 
	if(get_extension(src_image) == "swf") {
				
		ligne_code += afficher_flash("./upload/"+repertoire+"/"+src_image, width_max, height_max);
	}
	else {
		
		ligne_code += '<img src="./upload/'+repertoire+'/'+src_image+'" alt="Winnershorse" title="Winnershorse" name="'+nom_image+'" width="'+width_max+'" height="'+height_max+'" />';
	}
	if(url_lien_image != "") {ligne_code += '</a>';}


	document.getElementById("calk_image_"+nom_image).innerHTML = ligne_code;
//	document.getElementById("calk_image_"+nom_image).innerHTML = document.getElementById("calk_image_"+nom_image).innerHTML+ligne_code;
	
	
//	longueurCible = document.getElementById("calk_image_"+nom_image).firstChild.length;
//	document.getElementById("calk_image_"+nom_image).firstChild.replaceData(0, longueurCible, ligne_code);
	
//	alert(ligne_code);
	
	
	//document.images[nom_image].src = src_image;
	//document.getElementById('a_'+nom_image+'').href = url_lien_image;
	
	
//	var tab_taille = get_style_taille_image(nom_image, width_max, height_max);	
//	document.images[nom_image].style.width = tab_taille[0];
//	document.images[nom_image].style.height = tab_taille[1];
}
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function defil_image(nom_image, width_max, height_max, tab_url_lien_image, tab_src_image, cpt, temps_en_ms, repertoire){
	// on peut observer qu'après le premier passage tab_src_image[0] est égale au premier caractrèe de tab_src_image, comme s'il s'agissait d'une chaine ...	
	// tab_src_image (array) se transforme en chaine apres premier passage ?

	// Pour pallier au problème : on recréé le tableau
		
	//		alert(tab_src_image+ ' ' +tab_src_image[0]); // pour débug 
	old_tab_src_image = tab_src_image.toString();
	var reg=new RegExp("[ ,]+", "g");
	var tab_src_image=old_tab_src_image.split(reg);
	//		alert(tab_src_image+ ' ' +tab_src_image[0]); // pour débug
	
	
	//		alert(tab_url_lien_image+ ' ' +tab_url_lien_image[0]); // pour débug
	old_tab_url_lien_image = tab_url_lien_image.toString();
	var reg=new RegExp("[ ,]+", "g");
	var tab_url_lien_image=old_tab_url_lien_image.split(reg);
	//		alert(tab_url_lien_image+ ' ' +tab_url_lien_image[0]); // pour débug
	
	if (cpt == tab_url_lien_image.length ){ // si on est en bout de tableau, on reviens au début
	
		cpt = 0;
	}
	if (get_version() >= 3){
		
		modif_image(nom_image, width_max, height_max, tab_src_image[cpt], tab_url_lien_image[cpt], repertoire); // on modifie l'image
		cpt++;
		
		setTimeout("defil_image('"+nom_image+"', '"+width_max+"', '"+height_max+"', '"+tab_url_lien_image+"', '"+tab_src_image+"', '"+cpt+"', '"+temps_en_ms+"', '"+repertoire+"')", temps_en_ms); // on passe à la suivante dans temps_en_ms ms
	}
}
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function get_version(){
// renvoie la version du navigateur

	return navigator.appVersion.substring(0,1); 
}
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function get_style_taille_image(nom_image, width_max, height_max) {
// on determine si la taille de l'image est trop grande => on la fixe à plus petit (pour que ca dépce pas)

	var width = document.images[nom_image].width;
	var height = document.images[nom_image].height;
	
	var tab_taille = new Array;
	
	tab_taille[0] = '';
	tab_taille[1] = '';
	
	if(width >= height) {// si paysage
	
		if ( width > width_max ) {
		
			tab_taille[0] = width_max+'px';
		}
	}
	else { // si portrait
	
		if ( height > height_max ) {
		
			tab_taille[1] = height_max+'px';
		}
	}
	return tab_taille; 
}
//--------------------------------------------------------------------------------------------
//
//
//--------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------
function afficher_flash(fichier_swf, width, height) {

var ligne_code_flash = '';
ligne_code_flash += '<object wmode="transparent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="'+width+'" height="'+height+'">';
ligne_code_flash += '<param name="movie" value="'+fichier_swf+'"> ';
ligne_code_flash += '<param name="quality" value="high">';
ligne_code_flash += '<param name="bgcolor" value="#000000">';
ligne_code_flash += '<param name="wmode" value="transparent">';
ligne_code_flash += '<!--[if !IE]> <-->';
ligne_code_flash += '<object wmode="transparent" data="'+fichier_swf+'" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash">';
ligne_code_flash += '<param name="quality" value="high">';
ligne_code_flash += '<param name="bgcolor" value="#000000">';
ligne_code_flash += '<param name="wmode" value="transparent">';
ligne_code_flash += '<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer">';
ligne_code_flash += 'Erreur (le navigateur devrait renvoyer un contenu flash).';
ligne_code_flash += '</object>';
ligne_code_flash += '<!--> <![endif]-->';
ligne_code_flash += '</object>';

//ligne_code_flash = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='"+width+"' height='"+height+"'> <param name='movie' value='"+fichier_swf+"'> <param name='quality' value='high'> <embed src='"+fichier_swf+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+width+"' height='"+height+"'></embed> </object> ";

return ligne_code_flash;
}
