/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


GS_bibjs_portal = {
	
	exibirCarregandoGeral:
		function() {
			$('.loading_geral').show();
		},
        
	esconderCarregandoGeral:
		function(onde) {
			$('.loading_geral').hide();
        },
		
	exibirCarregandoConteudo:
		function() {
			
			$('.loading_conteudo').show();
			/*
			$('.loading_conteudo').fadeIn(1500, 
				function () {
					$('.loading_conteudo').show();
				}
			)
			*/
		},
        
	esconderCarregandoConteudoImagens:
		function(onde) {
			//$('.loading_conteudo').hide();
			
			$('.loading_conteudo_imagens').fadeOut(500, 
				function () {
					$('.loading_conteudo_imagens').hide();
				}
			)
			
        },

	exibirCarregandoConteudoImagens:
		function() {
			
			$('.loading_conteudo_imagens').show();
			/*
			$('.loading_conteudo').fadeIn(1500, 
				function () {
					$('.loading_conteudo').show();
				}
			)
			*/
		},
        
	esconderCarregandoConteudo:
		function(onde) {
			//$('.loading_conteudo').hide();
			
			$('.loading_conteudo').fadeOut(500, 
				function () {
					$('.loading_conteudo').hide();
				}
			)
			
        },

    textoLower:
        function(texto){
            if(texto){
                return texto.toLowerCase();
            }
        },
    
    textoUpper:
        function(texto){
            if(texto){
                return texto.toUpperCase();
            }
        },
    
	carregaPagina:
		function(url, div) {
			
			//GS_bibjs_portal.esconderMenu();
			//GS_bibjs_portal.exibirJanela();
			GS_bibjs_portal.exibirCarregandoConteudo();			
			
			$.ajax({
				url: url,
				type: "POST",
				data: {"div":div},
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
					
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				  
				},
				complete: function(xmlhttprequest){
					//GS_bibjs_portal.esconderCarregandoConteudo();
				},
				success: function(html){
					var jq = $('#' + div);
					jq.html(html);
					
                    GS_bibjs_portal.esconderCarregandoConteudo();
				}
			});
		},
        
	carregaImagens:
		function(url, div) {
			
			GS_bibjs_portal.exibirCarregandoConteudoImagens();			
			
			$('#janela_conteudo_imagem').html('');
			
			$.ajax({
				url: url,
				type: "POST",
				data: {"div":div},
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
					
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				  
				},
				complete: function(xmlhttprequest){
					//GS_bibjs_portal.esconderCarregandoConteudo();
				},
				success: function(html){
					var jq = $('#' + div);
					jq.html(html);
					
                    GS_bibjs_portal.esconderCarregandoConteudoImagens();
				}
			});
		},		
		
	carregaConteudo:
		function(url, div) {
			
			GS_bibjs_portal.exibirCarregandoConteudo();
			
			$.ajax({
				url: url,
				type: "POST",
				data: {"div":div},
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
					
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				  
				},
				complete: function(xmlhttprequest){
					//GS_bibjs_portal.esconderCarregandoConteudo();
				},
				success: function(html){
					var jq = $('#' + div);
					jq.html(html);
					
					GS_bibjs_portal.esconderCarregandoConteudo();
				}
			});
		},				
	
	expandir_treeview:
		function(div){
			$('#janela_conteudo_imagens').html('');
			
			if( $(div).css('display') == 'none'){
				$(div).css('display', 'block');
				$(div).css('visible', 'true');
			}
			else{
				$(div).css('display', 'none');
				$(div).css('visible', 'false');
			}
			//this.carregaConteudo(url, div);
		},
				
    exibirMenu:
		function() {
			var incre = 0;
            
            $("#menu ul:first > li").each(
                function(index){
					$(this).show();
                    
					$(this).animate(
                        {
                            marginTop: "0px",
                            opacity: 1
                        },
                        {
                            duration: parseInt(200 + incre)
                        }
                    );

                    incre = parseInt(incre + 200);
                }
            )
		},
        
    esconderMenu:
		function() {
			var incre = 0;
                                    
            $("#menu ul:first > li").each(
                function(index){

                    $(this).animate(
                        {
                            marginTop: "52px",
                            opacity: 0
                        },
						{
							duration: parseInt(50 + incre),
							complete: function(){
							   $(this).hide();
							}
						}    
                    );

                    incre = parseInt(incre + 50);
					
                }
            )
		},

    exibirJanela:
		function(metodo) {
			
            $( "#janela" ).animate(
                { 
                    height: "425px", 
                    top: "35%",
                    opacity: 1 
                },
                {
                    duration: 500,
                    complete: function(){
                       //GS_bibjs_portal.exibirCarregandoConteudo();
                       GS_bibjs_portal.esconderMenu();
                    }
                }    
            );
		},

    esconderJanela:
		function(metodo) {
			
            $( "#janela" ).animate(
                { 
                    height: "0px", 
                    top: "60%",
                    opacity: 0 
                },
                {
                    duration: 500,
                    complete: function(){
                       GS_bibjs_portal.exibirMenu();
					   $('#janela_conteudo').html('');
                       //GS_bibjs_portal.exibirCarregando();
                    }
                }    
            );
		},
		
	enviarFormulario:
		function(form, div) {
			var data = $('#' + form).serialize();// + "&div=" + div;

			data = data.replace(/%E2%80%93/gi, "-");
			data = data.replace(/%E2%80%9D/gi, "%22");
			data = data.replace(/%E2%80%9C/gi, "%22");
			
			$.ajax( {
				url: $('#' + form).attr("action"),
				type: this.textoMaiusculo($('#' + form).attr("method")),
				data: data,
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
				},
				complete: function(xmlhttprequest) {

				},
				error: function(XMLHttpRequest, textStatus, errorThrown){

				},
				success: function(html){
					$('#' + div).html(html);
				}
			});
		},
	
	textoMinusculo:
        function(texto){
            if(texto){
                return texto.toLowerCase();
            }
        },
    
    textoMaiusculo:
        function(texto){
            if(texto){
                return texto.toUpperCase();
            }
        },
		
	format: 
        
        function(value,format){
			
            value = value.replace(/\D/g,"");
			var result="";
			
			if(format.length < value.length)
				return value;
			
			for(i=0,j=0;(i<format.length)&&(j<value.length);i++)
			{
				var ch = format.charAt(i) ;
				if(ch == '#')
				{
					result += value.charAt(j++);
					continue;
				}
				result += ch;
			}
			return result;
		},
		
    formataMilhar: 
        
        function(objTextBox, SeparadorMilesimo, e){
			var sep = 0;
			var key = '';
			var i = j = 0;
			var len = len2 = 0;
			var strCheck = '0123456789';
			var aux = aux2 = '';
			var whichCode = (window.Event) ? e.which : e.keyCode;    
			// 13=enter, 8=backspace as demais retornam 0(zero)
			// whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
			if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
				return true;
			key = String.fromCharCode(whichCode); // Valor para o código da Chave

			if (strCheck.indexOf(key) == -1) 
				return false; // Chave inválida
			len = objTextBox.value.length;
			
			for(i = 0; i < len; i++)
				if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorMilesimo)) 
					break;
			aux = '';
			for(; i < len; i++)
				if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
					aux += objTextBox.value.charAt(i);
			aux += key;
			len = aux.length;

			if (len == 0) 
				objTextBox.value = '';
			if (len >= 1 && len <= 3 ) 
				objTextBox.value = aux; 

			if (len > 3) {
				aux2 = '';
				for (j = 0, i = len - 4; i >= 0; i--) {
					if (j == 3) {
						aux2 += SeparadorMilesimo;
						j = 0;
					}
					aux2 += aux.charAt(i);
					j++;
				}
				objTextBox.value = '';
				len2 = aux2.length;
				for (i = len2 - 1; i >= 0; i--)
					objTextBox.value += aux2.charAt(i);
				objTextBox.value += SeparadorMilesimo + aux.substr(len - 3, len);
			}

			return false;
		},
		//onKeyPress="return(formataMilhar(this, '.', event))";
			
    formataMoeda: 
        
        function(objTextBox, SeparadorDecimal, SeparadorMilesimo, e){
			var sep = 0;
			var key = '';
			var i = j = 0;
			var len = len2 = 0;
			var strCheck = '0123456789';
			var aux = aux2 = '';
			var whichCode = (window.Event) ? e.which : e.keyCode;    
			// 13=enter, 8=backspace as demais retornam 0(zero)
			// whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
			if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
				return true;
			key = String.fromCharCode(whichCode); // Valor para o código da Chave
		 
		 
			if (strCheck.indexOf(key) == -1) 
				return false; // Chave inválida
			len = objTextBox.value.length;
			for(i = 0; i < len; i++)
				if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
					break;
			aux = '';
			for(; i < len; i++)
				if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
					aux += objTextBox.value.charAt(i);
			aux += key;
			len = aux.length;
			if (len == 0) 
				objTextBox.value = '';
			if (len == 1) 
				objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
			if (len == 2) 
				objTextBox.value = '0'+ SeparadorDecimal + aux;
			if (len > 2) {
				aux2 = '';
				for (j = 0, i = len - 3; i >= 0; i--) {
					if (j == 3) {
						aux2 += SeparadorMilesimo;
						j = 0;
					}
					aux2 += aux.charAt(i);
					j++;
				}
				objTextBox.value = '';
				len2 = aux2.length;
				for (i = len2 - 1; i >= 0; i--)
					objTextBox.value += aux2.charAt(i);
				objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
			}
			return false;
		}
	
}





