/*
	Basado en el script de Jon Raasch (http://jonraasch.com/blog/a-simple-jquery-slideshow)
*/
function ADIslideshow(items){
	/*************************************************************************/
	// propiedades públicas
	/*************************************************************************/
	this.auto = false;
	/*************************************************************************/
	// propiedades privadas
	/*************************************************************************/
	var items = items;
	var root = items.parent();
	var tag = $(items[0]).get(0).tagName.toLowerCase();
	var intervalo;
	var pausa = 6000;
	/*************************************************************************/
	// métodos privados
	/*************************************************************************/
	
	function mostrarSiguiente(b){
		var $active = root.find(tag + '.active');
		 
		if($active.length == 0){
			$active = root.find(tag + ':last');
		}
		
		// use this to pull the items in the order they appear in the markup
		if($active.next().length > 0){
			var $next = $active.next();
		}else{
			var $next = root.find(tag + ':first');
		}
		
		$active.addClass('last-active');

		$next.css('display','none');
		$next.addClass('active');
		
		if(b){
			$next.fadeIn('slow',function(){
				$active.removeClass('active last-active');
			});
		}else{
			$next.show();
			$active.removeClass('active last-active');
		}
	}
	
	function mostrarAnterior(b){
		var $active = root.find(tag + '.active');
		 
		if($active.length == 0){
			$active = root.find(tag + ':first');
		}
		
		// use this to pull the items in the order they appear in the markup
		if($active.prev().length > 0){
			var $next = $active.prev();
		}else{
			var $next = root.find(tag + ':last');
		}
		
		$active.addClass('last-active');

		$next.css('display','none');
		$next.addClass('active');
		
		if(b){
			$next.fadeIn('slow',function(){
				$active.removeClass('active last-active');
			});
		}else{
			$next.show();
			$active.removeClass('active last-active');
		}
	}
	
	/*************************************************************************/
	// métodos públicos
	/*************************************************************************/
	this.setPausa = function(milisegundos){
		pausa = milisegundos;
	}
	this.atras = function(){
		mostrarAnterior(false);
	}
	this.adelante = function(){
		mostrarSiguiente(false);
	}
	this.detener = function(){
		this.auto = false;
		clearInterval(intervalo);
	}
	this.mover = function(b){
		
		var incrementoPausa = 0;
		
		this.auto = true;
		
		if(b){
			mostrarSiguiente();
			incrementoPausa = 1000;
		}
		
		intervalo = setInterval(function(){
			mostrarSiguiente(true);
		},pausa);
	}
}
