/*
	
	BASADO EN :
	
	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

*/


/* CONTROL DE LOS THUMBS EN HORIZONTAL*/

jQuery.fn.sliderFilms = function( settings ) {
    return this.each(function() {
		jQuery.fn.sliderFilms.run( jQuery( this ), settings );
    });
};

jQuery.fn.sliderFilms.run = function( $this, settings ) {
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	var numSeleccionado;
	
	function CargaDetalle(thumb_num){
		jQuery( li[numSeleccionado] ).removeClass("seleccionado");
		numSeleccionado = Number(thumb_num);
		jQuery( li[numSeleccionado] ).addClass("seleccionado");
		id_noticia = jQuery( li[thumb_num] ).attr("id").split("_")[1]; // Variable que pasaremos al control para saber que noticia hemos de cargar	
		$.ajax({
			url: "inc/control_ajax_slider.asp?thumb_num="+id_noticia,
			success: function(datos){
				$('#foto_fondo_seleccionada').stop();
				
				$('#pieFoto').stop();
				$('#pieFoto').animate({"top": "-50px"}, 400)
				$('#pieFoto_fondo').stop();
				$('#pieFoto_fondo').animate({"top": "-50px"}, 400)
				
				$('#foto_fondo_seleccionada').fadeOut(600, function(){			
					$('#inicio_contenedor_films').html(datos)
					$('#foto_fondo_seleccionada').css("display","none");
					$('#foto_fondo_seleccionada').ready(function() {
						$('#foto_fondo_seleccionada').stop()
						$('#foto_fondo_seleccionada').fadeIn(600);
						
						
						$('#pieFoto').stop()
						$('#pieFoto').css("top","-50px");
						$('#pieFoto').animate({"top": "0px"}, 400)
						
						$('#pieFoto_fondo').stop()
						$('#pieFoto_fondo').css("top","-50px");
						$('#pieFoto_fondo').animate({"top": "0px"}, 400)
					});									
				})							
			}
		});
	}
	
	CargaDetalle(0)
	
	$(li).click(function(){	
		CargaDetalle($(li).index($(this)));      
	})

	if ( li.length > settings.slideBy ) {
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		var liWidth = jQuery( li[0] ).width();
		var animating = false;
		ul.css( "width", ( li.length * liWidth ) );
		
		$next.click(function() {
			if ( !animating ) {
				// Cambiamos al siguiente			
				if((numSeleccionado+1) < li.length){
					CargaDetalle(numSeleccionado+1);			
				}
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				TMP_Total = Math.abs(parseInt( ul.css( "left" ) )) + liWidth * settings.numvisible;								
				if ( numSeleccionado >= settings.numvisible && TMP_Total <  ul.width()) {					
					$back.css( "display", "block" );
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) <= liWidth * settings.numvisible ) {
							$next.css( "display", "block" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		
		$back.click(function() {
			if ( !animating ) {
				// Cambiamos al anterior	
				if(numSeleccionado > 0){
					CargaDetalle(numSeleccionado-1);
				}
				
				animating = true;
				offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );

				if(Math.abs(parseInt( ul.css( "left" ))) > liWidth * numSeleccionado){
					$next.css( "display", "block" );
					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) == 0 ) {
							$back.css( "display", "block" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$next.css( "display", "block" )			
	}
};

