/* text Slider v2.9
  
	- eventos de arrastre de imagenes en pantallas tactiles, scrollable 
  
*/

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

(function($) {
			
		var methods = {
		 init : function( options ) {
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prev',
			nextId: 		'next',	
			speed1: 		1200,
			textoId:		'textoLayer',
			webPath: 		'/',
			auto:			true,  // modo slideshow
			border:			false,  // borde izdo y dcho en imagenes
			start_time:		5000,
			step_time:		3500,
			width:			1000,   // tamaño del textSlider por defecto
			height:			450,
			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();		
			
			var $obj = $(this);
			var mostrarBox = hayTitulosTextos();
			var $divPrev, $divNext;
			var offset_border = (options.border) ? 44 : 0;
			var $textoId;
			var $div_images;
			
			$obj.css('background',  'url('+ options.webPath + 'images-support/ajax-loader.gif) no-repeat center');
			$obj.append(makeStructure());
	
			$div_images = $("#textSlider-images", $obj);
			
			$obj.append('<div class="' + options.prevId +'"> </div>'+
						 '<div class="' + options.nextId +'"> </div>');
			
			$divPrev = $("div."+options.prevId);
		 	$divNext = $("div."+options.nextId);
			
			if (options.width != 0) {
				$obj.width(options.width + offset_border);
				//$li.width(options.width);
				//$div_images.width(options.width);
				if (mostrarBox) {
					$textoId = $("."+options.textoId, $obj);
					// 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");
				}
			}
			
			if (options.height != 0) {
				$obj.height(options.height+(mostrarBox ? options.height_text : 0));
				//$li.height(options.height+(mostrarBox ? options.height_text : 0));
				if (options.border) {
					var $sombra_left = $("#sombra-left", $obj);
					var $sombra_right = $("#sombra-right", $obj);
					
					$sombra_right.height ($obj.height());
					$sombra_left.height ($obj.height());
					
					$div_img_sombra = $("#content-sombra", $obj);
					$img_right = $("img", $sombra_right);
					
					var offset_top = (options.height - $img_right.height() > 0) ? ((options.height - $img_right.height()) /2) : 0;
					$div_img_sombra.css('top', + offset_top + 'px');

					
				}
			}
			
			
			if (options.border) {	
					$div_images.css('marginLeft', '22px');
			}
			
			
			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');
			});
			
			this.btnPrev = $divPrev;
			this.btnNext = $divNext;
			
			function setImagesButtons () {
				
				// preload de las imagenes de sustitucion 
				var prev_over = new Image();
				var next_over = new Image();
				
				prev_over.src =  options.webPath + 'images-support/negativo_01.png';
				next_over.src =  options.webPath + 'images-support/negativo_02.png';
				
				$(prev_over).load();
				$(next_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');
				
				// posicion de los botones del slider, en la mitad del alto de la imagen
				var p_pos = options.height/2 - $divPrev.height()/2;
				
				// establecer la posicion en la mitad del alto
				$divPrev.css('top', p_pos+'px');
				$divNext.css('top', p_pos+'px');
				if (options.border) {
					$divPrev.css('left', '22px');
					$divNext.css('right', '20px');
				}
				
				}
			
			
			function makeStructure(){
				
				var s= options.imagenes.length;
				var html = (options.border) ? 
							'<div id="sombra-left"><div id="content-sombra"><img src="' + options.webPath + 'images-support/sombitaizquierda.jpg" width="22px" height="474px"/></div></div>' 
							: '';
				
					html += '<div id="textSlider-images" class="items">';
				
				for (i=0; i<s; i++){
					hayLinks = (options.links[i] != null);
					html += '<div id="image-' + i + '">';
					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>";
				}
				
				html += '</div>';
				html += (options.border) ? 
						'<div id="sombra-right"><div id="content-sombra"><img src="' + options.webPath + 'images-support/sombitaderecha.jpg" width="22px" height="474px"/></div></div>'
						: '';
				
				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;
				}
			    return hay;
			}
			
		});
	  
	}, // fin init
	
   destroy : function( ) {

	   return this.each(function(){
				 var $obj = $(this);
				 var data = $obj.data('textSlider');
		

				var $images_i = $('div[id^="image-"]', $obj); 
				
				// eventos arratre pantallas tactiles
				$('img', $images_i).each (function (){
													var $imagen_i = $(this);
													$imagen_i.unbind('textSlider');
													});
				data.textSlider.remove();
				$obj.removeData('textSlider');

	   });

	 }
	 
  }; // fin declaracion methods

	$.fn.textSlider = function(method){
    
		if ( methods[method] ) {
		  return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.textSlider' );
		}    
  
  };
  




})(jQuery);


