// função para o banner home
destaque2 = 0
function alterna2() {
	qtd2 = $('.imgDestaques2 li').length;
    $('.navegaDestaques2 ul li a').removeClass('ativo');
    $('.navegaDestaques2 ul li a:eq(' + destaque2 + ')').addClass('ativo');
    $('.imgDestaques2 a:not(:eq(' + destaque2 + '))').hide();
    $('.imgDestaques2 a:eq(' + destaque2 + ')').fadeIn();
    destaque2 = destaque2 == (qtd2-1) ? 0 : destaque2 += 1;
}

destaque = 0
function alterna() {
	qtd = $('.imgDestaques li').length;
    $('.navegaDestaques ul li a').removeClass('ativo');
    $('.navegaDestaques ul li a:eq(' + destaque + ')').addClass('ativo');
    $('.imgDestaques ul li a:not(:eq(' + destaque + '))').hide();
    $('.imgDestaques ul li a:eq(' + destaque + ')').fadeIn();
    
	destaque = destaque == (qtd-1) ? 0 : destaque += 1;
}

///////////////
// No Label //
//////////////
noLabel = {
    init: function () {
        $(".noLabel").each(function () {
            $(this).addClass("vazio");
            noLabel.buffer[$(this).attr("id")] = $(this).val();
            $(this).unbind('focus', noLabel.foco).unbind('blur', noLabel.desfoco).bind('focus', noLabel.foco).bind('blur', noLabel.desfoco);
        }).parents("form:eq(0)").unbind('submit', noLabel.formulario).bind('submit', noLabel.formulario);
    },

    buffer: {},

    foco: function (obj) {
        obj = typeof ($(obj).attr("id")) == "undefined" ? this : obj;
        if ($(obj).hasClass("vazio")) {
            if ($(obj).is("textarea")) {
                $(obj).html("");
            } else {
                $(obj).val("");
            }
            $(obj).removeClass("vazio");
        }
    },

    desfoco: function () {
        if ($.trim($(this).val()) == "") {
            if ($(this).is("textarea")) {
                $(this).html(noLabel.buffer[$(this).attr("id")]);
            } else {
                $(this).val(noLabel.buffer[$(this).attr("id")]);
            }
            $(this).addClass("vazio");
        }
    },

    formulario: function () {
        $(".vazio", this).each(function () {
            noLabel.foco(this);
        });
        return true;
    }
}

/////////////////////////
// galeria de Imagens //
///////////////////////
$.extend({
	scrollPages: {
		version: 2.5,
		defaults: {
			elementSize: 100,
			elements:6,
			speed: "slow",
			direction: "up"
		}
	}
});
$.fn.extend({
	scrollPages: function(options){
		options = $.extend({}, $.scrollPages.defaults, options);
		return this.each(function(){
			
			// Definições de variáveis
			var total = $(".scrollMask li",this).length;
			var mask = options.elementSize * options.elements;
			var area = options.elementSize * total;
			var animation = false;
			var obj = this;
			var dimension = options.direction == "left" ? "width" : "height";
			var side = new Array();
			side[0] = options.direction;
			side[1] = options.direction == "up" ? "down" : "right";
			
			$(".scrollButtom",this).css("visibility","visible");
			if(options.elements >= total){
				$(".scrollButtom",this).css("visibility","hidden");
				mask = area;
			}else{
				$(".scrollButtom[rel*='"+options.direction+"']",obj).css("visibility","visible");
			}
			
			// Aplicação de estilos
			if(options.direction == "up"){
				//$(".scrollMask",this).css({"height": mask + "px", overflow: "hidden"}); // M?scara
				$(".scrollMask ul",this).css({"height": area + "px", marginTop: 0}); // UL
				$(".scrollMask li",this).css({"height": options.elementSize + "px", display: "block"}); // LI
			}else{
				//$(".scrollMask",this).css({"width": mask + "px", overflow: "hidden"}); // M?scara
				$(".scrollMask ul",this).css({"width": area + "px", marginTop: 0}); // UL
				$(".scrollMask li",this).css({"width": options.elementSize + "px", display: "block"}); // LI
			}
			if (area <= mask) $(".scrollButtom",obj).css("visibility","visible");
			//if (area <= mask + 596) $(".buttom2",obj).css("visibility","hidden");

			// Função que execulta a rolagem
			var rolagem = function(direction){

				// Variáveis
				var margem = -parseInt($(".scrollMask ul",obj).css("margin-"+(options.direction == "up" ? "top" : "left")));

				var intervalo = direction == side[1]
					? ( margem + ( mask * 2 ) ) < area
						? mask
						: area - (margem + mask)
					: ( margem - mask ) < 0
						? margem
						: mask
				;
				direction = direction == side[1]
					? -margem - intervalo
					: -margem + intervalo
				;
				
				$(".scrollButtom",obj).css("visibility","visible");
				var rel = -direction == 0 ? side[0] : (-direction + mask) == area ? side[1] : null;
				if(rel != null) $(".scrollButtom[rel*='"+rel+"']",obj).css("visibility","visible");
				
				
				//Condição especial para galeria com degrade
				//var rel2 = -direction == 0 ? side[0] : (-direction + mask + 596) == area ? side[1] : null;
				//if(rel2 != null) $(".scrollButtom[rel*='"+rel2+"']",obj).css("visibility","hidden");
				
				// Animação
				animation = true;
				if(options.direction == "up"){
					$(".scrollMask ul",obj).animate({marginTop: direction + "px"},options.speed,function(){ animation = false; });
				}else{
					$(".scrollMask ul",obj).animate({marginLeft: direction + "px"},options.speed,function(){ animation = false; });
				}
			}
			
			// Aplicação de eventos
			$(".scrollButtom",this).unbind("click").click(function(){
				if(!animation){
					var direction = $(this).attr("rel");
					rolagem(direction);
				}
			});
		});
	}
});

var fotos = [];

function contain(nome) {
	for (i = 0; i < fotos.length; i++)
		if(fotos[i].indexOf(nome) >= 0)
			return true;
	
	return false;
}

$(document).ready(function () {
	noLabel.init();

	/* js para destaques da home - defini altura para todas as divs*/
	$('.listaNoticiasHome').each(function () {


		$('.noticia:eq(3)').addClass('noticiaAtivo1');
		$('.noticia:eq(2)').addClass('noticiaAtivo1');
		$('.noticia:eq(1)').addClass('noticiaAtivo1');

		$('.noticia:eq(4)').addClass('noticiaAtivo2');
		$('.noticia:eq(5)').addClass('noticiaAtivo2');
		$('.noticia:eq(6)').addClass('noticiaAtivo2');

	});

	var divs = $('.listaNoticiasHome .noticiaAtivo1');
	var maior = 0;
	for (i = 0; i < divs.length; i++) {
		if ($(divs[i]).height() > maior) {
			maior = $(divs[i]).height();
		}
	}

	for (i = 0; i < divs.length; i++) {
		$(divs[i]).css('height', maior + 'px');
		// $('.listaNoticiasHome .noticia').css('height', maior + 'px');
	}


	var divs2 = $('.listaNoticiasHome .noticiaAtivo2');
	var maior2 = 0;
	for (i = 0; i < divs2.length; i++) {
		if ($(divs2[i]).height() > maior2) {
			maior2 = $(divs2[i]).height();
		}
	}

	for (i = 0; i < divs2.length; i++) {
		$(divs2[i]).css('height', maior2 + 'px');
		// $('.listaNoticiasHome .noticia').css('height', maior + 'px');
	}


	/* js para destaques no rodape da home - defini altura para todas as divs*/
	var divs1 = $('.noticia-destaque2 .noticia2');
	var maior1 = 0;
	for (i = 0; i < divs1.length; i++) {
		if ($(divs1[i]).height() > maior1) {
			maior1 = $(divs1[i]).height();
		}
	}

	for (i = 0; i < divs1.length; i++) {
		$(divs1[i]).css('height', maior1 + 'px');
		// $('.noticia-destaque2 .noticia2').css('height', maior1 + 'px');
	}



	/* $('.noticia-destaque2').each(function () {

	$('.chamadaNoticia2:eq(5)').css('width', '150px !important');
	$('.chamadaNoticia2:eq(6)').css('width', '150px !important');
	$('.chamadaNoticia2:eq(7)').css('width', '150px !important');

	})
	*/
	$('.listaNoticiasHome .noticia').each(function () {

		$('.listaNoticiasHome .noticia:eq(3)').css('background', 'none !important');
		$('.listaNoticiasHome .noticia:eq(6)').css('background', 'none !important');

	})
	$('.listaNoticiasHome').each(function () {

		$('.separaLinha:eq(1)').css('background', 'none !important');

	})
	$('.noticia').each(function () {
		$('.bordanewsLinha:eq(1)').css('background', 'none !important');
	})

	/*$('.listaNoticiasHome .noticia2').each(function () {

	$('.noticia2:eq(2)').css('background', 'none !important');
	$('.noticia2:eq(1)').css('background', 'none !important');
	$('.noticia2:eq(5)').css('background', 'none !important');

	})*/

	$('.noticia').each(function () {
		$('.bordanewsLinha:gt(2)').css('margin-top', '20px !important');
		$('.noticia2:eq(2)').css('background', 'none !important');

	})

	/////////////////////////////////////	
	// Centraliza Menu 1
	/////////////////////////////
	$('.menu1 ul').each(function () {
		var larguraDiv = $(this).parent().width();
		var larguraUl = $(this).width();

		var margem = larguraDiv / 2 - larguraUl / 2;
		$(this).css('margin-left', margem + 'px');
	})

	/////////////////////////////
	// autoTab
	////////////////////////////

	//$('#cep1').autotab({ format: 'number', target: 'cep2' });
	//$('#cep2').autotab({ format: 'number' });

	/////////////////////////////
	// Validar Form
	///////////////////////////

	$('form').submit(function () {
		return validar.form($(this))
	});

	////////////////////////
	// colorBox
	///////////////////////

	$(".ampliar").colorbox();

	////////////////////////////
	// Mini Banner Home  
	//////////////////////////

	alterna2();


	if ($('.navegaDestaques2 ul').is('ul')) {
		intervalo2 = setInterval("alterna2()", 8000);
	}

	$('.navegaDestaques2 ul li a').click(function () {
		var obj2 = $(this).parents('ul:eq(0)');
		var indice2 = $('a', obj2).index(this);
		$('li a', obj2).removeClass('ativo2');
		$(this).addClass('ativo2');
		$('.imgDestaques2 a:not(:eq(' + indice2 + '))').hide();
		$('.imgDestaques2 a:eq(' + indice2 + ')').fadeIn('slow');
	});

	$('.navegaDestaques2').hover(function () {
		clearInterval(intervalo2);
	}, function () {
		intervalo2 = setInterval("alterna2()", 8000);
	});



	///////////////////////////////
	//   TEASER Timer
	//////////////////////////////

	alterna();

	if ($('.navegaDestaques ul').is('ul')) {
		intervalo = setInterval("alterna()", 5000);
	}

	$('.navegaDestaques ul li a').click(function () {
		var obj = $(this).parents('ul:eq(0)');
		var indice = $('a', obj).index(this);
		$('li a', obj).removeClass('ativo');
		$(this).addClass('ativo');
		$('.navegaDestaques ul li', obj).removeClass('ativo');
		$('.imgDestaques a:not(:eq(' + indice + '))').hide();
		$('.imgDestaques a:eq(' + indice + ')').fadeIn('slow');
	});

	//$('.navegaDestaques ul li').addClass('ativo2');
	//$('.navegaDestaques ul li', obj).removeClass('ativo2');


	$('.navegaDestaques').hover(function () {
		clearInterval(intervalo);
	}, function () {
		intervalo = setInterval("alterna()", 5000);
	});



	/////////////////////////////
	// Abre Footer
	/////////////////////////////

	$('.abreFooter').click(function () {
		$('.fechaFooter').addClass('ativo');
		$('.footerConteudo').slideDown('slow');
	});

	$('.fechaFooter').click(function () {
		$(this).removeClass('ativo');
		$('.footerConteudo').hide();
	});

	/////////////////////////////////////
	// Sanfona Perguntas e respostas
	////////////////////////////////////

	$('a.pergunta').click(function () {
		$(this).css('color', '#a91319');
		$(this).siblings('.resposta').slideDown('slow');
	});

	$('a.fechaResposta').click(function () {
		$(this).parents('.resposta').siblings('a.pergunta').css('color', '#737373');
		$(this).parents('.resposta').slideUp('fast');
	});

	/////////////////////////////////////
	// Sanfona Mapas das Regionais
	////////////////////////////////////

	$('a.linkMapa').toggle(function () {
		$(this).addClass('ativo')
		$(this).parents('.boxSede').children('.mapa').animate({ height: "420px" });
	}, function () {
		$(this).removeClass('ativo')
		$(this).parents('.boxSede').children('.mapa').animate({ height: "0" });
	});

	/////////////////////////////////////////
	// Barra de Resultados da Enquete
	////////////////////////////////////////

	$('.respostaPercent').each(function () {
		obj = $(this)
		percent = $('p span.percentResp', obj).text();

		$('.barraInt', obj).css('width', percent);
	});

	////////////////////////////////////////////////
	// Altura dos links da lista de 3 colunas
	///////////////////////////////////////////////

	$('.boxLista3Col li').each(function () {
		var links = $('a', this);
		var maior = 0;
		for (i = 0; i < links.length; i++) {
			if ($(links[i]).height() > maior) {
				maior = $(links[i]).height();
			}
		}

		for (i = 0; i < links.length; i++) {
			$(links[i]).css('height', maior + 'px');
		}
	});

	/////////////////////////////////////////
	// Mostra form Classificados
	///////////////////////////////////////

	$('.abreClassificado').toggle(function () {
		$(this).addClass('ativo');
		$('.form').slideDown('slow');
	}, function () {
		$(this).removeClass('ativo');
		$('.form').slideUp('fast');
	});

	///////////////////////////////////////
	// Galeria FB Resumo
	////////////////////////////////////

	$('.galeriaFB').scrollPages({ elementSize: 139, elements: 5, direction: 'left' });

	///////////////////////////////
	// Sub Menu ServiÃ§os
	/////////////////////////////

	var offset = $('.linkServicos').offset();

	/*if ($('.menuServicos').attr('class') != 'undefined') {
	$('.menuServicos').appendTo('body').css({
	'left': offset.left - 18,
	'top': offset.top + 38
	}) 
	}*/


	$('.linkServicos').hover(function () {
		$('.menuServicos').fadeIn();
	}, function () { })

	$('.menuServicos').hover(function () { }, function () {
		$(this).fadeOut();
	})

	//////////////////////////
	// Amplia Charges
	////////////////////////

	$('.listaCharges li a[rel="charge"]').colorbox();

	///////////////////////////////////////
	// Galeria de Fotos
	////////////////////////////////////

	//$('.galeria').scrollPages({elementSize:42, elements:5, direction:'left'});

	//var fotoInicial = $('.mascaraSlider li a:first').attr('rel');
	//	$('.fotoGrande img').attr('src',fotoInicial);
	//	
	//	$('.mascaraSlider li a').click(function(){
	//		var fotoGrande = $(this).attr('rel');
	//		$('.fotoGrande img').attr('src',fotoGrande);
	//	});


	//ABAS VIDEO
	$('.abas li .link:not(.pesquisar a)').click(function () {
		var obj = $(this).parents('ul:eq(0)');
		var indice = $('.link', obj).index(this);
		$('li .link', obj).removeClass('ativo');
		$(this).addClass('ativo');
		$('.combo > .conteudoAbas:not(:eq(' + indice + '))').hide();
		$('.combo > .conteudoAbas:eq(' + indice + ')').show();
	});

	//ABAS VIDEO 2
	$('.abas2 li a').click(function () {
		var obj = $(this).parents('ul:eq(0)');
		var indice = $('a', obj).index(this);
		$('li a', obj).removeClass('ativo');
		$(this).addClass('ativo');
		$('.combo > .conteudoAbas2:not(:eq(' + indice + '))').hide();
		$('.combo > .conteudoAbas2:eq(' + indice + ')').show();
	});

	//ABAS VIDEO 2
	$('.abas3 li a').click(function () {
		var obj = $(this).parents('ul:eq(0)');
		var indice = $('a', obj).index(this);
		$('li a', obj).removeClass('ativo');
		$(this).addClass('ativo');
		$('.combo > .conteudoAbas3:not(:eq(' + indice + '))').hide();
		$('.combo > .conteudoAbas3:eq(' + indice + ')').show();
	});


	//ABAS VIDEO
	$('.abasNews li .linkNews:not(.pesquisarNews a)').click(function () {
		var objNews = $(this).parents('ul:eq(0)');
		var indiceNews = $('.linkNews', objNews).index(this);
		$('li .linkNews', objNews).removeClass('ativoNews');
		$(this).addClass('ativoNews');
		$('.comboNews > .conteudoAbasNews:not(:eq(' + indiceNews + '))').hide();
		$('.comboNews > .conteudoAbasNews:eq(' + indiceNews + ')').show();
	});

	//ABAS VIDEO 2
	$('.abas2News li a').click(function () {
		var objNews = $(this).parents('ul:eq(0)');
		var indiceNews = $('a', objNews).index(this);
		$('li a', objNews).removeClass('ativoNews');
		$(this).addClass('ativoNews');
		$('.comboNews > .conteudoAbas2News:not(:eq(' + indiceNews + '))').hide();
		$('.comboNews > .conteudoAbas2News:eq(' + indiceNews + ')').show();
	});

	//ABAS VIDEO 2
	$('.abas3News li a').click(function () {
		var objNews = $(this).parents('ul:eq(0)');
		var indiceNews = $('a', obj).index(this);
		$('li a', objNews).removeClass('ativoNews');
		$(this).addClass('ativoNews');
		$('.comboNews > .conteudoAbas3News:not(:eq(' + indiceNews + '))').hide();
		$('.comboNews > .conteudoAbas3News:eq(' + indiceNews + ')').show();
	});


	/////GERA IMAGEM DO YOUTUBE PELO ATRIBUTO 'REL' DO SEU PAI
	/*$('a.youtube img').each(function () {
	$(this).attr({ src: 'http://i4.ytimg.com/vi/' + $(this).parents('a').attr('rel') + '/default.jpg' });
	});*/


	$('.pesquisar a').click(function () {
		$('.abas li a').removeClass('ativo');
		$(this).addClass('ativo');
	});

	$('.pesquisar').css('width', $('.pesquisar').width());
	$('.pesquisar').hover(function () {
		$('.box-pesquisar').fadeIn();
	}, function () {
		$('.box-pesquisar').fadeOut('fast');
	});






	// ////////////////////////////////////
	// GALERIA DE FOTOS
	// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

	$('.galeria').scrollPages({ elementSize: 100, elements: 6, direction: 'left' });

	$(".imagem a.ampliar2").css({ opacity: 0.5 });
	//$(".ampliar, .ampliar2").click(function(){
	//		$("img.ampliada").fadeOut();	
	//});
	var titulo = $('.mascaraSlide ul li:first img').attr('alt');
	$('.galeriaConteudo #legenda').html(titulo);

	$(".ampliar3, .ampliar2").click(function () {

		$("img.ampliada").hide();

		obj = $(this).parents(".internasDir");
		if (!$(".imagem a", $(this).parents(".conteudoRolagem:eq(0)")).hasClass("ativo")) {
			$(".imagem a.ativo", obj).removeClass("ativo").animate({ opacity: 0.5 });
			$(".imagem a", $(this).parents(".conteudoRolagem:eq(0)")).addClass("ativo").animate({ opacity: 1 });

			//$("img.ampliada", obj).attr("src", "imagens/blank.gif");

			src = $(this).attr("rel");
			legen = $(this).parents(".conteudoRolagem").find(".legenda").html();

			if (contain(src)) {
				$("img.ampliada", obj).attr("src", src).fadeIn(800);
			}
			else {
				img = new Image().src = src;

				$(".carregando", obj).show();
				$("img.ampliada").attr("src", src).load(function () {

					$("img.ampliada").fadeIn(800);
					$(".carregando", obj).hide();
				});

				fotos.push(src);
			}


			//$("img.ampliada").fadeIn(800); 
			/*$("img.ampliada", obj).attr("src", src).fadeIn(800);
			$(".carregando", obj).hide();*/


			// Colocar Legenda da Foto -------------------
			if (typeof legen === "undefined") {
				$("#legenda").html($(this).attr("title"));
			} else {
				$("#legenda").html(legen);
			}

			// Exibe o Autor da Foto no Title do hLink
			$(".title_amp").attr("title", $(this).attr("author"));
			//-------------------------------------------


			//img.onload = function(){
			//				var altura	= img.height;
			//				var largura = img.width;
			//				
			//				if(altura < 379){
			//					margem = parseInt((379 - altura) / 2)
			//					$("img.ampliada", obj).css("margin",margem+"px auto 0 auto");
			//				}else{
			//					$("img.ampliada", obj).css("margin","0 auto");
			//				}
			//				$(".carregando", obj).hide();
			//				$("img.ampliada", obj).attr("src",src);
			//			}

			var rolar = parseInt($(".scrollMask ul").css("margin-left"));

			var valor = 1;
			var valor2 = 0;
			var thumb = parseInt($(".scrollMask ul li")).width;
			var totalThumb = $(".scrollMask ul li").lenght;
			totalThumb = totalThumb * thumb;

			$(".ampliar2", obj).each(function () {
				if ($(this).attr("class") == "ampliar2 ativo") {
					valor2 = valor;
				} else {
					valor = valor + 1;
				}
			});

			if ($(".mudarFoto").attr("rel") == "right") {
				valor2 = valor2 + 1;
			} else {
				valor2 = valor2 - 1;
			}

			if (valor > 5) {
				$(".seta[rel=left]", obj).css("visibility", "visible");
				$(".seta[rel=right]", obj).css("visibility", "visible");
			}

			if (valor2 > 0) {
				$('.boxDescricao:not(:hidden)').slideUp('fast');
			}

			//valor 148 - tamanho da thumb
			//valor 444 - tamanho total das thumbs
			if (valor2 > 2 && valor2 + 2 < valor) {
				rolar = valor2 * thumb - (thumb * 3);
			} else if (valor2 <= 2) {
				rolar = 0;
				$(".seta[rel=left]", obj).css("visibility", "visible");

				//valor 148 - tamanho da thumb

			} else if (valor2 + 2 >= valor) {
				rolar = valor * thumb - totalThumb;
				$(".seta[rel=right]", obj).css("visibility", "visible");
			}
			//alert(valor);
			//alert(rolar);
			$(".scrollMask ul").animate({ marginLeft: -rolar + "px" });


		} else {
			$("img.ampliada").show();
		}


	});

	$(".internasDir .ampliar3:eq(0)").click();
	$.post("Fotos.aspx", { id_foto: $(".internasDir .ampliar3:eq(0)").parent().attr("id") });


	$(".mudarFoto").click(function () {
		var obj = $(this).parents("div.internasDir");
		var valor = 0;
		var valor2 = 0;
		$(".ampliar2", obj).each(function () {
			if ($(this).attr("class") == "ampliar2 ativo") {
				valor2 = valor;
			} else {
				valor = valor + 1;
			}
		});

		if ($(this).attr("rel") == "right") {
			valor2 = valor2 + 1;
		} else {
			valor2 = valor2 - 1;
		}

		$(".ampliar3:eq(" + valor2 + ")", obj).click();

	});

	/*video*/
	//$("#player_video").addFlash({ src: "swf/Player.swf", src: "expressInstall.swf", width: 480, height: 384, value: "transparent", name: "wmode" });

	/*div de bloqueio*/
	$(".linkEmail").click(function () {
		bloqueia({ speed: "fast", bgcolor: "#5b5b5b" });
		$(".box-enviar-email").appendTo('body form').absoluteCenter({ animation: false }).absoluteCenter().show();
	});

	$(".fechaBloqueio, .cancelButton").live("click", function () {
		$(".box-enviar-email").hide();
		$(".envioBox").hide();
		bloqueia({ speed: "slow", evento: "fim" });
		$(window).unbind("scroll");
		$(window).unbind("resize");
	});

//	$(".fechaBloqueio, .cancelButton").click(function () {
//		$(".envioBox").hide();
//		bloqueia({ speed: "slow", evento: "fim" });
//		$(window).unbind("scroll");
//		$(window).unbind("resize");
//	});

});
