$(function() {

//Quotes
var quoteNum = 0;

$('#quoteJS').remove();
if (typeof quote != "undefined") {
if(quote.length>0) {

	
	$('#photos').after('<a href="#quote" id="quoteImg"><img src="../img/quotes.png" alt="quotes" width="41" height="14"/></a>').append('<div id="quoteCont"><span id="quote"></span></div>');

	$('#quoteCont').hide();

	if(quote.length>1) {
		$('#quoteCont').append("<a href='#close' id='closeQ'>CLOSE</a> <a href='#next' id='nextQ'>NEXT</a>");
	}
			
	$('#quote').text(quote[0]);
	
	$('#nextQ').click(function(){
		if(quoteNum<(quote.length-1)) {
			quoteNum++;		
		} else {
			quoteNum=0;
		}
		$('#quote').text(quote[quoteNum]);
		return false;
	});
	
	$('#prevQ').click(function(){
		if(quoteNum>=(quote.length-1)) {
			quoteNum--;		
		} else {
			quoteNum=quote.length-1;
		}
		$('#quote').text(quote[quoteNum]);
		return false;
	});

	$('#quoteImg').click(function(){
		$('#quoteCont').slideToggle("slow");
		return false;
	});
	$('#closeQ').click(function(){
		$('#quoteCont').slideToggle("slow");
		return false;
	});
}
}

//Photos

// -- Normalizar para el JS activado
$('#photos').after('<div id="control"><span id="rev" title="Previous photo"></span><span id="play" title="Play/Pause Slideshow"></span><span id="fwd" title="Next Photo"></span></div>').addClass('photosJS');

$('#photos img').addClass('photosImg');

$('#thumbs').css('display','block').wrap('<div id="contThumb"></div>');

// -- fin normalizacion

///Carrousel
var largothumb = $('#thumbs li').length * 90;
$('#thumbs').css('width',largothumb);
$('#contThumb').after('<span id="thleft"></span><span id="thright"></span>');
$('#thright').click(function(){
	var offth = $('#thumbs').position();
	if(905-offth.left<$('#thumbs').width()) {
		$('#thumbs').animate({left: '-=90px'}, "slow");
	}
});

$('#thleft').click(function(){
	var offth = $('#thumbs').position();
	if(offth.left<0) {
		$('#thumbs').animate({left: '+=90px'}, "slow");
	}
});

///Fin Carrousel

var animacion = false;
var indexImg = 0;
var total = $('#thumbs img').length - 1;
$('#thumbs img:eq(0)').addClass('activeSlide');
$('#photos img').hide();

function cambiar(iImg) {

	oldIndex = indexImg;
	indexImg = iImg;
	
	//var randomnumber=Math.floor(Math.random()*quote.length);
	$('#thumbs img:eq('+oldIndex+')').removeClass('activeSlide');
	$('#thumbs img:eq('+indexImg+')').addClass('activeSlide');
	
	var imgleft = $('.activeSlide').position();
	var thumbleft = $('#thumbs').position();
	
	//Cambiar foco cuando se va a la derecha
	if(imgleft.left>=(905+thumbleft.left)) {
		thumbml = -(90*(indexImg-9));
		$('#thumbs').animate({left: thumbml}, "slow");
	}
	
	//Cambiar foco cuando se va a la izquierda
	if(imgleft.left<Math.abs(thumbleft.left)) {
		$('#thumbs').animate({left: -(imgleft.left-5)}, "slow");
	}
	
	$('#photos img:eq(' + oldIndex + ')').fadeOut('slow',function(){
		var hor = (895 - $('#photos img:eq(' + indexImg + ')').attr('width')) / 2;
		var ver = (600 - $('#photos img:eq(' + indexImg + ')').attr('height')) / 2;
		var cssObj = {
			'left' : hor,
			'top' : ver
		}
		$('#photos img:eq(' + indexImg + ')').css(cssObj).fadeIn();
	});

}

function anima() {
	animaset = setTimeout(anima,4000);
	if(indexImg < total) {
		cambiar(indexImg+1);
	} else {
		cambiar(0);
		//stopAnima();
	}
}

function playAnima() {
	if(!animacion) {
		animacion = true;
		$('#control').toggleClass('pause');
		anima();
	}
}

function stopAnima() {
	if(animacion) {
		animacion = false;
		$('#control').toggleClass('pause');
		clearTimeout(animaset);
	}
}

$('#thumbs li').click(function () {
	stopAnima();
	cambiar($('#thumbs li').index(this));
});


$('#rev').click(function(){
	stopAnima();
	if(indexImg > 0) {
		cambiar(indexImg-1);
	} else {
		var offleft = 905-$('#thumbs').width();
		$('#thumbs').animate({left: offleft}, "slow");
		cambiar(total);
	}
});

$('#fwd').click(function(){
	stopAnima();
	if(indexImg < total) {
		cambiar(indexImg+1);		
	} else {
		$('#thumbs').animate({left: '0'}, "slow");
		cambiar(0);
	}
});

$('#play').click(function(){
	if(!animacion) {
		playAnima();
	} else {
		stopAnima();
	}
});

cambiar(0);

});