/* text Slider v2.2
  
  - posibilidad de establecer tamaño del slider a través de los nuevos parámetros: width y height
  - posibilidad de establecer el alto del box-texto a través del nuevo parámetro height_text, en el caso 'mostrarBox'
  - posición box-texto en el pie de la imágen => cambio del alto del slider a "height + height_text"
  
*/

/*
 *	markup example for $("#textSlider").textSlider();
 *	
 * 	<div id="textSlider">
 *	</div>
 *
 */

(function($) {

	$.fn.textSlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			nextId: 		'nextBtn',	
			orientation:	'', //  'vertical' is optional;
			speed1: 		1200,
			textoId:		'textoLayer',
			webPath: 		'/',
			auto:			true,  // modo slideshow
			startTime:		7000,
			timeStep:		3500,
			width:			674,   // tamaño del textSlider por defecto
			height:			346,
			height_text:    40,    // tamaño del box de texto por defecto
			imagenes: [],          // contenido del textSlider
			titulos: [],
			textos: [],
			links: []
		}; 
		
		var options = $.extend(defaults, options);  
		
		// loading
	
		return this.each(function() {  
			errors();					  
			$obj = $(this);
			var mostrarBox = hayTitulosTextos();
			$obj.css('background',  'url('+ options.webPath + 'images-support/ajax-loader.gif) no-repeat center');
			
			$obj.append(makeStructure());
			
			var $ul = $("ul", $obj);
			var $li = $("li", $ul);
			var $textoId;
			
			$obj.append(
						 '<div id="' + options.prevId +'"> </div>'+
						 '<div id="' + options.nextId +'"> </div>');
			
			var $divPrev = $("div#"+options.prevId);
		 	var $divNext = $("div#"+options.nextId);
			
			if (options.width != 0) {
				$obj.width(options.width);
				$li.width(options.width);
				if (mostrarBox) {
					$textoId = $("."+options.textoId, $li);
					// alto de la caja de texto (-2) por borde de un pixel
					$textoId.width(options.width);
					$textoId.height(options.height_text);
					$textoId.css("top", "-2px");
					
					// subir botones: valor negativo
					var offset = parseInt($divPrev.css("top").substring(0, $divPrev.css("top").length)) - options.height_text;
					$divPrev.css("top", (offset + "px"));
					$divNext.css("top", (offset + "px"));
					
				}
			}
			
			if (options.height != 0) {
				$obj.height(options.height+(mostrarBox ? options.height_text : 0));
				$li.height(options.height+(mostrarBox ? options.height_text : 0));
			}

			var s = $li.length;
			var w = $obj.width(); 
			var h = $obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			var cycleTimeout;
			
	
			// prev y next elements
			$ul.css('width',s*w);	
			if(!vertical) $li.css('float','left');
			
			setImagesButtons();
			
			$divPrev.hover(
			function() {
				$divPrev.addClass("mano");
				$divPrev.css('background',  'url('+ options.webPath + 'images-support/negativo_01.png) no-repeat');
			}, function(){
				$divPrev.removeClass("mano");
				$divPrev.css('background',  'url('+ options.webPath + 'images-support/positivo_01.png) no-repeat');

			});
			
			$divNext.hover(
			function() {
				$divNext.addClass("mano");
				$divNext.css('background',  'url('+ options.webPath + 'images-support/negativo_02.png) no-repeat');
			}, function(){
				$divNext.removeClass("mano");
				$divNext.css('background',  'url('+ options.webPath + 'images-support/positivo_02.png) no-repeat');
			});

			$divPrev.hide();
			$divNext.hide();
			
			$divNext.click(function(){
				options.auto = false;
				$ul.stop(true, true);
				clearTimeout(cycleTimeout);
				animate("next");
				if (t>=ts) $(this).fadeOut();
				$divPrev.fadeIn();
			});
			
			$divPrev.click(function(){
				options.auto = false;
				$ul.stop(true, true);
			    clearTimeout(cycleTimeout);
				animate("prev");
				if (t<=0) $(this).fadeOut();
				$divNext.fadeIn();
			});
			
			function setImagesButtons () {
				
				// preload de las imagenes de sustitucion 
				var p_over = new Image();
				var n_over = new Image();
				
				p_over.src =  options.webPath + 'images-support/negativo_01.png';
				n_over.src =  options.webPath + 'images-support/negativo_02.png';
				
				$(p_over).load();
				$(n_over).load();
				
				// establecer las imágenes 
				$divPrev.css('background',  'url('+ options.webPath + 'images-support/positivo_01.png) no-repeat');
				$divNext.css('background',  'url('+ options.webPath + 'images-support/positivo_02.png) no-repeat');
				}
				
			function makeIcons(){
				var s= options.imagenes.length;
				var html = '<div id="textSlider-icons"><ul>';
				
				for (i=0; i<s; i++){
					html += '<li id="icon' + i +'">';
					html += '</li>';
				}
				
				html += '</ul></div>';
				return (html);				
				}
			
			
			function makeStructure(){
				
				var s= options.imagenes.length;
				var html = '<div id="textSlider-images"><ul>';
				
				for (i=0; i<s; i++){
					hayLinks = (options.links[i] != null);
					html += '<li><div>';
					if (hayLinks) {
						html += '<a href=' + options.links[i] + '>';
						html += '<img src="' + options.imagenes[i] + '" style="width:' + options.width + 'px; height:' + options.height + 'px;border:0;" /></a>';
					}
					else
						html += '<img src="' + options.imagenes[i] + '" style="width:' + options.width + 'px; height:' + options.height + 'px;" />';
						
					if (mostrarBox) {
						var sTexto = (options.textos[i]!=null) ? options.textos[i] : '';
						var sTitulo = (options.titulos[i]!=null) ? options.titulos[i] : '';
						if ((sTexto != '') || (sTitulo != '')) {
							html += (hayLinks) ? '<a href =' + options.links[i] + ' border=0>' : '';
							html += '<div class="' +  options.textoId +'">';
							html += '<h1>' + sTitulo+ '</h1>';
							html += '<p>' + sTexto + '</p>';
							html += '</div>';
							html += (hayLinks) ? '</a>' : '';
						}
					}
					html += '</div></li>';
				}
				
				html += '</div></ul>';
				return (html);
			};
		
			function errors() {
				if (options.imagenes.length == 0){
					alert("No se han definido imágenes en el text slider");
					exit;
				}
				
				return;
			}
			
			function hayTitulosTextos() {
				var hay = false;
				
				for (var i=0; i-options.titulos.length-1; i++) {
					hay = (options.titulos[i] || options.textos[i]);
					if (hay)
						return hay;
				}
			
			}
					
			
			function animate(dir){
				
				var prev_t = t;
				if(dir == "next"){		
					if (!options.auto) {
						t = (t>=ts) ? ts : t+1;
					} else { 
						

						
					t = (t>=ts) ? 0 : t+1; }
					

					// prev
				} else {
					t = (t<=0) ? 0 : t-1;
				};
				
				if(!vertical) {
					p = (t*w*-1);
					if (!options.auto) {
						$ul.animate({ marginLeft: p }, { duration: options.speed1, easing: 'easeOutExpoB18' });
					}else{
						
					    if (t==0) { $divPrev.fadeOut(); $divNext.fadeIn(); }
						else if (prev_t==ts-1) { $divNext.fadeOut(); }
						else if (t>0) { $divNext.fadeIn(); $divPrev.fadeIn(); }
						
						$ul.animate({ marginLeft: p }, options.speed1, 'easeOutExpoB18', function () { 
																	cycleTimeout = setTimeout(function() {animate("next");}, options.timeStep);}
											);
						 
					}

				} else {
					p = (t*h*-1);
					$ul.animate({ marginTop: p }, { duration: options.speed1, easing: 'easeOutExpoB18'});				
				}	
			};
			
			if(s>1) $divNext.fadeIn();
			
			
			if (options.auto) 
				cycleTimeout = setTimeout(function() {animate("next");}, options.startTime);
		});
	  
	};

})(jQuery);
