$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

function goPage(pag,div){
	$.ajax({
		type: "GET",
		url: pag,
		success: function(texto){
			$("#"+div).html(texto);
		},
		error: function(){
			$("#"+div).html('erro');
		}
	});
}

function goPageEnqueteResult(pag,div){
	$.ajax({
		type: "GET",
		url: pag,
		success: setTimeout(function(texto){
			$("#"+div).html(texto);
		}, 2000)
	});
}

function showHide(show, hide) {
	$(show).show('fast');
	$(hide).hide('fast');
}

///////////////// PAGINACAO EM AJAX /////////////////

function processReqChange() {
	if (req.readyState == 1) {
		document.getElementById('pagina').innerHTML = 'Carregando...';
	} else if (req.readyState == 4) {
		if (req.status ==200) {
			document.getElementById('pagina').innerHTML = req.responseText;
		} else {
			alert("Houve um problema ao obter os dados: " + req.statusText);
		}
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

//////////////////// EXECUTAR ACAO ////////////////////
function executeAcao (acao, id_registro) {
	var head = document.getElementsByTagName('head').item(0);
	var eScript = document.createElement("script");
	if (id_registro)
		eScript.setAttribute('src','includes/contador.php?acao='+acao+'&idfoto='+id_registro);
	head.appendChild(eScript);
}

//////////////////// EXIBE / ESCONDE DIV ////////////////////
function displayDiv(div) {
	$("#"+div).animate({"height": "toggle"}, { duration: 200 });
}

//////////////////// POP UP ////////////////////

function popupWindow(url, nome, largura, altura) {
	window.open(url, nome, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+largura+',height='+altura+',screenX=20,screenY=20,top=20,left=20');
}

///////////////// ALTERNAR CAMPOS NO FORMULARIO /////////////////

function muda_campo (tamanho, campo, prox) {
	if (document.getElementById(campo).value.length == tamanho) {
		document.getElementById(prox).focus();
	}
}

///////////////// VERIFICA VERS&atilde;O DO FLASH /////////////////
function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}


/*
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
			FUNÇÃO PARA FORMATAÇÃO DE MASCARA DO CAMPO DO JEITO QUE COLOCAR
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
*/

function txtBoxFormat(objeto, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
if(document.all) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(document.layers) { // Nestcape
nTecla = evtKeyPress.which;
}else{
nTecla = evtKeyPress.which;
}
sValue = objeto.value;
// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( ":", "" );
sValue = sValue.toString().replace( ":", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;
i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;
if (nTecla != 8) { // backspace
while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}
i++;
}
objeto.value = sCod;
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
return true;
} }
else {
return true;
}
}

	function triggerClose(){
		$("#anuncio_hom").fadeOut("slow");
		
	}
	
	function triggerShow(){
		$("#anuncio_hom").fadeIn("slow");
		
		autoClose();
	}

	function autoClose(){
		setTimeout("triggerClose()",8000);
	}
	
	function autoShow(){
		setTimeout("triggerShow()",4000);
	}
	
	function autoHideFale(){
		setTimeout("triggerCloseFale()",4000);
	}
	
	function triggerCloseFale(){
		$(".mensagem_fale_conosco").fadeOut("slow");
	}

$(document).ready(function(){
	$("a[rel='galeria']").colorbox({slideshow:true});
	
	$('#menucanais').mouseover(function() {
		$('#divMenu').show();
		$(this).css({
			'background-color' : '#7FAC15'
		});
	});

	//////////////////// MASCARAS ////////////////////
	$('#fcpf').setMask('999.999.999-99');
	$.mask.masks = $.extend($.mask.masks,{
		telefone:{ mask: '(99) 9999-9999' },
		cep:{ mask: '99999-999' }
	});

	$(".fancy").fancybox({
		'padding': 	0,
		'height': 	800,
		'width':	660,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".fancy2").fancybox({
		'padding': 	0,
		'height': 	500,
		'width':	600,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".fale_conosco_pop").fancybox({
		'padding': 	0,
		'height': 	282,
		'width':	484,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
	
	$(".embreve").fancybox({
		'padding': 	0,
		'height': 	100,
		'width':	484,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
	
	$(".qmsomos").fancybox({
		'padding': 	0,
		'height': 	400,
		'width':	630,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});	
		
	$(".fancy_indiqueartigo").fancybox({
		'padding': 	0,
		'height': 	530,
		'width':	361,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".fancy_indiqueshop").fancybox({
		'padding': 	0,
		'height': 	290,
		'width':	361,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".newsletter_pop").fancybox({
		'padding': 	0,
		'height': 	290,
		'width':	360,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".seguro_pop").fancybox({
		'padding': 	0,
		'height': 	590,
		'width':	460,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});
		
	$(".pagamento_fancy").fancybox({
		'padding': 	0,
		'height': 	600,
		'width':	630,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});

	$(".respsocial").fancybox({
		'padding': 	0,
		'height': 	330,
		'width':	630,
		'type': 'iframe',
		'scroll': 'no',
		'showCloseButton' : false
	});		
		
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
   		return '<div style="z-index:9999; bottom: none; float: left; top:-10px; width:100%; left:0px; text-align:left; background:#fff; font-size: 18px; font-weight: bold;"><div style="padding: 0px; z-index: 1500;">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div></div><div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="fancybox/closelabel.gif" /></a></span>Imagem ' + (currentIndex + 1) + ' de ' + currentArray.length + '</div>';
	}
		
	$("a.img_gal").fancybox({
		'padding': 10,
		'showCloseButton': false,
		'titlePosition': 'inside',
		'titleFormat': 	function formatTitle(title, currentArray, currentIndex, currentOpts) {
   			return '<div style="z-index:9999; bottom: none; float: left; top:-10px; width:100%; left:0px; text-align:left; background:#fff; font-size: 18px; font-weight: bold;"><div style="padding: 0px; z-index: 1500;">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div></div><div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="fancybox/closelabel.gif" /></a></span>Imagem ' + (currentIndex + 1) + ' de ' + currentArray.length + '</div>';
		}, 
		'onComplete': function() { $("#fancybox-title").css({'top':'0'}); }
	});
	
	$("a.img_gal2").fancybox({
		'padding': 10,
		'showCloseButton': false,
		'titlePosition': 'inside',
		'titleFormat': 	function formatTitle(title, currentArray, currentIndex, currentOpts) {
   			return '<div style="z-index:9999; bottom: none; float: left; top:-10px; width:100%; left:0px; text-align:left; background:#fff; font-size: 18px; font-weight: bold;"><div style="padding: 0px; z-index: 1500;">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div></div><div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();" style="position:absolute; bottom:5px; right:12px; color:#000000">Fechar</a></span>'+'<div style=" position:absolute; bottom:5px; left:12px;">Imagem ' + (currentIndex + 1) + ' de ' + currentArray.length +'</div>'+'</div>'+'<a href="javascript:;" onclick="$.fancybox.next();" id="fancybox-right">Próximo</a>'+'<a href="javascript:;" onclick="$.fancybox.prev();" id="fancybox-left">Anterior </a>';
		}, 
		'onComplete': function() { $("#fancybox-title").css({'top':'0'}); }
	});
		
	$(".img_gala").fancybox({'showCloseButton': false});

	$("a.single_image").fancybox();

	$("#erro_box").fancybox({
		'padding': 	0,
		'height': 	600,
		'width':	500,
		'type': 'inline',
		'scroll': 'no',
		'showCloseButton' : false
	});

	$(".fancy3").fancybox({
		'padding': 	0,
		'height': 	600,
		'width':	500,
		'type': 'iframe',
		'scroll': 'no',
		'href': 'popup/termos_agenda.php',
		'showCloseButton' : false
	});
    
    
	$('.previsu').click(function(){
		var vImage=$('#fileField').val();
		vHeight=170;
		if(vImage!=''){
			vHeight=vHeight+20;
		}
		var vUrl=$('#flink').val();
		if(vUrl!=''){
			vHeight=vHeight+20;
		}
		$.fancybox({
			'padding': 	0,
			'height': 	vHeight,
			'width':	620,
			'type': 'iframe',
			'scroll': 'no',
			'centerOnScroll': true,
			'href': 'popup/previsu.php?image='+vImage+'&amp;url='+vUrl,
			'showCloseButton' : false
		});
    });
		
	$(".f2").fancybox({
		'padding': 	10,
		'height': 	600,
		'width':	530,
		'type': 'iframe',
		'scroll': 'no',
		'href': 'preview.php',
		'showCloseButton' : false
	});
		
	var titulo = '';
		
	$(".fp").click(function(){
		
	if($('#ftipo_pessoa').val() == 'j'){
		titulo = $('#fnome_fantasia').val()
	} else if($('#ftipo_pessoa').val() == 'f') {
		titulo = $('#fnome_empresa').val()
	} 
			
	var telefone_contato = '';
			
	if($('#fddd_1').val() != '' && $('#ftelefone_1').val() != '' ) {
		telefone_contato = ' ('+$('#fddd_1').val()+') '+$('#ftelefone_1').val()+' ';
	} 
			
	if($('#fddd_2').val() != '' && $('#ftelefone_2').val() != '' ) {
		telefone_contato += '/ ('+$('#fddd_2').val()+') '+$('#ftelefone_2').val()+' ';
	} 
			
			
	$.fancybox({
		'padding': 	10,
		'height': 	600,
		'width':	530,
		'content':  '<span class="bloco titulo">'+titulo+'</span><span class="bloco descricao">'+$('#fdescricao').val()+'</span><span class="bloco endereco">'+$('#fendereco').val()+'. '+$('#fcidade').val()+','+$('#fuf').val()+'. Telefone:' +telefone_contato+'</span>',
		'scroll': 'no'
	});
	});


	$('.preview').click(function(){
		$.post('preview.php',{nome_fantasia:$('#fnome_fantasia').val(),
							  nome_empresa:$('#fnome_empresa').val(),
							  endereco:$('#fendereco').val(),
							  cidade:$('#fcidade').val(),
							  uf:$('#fuf').val(),
							  ddd_1:$('#fddd_1').val(),
							  telefone_1:$('#ftelefone_1').val(),
							  ddd_2:$('#fddd_2').val(),
							  telefone_2:$('#ftelefone_2').val(),
							  descricao:$('#fdescricao').val()
							  },
		function(data){
			popupWindow('previsu.php?token='+data.token, 'previsu', 650, 130);
        }
		,'json');

	});

	// Alternar para pessoa física ou jurídica
	$('#ftipo_pessoa').change(function(){
		if (this.value == 'f') {
			$('.f').toggleClass('pessoa');
			$('.j').toggleClass('pessoa');
		} else if (this.value == 'j') {
			$('.f').toggleClass('pessoa');
			$('.j').toggleClass('pessoa');
		}
	});
});

$(function($){
	$(function(){
		$('input:text').setMask();
	});
});

function showGrey() {
	$.fancybox({
		'padding': 	0,
		'height': 	560,
		'width':	813,
		'type': 'iframe',
		'scroll': 'no',
		'href': '/banner.html',
		'wmode':'transparent',
		'showCloseButton' : false
	});
}

