 $(document).ready(function()
 {
   
    $("#pesquisarAuth").click(function()
    {
        
        if( $('#autenticar').val() != '')
        {
            
          
          $("#resultAuth").ajaxStart(function()
          {
                $("#resultAuth").show();
                $("#resultAuth").html("<img src='/public/default/img/ajax-loader.gif' align = 'absmiddle'/> &nbsp;Pesquisando..");
          });

          $("#resultAuth").ajaxStop(function()
          {
              $("#resultAuth").html('');  
              $("#resultAuth").hide();
              $('#resultMateria').show();
              
           
              
                
          });
          
/**
*Pesquisa ajax pelo identificador. Caso o resulset seja maior que zero, abre mostra
*uma div oculta na view e insere o resultado da busca.
*Caso o resulset seja menor ou igual a zero, mostra a mesma div, mas com um aviso de 
*materia nao encotrada
*/
            
    var valor = $('#autenticar').val();
    
    $.ajax({
            type: "POST",
            url: urlProjeto + 'pesquisa-codigo/',
            data:'auth='+valor,
            success: function(msg)
            {
                var style, tamanho, separador, materia, estilo;
                var resultadoMateria = eval(msg);
                var imprime     = '<a id="imprime" href="javascript:PrintElementID(\'resultMateria\')"><img src="/public/default/img/document-print.png" border="0"/></a>';
                var link        = '<a id="link" href="#">Fechar</a>';
                var linkBottom  = '<a id="linkBottom" href="#">Fechar</a>';
                var linkNonOne  = '<a id="NoOne" href="#">Fechar</a>';
                
                   if( resultadoMateria.length > 0 )
                   {
                	   
                	  //$('#resultMateria').addClass('resultMateria');*/
                	  
                	  $('#resultMateria').addClass('resultMateria');
                	  estilo = resultadoMateria[0].style;
                	  
                	  separador = resultadoMateria[0].styleCabecalhoEstadoSeparador;
                	  
                	  materia = estilo + ' ' +resultadoMateria[0].classeTipoEstrutura;
                	  
                	  //style = '<style>@import url("/public/diario/css/materia.css");</style>';
                	   //var style = '<style>#materia{ font-family: "Times New Roman",Roman,serif; font-size: 10pt; color: #000; margin: 0px; paddin: 0; line-height: 14px; }#materia table, #materia table th, #materia table td{ border: 1px solid #000; border-collapse: collapse; overflow: hidden; padding: 1pt; vertical-align: top; }#materia table, #materia table p{ width: 100%; }#materia table p, #materia p{ text-align: justify; }#materia h5{text-align: center; }#materia h1{text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2; text-transform: uppercase;}#materia img{width: 50px; height: 55px;}</style>';
                  	   $('#transparent').show('slow');
           			   $('#transparentFixPopUp').show('slow');
           			   $('#resultMateria').show('slow');
           			        var conteudo = '<br />'+link + '<br />'
                            +  '<div id="separador_imprime" class=" cabecalhoEstadoSeparador '+separador+'">'+resultadoMateria[0].separador+'</div>'
                            + resultadoMateria[0].img+ '<div id="materia_imprime" class="'+materia+'">'+resultadoMateria[0].texto+'</div>' 
                            ;
                            $('#resultMateria').html( conteudo ); 
                   }
                   else
                   {
                        $('#resultMateria').addClass('zero');
                        var conteudo = '<p />'+ linkNonOne + '<br /><br /><br /><br /><br /><img src="/public/default/img/erro.jpg"  align="absmiddle"/>&nbsp;&nbsp;Não há Matéria para o número informado';
                        $('#resultMateria').html( conteudo ); 
                   }

/**
*link dentro da div que mostra o resultado, para ocultar a div
*comportamento apdrão foi bloqueado
*
*/
               $("#link").click(function( event )
               {
                   event.preventDefault();   
                   $('#resultMateria').hide();
                   $('#transparent').hide();
      			   $('#transparentFixPopUp').hide();
               }); 
               
               $("#linkBottom").click(function( event )
               {
                   event.preventDefault();   
                   $('#resultMateria').hide();
                   $('#transparent').hide();
      			   $('#transparentFixPopUp').hide();
               });
               
               $("#NoOne").click(function( event )
               {
                   event.preventDefault();   
                    $('#resultMateria').slideUp();
                    $('#transparent').hide();
       			   $('#transparentFixPopUp').hide();
               });
            }
          });
                           

        }else{
            alert('Informe o Número da Autenticação');
            $('#autenticar').focus();
        }
    });
    
/**
*renderiza o calendario na view num element fieldset na view index
*/   
    $('fieldset.jcalendar').jcalendar(); 
    
    $("a.verConteudo").click(function(event){

            event.preventDefault();
            $(this).parent().css('overflow','auto');
        
        });
 });
 
 function PrintElementID(id){
     var oPrint, oJan, pg, style, subst;
     pg = urlProjeto + "/index";
     //style = '<style>#link{display:none;}#imprime{display:none;}resultMateria{overflow:auto;padding:10px 10px 10px 10px;text-align:justify;border:1px solid #AB251C;margin-left:10px;overflow-x:hidden;}.textomateria * {font-family: "Times New Roman", Roman, serif; font-size: 12px;}.textomateria h5{text-align:center;}</style>';
     style = '<style>#materia{ font-family: "Times New Roman",Roman,serif; font-size: 10pt; color: #000; margin: 0px; paddin: 0; line-height: 14px; }#materia table, #materia table th, #materia table td{ border: 1px solid #000; border-collapse: collapse; overflow: hidden; padding: 1pt; vertical-align: top; }#materia table, #materia table p{ width: 100%; }#materia table p, #materia p{ text-align: justify; }#materia h5{text-align: center; }#materia h1{text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2; text-transform: uppercase; font-size: 10pt;}img{width: 50px; height: 55px;}</style>';
     oPrint   = window.document.getElementById(id).innerHTML;
     oJan     = window.open(pg,"exibe","height = 445, width = 345, scrollbars=yes");
     oJan.document.write(oPrint);
     oJan.history.go();
     oJan.window.print();
 }
 
 function PrintElementDetalhar()
 {
	 var oSep, oPrint, oJan, pg, style, tamanho;
	 pg = urlProjeto + "pesquisa-avancada/detalhar/";
	
	 var classe_separador = window.document.getElementById('separador').attributes["class"].value;
	 
	 var classe_materia = window.document.getElementById('materia').attributes["class"].value;
	 
	 
	 
	 style = '<style>@import url("/public/diario/css/materia.css");</style>';
	 
	 if($("#controle_qt").val() == 1)
	    {
	    	tamanho = 350;
	    	
	    	//style = '<style>.textomateria *{font-family: "Times New Roman", Roman, serif; font-size: 12px; width:340px;}.textomateria h5 {text-align:center;}.textomateria h1{font-size: 12px;text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2;text-transform: uppercase;}</style>';
	    	  
	    	
	    	//style = '<style>.materiaImpressao{font-family: "Times New Roman",Roman,serif; font-size: 10pt; color: #000; margin: 0px; paddin: 0; line-height: 14px; width: 340px;}.materiaImpressao table,.materiaImpressao table th, .materiaImpressao table td{border: 1px solid #000; border-collapse: collapse; overflow: hidden; padding: 1pt; vertical-align: top;}.materiaImpressao table, .materiaImpressao table p{width: 100%;}.materiaImpressao table p, .materiaImpressao p{text-align: justify;}.materiaImpressao h5{text-align: center;}.materiaImpressao h1{text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2; text-transform: uppercase; font-size: 12px;}img{width: 50px; height: 55px;}</style>';
	    		  
	    }
	    else {
	    	tamanho = 720;
	    	//style = '<style>.textomateria *{font-family: "Times New Roman", Roman, serif; font-size: 12px; width:718px;}.textomateria h5 {text-align:center;}.textomateria h1{font-size: 12px;text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2;text-transform: uppercase;}</style>';
	    	//style = '<style>.materiaImpressao{font-family: "Times New Roman",Roman,serif; font-size: 10pt; color: #000; margin: 0px; paddin: 0; line-height: 14px; width: 720px;}.materiaImpressao table,.materiaImpressao table th, .materiaImpressao table td{border: 1px solid #000; border-collapse: collapse; overflow: hidden; padding: 1pt; vertical-align: top;}.materiaImpressao table, .materiaImpressao table p{width: 100%;}.materiaImpressao table p, .materiaImpressao p{text-align: justify;}.materiaImpressao h5{text-align: center;}.materiaImpressao h1{text-align: center;border-width: 2px 0px;border-style: solid;border-color: #000;background: #f2f2f2; text-transform: uppercase; font-size: 12px;}img{width: 50px; height: 55px;}</style>';
	    }
	 
	 
	 var esquerda = (screen.width - 400)/2;
	 var topo = (screen.height - 400)/2;
	 oSep = '<div id="separador_impressao" class="'+classe_separador+'">'+window.document.getElementById('separador').innerHTML+'</div>';
	 $("#textoaviso").css({'display': 'block'});
	 oPrint = '<div id="materia_impressao" class="'+classe_materia+'">'+window.document.getElementById('materia').innerHTML+'<br />'+window.document.getElementById('textoaviso').innerHTML+'</div>';
	 $("#textoaviso").css({'display': 'none'});
	 oJan = window.open(pg,"JANELA","height = 445, width = "+tamanho+" ,top= "+topo+",left = "+esquerda+", scrollbars=yes");
     oJan.document.write(style +oSep + oPrint);
     //oJan.history.go();
     oJan.window.print();
     //abrir(pg,esquerda,topo);
 }
 
 function abrir(pagina, largura, altura) {
	   /* Definindo meio da tela */
	   
	   var esquerda = (screen.width - largura)/2;
	   var topo = (screen.height - altura)/2;
	   /* Abre a nova janela */
	   window.open(pagina,"Janela",'height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda + ', scrollbars=yes');
	   
}
 
/*
*mascara de data na view de pesquisa avançada
*/   
 jQuery(function($){
   $.mask.addPlaceholder("~","[+-]"); 
   $("#dataIncial").mask("99/99/9999");
   $("#dataFinal").mask("99/99/9999");
   
});