/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
(function($) {
	$.fn.easySlider = function(options){
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',
			nextText: 		'',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'',
			firstShow:		false,
			lastId: 		'lastBtn',
			lastText: 		'',
			lastShow:		false,
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false,
			width:          696,
			height:         241,
			prevBtnClass:   'prevBtn',
			nextBtnClass:   'nextBtn',
			prevBtnPos:     null,
			nextBtnPos:     null,
			controlsId:     ""
		};
		var options = $.extend(defaults, options);
		this.each(function() {
			// Incializaciones
			var obj = $(this);
			var primeraEjecucion = true;
			var h  = options.height;
			var s  = $("li", obj).length;	// Cantidad original de objetos en la lista
			var w  = options.width;
			obj.width(w);
			obj.height(h);
			obj.css("overflow","hidden");
			obj.css("display", "block");
			// Copia el primer elemento al final y el ultimo al inicio para dar el efecto de carrousel circular.
			var ul  = $("ul", obj);
			var tLi = $("li", ul);
			ul.prepend(tLi.slice(s-1).clone()).append(tLi.slice(0, 1).clone());
			// Mas inicializaciones
			s  = $("li", obj).length;	// Cantidad total de objetos en la lista
			var t = 1;					// Visualiza el primer objeto de la lista original
			var ts = s - 2;
			// Se ubica en la posicion del primer elemento de la listaoriginal
			$("ul", obj).css('width',s*w);
			if(!options.vertical) {
				$("li", obj).css('float','left');
				$("ul", obj).css("margin-left", -w + "px");
			} else {
				$("ul", obj).css("margin-top", -h + "px");
			}
			if(options.controlsShow){
				var html = options.controlsBefore;
				var left = 0;
				var top  = 0;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				// Si esta en true la opcion vertical no muestra los botones de 'anterior' y 'siguiente'
				if (!options.vertical) {
					if (options.prevBtnPos == null) {
						top  = - (options.height / 2 + 16) + 4;
						left = -1;
					} else {
						top  = options.prevBtnPos.Top;
						left = options.prevBtnPos.Left;
					}
					html += ' <span id="'+ options.prevId +'" class="' + options.prevBtnClass + '" style="top: ' + top + '; left: ' + left + ';"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					if(options.nextBtnPos == null) {
						top  = - (options.height / 2 + 16 + 23) + 4;
						left = options.width - 23;
					} else {
						top  = options.nextBtnPos.Top;
						left = options.nextBtnPos.Left;
					}
					html += ' <span id="'+ options.nextId +'" class="' + options.nextBtnClass + '" style="top: ' + top + '; left: ' + left + ';"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				}
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;
				$(obj).after(html);
			};
			$("a","#"+options.nextId).click(function(){
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){
				animate("prev",true);
			});
			$("a","#"+options.firstId).click(function(){
				animate("first",true);
			});
			$("a","#"+options.lastId).click(function(){
				animate("last",true);
			});
			function animate(dir,clicked){
				var tele = false;		// Teletransporta a una ubicacion particular
				var t2 = 0;				// Indice a donde teletransportar
				if (s > 3) {
					var ot = t;
					var direccion = dir;
					switch(dir){
						case "next":
							if (ot >= ts) {
								if (options.continuous) {
									t  = 1;
									t2 = 0;
									ot = 0;
									direccion = "next";
									tele = true;
								} else {
									t = ts;
								}
							} else {
								t = t + 1;
							}
							break; 
						case "prev":
							if (t <= 1) {
								if (options.continuous) {
									t  = ts;
									t2 = ts + 1;
									ot = ts + 1;
									direccion = "prev";
									tele = true;
								} else {
									t = 1;
								}
							} else {
								t = t - 1;
							}
							break; 
						case "first":
							t = 1;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;
					var p2 = 0;			// Ubicacion en px a donde teletransportar
					if(!options.vertical) {
						p = (t*w*-1);
						if (tele) {
							p2 = (t2*w*-1);
							$("ul", obj).css("margin-left", p2 + "px");
						}
						$("ul",obj).animate(
							{ marginLeft: p }, 
							speed
						);
					} else {
						p = (t*h*-1);
						if (tele) {
							p = (t2*h*-1);
							$("ul", obj).css("margin-top", p2 + "px");
						}
						$("ul",obj).animate(
							{ marginTop: p }, 
							speed
						);
					};
					if(!options.continuous && options.controlsFade){
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();
						};
						if(t==1){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};
					};
					if(clicked) clearTimeout(timeout);
					if(options.auto) {
						timeout = setTimeout(function(){
							animate(direccion, false);
						},diff*options.speed+options.pause);
					};
				}
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};
})(jQuery);

