$(document).ready(function(){
	// verificar se opção Pesquisa Livre está selecionada
	if ($("input[@type='radio'][@name='tipoPesquisa'][@value='livre']").attr("checked")) {
		$("#filtro").show();
		$("#dicas").show();
		$("input[@name=texto]").attr("value","");
		//$("input[@name=texto]").unmask();
		$("input[@name=texto]").attr("maxlength","");
		$("input[@name=texto]").focus();
	} else 	if ($("input[@type='radio'][@name='tipoPesquisa'][@value='acordao']").attr("checked")) {
		$("#filtro").hide();
		$("#dicas").hide();
		//$("input[@name=texto]").unmask();
		$("input[@name=texto]").attr("value","");
		$("input[@name=texto]").attr("maxlength","6");
		//$("input[@name=texto]").mask("999999",{placeholder:" "});
		$("input[@name=texto]").focus();
	} else 	if ($("input[@type='radio'][@name='tipoPesquisa'][@value='processo']").attr("checked")) {
		$("#filtro").hide();
		$("#dicas").hide();
		$("input[@name=texto]").attr("value","");
		//$("input[@name=texto]").unmask();
		$("input[@name=texto]").attr("maxlength","12");
		//$("input[@name=texto]").mask("999999999999",{placeholder:" "});
		$("input[@name=texto]").focus();
	}	
	
	// mostrar filtro
	function showFiltro() {
		$("#filtro").slideDown();
		$("#dicas").show();
	}
	
	// esconder filtro
	function hideFiltro() {
		$("#filtro").slideUp();
		$("#dicas").hide();
	}
	

	$("#dicas").mouseover(function(){
		$("#divDicas").show();
	});
	$("#dicas").mouseout(function(){
		$("#divDicas").hide();
  	}); 
	
	// configurar radiobuttons de tipo de consulta
	$("input[@type='radio'][@name='tipoPesquisa']").each(function(){
		switch ($(this).attr("value")) {
			case "processo":
				$(this).click(function(){
					hideFiltro();
					$("input[@name=texto]").attr("value","");
					/*$("input[@name=texto]").unmask();
					$("input[@name=texto]").mask("999999999999",{placeholder:" "});*/
					$("input[@name=texto]").attr("maxlength","12");
					$("input[@name=texto]").focus();
				}); 
				break;
			case "acordao":
				$(this).click(function(){
					hideFiltro();
					$("input[@name=texto]").attr("value","");
					/*$("input[@name=texto]").unmask();
					$("input[@name=texto]").attr("value","");*/
					$("input[@name=texto]").attr("maxlength","6");
					//$("input[@name=texto]").mask("999999",{placeholder:" "});
					$("input[@name=texto]").focus();
				}); 
				break;
			case "livre":
				$(this).click(function(){
					showFiltro();
					$("input[@name=texto]").attr("value","");
				    $("input[@name=texto]").attr("maxlength","100");
					//$("input[@name=texto]").unmask();
					$("input[@name=texto]").focus();
				});
				break;
		}
	});
	
	// chamada ajax do formulário
	$("#formJurisp").submit(function(){
          var inputs = [];
          
          if ($("input[@type='radio'][@name='tipoPesquisa'][@value='acordao']").attr("checked")) {
          	var reDigits = /^\d+$/;
          	var txt = $("input[@name='texto']").val();
          	if (!reDigits.test(txt)) {
          		alert('Número de acórdão inválido!\nUse somente números!');
          		$("input[@name='texto']").focus();
          		return false;
          	}
          	
          	if (txt.length>6) {
          		alert('Número de acórdão inválido!\nO número do acórdão precisa ter até 6 dígitos!');
          		$("input[@name='texto']").focus();
          		return false;
          	}
          }
          
          if ($("input[@type='radio'][@name='tipoPesquisa'][@value='processo']").attr("checked")) {
          	var reDigits = /^\d+$/;
          	var txt = $("input[@name='texto']").val();
          	if (!reDigits.test(txt)) {
          		alert('Número de processo inválido!\nUse somente números!');
          		$("input[@name='texto']").focus();
          		return false;
          	}
          	if (txt.length!=12) {
          		alert('Número de processo inválido!\nO número do processo precisa ter 12 dígitos!');
          		$("input[@name='texto']").focus();
          		return false;
          	}
          	
          }
          
          // verifica se o parâmetro de busca está preenchido
          if (!$("input[@type='radio'][@name='tipoPesquisa'][@value='livre']").attr("checked")) {
          	if ($("input[@name='texto']").attr("value")==null) {
          		alert('Informe um parâmetro para pesquisar!');
          		return false;
          	}
          } else if ($("input[@type='radio'][@name='tipoPesquisa'][@value='livre']").attr("checked")) {
          	if (($("#texto").attr("value")==null) && ($("#relator").attr("value")==null) && ($("#orgao").attr("value")==null)
          	     && ($("#dataInicial").attr("value")==null) && ($("#dataFinal").attr("value")==null) ) {
          		alert('Informe pelo menos um parâmetro para pesquisar!');
          		return false;
          	}
          }
          
          $(":input", $(this)).each(function() {
	          if ($(this).attr("value") != null) {
	          	  if (($(this).attr("name")=="tipoPesquisa")) {
	          	  	if ($(this).attr("checked")) {
			          inputs.push($(this).attr("name") + '=' + escape($(this).attr("value")));
			        }
			      } else {
			          inputs.push($(this).attr("name") + '=' + escape($(this).attr("value")));
			      }
		      }
          });

          $.ajax({
			type: "POST",
			url: $(this).attr("action"),
			dataType: "html",
			data: inputs.join('&'),
			success: function(msg){
						$("#loading").hide();
						$("#resultado").html(msg);
						$("#resultado").show();
						setAjaxLinksPaginacao();
					 },
			beforeSend: function(){
							$("#resultado").hide();
							$("#loading").show();
						}			
		});
		
		return false;
	}); 
	
	// seta todos os links de paginação com chamadas ajax
	function setAjaxLinksPaginacao() {
		$("p.barraPaginacao > a").each(function(i){
			$(this).click(chamaPagina);	
		});
	}
	
	// chamada ajax dos links de paginação
	function chamaPagina(){
          $.ajax({
			type: "GET",
			url: $(this).attr("href"),
			dataType: "html",
			success: function(msg){
						$("#loading").hide();
						$("#resultado").html(msg);
						$("#resultado").show();
						setAjaxLinksPaginacao();
					 },
			beforeSend: function(){
							$("#loading").show();
						}			
		});
		
		return false;
	}; 

	$.datepicker.setDefaults({showOn: 'both', 
	                          buttonImageOnly: true,
	                          buttonImage: '../../lib/script/jquery/datepicker/calendar.gif',
	                          buttonText: 'Calendário',
	                          dateFormat: 'dd/mm/yy'});
	
	$("input[@type='text'][@name='dataInicial']").datepicker();
	$("input[@type='text'][@name='dataFinal']").datepicker();
	
	$("input[@name=texto]").focus();
});

	
