// JavaScript Document

// Valor para busca
function valor_busca(busca){
	resultado = '';
	add = '+';
	valor = busca.split(' ');
	// Substitui os espaços por '+'
	for(i=0; i<valor.length; i++){
		if(i==(valor.length-1)){ add = ''; }
		resultado += valor[i]+add;
	}
	return resultado;
}

// Pegar o valor inteiro de um String
function int(texto,base){
	if(!base){ return parseInt(texto); }
	valor = texto.split(base);
	return parseInt(valor[1]);
}

// Load de imagens
function imagem_load(imagem,destino,loader){
	if(loader) $(loader).show();
	
	var img = new Image();
	$(img).load(function(){
		$(this).hide();
		if(loader) $(loader).hide();
		$('#'+destino).html(this);
		$(this).fadeIn();
	}).error(function(){
		$(this).html('<span>Erro ao carregar a imagem!</span>');
	}).attr('src', imagem);
}


// Mostra objeto com TIMER
function mostra(objeto,tempo){
	altura = $(objeto).css('height');
	$(objeto).css('height',0);
	$(objeto).show();
	
	window.setTimeout(function(){
		$(objeto).animate({
			opacity: 1,
			height: altura
		},300,function(){
			// Fim animação
		});
	},tempo);
}

// Retira objeto do palco com TIMER
function some(objeto,tempo){
	window.setTimeout(function(){
		$(objeto).animate({
			opacity: 0,
			height: 0
		},500,function(){
			$(objeto).hide();
		});
	},tempo);
}


// Ajax padrão
function ajax(arquivo,vars,destino,loader){
	if(loader) $(loader).show();
	$.ajax({
		type: 'POST',
		url: arquivo,
		data: vars,
		success: function(html){
			$(destino).html(html);
			if(loader) $(loader).hide();
		}
	});
}


function valida_email(valor){
	if(!valor) return false;
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) return valor; else return false;
}


// Limpa Formulário
function limpa_form(form){
	$(form).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
				$(this).val('');
				break;
			case 'checkbox':
				case 'radio':
				this.checked = false;
		}
	});
}


// Pop'up centralizado
function pop(pagina,largura,altura) {
	meio_w = screen.width/2;
	meio_h = screen.height/2;
	
	altura2 = altura/2;
	largura2 = largura/2;
	meio1 = (meio_h-altura2)-(altura2/4);
	meio2 = meio_w-largura2;
	
	window.open(pagina,'','height='+altura+',width='+largura+',top='+meio1+',left='+meio2+''); 
}



