$(function(){

	$("a.returnFalse").click(function(){ return false; });

	$('select#selBarraGoverno').change(function(){
		var $this = $(this);
		if($this.val() != ""){
			window.open($this.val(),'window','toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes');
		}
		$this[0].selectedIndex = 0;
	});
	
		   
	$.menuRodape();

	var campoBusca = $('input.campoBusca').val();
	$.trocaValorCampoBusca(campoBusca);

	$("#mapaBusca #containerMapa img").css("cursor","pointer").click(function(){
		criaModalMapa();
	});

	$("#rodape #copyright input.buscaRapida").addInitialPhrase($("div#copyright input.buscaRapida").val());

	validaTrocaIdioma();


	//$("#itensRodape > li:nth-child(2)").addClass("noBorder").removeClass("itemPrincipal");
	//$("#itensRodape > li:first-child").remove();
	
	//$("#itensRodape > li:nth-child(1) ul li:nth-child(2)").remove();


});

$(window).bind('load',function(){
	//TROCA O TITULO
	var titulo01 = $('h3#titulo01');
	var titulo02 = $('h3#titulo02');
	var titulo03 = $('h3#titulo03');	
	var tituloMapa = $('h3#tituloMapa');
	var tituloRedes = $('h3#tituloRedes'); 
	var tituloHotsites = $('h3#tituloHotsites');   

	var titulo1 = new Titulo( titulo01[0], titulo01.html(), 'e0a100', "20", "false", "none", 180, 22, '' );
	var titulo2 = new Titulo( titulo02[0], titulo02.html(), '2d794b', "20", "false", "none", 200, 22, '' );
	var titulo3 = new Titulo( titulo03[0], titulo03.html(), '858585', "14", "false", "none", 140, 16, '' );
	var tituloMapa = new Titulo( tituloMapa[0], tituloMapa.html(), '9F9F9F', "18", "false", "none", 245, 20, '' );
	var tituloHotsites = new Titulo( tituloHotsites[0], tituloHotsites.html(), '9F9F9F', "20", "false", "none", 450, 30, '' );
	var tituloRedes = new Titulo( tituloRedes[0], tituloRedes.html(), '9F9F9F', "18", "false", "none", 245, 20, '' );
});

$.trocaValorCampoBusca = function( campoBusca ){
	$('input.campoBusca').focus(function(){
		if( $(this).val() == campoBusca ) { $(this).val('');	}
	}).blur(function(){
		if( $(this).val() == '' ) { $(this).val( campoBusca ); }
	});
}

//MENU RODAPE
$.menuRodape = function(){
	// antigo $('ul#menuRodape li.itemPrincipal').hover(function(){

	$('ul#itensRodape li.itemPrincipal').hover(function(){
		//mouse in
		$(this).children('ul.subItem').fadeIn('fast');
		if ( $.browser.msie ) {
			var correcaoMarginLeft = -1 * $(this).width() -8 ;
			$(this).children('ul.subItem').css({ 'margin-left': correcaoMarginLeft });
		}
	},
	function(){
		//mouse out
		$(this).children('ul.subItem').fadeOut('fast');
	});
}


function criaModalMapa() {
    if ($("#modalMapa").length > 0) {
        $("#modalMapa").modalToggle({zIndex:20000});
        window.scroll(0, 0);
    } else {

	var so = new SWFObject("../global/swf/mapa/mapa.swf","mapaFlash","782","464","8","#FFFFFF");
	so.addParam("quality","high");
	so.addParam("wmode","transparent");
	so.addVariable("servico","../global/xml/mapa.xml");
	so.addVariable("urlIdioma","../global/xml/mapa_" + $("select[@name=__locale] option:selected").val() + ".xml");
	//so.addVariable("servico","../global/swf/mapa/mapa.xml");

        $("body").append("<div id=\"modalMapa\">"+so.getSWFHTML()+"</div>");
        $("#modalMapa").modalToggle({zIndex:20000});
        window.scroll(0, 0);
    }
}

function fechaModalMapa(){
    $("#modalMapa").modalToggle();
}


$.fn.addInitialPhrase = function(frase){
	if(frase){
		this.focus(function(){
			if(this.value == frase){
				this.value = "";
			}
		});
		this.blur(function(){
			if(this.value == ""){
				this.value = frase;
			}
		});
	}
}

function criaModalIdiomas(frase){

	if($('#modalIdiomas').length > 0){
		$('#modalIdiomas').modalToggle({zIndex: 20000});
		window.scroll(0,0);
	} else {
		$('body').append('<div id="modalIdiomas"><a href="#" class="indent">Fechar</a><p>'+frase+'</p></div>')
		$('#modalIdiomas').modalToggle({zIndex: 20000});
		window.scroll(0,0)
	}

	$('#modalIdiomas a').click(function(){
		$('#modalIdiomas').modalToggle();
	});
}

function validaTrocaIdioma(){

	var selected = $('#seletorIdiomas select')[0].selectedIndex;
	$('#seletorIdiomas select').change(function(){

		var validos = $('form[@name=formIdiomas] input').val();
		var rex = eval('/'+this.options[this.selectedIndex].value+'+[^_BR]/');
		if( rex.exec(validos) != null ){
			this.form.submit();
		} else {
			criaModalIdiomas($("input#fraseModalIdiomas").val());
			this.selectedIndex = selected;
		}
		return false;

	});

}
