$(document).ready(function(){
				
	function recup_nom(src) {
		
		var nom = "";
		
		/* Récupération du nom de l'image entre un / et le point ou si pas de / depuis le début de la chaine */
		var point = src.lastIndexOf(".");
		var sep = src.lastIndexOf("/");
		
		if(sep == -1) sep = 0
		else sep = sep + 1;

		nom = src.substring(sep,point);
		
		// Suppression de la mention indiquant la taille de l'image (l, m ou s)
		/*nom = nom.substring(0,nom.length - 1); */
		
		return nom;
	}
	
	function recup_ext(src) {
		
		var ext = "";
		
		/* Récupération de l'extension de l'image */
		var point = src.lastIndexOf(".");
		
		ext = src.substring(point + 1,src.length);
		
		return ext;
	}
	
	function recup_chemin(src) {
		
		var chemin = "";
		
		/* Récupération du chemin de l'image (tout ce qui précède le nom de l'image */
		var nom = recup_nom(src);
		var pos = src.lastIndexOf(nom);
		
		chemin = src.substring(0,pos);
		
		return chemin;
	}
	
	function recup_id(chaine) {
		
		var id = "";
		
		/* Récupération de l'identifiant de la réalisation */
		var pattern = chaine.lastIndexOf("_");
		
		id = chaine.substring(pattern + 1,chaine.length);
		
		return id;
	}
	
	function pause() {
		
		//
		
	}
	
	$("a[class='diapo'] >img").click(function () {
		
		var chaine = $(this).parents("div[id^=location_]").attr("id");
		var id_location = recup_id(chaine);
		
		var id_zoom = $("a[class^=zoom] >img").attr("class");
		
		/* Attributs diapo*/
		var imgsrc_d = $(this).attr("src");
		var width_d = $(this).attr("width");
		var height_d = $(this).attr("height");
		var nom_d = recup_nom(imgsrc_d);
		var ext_d = recup_ext(imgsrc_d);
		var chemin_d = recup_chemin(imgsrc_d);
		var class_d = $(this).attr("class");
		var id = $(this).attr("class");
		
		/* Attributs Zoom */
		var imgsrc_z = $("a[class=zoom" + id_zoom + "] >img").attr("src");
		var width_z = $("a[class=zoom" + id_zoom + "] >img").attr("width");
		var height_z = $("a[class=zoom" + id_zoom + "] >img").attr("height");
		var nom_z = recup_nom(imgsrc_z);
		var ext_z = recup_ext(imgsrc_z);
		var chemin_z = recup_chemin(imgsrc_z);
		var class_z = $("a[class=zoom" + id_zoom + "] >img").attr("class");        
        
        $(this).attr("src", chemin_z + nom_z + '.' + ext_z);
        $(this).attr("class", class_z);
        
        // Récupération et affichage de la légende
        var resultat = '&nbsp;';
		$.ajax(
		{
		        url: "ajax.locationsphotos.php",
		        dataType: "text",
		        data: {id_photo:id},
		        async: false,
		        success: function(data)
		        {
		                resultat = data;
		        }
		});
		$("p[class=legende" + id_location + "]").html(resultat);
        
		// Modification de la taille des images
		var rapport_w;
		var rapport_h;
		var width_div_d = $(this).parents("div[class=bordureImgCadre]").css("width");
		var height_div_d = $(this).parents("div[class=bordureImgCadre]").css("height");
		var width_div_z = $("a[class=zoom" + id_zoom + "]").parents("div[class=bordureImgCadre]").css("width");
		var height_div_z = $("a[class=zoom" + id_zoom + "]").parents("div[class=bordureImgCadre]").css("height");
		width_div_d = width_div_d.substring(0,width_div_d.indexOf("px",width_div_d));
		height_div_d = height_div_d.substring(0,height_div_d.indexOf("px",height_div_d));
		width_div_z = width_div_z.substring(0,width_div_z.indexOf("px",width_div_z));
		height_div_z = height_div_z.substring(0,height_div_z.indexOf("px",height_div_z));		
		
		var zoom_img = chemin_d + nom_d + '.' + ext_d;
		var new_id_zoom = class_d;
		$("a[class=zoom" + id_zoom + "] >img").attr("src", zoom_img);
        $("a[class=zoom" + id_zoom + "]").attr("href",zoom_img);
        $("a[class=zoom" + id_zoom + "] >img").attr("class",new_id_zoom);
        $("a[class=zoom" + id_zoom + "]").attr("class","zoom" + new_id_zoom);
        
		//alert(" DIV zoom : " + width_div_z + " * " + height_div_z + "\n\nDIV diapo : " + width_div_d + " * " + height_div_d + "\n\n Photo zoom : " + width_z + " * " + height_z + "\n\n Photo diapo : " + width_d + " * " + height_d);
		
		// Rapports Images
		rapport_z = width_z / height_z;
		rapport_d = width_d / height_d;
		
		// Rapports div
		rapport_div_z = width_div_z / height_div_z;
		rapport_div_d = width_div_d / height_div_d;
		
		//alert("rapport Z : " + rapport_z + "\n\n Rapport D : " + rapport_d);
		if( ( rapport_z > 1 && rapport_d > 1 ) || ( rapport_z < 1 && rapport_d < 1 ) ) {
			
			var width_zoom = width_z;
			var height_zoom = height_z;
			var width_diapo = width_d;
			var height_diapo = height_d;			
				
		} else {
			
			rap_w = ( width_div_z - 4) / ( width_div_d - 4);
			rap_h = ( height_div_z - 4) / ( height_div_d - 4);	
				
			var width_diapo = width_z / rap_w;
			var height_diapo = height_z / rap_h;
			
			var width_zoom = width_d * rap_w;
			var height_zoom = height_d * rap_h;
			
		}
		
		
		$(this).attr("width", width_diapo);
		$(this).attr("height", height_diapo);
		$("a[class=zoom" + new_id_zoom + "] >img").attr("width", width_zoom);
		$("a[class=zoom" + new_id_zoom + "] >img").attr("height", height_zoom);

		

	});
	
});


