// `OMA Xtreme Web Development´ 
$(document).ready(function(){
	$('li.add_comment a').click(function(){
		$.scrollTo("#comment_form_anchor", {speed:1000} );
	});
	
	$('li.view_comments a').click(function(){
		$.scrollTo("#comments_anchor", {speed:1000} );
	});
});

function afegir_comentari(){
	text_value = $("#comment").val();
	nom_value = $("#nick_name").val();
	idVideo = $("#idVideo").val();
	captcha = $("#captcha").val();
	
	$('#comment_submit').attr("disabled", true); 
	
	
	$.post("/ajax/addcomment", 
		{comment: text_value, nick_name: nom_value, idVideo: idVideo, captcha: captcha}, 
		function(data){
			//mostrem missatge de ok / ko
			$("#message_comments").empty();
			$("#message_comments").prepend(data);
			
			//desactivem el botó d'enviar
			$('#comment_submit').removeAttr("disabled");

			if($(".ok").html()){
				//vuidem els camps del formulari
				$("#nick_name").val("");
				$("#comment").val("");
				$("#captcha").val("");
				
				
				//Actualitzem el numero de comentaris totals
				$.post("/ajax/totalcomment", 
					{idVideo: idVideo}, 
					function(data){
						$("#total_comments").html(data);
					}
				);
				
				//fem un reload dels comentaris
				$.post("/ajax/loadcomment", 
					{idVideo: idVideo}, 
					function(data){
						$("#list_comments").html(data);
					}
				);
			}
			//actualitzem la imatge del captcha
			$("#captcha_img").html('<img src="/captcha/captcha.php?rand='+Math.floor ( Math.random ( ) * 10000 + 1 )+'" />')
			
		}
	);
	
	
	return false;
}