/*******************************

	SLIDE SHOW NOTICIAS


******************************/
// JavaScript Document
	var contador = 1;
	var prev     = false;

	
	$(function() {
		$('#bannerslide').cycle({
			fx:     'fade',
			timeout: 6000
		});
	});

		function fnPrev(){			
			prev = false;			
		}

	$(function() {
		$('#slideshow').cycle({
			fx:     'turnDown',
			speed:  'fast',
			timeout: 8000,
			pager:  '#nav',
			next:   '#next',			 
    		prev:   '#prev',
			onPrevNextEvent:function(isNext,zeroBasedSlideIndex){
					var cont = zeroBasedSlideIndex + 1; //Número trazido automaticamente pelo cycle que referencia corretamente qual o próximo indice.
					if (isNext==false){ //Só entra aqui dentro quando se aperta o botão de voltar
						prev = true; //Seta a variavel para verdadeiro para não executar a função onBefore
						//Zera todas as imagens
						for(var i=1;i<5;i++)
							document.getElementById("fig"+i).src = "_img/control/inativa.png";	
						//Marca apenas a imagem escolhida			
						document.getElementById("fig"+cont).src = "_img/control/ativa.png";
						contador = cont + 1; //O cont vai de 0 a 3, por isso tenho que somar 1 ao contador que vai de 1 a 4.
						if (contador==5) contador = 1; //Se contador igual a 5 é porque chegou ao fim, volta pra 1
					}else prev = false; //Se for next então prev deve ser falso para executar onBefore
					
					
				},

			
			before:  onBefore,
			after: fnPrev,
			
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
							
				return '#nav li:eq(' + (idx) + ') a';
			}
		});
		

			
	});
	


	function onBefore() {
		if (prev==false){ //Só entra no caso de next manual ou next automático
			
			//Zera todas as imagens
			for(var i=1;i<5;i++)
				document.getElementById("fig"+i).src = "_img/control/inativa.png";						
			document.getElementById("fig"+contador).src = "_img/control/ativa.png";
			//if (contador>1) document.getElementById("fig"+(contador-1)).src = "_img/control/inativa.png";
			//else document.getElementById("fig4").src = "_img/control/inativa.png";
			if (contador==4) contador = 0;
			contador++;
		}
	}
	
	function escolhe(idx){
		for(var i=1;i<5;i++)
			document.getElementById("fig"+i).src = "_img/control/inativa.png";			
		document.getElementById("fig"+idx).src = "_img/control/ativa.png";

	}

		

	$(document).ready(function() {
		$('.convenios').cycle({
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
	});
	
	$(document).ready(function() {		
		
		//Execute the slideShow
		slideShow();
	 
	});
	
	 
	function slideShow() {
	 	
		//Set the opacity of all images to 0
		$('#gallery a').css({opacity: 0.0});
		
		//Get the first image and display it (set it to full opacity)
		$('#gallery a:first').css({opacity: 1.0});
		
		//Set the caption background to semi-transparent
		$('#gallery .caption').css({opacity: 0.7});
	 
		//Resize the width of the caption according to the image width
		$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
		
		//Get the caption of the first image from REL attribute and display it
	//	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	//	.animate({opacity: 0.7}, 400);
		
		//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
		setInterval('gallery()',6000);
		
		
	}
	 
	function gallery() {
		
		//if no IMGs have the show class, grab the first image
		var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
	 
		//Get next image, if it reached the end of the slideshow, rotate it back to the first image
		var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
		
		//Get next image caption
		var caption = next.find('img').attr('rel');	
		
		//Set the fade in effect for the next image, show class has higher z-index
		next.css({opacity: 0.0})
		.addClass('show')
		.animate({opacity: 1.0}, 1000);
	 
		//Hide the current image
		current.animate({opacity: 0.0}, 1000)
		.removeClass('show');
		
		//Set the opacity to 0 and height to 1px
		$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });	
		
		//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
		$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '50px', width: '556px'},500 );
		
		//Display the content
		$('#gallery .content').html(caption);
		
		
		
	}

/*******************************

	MODAL COMENTARIO ENQUETE


******************************/

$(document).ready(function() {	
 
	$('a[name=modal]').click(function(e) {
		e.preventDefault();
		
		var id = $(this).attr('href');
	
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		$('#mask').css({'width':maskWidth,'height':maskHeight});
 
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		$(id).fadeIn(2000); 
	
	});
	
	$('.window .close').click(function (e) {
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
});
