var textoActivo = 1;
var nuevoTexto;
var textoAnterior;
var textoSiguiente;

$(document).ready(function(){
	
	/*LOGO CABECERA*/
	
	$('#logo a').mouseenter(function(){
		$('img', this).stop().fadeTo('normal', 0.5);
	}).mouseleave(function(){
		$('img', this).stop().fadeTo('normal', 1);
	});
	
	/*TEXTOS*/
	
	$('#contentedoraTextos').width(600 * $('.blokeTexto').length);
	$('#centroTextos').height($('.blokeTexto:first').height());
	
	$('#listaTextos li:not(.noActivo)').mouseenter(function(){
		$(this).stop().fadeTo('fast', 0.5);
	}).mouseleave(function(){
		$(this).stop().fadeTo('fast', 1);
	}).click(function(){
		textoActivo = parseInt($(this).attr('id').replace('previo', ''));
		
		$('#contentedoraTextos').stop(false, true).animate({
			marginLeft: '-'+ ((textoActivo - 1) * 600) +'px'
		});
		textoMas1 = parseInt(textoActivo) + 1;
		textoMenos1 = parseInt(textoActivo) - 1;
		
		if (textoActivo == 1){
			$('#anterior').fadeOut();
		} else {
			$('#anterior span').text($('.blokeTexto:nth-child('+ textoMenos1 +')').attr('id').replace('pagina', ''));
			$('#anterior').fadeIn();
		};
		if (textoActivo == $('.blokeTexto').length){
			$('#siguiente').fadeOut();
		} else {
			$('#siguiente span').text($('.blokeTexto:nth-child('+ textoMas1 +')').attr('id').replace('pagina', ''));
			$('#siguiente').fadeIn();
		};
		
		$('#listaTextos li').removeClass('sel');
		$(this).addClass('sel');
		
		$('#centroTextos').stop(false, true).animate({
			height: $('.blokeTexto:nth-child('+ textoActivo +')').height()
		});
	});
	
	$('#anterior').click(function(){
								  
		if ((textoActivo) >= 2){
			if ((textoActivo) == 2){
				$('#anterior').fadeOut();
			} else {
				temp = textoActivo - 2;
				textoAnterior = parseInt($('.blokeTexto:nth-child('+ temp +')').attr('id').replace('pagina', ''));
			};
			textoActivo --;						   
			nuevoTexto = parseInt($('.blokeTexto:nth-child('+ textoActivo +')').attr('id').replace('pagina', ''));
			textoSiguiente = parseInt($('.blokeTexto:nth-child('+ (textoActivo + 1) +')').attr('id').replace('pagina', ''));
	
			$('#listaTextos li').removeClass('sel');
			$('#listaTextos li:nth-child('+ nuevoTexto +')').addClass('sel');
			
			$('#anterior span').text(textoAnterior);
			$('#siguiente span').text(textoSiguiente);
			
			if (textoActivo == $('.blokeTexto').length - 1){
				$('#siguiente').fadeIn();
			};
									   
			$('#contentedoraTextos').stop(false, true).animate({
				marginLeft: '+=600px'
			});
			
			$('#centroTextos').stop(false, true).animate({
				height: $('.blokeTexto:nth-child('+ textoActivo +')').height()
			});
		};
		
	});
	$('#siguiente').click(function(){
		if ((textoActivo) < $('.blokeTexto').length){
			if ((textoActivo) == $('.blokeTexto').length - 1){
				$('#siguiente').fadeOut();
			} else {
				temp = textoActivo + 2;
				textoSiguiente = parseInt($('.blokeTexto:nth-child('+ temp +')').attr('id').replace('pagina', ''));
			};
			textoActivo ++;						   
			nuevoTexto = parseInt($('.blokeTexto:nth-child('+ textoActivo +')').attr('id').replace('pagina', ''));
			textoAnterior = parseInt($('.blokeTexto:nth-child('+ (textoActivo - 1) +')').attr('id').replace('pagina', ''));
			
	
			$('#listaTextos li').removeClass('sel');
			$('#listaTextos li:nth-child('+ nuevoTexto +')').addClass('sel');
			
			$('#anterior span').text(textoAnterior);
			$('#siguiente span').text(textoSiguiente);

			if (textoActivo == 2){
				$('#anterior').fadeIn();
			};
									   
			$('#contentedoraTextos').stop(false, true).animate({
				marginLeft: '-=600px'
			});
			
			/*var movimiento = -((textoActivo - 1) * 600) + 'px'
			alert(movimiento);
			$('#contentedoraTextos').stop(false, true).animate({
				marginLeft: movimiento
			});*/
			
			$('#centroTextos').stop(false, true).animate({
				height: $('.blokeTexto:nth-child('+ textoActivo +')').height()
			});
		};
	});
	
});
