/**
 * VisãoI Sistemas
 * visaoi@visaoi.com.br
 * 
 * Rua Marcílio Dias, 26
 * Bairro Americano - Lajeado - RS
 * (51)3011-7001 | (51)8424-4494
 * 
 * DESCRIÇÃO
 * Javascripts com funções genéricas, comuns aos sites
 *
 * @author		Francisco Schwertner
 * @copyright   Copyright (c) 2005-2008 VisãoI Sistemas. (http://www.visaoi.com)
 */
 
$(function() {
	
	/**	
	 * cria o datepiker para os inputs do tipo data, elementos com a classe 'input_date' 
	 */
	$('.input_date').each(function() {
		$(this).datepicker({
			dateFormat:'dd/mm/yy',
			showOn: "both", 
		    buttonImage: iconspath+'/silk_icons/date_magnify.png', 
		    buttonImageOnly: true 
		});
	});
	
	/**	
	 * cria máscara para inputs do tipo hora, input com id comcamndo com 'hora_' 
	 */
	$('INPUT[id*=hora_]').each(function(){$(this).mask('99:99');});
	/**	
	 * cria máscara do para inputs do tipo hora, elemtnos com a classe 'input_time' 
	 */
	$('.input_time').each(function(){$(this).mask('99:99');});
	
	
	
	/**	
	 * cria máscara para inputs do tipo fone, input com id contendo 'fone' ou 'celular' 
	 */
	$('INPUT[id*=fone]').each(function(){$(this).mask('(099)9999-9999');});
	$('INPUT[id*=celular]').each(function(){$(this).mask('(099)9999-9999');});
	/**	
	 * cria máscara do para inputs do tipo fone, elementos com a classe 'input_fone' 
	 */
	//$('.input_fone').each(function(){$(this).mask('(099)9999-9999');});
	
	
	
	/**	
	 * cria máscara para inputs para cpf, input comecando com 'cpf_'
	 */	
	$('INPUT[id^=cpf_]').each(function(){$(this).mask('999.999.999-99');});
	
	/**	
	 * cria máscara para inputs para cnpj
	 */
	$('INPUT[id^=cnpj_]').each(function(){$(this).mask('99.999.999/9999-99');});
	
	
	/**	
	 * cria a formatação e mascaramento para campos numeric
	 */
	$('.input_numeric').each(function() {
		$(this).maskMoney({
			symbol: "",
			decimal: ",",
			precision: 2,
			thousands: ".",
			showSymbol:true
		});
	});
	
	/**
	 * cria textareahtml, para elementos textarea q contenham a palavra 'conteudo'
	 * ou comecem com 'texto_'
	 *
	 */
	$('TEXTAREA[id*=conteudo]').each(function(count) {
		var textareahtml_id = $(this).attr('id');
		tinyMCE.execCommand("mceAddControl", false, textareahtml_id);
	});
	$('TEXTAREA[id^=texto_]').each(function(count) {
		var textareahtml_id = $(this).attr('id');
		tinyMCE.execCommand("mceAddControl", false, textareahtml_id);
	});
	
	
	/**
	 * cria máscara para inputs do tipo CPF, elementos com o atributo ID começando com "cpf"
	 *
	 */
	$('INPUT[id^=cep]').each(function(){$(this).mask('99999-999');});
	


	
	
	
	
	
	
	
}); // fim jquery

/**
 * abre uma janela popUp 
 * para abrie a pop up no centro da tela os parâmetros top e left devem ser 0 
 *
 * @param string url : endereço
 * @param string name : nome da janela
 * @param int w : largura da janela
 * @param int h : altura da janela
 * @param int top : posição em relação ao topo 
 * @param int left : posição em relação a esquerda
 */
// TODO terminar esta função
function abrePopUp(url, name, width, height, top, left) {
	var winl = (screen.width - width)/2;
	var wint = (screen.height - height)/2;

	if((top == 0)&&(left == 0)) {
		window.open(url,null,"height="+height+",width="+width+",top="+wint+",left="+winl+",status=yes,toolbar=no,menubar=no,location=no");	
	} else {
		window.open(url,null,"height="+height+",width="+width+",status=yes,toolbar=no,menubar=no,location=no");
	}



	
}

function abrePopTelaInteira(url, name) { 
	
	var width = window.screen.width;
	var height = window.screen.height;
	
	window.open(url,name,'status=no,resizable=yes,scrollbars=no,menubar=no,width='+width+',height='+height+',left=15,top=20') ;
}


/**
* faz um POST na ação 'excluir' do controller 'arquivo'
* id = id do registro arquivo
* tabela = tabela que referencia este registro do arquivo
* tip_arquivo_atual_id = id elemento dom que mostrara o mensagem de OK
* @author Francisco
*/
function excluirArquivo(id, tabela, tip_arquivo_atual_id) {
	
	var elemento = $('#'+tip_arquivo_atual_id);
	$(elemento).html("<img src='"+baseurl+"/img/carregando.gif' />");
	$.post(baseurl+'/'+module+'/arquivo/excluir/',
		{
			id: id,
			tabela: tabela
		},
		function(data){
			$(elemento).html("arquivo excluído com sucesso.");
		}
	);
}


/*
tipo = success ou error
mensagem = mensagem para exibição
duração = duração tem em ms que tela ficara com o bloqueada, padrao 2000
redirecionamento = url para redirecionamento
*/
function mensagemBlockUI(tipo, mensagem, duracao, redirecionamento) {

	$("body").append("<div id='mensagem-blockUI' class='"+tipo+"'>"+mensagem+"</div>");


	jQuery.blockUI({ 
    	message: jQuery('#mensagem-blockUI'),
    	overlayCSS: { backgroundColor: '#000' },
    	css: {
    		border: 'none'
		} 
	});
	
	// após o limite definido pela variavel time a mensagem desaparece
	// redireciona para a url definada após exibir a mensagem
    setTimeout(function() { 
        jQuery.unblockUI({ 
            onUnblock: function(){ 
            	$('#mensagem-blockUI').remove();
            	if(redirecionamento != '') {
					window.location = redirecionamento;
				}
			} 
        }); 
    }, duracao);

} 



