// JavaScript Document

// Popup home
function somepop() {
	$("#popmsg").slideUp("slow");
}
function surgepop() {
	$("#popmsg").slideDown("normal");
}
function createpopup(adiv,arquivo,w,h) {
	var oiframe ='<div class="barracmd"><a href="javascript:somepop()">[X] Fechar</a></div>\n'
				+'<iframe frameborder="0" width="100%" height="90%" id="fr_msg" src="'+arquivo+'"></iframe>\n';
	   pai = document.getElementById(adiv);
	   elem = document.createElement("div");
	   elem.setAttribute("id", "popmsg");
	   elem.setAttribute("class", "popmsg");
	   if(w!='')elem.style.width=w+'px';
	   if(h!='')elem.style.height=h+'px';
	   txt = document.createTextNode(oiframe);
	   elem.innerHTML = oiframe;
	   pai.appendChild(elem);
	   var tempo1 = setTimeout('surgepop()', 2000);
	   var tempo2 = setTimeout('somepop()', 60000);
	   
}

jQuery.preloadImages = function() {
  for(var i=0; i<arguments.length; i++)   {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function mudabg() {  // Revesa background Home
	var tot_bg = 6; // Total de imagens
	
	var numero = Math.round(Math.random()*tot_bg);
	while(numero<2) numero = Math.round(Math.random()*tot_bg);
	var urlImage = 'url(' + 'img/bghome/h_bg_conteudo' + parseInt(numero) + '.jpg)';
	window.document.getElementById('bx_conteudo').style.backgroundImage = urlImage;
}

// jQuery

$(document).ready(function() {
	
	
	$.preloadImages("img/pop-peq.jpg","img/pop-peq.jpg","img/pop-grd-2.jpg");
	
	if($('#bx_conteudo').length>0) { // Chama bg home
		mudabg();
		if($('body').width() < 1000) { // Scroll na home para resol. < 1024
			$('body').css('overflow','auto');
		};
	}
	
	// Lista serviços
	$('.servicos_js').find('dd').hide().end().find('dt').attr('className','dt_off').click(function() {
         var resposta = $(this).next();
         if (resposta.is(':visible')) {
             resposta.slideUp();
			 $(this).attr('className','dt_off');
         } else {
             resposta.slideDown();
			 $(this).attr('className','dt_on');
         }
	});
	
	//focus no primeiro campo
	$("#f_contato :input:visible:enabled:first").focus();
	
	// fix for target="_blank"
	$("a[rel='external']").click(function(event){
	event.preventDefault(); 
	window.open($(this).attr("href"), 'janela','scrollbars=yes,resizable=yes,width=850,height=600');
	return false;
	});
	
	// links para imagens em popup
	$("a[href$=jpg]").click(function() { 
		var tmpImg = new Image();
		tmpImg.src=$(this).attr("href");
		var wImg = $(tmpImg).attr("width");
		var hImg = $(tmpImg).attr("height");
		if (wImg<10) wImg=500;
		if (hImg<10) hImg=400;

		var hImg = $(tmpImg).attr("height");
		var atrJan = 'scrollbars=yes,resizable=yes,width='+(wImg+50)+',height='+(hImg+50);

		window.open($(this).attr("href"), 'popup',atrJan);
		return false;
	});
	
	// legenda ajuda
	$("a[title='?']").toggle(function() {
		$("#legenda").slideDown("medium");
	},function() {
		$("#legenda").slideUp("medium");
	});
	$("a[title='close']").click(function() {
	  $("a[title='?']").trigger('click');
	})

	if($("#abobora").length>0) {
		$(window).scroll(function() {
			$("#abobora").css("bottom", "-"+$(document).scrollTop()+"px");
			$("#abobora").css("right", "-"+$(document).scrollLeft()+"px");
		});
	}

	$('.over').hover(
		function() { this.src = this.src.replace("_off","_on");  },
		function() {  this.src = this.src.replace("_on","_off"); }  
	)
	
	// Popu-up arrastável
	$("#popmsg").draggable();
	
	// Fecha animação flash home
	$("#bt_fechar").click(function() {
		$(this).parent().fadeOut(function() { setTimeout(function() {
				$("#abobora").fadeOut().delay(1000).fadeIn('fast').delay(500).fadeOut('slow'); }, 1000) });
	})
});


