var time;

function initializeTextAnimation(){
	clearTimeout(time);
	jQuery('div.imagesCaptions .flying-text').css({opacity:0}); //set all text opacity to 0
	jQuery('div.imagesCaptions .flying-text').css({marginLeft:'-100px'});
	jQuery('div.imagesCaptions').show();
	
	var nextText = jQuery('div.imagesCaptions .active-text');
	var tam = nextText.width();
	tam = 650 -tam;

	jQuery('div.imagesCaptions .active-text').animate({opacity:1, marginLeft: tam+'px'}, 3000); //animate first text
	time = setTimeout(changeText, 5000); // call changeText function every 5 seconds
}
function changeText(){

	var activeText = jQuery('#imagesCaptions .active-text'); //get current text
	var nextText = activeText.next();  //get next text
	
	if(activeText.next().length == 0){
		/*
		nextText = jQuery('#imagesCaptions .flying-text:first'); //if it is last text, loop back to first text
		jQuery('div.imagesCaptions .flying-text').css({opacity:0}); //set all text opacity to 0
		jQuery('div.imagesCaptions .flying-text').css({marginLeft:'-100px'});
		*/
		
	}else{
		//animate next text
		var tam = nextText.width();
		
		tam = 650 -tam;
		
		/*ROUND */
		tam = Math.round(tam*Math.pow(10,2))/Math.pow(10,2);
		
		nextText.css({opacity: 0}).addClass('active-text').animate({opacity:1, marginLeft: tam+'px'}, 3000, function(){
								activeText.removeClass('active-text');

								
						});
		 time = setTimeout(changeText, 5000);
	}
}

