jQuery.fn.carrusel = function(){
	
	// VARIABLES
	var intervaloCarrusel;
	var listaEnlaces = $(this).find("a");
	var cantidadEnlaces = listaEnlaces.length;
	var fotoActual = 0;
	var tiempoTransicion = 2000;
	var tiempoPausa = 4000;
	var indiceZ = cantidadEnlaces;
	var intervaloEnMarcha = true;
		
	// INIT
	$(listaEnlaces).each( function(){ $(this).css({ display:"block", height:"372px", width:"293px", position: "absolute", left:"0px", top:"0px", zIndex: indiceZ }); indiceZ--; });
	indiceZ = cantidadEnlaces;
	$(".prevPic").click(prevImage);
	$(".nextPic").click(nextImage);
	$(".playPause").click(playPauseImage);
	$(this).parent().hover(muestraTip, ocultaTip);
	
	clearInterval(intervaloCarrusel);
	lanzaIntervalo();
	
	// INTERVALO
	function lanzaIntervalo(){
		intervaloCarrusel = setInterval(nextImage, tiempoPausa);
	}
	
	// CONTROLES NAVEGACIÓN
	function prevImage(){
		if (fotoActual>0){ fotoActual--; } else { fotoActual=cantidadEnlaces-1; }
		muestraFoto();	
		return false;
	}
	function nextImage(){
		if (fotoActual<cantidadEnlaces-1){ fotoActual++; } else { fotoActual=0; }
		muestraFoto();
		return false;
	}
	function playPauseImage(){
		if (intervaloEnMarcha){
			intervaloEnMarcha = false;
			clearInterval(intervaloCarrusel);
			$(this).css({ backgroundPosition:"-30px 0px" });
		} else{
			intervaloEnMarcha = true;
			lanzaIntervalo();
			$(this).css({ backgroundPosition:"-15px 0px" });
		}
		return false;
	}
	// CARRUSEL
	function muestraFoto(){
		clearInterval(intervaloCarrusel)
		indiceZ++;
		$(listaEnlaces[fotoActual]).css({ zIndex:"20" }).hide().fadeIn(tiempoTransicion, function(){ for (i=0;i<listaEnlaces.length;i++){
																																													if(i!=fotoActual){  $(listaEnlaces[i]).css({ zIndex:"1" });																																											}
																																												}
																																										});
		if (intervaloEnMarcha){
			lanzaIntervalo();
		}
	}
	// LUPA
	function muestraTip(){
		$("#carruselFotos #controlesCarrusel").css({ zIndex:"50"}).animate({ bottom: "0px" }, 300);
	}
	function ocultaTip(){
		$("#carruselFotos #controlesCarrusel").animate({ bottom: "-21px" }, 300);
	}
}

