 /*
@Autor: Nahuel Jose
	http://nahueljose.com.ar/articles/slider-facil-2000-un-sencillo-slider-en-jquery/
@Enmarcado:
	<div id="slider-id">
		<ul>
			<li>[contenido]</li>
			<li>[contenido]</li>
			<li>[contenido]</li>
		</ul>
	</div>
@Enmarcado generado:
	<div id="slider-id">
		<ul>
			<li>[contenido]</li>
			<li>[contenido]</li>
			<li>[contenido]</li>
		</ul>
	</div>
	<div class="sliderFacilBotones">
		<a href="#" class="botonAnterior">Anterior</a>
		<a href="#" class="botonSiguiente">Siguiente</a>
	</div>
@Iniciamiento:
	$(function(){
		$('#slider-id').sliderFacil2000();
	});
*/
(function($){
	$.fn.sliderFacil2000 = function(opciones){
		//Valores por defecto
		var defaults = ({
			velocidad : 700,
			easing : 'easeOutExpo',
			auto: true,
			tiempo: 3000,
			pausarHover: true,
			direccionInicial : 'izquierda',
			cambiarDireccion : true
	  	});
		var opciones = $.extend(defaults, opciones);
		
		$(this).each(function(){
			//config
			var int_margenseguridad = 5;
			var str_html_botones = '<div class=\"sliderFacilBotones\"><a href=\"#\" class=\"botonAnterior\"> Anterior </a><a href=\"#\" class=\"botonSiguiente\"> Siguiente </a></div>';
			//objetos
			var obj_slider = $(this);
			var obj_slider_ul = obj_slider.children('ul, ol');
			var obj_slider_li = obj_slider_ul.children('li');
			//globales
			var int_indice_slide_actual = 0;
			var bool_animando = false;
			var bool_pausado = false;
			var obj_timer = 0;
			var str_direccion = opciones.direccionInicial;
			//agregar html
			obj_slider.after(str_html_botones);
			if(obj_slider_li.length <= 2){
				//si tiene menos de dos slides, clonarlos para tener mas.
				obj_slider_ul.append(obj_slider_li.first().clone());
				obj_slider_ul.append(obj_slider_li.last().clone());
				obj_slider_li = obj_slider_ul.children('li');
			}
			//aplicar estilos iniciales
			var int_ancho_slides = obj_slider_li.width();
			var int_alto_slides = obj_slider_li.height();
			
			obj_slider_ul.css({
				'width' : int_ancho_slides,
				'height': int_alto_slides,
				'overflow':'hidden',
				'position':'relative'
			});
			obj_slider_li.css({
				'position':'absolute',
				'margin' : 0,
				'top':int_alto_slides + int_margenseguridad,
				'left':0
			});
			//asignar eventos
			obj_contenedor_botones = obj_slider.siblings('.sliderFacilBotones');
			obj_contenedor_botones.children('.botonAnterior').click(function(e){
				e.preventDefault();
				if(!bool_animando){
					_detenerAutomatico();
					_moverSlide('anterior', opciones.auto);
				}
			});
			obj_contenedor_botones.children('.botonSiguiente').click(function(e){
				e.preventDefault();
				if(!bool_animando){
					_detenerAutomatico();
					_moverSlide('siguiente', opciones.auto);
				}
			});
			if(opciones.pausarHover){
				obj_slider_li.add('.botonSiguiente, .botonAnterior')
					.mouseover(function(){bool_pausado = true;})
					.mouseleave(function(){bool_pausado = false;});
			}
			/* ---------- Iniciar Slider --------------- */
			_acomodarSlides();
			if(opciones.auto){
				_iniciarAutomatico();
			}
			/* ----------- Funciones ---------- */
			function _acomodarSlides(){
				obj_slider_li.css({
					'top':int_alto_slides + int_margenseguridad,
					'left':0
				});
				var _indices = _obtenerIndices();
				_indices['actual'].css({'top':0, 'left':0});
				_indices['anterior'].css({'top':0, 'left': -1*int_ancho_slides});
				_indices['siguiente'].css({'top':0, 'left': int_ancho_slides});
			}
			function _obtenerIndices(){
				var indices = new Array(3);
				var temp_ind_act = int_indice_slide_actual;
				var temp_ind_ant = (int_indice_slide_actual - 1) < 0 ? (obj_slider_li.length - 1) : (int_indice_slide_actual - 1);
				var temp_ind_sig = (int_indice_slide_actual + 1) > (obj_slider_li.length - 1) ? 0 : (int_indice_slide_actual + 1);
				indices['actual'] = $(obj_slider_li.get(temp_ind_act));
				indices['anterior'] = $(obj_slider_li.get(temp_ind_ant));
				indices['siguiente'] = $(obj_slider_li.get(temp_ind_sig));
				return indices;
			}
			function _moverSlide(direccion, iniciar){
				bool_animando = true;
				var arr_obj_elementos = _obtenerIndices();		
				var factor = direccion=='siguiente' ? -1 : 1;
				arr_obj_elementos['actual'].animate({
					'left' : factor*int_ancho_slides
				},opciones.velocidad, opciones.easing);
				arr_obj_elementos[direccion].animate({
					'left' : 0
				},opciones.velocidad, opciones.easing,function(){
					int_indice_slide_actual = arr_obj_elementos[direccion].index();
					_acomodarSlides();
					bool_animando = false;
					if(iniciar){
						_iniciarAutomatico();
					}
				});	
				if(opciones.cambiarDireccion){
					str_direccion = direccion=='anterior'?'derecha':'izquierda';
				}
			}
			function _iniciarAutomatico(){
				if(obj_timer !== 0){return false}
				obj_timer = setInterval(function(){
					if(bool_pausado || bool_animando){return false}
					if(str_direccion == 'izquierda'){
						_moverSlide('siguiente');
					}else if(str_direccion == 'derecha'){
						_moverSlide('anterior');
					}
				},opciones.tiempo);
			}
			function _detenerAutomatico(){
				clearInterval(obj_timer);
				obj_timer = 0;
			}			
		});		
		return false;
	};	
})(jQuery);
