$(function(){
	
		
	imgWidth = 960;
	imgHeight = 370;
	startAtImage = 3;
	slideShowTotalWidth = $('.marquee_panels').find('.marquee_panel').size() * imgWidth;
	imgSlideSpeed = 500;
	infoBoxFadeSpeed = imgSlideSpeed / 2;
	animating = false;
	intervaloDaRotacao = 6000;
	mouseIsOver = false;


		
// --- Set CSS --- //

		$('.marquee_photos').css( {
			width: slideShowTotalWidth, 
			display: 'block'			 
		});
		
		$('.marquee_photos img').css('float', 'left');
		
		
// --- Ciclo que injecta as imagens no respectivo lugar --- //

		$('.marquee_panels').find('.marquee_panel').each(function(){
			
			thisSrc = $(this).find('img').attr('src');
			
			$('.marquee_photos').append('<img src="'+thisSrc+'" width="'+imgWidth+'" height="'+imgHeight+'" />');
			$('.marquee_nav').append('<a class="marquee_nav_item">&nbsp;</a>');
		});
		
		
		$('.marquee_photos').css('left', -(imgWidth * (startAtImage - 1)));
		$('.marquee_nav_item:nth-child('+startAtImage+')').addClass('selected');
		$('#info').html( $('.marquee_panel:nth-child('+startAtImage+')').find('.destaqueDynamicText').html() );
		
		
		
		
// ------- CLICK -------- //		
		
		
		
		$('.marquee_nav_item').click(function(event){
			clearInterval(imgSlideInterval);
			$this = $(this);
			thisIndex = $this.index();
			thisImgPos = -(thisIndex * imgWidth);
			
			$('.marquee_photos').animate( { left: thisImgPos +'px' }, imgSlideSpeed, function(){
				//imgSlideInterval = setInterval('slideToNextImage()', intervaloDaRotacao);
			});
			$('.marquee_nav .selected').removeClass('selected');
			$this.addClass('selected');
			
			$('#info h1, #info p').fadeOut(infoBoxFadeSpeed, function(){
				$('#info').html( $('.marquee_panel:nth-child('+(thisIndex + 1)+')').find('.destaqueDynamicText').html() );
				$('#info h1, #info p').css('display', 'none');
				$('#info h1, #info p').fadeIn(infoBoxFadeSpeed);
			});
		});
		
		
		
		$('#slideShowWraper').mouseover(function(){
			clearInterval(imgSlideInterval);
			mouseIsOver = true;
		});
		
		$('#slideShowWraper').mouseleave(function(){
			imgSlideInterval = setInterval('slideToNextImage()', intervaloDaRotacao);
			mouseIsOver = false;
		});
		
		
	imgSlideInterval = setInterval('slideToNextImage()', intervaloDaRotacao);
	
});






function slideToNextImage() {
	clearInterval(imgSlideInterval);
	imgCurrentLeftValue = parseInt($('.marquee_photos').css('left'));
	imgCurrentPos = ($('.marquee_nav .selected').index()) + 1;
	imgNextPos = -(imgCurrentPos * imgWidth);
	
	if(imgCurrentLeftValue <= -(slideShowTotalWidth - imgWidth)) {
		$('.marquee_photos').animate( { left: 0 }, imgSlideSpeed, function(){
			if(mouseIsOver == true) {
				
			}
			else {
				imgSlideInterval = setInterval('slideToNextImage()', intervaloDaRotacao);
			}
		});
		$('.marquee_nav .selected').removeClass('selected');
		$('.marquee_nav a:first').addClass('selected');
		
		$('#info h1, #info p').fadeOut(infoBoxFadeSpeed, function(){
			$('#info').html( $('.marquee_panel:nth-child(1)').find('.destaqueDynamicText').html() );
			$('#info h1, #info p').css('display', 'none');
			$('#info h1, #info p').fadeIn(infoBoxFadeSpeed);
		});
	}
	else {
		$('.marquee_photos').animate( { left: imgNextPos }, imgSlideSpeed, function(){
			if(mouseIsOver == true) {
				
			}
			else {
				imgSlideInterval = setInterval('slideToNextImage()', intervaloDaRotacao);
			}
		});
		$('.marquee_nav .selected').next().addClass('selected');
		$('.marquee_nav .selected').prev().removeClass('selected');
		
		
		$('#info h1, #info p').fadeOut(infoBoxFadeSpeed, function(){
			$('#info').html( $('.marquee_panel:nth-child('+(imgCurrentPos + 1)+')').find('.destaqueDynamicText').html() );
			$('#info h1, #info p').css('display', 'none');
			$('#info h1, #info p').fadeIn(infoBoxFadeSpeed);
		});
		
	}
}


	









noticiasSlideIntervalo = 5000; // valor em milisegundos

$(document).ready(function(){

	var marqueeNoticiaInterval = setInterval('proximaNoticia()',noticiasSlideIntervalo);
	
	var marqueePosition = 0;
	var marqueeTotalWidth = 0;
	
	$('.noticiasMarqueeAbsolutePositioning .destaqueNoticia').each(function(){
		marqueeTotalWidth = marqueeTotalWidth + 800;
	});
	
	var prevStopAt = -(marqueeTotalWidth) + 800;

	
	$('.noticiasMarqueeAbsolutePositioning').css('width', marqueeTotalWidth);
	
	
	$('.noticiasMarqueeNext').bind('click', function(event){
		event.preventDefault();
		if(marqueePosition == prevStopAt) {
		}
		else {
			marqueePosition = marqueePosition - 800;
			$('.noticiasMarqueeAbsolutePositioning').animate( { left: marqueePosition } );
			$('.noticiaMarqueePrevDisabled').addClass('displayNone');
			$('.noticiaMarqueePrevEnabled').removeClass('displayNone');
			
			if(marqueePosition == prevStopAt) {
				$('.noticiaMarqueeNextEnabled').addClass('displayNone');
				$('.noticiaMarqueeNextDisabled').removeClass('displayNone');
			}
		}
	});
	
	
	$('.noticiasMarqueePrev').bind('click', function(event){
		event.preventDefault();
		if(marqueePosition == 0) {
		}
		else {
			marqueePosition = marqueePosition + 800;
			$('.noticiasMarqueeAbsolutePositioning').animate( { left: marqueePosition }, 200 );
			$('.noticiaMarqueeNextDisabled').addClass('displayNone');
			$('.noticiaMarqueeNextEnabled').removeClass('displayNone');
			if(marqueePosition == 0) {
				$('.noticiaMarqueePrevEnabled').addClass('displayNone');
				$('.noticiaMarqueePrevDisabled').removeClass('displayNone');	
			}
		}
	});

	$('#noticiaDestaqueWraper').bind('mouseover', function(){
		clearInterval(marqueeNoticiaInterval);
	});
	
	$('#noticiaDestaqueWraper').bind('mouseout', function(){
		marqueeNoticiaInterval = setInterval('proximaNoticia()',noticiasSlideIntervalo);
	});

});



function proximaNoticia() {
	var marqueeTotalWidth = 0;
	$('.noticiasMarqueeAbsolutePositioning .destaqueNoticia').each(function(){
		marqueeTotalWidth = marqueeTotalWidth + 800;
	});
	
	marqueeTotalWidth = marqueeTotalWidth - 800;
	marqueeTotalWidth = -(marqueeTotalWidth);
	marqueeTotalWidth = marqueeTotalWidth +'px'
	
	var divPostion = $('.noticiasMarqueeAbsolutePositioning').css('left');
	
		if(divPostion == marqueeTotalWidth){
			$('.noticiasMarqueeAbsolutePositioning .destaqueNoticia').each(function(){
				$('.noticiasMarqueePrev:first').click();
			});
		}
		else {
			$('.noticiasMarqueeNext:first').click();
		}	
	}
