/*
EASING!!!!!!!!!!!!!!!
*/


(function($){ 

	$.fn.cross_slider = function(options){ 
	
	
		// Opciones por defecto:
		var defaults = {			
			rightListClass: "right",
			leftListClass: "left",
			rightPosition: "center", //Donde se situará el elemento de la derecha, si es un número será el left: left|center|right|random|numero
			leftPosition: "center", //Donde se situará el elemento de la izquierda, si es un número será el left: left|center|right|random|numero
			leftOut: "right", // Por dónde saldrá el elemetno de la izquierda: left|right|up|down|random
			rightOut: "left", // Por dónde saldrá el elemento de la derecha: left|right|up|down|random
			rightSizeIn: "100", // Tamaño, en porcentaje, inicial del elemento de la derecha, para redimensionar en el fadeIn
			leftSizeIn: "100", // Tamaño, en porcentaje, inicial del elemento de la izquierda, para redimensionar en el fadeIn
			rightSizeOut: "100", // Tamaño, en porcentaje, inicial del elemento de la derecha, para redimensionar en el fadeOut
			leftSizeOut: "100", // Tamaño, en porcentaje, inicial del elemento de la izquierda, para redimensionar en el fadeOut
			rightOverflowIn: false, // Especifica si el elemento de la derecha tiene que entrar desde FUERA del elemento padre
			leftOverflowIn: false, // Especifica si el elemento de la izquierda tiene que entrar desde FUERA del elemento padre
			rightOverflowOut: false, // Especifica si el elemento de la derecha tiene que salir hasta FUERA del elemento padre
			leftOverflowOut: false, // Especifica si el elemento de la izquierda tiene que salir hasta FUERA del elemento padre
			syncronizeLists: true, // Si está a true, el 1 de la lista de la dercha corresponderá con el 1 de la izq y así..
			loop: 'infinite', // puede ser un número (0 para reproducir sólo una vez) o infinite.
			duration: 3, // tiempo que se verán las imágenes
			sleep: 0, // tiempo desde que se va una hasta que sale otra			
			showDuration: 0.9, // tiempo para el fade-in y el moviemiento
			hideDuration: 1, // tiempo para el fade-out y el movimiento
			start: 1 // Elemento por el que empezará. random para aleatorio.
		};  
		var opt = $.extend(defaults, options);			
		
		this.each(function(){
		
			//[***] OPTIONS TO LOWER!!!
		
		
			var elementWidth = $(this).width();
			var currentElement = 0;			
			var iteration = 0;
			
			var showDuration = opt.showDuration * 1000;
			var hideDuration = opt.hideDuration * 1000;
			
			
			//[***]AQUÍ TENEMOS QUE COMPROBAR QUE ES NUMERICO
			//Trampeamos lo del loop infinito...
			if (opt.loop == 'infinite') {
				opt.loop = 999999;
			}
						
			
			// Overflow Hiden para el contenedor:
			$(this).css({"overflow": "hidden"});
			//$(this).css({"position": "relative"});
			
			// Pillamos los elementos de la derecha, les quitamos la opacidad y los ponemos en posición relativa:
			var rightElements = $("ul." + opt.rightListClass + " li", this);
			var rightElementsNum = $(rightElements).size(); 			
			$(rightElements).css({opacity: 0});
			
			
			// Pillamos los elementos de la izquierda, les quitamos la opacidad y los ponemos en posición relativa:
			var leftElements = $("ul." + opt.leftListClass + " li", this);			
			var leftElementsNum = $(leftElements).size();			
			$(leftElements).css({opacity: 0});
			
		
		
		
			// Elemento inicial:
			var currentLeft = 0;
			var currentRight = 0;			
			if (IsNumeric(opt.start) && opt.start > 0) {
				currentLeft = opt.start - 1;
				currentRight = opt.start - 1;
			} else {
				if (opt.start == "random")  {
					var randomnumber = Math.floor(Math.random()*(leftElementsNum));
					currentLeft = randomnumber;
					currentRight = randomnumber;
				}
			}
		
		
			
			//Si hay elementos empezamos con el tema:
			if (rightElementsNum > 0 && leftElementsNum > 0)
				doSlider(this);
		
			
		
			function doSlider(div) {				
			
				if (currentRight >= rightElementsNum) {
					currentRight = 0					
				}				
				if (currentLeft >= leftElementsNum) {
					currentLeft = 0					
				}				
				if (opt.syncronizeLists == true && currentLeft != currentRight) {					
					currentLeft = 0;
					currentRight = 0;
				}				
				
				
				
				
				
				// Situar los elementos en su posición correcta:
				var divWidth = $(div).width();
				var currentRightElement = $(rightElements).get(currentRight);								
				var currentLeftElement = $(leftElements).get(currentLeft);
				//var leftUlWidth = $(currentLeftElement).parent().width();				
				//var rightUlWidth = $(currentRightElement).parent().width();
				var leftUlWidth = $(currentLeftElement).width();				
				var rightUlWidth = $(currentRightElement).width();
				
				var leftUlHeight = $(currentLeftElement).height();				
				var rightUlHeight = $(currentRightElement).height();
				
				
				
				if (opt.leftOverflowIn == true) {
					$(currentLeftElement).css({"left": 0 - leftUlWidth});
				} else {
					$(currentLeftElement).css({"left": 0});
				}				
				
				if (opt.rightOverflowIn == true) {
					$(currentRightElement).css({"left": divWidth});
				} else {
					$(currentRightElement).css({"left": divWidth - rightUlWidth});					
				}
				
				
				//[***]OVERFLOW OUT!!!!!!!!!!!!
				
				
				
				
				
				// Tamaño inicial:
				if (opt.rightSizeIn != 100) {
					var auxW = opt.rightSizeIn * rightUlWidth / 100;
					var auxH = opt.rightSizeIn * rightUlHeight / 100;					
					
					$(currentRightElement).css({"width": auxW + 'px'});
					$(currentRightElement).css({"height": auxH + 'px'});				
				}
				
				if (opt.leftSizeIn != 100) {
					var auxW = opt.leftSizeIn * leftUlWidth / 100;
					var auxH = opt.leftSizeIn * leftUlHeight / 100;
					$(currentLeftElement).css({"width": auxW + 'px'});
					$(currentLeftElement).css({"height": auxH + 'px'});				
				}
				
				// Tamaño final:
				if (opt.rightSizeOut != 100) {
					var auxW = opt.rightSizeOut * rightUlWidth / 100;
					var auxH = opt.rightSizeOut * rightUlHeight / 100;										
				} else {
					var rightUlWidth2 = rightUlWidth;
					var rightUlHeight2 = rightUlHeight;					
				}
				
				if (opt.leftSizeOut != 100) {
					var leftUlWidth2 = opt.leftSizeOut * leftUlWidth / 100;
					var leftUlHeight2 = opt.leftSizeOut * leftUlHeight / 100;					
				} else {
					var leftUlWidth2 = leftUlWidth;
					var leftUlHeight2 = leftUlHeight;					
				}
				
				
				
				
				
				
								
				
				
				// Calcular el desplazamiento necesario para mostrar los elementos:
				/*if opt.rightPosition == 'random') {
					//[***] ALEATORIO!!!!									
				}
				if opt.leftPosition == 'random') {
					//[***] ALEATORIO!!!!				
				}*/

				if (IsNumeric(opt.rightPosition)) {
					auxLeft2 = opt.rightPosition;
				} else {				
					if (opt.rightPosition == 'left') {
						auxLeft2 = 0;
					} else {
						if (opt.rightPosition == 'right') {
							auxLeft2 = divWidth - rightUlWidth;
						} else {
							auxLeft2 = (divWidth - rightUlWidth) / 2;
							/*alert(divWidth);
							alert(rightUlWidth);
							alert(auxLeft2);*/
						}					
					}
				}
				
				
				if (IsNumeric(opt.leftPosition)) {
					auxLeft1 = opt.leftPosition;
				} else {
					if (opt.leftPosition == 'left') {
						auxLeft1 = 0;
					} else {
						if (opt.leftPosition == 'right') {
							auxLeft1 = divWidth - leftUlWidth;
						} else {
							auxLeft1 = (divWidth - leftUlWidth) / 2;
						}				
					}
				}
				
				// Calcular el desplazamiento para sacar a los elementos:
				/*if opt.rightOut == 'random') {
					//[***] ALEATORIO!!!!									
				}
				if opt.leftOut == 'random') {
					//[***] ALEATORIO!!!!				
				}*/
				
				if (IsNumeric(opt.rightOut)) {
					auxOut2 = opt.rightOut;
				} else {
					switch(opt.rightOut) {
						case 'left':				
							auxOut2 = 0 - rightUlWidth;
						break;
						case 'right': 
							auxOut2 = divWidth + rightUlWidth;
						break;			
						case 'up': 
							//[***] UP!!!
						break;	
						case 'down': 
							//[***] DOWN!!!
						break;							
					} 
				}
				if (IsNumeric(opt.leftOut)) {
					auxOut1 = opt.leftOut;
				} else {
					switch(opt.leftOut) {
						case 'left':				
							auxOut1 = 0 - leftUlWidth;
						break;
						case 'right': 
							auxOut1 = divWidth + leftUlWidth;
						break;			
						case 'up': 
							//[***] UP!!!
						break;	
						case 'down': 
							//[***] DOWN!!!
						break;							
					} 
				}
				
				setTimeout(function(){				
					// Animación para mostrar los elementos:			
					$(currentLeftElement).animate({'left' : auxLeft1, opacity: 1, 'width': leftUlWidth + 'px', 'height': leftUlHeight + 'px'}, showDuration);				
					$(currentRightElement).animate({'left' : auxLeft2, opacity: 1, 'width': rightUlWidth + 'px', 'height': rightUlHeight + 'px'}, showDuration);				
			
			
					

					setTimeout(function(){					
						// Animación para ocultar los elementos:						
						$(currentLeftElement).animate({'left' : auxOut1, opacity: 0, 'width': leftUlWidth2 + 'px', 'height': leftUlHeight2 + 'px'}, hideDuration);
						$(currentRightElement).animate({'left' : auxOut2, opacity: 0, 'width': rightUlWidth2 + 'px', 'height': rightUlHeight2 + 'px'}, hideDuration);				
						
						
						setTimeout(function(){							
							currentRight++;
							currentLeft++;
							iteration++;									
							
							if (iteration < opt.loop) {
								doSlider(div);
							}
						},hideDuration);					
						
					},opt.duration*1000 + showDuration);								
					
	
					
				},opt.sleep*1000);
				
			
			
			} // doSlider End
		

		}); // Each End
		
	}; // fn End
	
})(jQuery);



function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
