/*
	
	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");
		
		idCarga = $('#ulNovedadesEditoriales #'+numSeleccionado+' a').attr("id")		
		
		$.ajax({
			url: "inc/control_ajax_slider.asp?thumb_num="+idCarga,
			success: function(datos){
				$('#cont_texto_novedad').stop();
				$('#info_seleccionada').stop();
				$('#cont_texto_novedad').fadeOut(400)
				$('#novedad_foto').fadeOut(500, function(){			
					$('#info_novedad_destacada').html(datos)
					
					$('#novedad_foto').css("display","none");
					$('#cont_texto_novedad').css("display","none");
					
					
					$('#novedad_foto img').load(function() {
						$('#novedad_foto').stop()
						$('#cont_texto_novedad').stop()
						$('#novedad_foto').fadeIn(500);
						$('#cont_texto_novedad').fadeIn(300);
					});									
				})							
			}
		});
	}
	
	CargaDetalle(0)
	
	$(li).click(function(){
		CargaDetalle($(this).attr("id"));      
	})

	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" )			
	}
};


/* CONTROL DE LOS THUMBS EN VERTICAL*/

jQuery.fn.sliderNews = function( settings ) {
    return this.each(function() {
		jQuery.fn.sliderNews.run( jQuery( this ), settings );
    });
};

jQuery.fn.sliderNews.run = function( $this, settings ) {
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	var numSeleccionado; // Para saber qué li tenemos seleccionado
	
	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?id_noticia="+id_noticia,
			success: function(datos){
				$('#noticia_contenido').stop();
				$('#noticia_foto img').stop();
				$('#noticia_contenido').animate({"left": "650px"}, 600)	
				$('#noticia_foto img').fadeOut(600, function(){			
					$('#frame_noti_ampliada').html(datos)					
					$('#noticia_contenido').css("left","650px");
					//$('#noticia_foto img').fadeOut(0);
					$('#noticia_foto img').css("display","none");
					$('#noticia_foto img').load(function() {
						$('#noticia_foto img').stop();
						$('#noticia_contenido').stop();
						$('#noticia_foto img').fadeIn(600);
						$('#noticia_contenido').animate({"left": "320px"}, 600)						
					});									
				})							
			}
		});
		return false;
	}
	
	
	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 liHeight = jQuery( li[0] ).height();
		var animating = false;
		ul.css( "height", ( li.length * liHeight ) );
		
		$('#noti_bt_down').click(function() {
			if((numSeleccionado+1) < li.length){
				CargaDetalle(numSeleccionado+1);			
			}			
			return false;
		});
		
		$('#noti_bt_up').click(function() {
			if(numSeleccionado > 0){
				CargaDetalle(numSeleccionado-1);
			}
			return false;
		});
		$next.css( "display", "block" )			
	}
};