/**
*
*	JavaScript para contener toda la funcionalidad
*	necesaria para poder hacer el slideshow de imagenes.
*
*/

var intervalId = 0;
var waitTime = 3000;

function slideShow() {

	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0,display:'none'});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:first').css({opacity: 1.0,display:'block'}).addClass('show');
	
	//Set the caption background to semi-transparent
	$('#gallery .caption').css({opacity: 0});

	//Resize the width of the caption according to the image width
	$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .caption #title').html($('#gallery a:first').find('img').attr('tit'))
	.animate({opacity: 0.7}, 400);
	
	$('#gallery .caption #description').html($('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 0.7}, 400);
	
	//situamos la posición de las flechas justament a la mitad de la altura
	var imageHeight = $('#gallery a').find('img').height();
	$('#gallery').css({height: imageHeight});
	$('#gallery #leftArrow').css({bottom: imageHeight / 2,display:'block'});
	$('#gallery #rightArrow').css({bottom: imageHeight / 2,display:'block'});
	
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	//intervalId = setInterval('goNextImage()',waitTime);
	
}

function goPreviousImage() {
	
	//primero limpiamos el interval en caso de que se haya pulsado manualmente
	//clearInterval(intervalId);
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	
	/*
	$('#gallery').stop(true);
	$('#gallery #leftArrow').stop(true);
	$('#gallery #rightArrow').stop(true);
	//current.stop(true);
	*/

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image

	var next = current.prev().length ? current.prev() : $('#gallery a:last');
	
	//Get next image caption
	var title = next.find('img').attr('tit');	
	var description = next.find('img').attr('rel');	
	
	//primero canviamos la altura de la imagen
	//$('#gallery').stop(true);
	next.css({opacity: 0.0,display:'block'});
	$('#gallery').animate({height:next.height()},1000,function(){
		next.addClass('show').animate({opacity: 1.0}, 1000);
	});
	
	
	$('#gallery #leftArrow').animate({bottom: next.height() / 2},1000);
	$('#gallery #rightArrow').animate({bottom: next.height() / 2 },1000);

	//Hide the current image
	
	current.animate({opacity: 0.0}, 1000,function(){
			$(this).css({display:'none'});
		})
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	/*$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	*/
	
	$('#gallery #title').fadeOut(500,function(){
			//Change the text
			$('#gallery #title').html(title);
			$(this).fadeIn(600);
		});
		
	$('#gallery #description').fadeOut(500,function(){
			//Change the text
			$('#gallery #description').html(description);
			$(this).fadeIn(600);
		});
		
	$('.captionLine').fadeOut(500,function(){
			$(this).fadeIn(600);
		});
		
	//reactivamos el interval
	//intervalId = setInterval('goNextImage()',waitTime);
	
}

function goNextImage() {
	
	//primero limpiamos el interval en caso de que se haya pulsado manualmente
	//clearInterval(intervalId);
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	
	/*
	$('#gallery').stop(true);
	$('#gallery #leftArrow').stop(true);
	$('#gallery #rightArrow').stop(true);
	//current.stop(true);
	*/

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var title = next.find('img').attr('tit');	
	var description = next.find('img').attr('rel');	
	
	//primero canviamos la altura de la imagen
	next.css({opacity: 0.0,display:'block'});
	$('#gallery').animate({height:next.height()},1000,function(){
		next.addClass('show').animate({opacity: 1.0}, 1000);
	});
	
	
	$('#gallery #leftArrow').animate({bottom: next.height() / 2},1000);
	$('#gallery #rightArrow').animate({bottom: next.height() / 2 },1000);

	//Hide the current image
	
	current.animate({opacity: 0.0}, 1000,function(){
			$(this).css({display:'none'});
		})
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	/*$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	*/
	
	$('#gallery #title').fadeOut(500,function(){
			//Change the text
			$('#gallery #title').html(title);
			$(this).fadeIn(600);
		});
		
	$('#gallery #description').fadeOut(500,function(){
			//Change the text
			$('#gallery #description').html(description);
			$(this).fadeIn(600);
		});
		
	$('.captionLine').fadeOut(500,function(){
			$(this).fadeIn(600);
		});
		
	//reactivamos el interval
	//intervalId = setInterval('goNextImage()',waitTime);
}
