/**
 * @author Diego
 */

var cantidad = 0;
var actual = 1;
var anterior = actual;
var total = 0;
var animando = false;

var $j = jQuery.noConflict();  

$j(document).ready(function()
{
	$elementos = $j('#textos #inside div');
	total = $elementos.length;
	controles();	
});

function controles()
{
	
	$j('a#flechaDerecha').mousedown(function(e) {
		
		e.preventDefault();
		
		if (($j(this).hasClass('desactivado'))||(animando)) { return; }
		
		animando = true;
				
		actual++;
		
		if (anterior == 1) { $j('a#flechaIzquierda').removeClass('desactivado'); }
		if (actual == total) { $j('a#flechaDerecha').addClass('desactivado');  }
	
		$textoAnterior = $j('#textos #inside #texto' + anterior + '');
		$textoSiguiente = $j('#textos #inside #texto' + actual + '');
		
		$imagenAnterior = $j('#imagenes #imagen' + anterior + '');
		$imagenSiguiente = $j('#imagenes #imagen' + actual + '');
		
		$imagenSiguiente.css('z-index','2');
		$imagenSiguiente.css('opacity','1');
		
		$imagenAnterior.animate({'opacity':'0'}, 450, function() { $imagenAnterior.css('z-index','1'); $imagenSiguiente.css('z-index','3'); });
		
		$textos = $j('#textos #inside');
		
		cantidad-=204;
		
		$textos.animate({'top':cantidad},250);
		
		$textoAnterior.animate({'opacity':'0'}, 450, function() { anterior = actual; });
		$textoSiguiente.animate({'opacity':'1'}, 550, function() { 
			if (jQuery.browser.msie) {
				$j(this).get(0).style.removeAttribute('filter');
			}
			animando = false; });
		});
	
	$j('a#flechaIzquierda').mousedown(function(e) {
		
		e.preventDefault();
		
		if (($j(this).hasClass('desactivado'))||(animando)) { return; }
		
		animando = true;
				
		actual--;
		
		if (actual == 1) { $j('a#flechaIzquierda').addClass('desactivado'); }
		if (anterior == total) { $j('a#flechaDerecha').removeClass('desactivado');  }
	
		$textoAnterior = $j('#textos #inside #texto' + anterior + '');
		$textoSiguiente = $j('#textos #inside #texto' + actual + '');
		
		$imagenAnterior = $j('#imagenes #imagen' + anterior + '');
		$imagenSiguiente = $j('#imagenes #imagen' + actual + '');
		
		$imagenSiguiente.css('z-index','2');
		$imagenSiguiente.css('opacity','1');
		
		$imagenAnterior.animate({'opacity':'0'}, 450, function() { $imagenAnterior.css('z-index','1'); $imagenSiguiente.css('z-index','3'); });
		
		$textos = $j('#textos #inside');
		
		cantidad+=204;
		
		$textos.animate({'top':cantidad},250);
		
		$textoAnterior.animate({'opacity':'0'}, 450, function() { anterior = actual; });
		$textoSiguiente.animate({'opacity':'1'}, 550, function() { 
			if (jQuery.browser.msie) {
				$j(this).get(0).style.removeAttribute('filter');
			}
			animando = false; });
		});
}
