$(document).ready(function(){
	$('#oscomen').css('display','none');
	var id = $('#idmateria').val();
	$('#enviar-comen').click(function(){

		if($('div#comentario input').val().length > 1
				&& $('div#comentario #texta').val().length > 1){
			
			var nome = $('div#comentario input').val();
			var comen = $('div#comentario #texta').val();
			$.gravaComentario(id, nome, comen);
		}	

	});
	
	$('#amais').click(function(){
		var fonte = parseInt($('#texto').css("font-size"));
		if (fonte < 24) {
			fonte += 1;
			$('#texto').css("font-size", fonte + "px");
		}
	});
	$('#amenos').click(function(){
		var fonte = parseInt($('#texto').css("font-size"));
		if (fonte > 10) {
			fonte -= 1;
			$('#texto').css("font-size", fonte + "px");
		}
	});
	
	$('#numcomen').click(function(){
		$('#oscomen').toggle('slow');
		return false;
	});
	
	$('#enviar-email').click(function(){
		$('#dados-envio').toggle('slow');
		$('#dados-envio').children('input').each(function(){
			$(this).val("");
		});
	});
	
	$('#submit-enviar-email').click(function(){
		var num = 0;
		var i =0 ;
		var resps= new Array();
		var aux;
		//alert("teste1");
		$('#dados-envio').children('input').each(function(){
			if($(this).val().length > 1){
				num += 1;
				aux = $(this).attr('name');
				resps[i++] = aux+","+$(this).val().replace(","," ");
			}	
		});
		if (num == 5) {
			$.enviaEmail(id, resps);
			$('#dados-envio').toggle('slow');
		}
	});
	
	$('#autor').children('span').click(function(){
		var id = $(this).attr('name');
		$('#curri').empty().html('<img atl="carregando" src="../images/carreg.gif" /> Buscando informa&ccedil;oes do Autor');
		$.ajax({
		type: "POST",
		url: "../php/ajaxAutor.php",
		data: "id=" + id,
		success: function(resp){
		//var resp = comentarios.split(",");
		//$("#div-autor").fadeOut("slow");
		
		$('#curri').fadeIn('slow').html(resp);
		
		}});	
	});
		
});

$.gravaComentario = function(id, nome, comen){
	if (id) {
		$.ajax({
			type: "POST",
			url: "../php/ajaxcomentario.php",
			data: "id=" + id + "&nome=" + nome + "&comen=" + comen,
			success: function(comentarios){
				var resp = comentarios.split(",");
				$("#comentario").fadeOut("slow");
				$("#oscomen").delay(1000, function(){
					$("#oscomen").empty().html(resp[0]);
				});
				$('#numcomen').html('<p>Ver coment&aacute;rios (' + resp[1] + ')');
				
			}
		});
	}
}

//$.buscaComentario = function(id){
//	if (id) {
//		$.ajax({
//			type: "POST",
//			url: "../php/ajaxcomentario.php",
//			data: "id=" + id,
//			success: function(comentarios){
//				var resp = comentarios.split(",");
//				//$("#comentario").fadeOut("slow");
//				$("#oscomen").delay(1000, function(){
//					$("#oscomen").html(resp[0]);
//				});
//				
//				$('#numcomen').html('<p>Comentarios (' + resp[1] + ')');
//				
//			}
//		});
//	}
//}

$.buscaComentario = function(id){
	if (id) {
		$.ajax({
			type: "POST",
			url: "../teste/teste.php",
			data: "id=" + id,
			success: function(comentarios){
//				var resp = comentarios.split(",");
				//$("#comentario").fadeOut("slow");
				$("#oscomen").delay(1000, function(){
					$("#oscomen").html(comentarios);
				});
				
				$('#numcomen').html('Ver coment&aacute;rios ');
				
			}
		});
	}
}

$.enviaEmail = function(id, resps){
	//alert("teste");
	if (id) {
		$.ajax({
			type: "POST",
			url: "../php/ajaxEnviaEmail.php",
			data: "id=" + id + "&dados=" + resps,
			success: function(resp){
				//alert("Enviado com sucesso!");
				$('#enviado').html('<p>E-mail enviado com sucesso!</p>');
			}
		});
	}
}