var tiempo=7000;

jQuery.fn.rotar = function() {
	//calcular total_entries
	var currentImage = 0, oldImage = 0; 
	var pause='';
	//alert('rotar');
	var $container = $(this); 
	//alert('id animar '+$container.attr('id'));
	var total_entries=$container.find('div[id*=slide]').length;
		
	//alert($container.attr('id')+' imagenes '+total_entries);
	var imageRotate = function() { 
		//alert('id animar '+$container.attr('id'));
		//alert ('oldimage '+oldImage+' current '+currentImage);
		if (total_entries > 1) {
		currentImage = oldImage + 1; 
		if (oldImage >= (total_entries-1)) {
			currentImage=0;
		}
		//alert ( 'fadeout '+oldImage+' fadeIN' +currentImage);
		//coger para los $container!!
		//coger por class las imagenes?? img.someClass
		//oldImage fades-out currentimage fadesin
		//$container.find('div[id*=slide]').eq(oldImage).fadeOut(3000);
		$container.find('div[id*=slide]').eq(oldImage).fadeOut(3000);
		$container.find('div[id*=ss_texto_]').eq(currentImage).animate({'height':'toggle','opacity':'1.0'},3000);//3000 es el tiempo que tarda en hacer el animate
	//$container.find('div[id*=ss_texto_]').eq(oldImage).css('display','none');
	//$container.find('div[id*=ss_texto_]').eq(oldImage).fadeOut('slow');
		$container.find('div[id*=ss_texto_]').eq(oldImage).animate({'opacity':'0.4'},3000).fadeOut('300');
		$container.find('div[id*=slide]').eq(currentImage).animate({'height':'toggle'},3000).fadeIn(3000);
		//$container.find('div[id*=ss_texto_]').eq(currentImage).css('display','block');
		
		oldImage = currentImage;
		pause = setTimeout(imageRotate, tiempo);
		//alert(oldHeadline+'.. old (abajo) current (arriba)  '+currentHeadline); 
		}
	}
	pause = setTimeout(imageRotate, tiempo);
	
	$container.find('a.ssImagen').click( function() {
		clearTimeout(pause);
		//buscar cual es 
		//alert('Handler for .click() called. '+this+' '+$(this).attr('id'));
		var currentSlide=$(this).attr('id');
		currentSlide=currentSlide.substr(currentSlide.indexOf('_')+1,currentSlide.length);
		//poner ese como por defecto
		if (currentImage == currentSlide) {
			//no hace nada
		} else {
			currentImage=currentSlide*1;
			$container.find('div[id*=slide]').eq(oldImage).fadeOut('slow');
			$container.find('div[id*=ss_texto_]').eq(currentImage).css('display','block');
			$container.find('div[id*=ss_texto_]').eq(oldImage).css('display','none');
			$container.find('div[id*=slide]').eq(currentImage).fadeIn('slow');
			oldImage = currentImage*1;
		}
		pause = setTimeout(imageRotate, tiempo);
		return false;
	})
	
}
jQuery.fn.rotar2 = function() {
	//calcular total_entries
	var currentImage = 0, oldImage = 0; 
	var pause='';
	//alert('rotar');
	var $container = $(this); 
	//alert('id animar '+$container.attr('id'));
	var total_entries=$container.find('div[id*=slide]').length;
		
	//alert($container.attr('id')+' imagenes '+total_entries);
	var imageRotate = function() { 
		//alert('id animar '+$container.attr('id'));
		//alert ('oldimage '+oldImage+' current '+currentImage);
		if (total_entries > 1) {
		currentImage = oldImage + 1; 
		//alert('total entries '+total_entries_r);
		if (oldImage >= (total_entries-1)) {
			currentImage=0;
		}
		//alert ( 'fadeout '+oldImage+' fadeIN' +currentImage);
		//coger para los $container!!
		//coger por class las imagenes?? img.someClass
		//oldImage fades-out currentimage fadesin
		//$container.find('div[id*=slide]').eq(oldImage).fadeOut(3000);
		$container.find('div[id*=slide]').eq(oldImage).fadeOut(3000);
		//$container.find('div[id*=ss_texto_]').eq(currentImage).animate({'height':'toggle','opacity':'1.0'},3000);//3000 es el tiempo que tarda en hacer el animate
	//$container.find('div[id*=ss_texto_]').eq(oldImage).css('display','none');
	//$container.find('div[id*=ss_texto_]').eq(oldImage).fadeOut('slow');
		//$container.find('div[id*=ss_texto_]').eq(oldImage).animate({'opacity':'0.4'},3000).fadeOut('300');
		$container.find('div[id*=slide]').eq(currentImage).animate({'height':'toggle'},3000).fadeIn(3000);
		//$container.find('div[id*=ss_texto_]').eq(currentImage).css('display','block');
		
		oldImage = currentImage;
		pause = setTimeout(imageRotate, tiempo);
		//alert(oldHeadline+'.. old (abajo) current (arriba)  '+currentHeadline); 
		}
	}
	pause = setTimeout(imageRotate, tiempo);
	
	$container.find('a.ssImagen').click( function() {
		clearTimeout(pause);
		//buscar cual es 
		//alert('Handler for .click() called. '+this+' '+$(this).attr('id'));
		var currentSlide=$(this).attr('id');
		currentSlide=currentSlide.substr(currentSlide.indexOf('_')+1,currentSlide.length);
		//poner ese como por defecto
		if (currentImage == currentSlide) {
			//no hace nada
		} else {
			currentImage=currentSlide*1;
			$container.find('div[id*=slide]').eq(oldImage).fadeOut('slow');
			//$container.find('div[id*=ss_texto_]').eq(currentImage).css('display','block');
			//$container.find('div[id*=ss_texto_]').eq(oldImage).css('display','none');
			$container.find('div[id*=slide]').eq(currentImage).fadeIn('slow');
			oldImage = currentImage*1;
		}
		pause = setTimeout(imageRotate, tiempo);
		return false;
	})
	
}


