/**
 * Load dos sistemas
 * @param {Object} document
 */
$(document).ready(function(){

	//Ajax.init();
	Texto.init();
	
	var fonte = 12;
	$('#large').click(function(){
		if (fonte<16){
			fonte = fonte+1;
			$("p").css({
				'font-size' : fonte+'px'
				});
			$("h4").css({
				'font-size' : fonte+'px'
				});
			$("h1").css({
				'font-size' : fonte+'px'
				});
			$(".BlocoInternas").css({
				'font-size' : fonte+'px'
				});
			return false;
		}
	});
	$('#small').click(function(){
		if (fonte>11){
			fonte = fonte-1;
			$("p").css({
				'font-size' : fonte+'px'
				});
			$("h4").css({
				'font-size' : fonte+'px'
				});
			$("h1").css({
				'font-size' : fonte+'px'
				});
			$(".BlocoInternas").css({
				'font-size' : fonte+'px'
				});
			return false;
		}
	});
	 
	$(".BuscaPalavraChave #FormBuscaNome").autocomplete("index.php?id=/includes/paginas/palavra.php" ,{
		selectFirst: true,
		scroll: true,
		delay:10,
		matchSubset:1,
		matchContains:1,
		cacheLength:10,
		autoFill:false,
		mustMatch:false,
		maxItemsToShow:10
	});
	$('.btn_enviar').click(function () {
		envia = validaContato('formContato');
		if(envia) {
			$('#formContato').submit();
		}
	})
});

function validaContato(id) {
	msg = '';
	$('#'+id).find('input, textarea').each(function() {
		if($(this).hasClass('needed') && !$(this).val()) {
			msg += 'O campo <b>'+$(this).attr('title')+'</b> precisa ser preenchido <br />';
		}
	})
	if(msg) {
		$('#response').html(msg).fadeIn('slow');;
	} else {
		return true;
	}
	
}

function validaInscricao(id) {
	msg = '';
	$('#'+id).find('input, textarea, select').each(function() {
		if($(this).hasClass('needed') && !$(this).val()) {
			msg += 'O campo <b>'+$(this).attr('title')+'</b> precisa ser preenchido <br />';
		}
	})
	if(msg) {
		$('#response-inscricao').html(msg).fadeIn('slow');
	} else {
		return true;
	}
}

function mailIndica(id) {
	var inputs = Array();
	var msg = '';
	$('#'+id+' .formInput').each(function(){
		if( $(this).val() != '') {
			inputs[$(this).attr('name')] = $(this).val();
		} else {
			msg += "O campo <b>"+$(this).attr('title')+"</b> não foi preenchido. <br />";
		}
	});

	if(!checkEmail(inputs['ds_email_indicador'])) {
		msg += "O <b>E-mail</b> de quem está indicando não é válido. <br />";
	}

	if(!checkEmail(inputs['ds_usuin_email'])) {
		msg += "O <b>E-mail</b> de quem irá receber a indicação não é válido. <br />";
	}

	if(msg == '') {
		$.get("includes/paginas/sendMail.php?"+$('#'+id+' input, #'+id+' textarea').serialize(),
			function(data){
				//alert("Data Loaded: " + data);
				if(data == '1') {
					$('.mais_ferramentas').html('<span style="color:#f00; font-size:14px;">Sua mensagem foi enviada com sucesso!</span>').fadeIn();;
				} else {
					$('.mais_ferramentas').html('<span style="color:#f00; font-size:14px;">Não foi possível enviar a mensagem!</span>').fadeIn();
				}
			});
	} else {
		$('#response_indicar').html(msg).fadeIn();
	}
}

function mailSugere() {
	var inputs = Array();
	var msg = '';
	$('#FormSugere .formInput').each(function(){
		if( $(this).val() != '' && $(this).val() != 'Digite seu email' && $(this).val() != 'Digite seu nome') {
			inputs[$(this).attr('name')] = $(this).val();
		} else {
			msg += "O campo <b>"+$(this).attr('title')+"</b> não foi preenchido. <br />";
		}
	});

	if(!checkEmail(inputs['ds_usuin_email'])) {
		msg += "O <b>E-mail</b> não é válido. <br />";
	}

	if(msg == '') {
		$.get("includes/paginas/sendMail.php?"+$('#FormSugere input, #FormSugere textarea').serialize(),
			function(data){
				//alert("Data Loaded: " + data);
				if(data == '1') {
					$('#resposta_sugerir').html('Sua mensagem foi enviada com sucesso!').fadeIn();
					document.sugerir.reset();
				} else {
					$('#resposta_sugerir').html('Não foi possível enviar a mensagem!').fadeIn();
				}
			});
	} else {
		$('#resposta_sugerir').html(msg).fadeIn();
	}
}

function valida(id){
	var input = document.getElementById('folder');
	alert("Alo mundo! "+input.value);
}

function checkEmail(strEmail){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(!reg.test(strEmail))
		return false;

	return true;
}
