/**
 * Función que aplicará eventos para aplicar eventos de cambio de imagen en un diaporama de imágenes
 * Requiere la librería jquery
 * @link http://jquery.com/
 * @author Xan Torres <xan@icare-net.com>
 */
jQuery.noConflict();
jQuery(document).ready(function($) {
	
	// IMPORTANTE: las imágenes pequeñas deben estar dentro de un enlace <a> con el atributo href asignado a la versión grande
	
	var main_div		= ".bloc_actu_image";	// Id/clase del contenedor de las imágenes
	var main_img		= "#bloc_actu_image_photoprincipale img";		// Id/clase de la foto principal
	var mini 			= ".mini";				// Clase de las fotos pequeñas
	var max_size		= 300;					// Tamaño máximo de las imágenes que se mostrarán en el cuadro de la imagen principal
	var veloc			= 150;				// Velocidad del efecto al cambiar de imagen (slow, normal, fast o el número de milisegundos), 0 para deshabilitar el efecto
	
	
	// INICIO CÓDIGO -->

	changeImg = function(photo,img) {
		
		filename = function(src){return src.substring(src.lastIndexOf('/')+1)};
		if (filename(photo.attr("src")) != filename(img.src)) {
			var width = img.width;
			var height = img.height;
			var rapport = width / height;
			if (width > max_size && width >= height) {
				width = max_size;
				height = max_size / rapport;
			} else if (height > max_size && width <= height) {
				height = max_size;
				width = max_size * rapport;
			}
			$(photo).fadeOut(veloc, function(){
				$(this).attr({
					"src"	: img.src,
					"height": height,
					"width"	: width });
				$(this).fadeIn(veloc);
			});
		}
	}

	var maxi_img	= [];
	var mini_img	= [];
	$(main_div,document).each(function(i) {
		var cont	 	= this;
		maxi_img[i] 	= new Image();		// Precarrega imatge principal
		maxi_img[i].src = $(main_img,this).attr("src");
		mini_img[i]		= [];
		
		// Mouse over
		$(mini, this).each(function(j){ 
			mini_img[i][j] = new Image();	// Precarrega imatge petita
			mini_img[i][j].src = $(this).closest("a").attr("href");
			$(this).hover(function(){			
				changeImg($(main_img, cont), mini_img[i][j]);
			});
		});
		
		// Mouse out
		$(main_img, cont).hover(function(){
			changeImg($(main_img, cont), maxi_img[i]);
			
		});
		$(cont).hover(null,function(){ 		
			changeImg($(main_img, cont), maxi_img[i]);
		});
	});
	
	// <-- FIN CÓDIGO
});