
$(document).ready(function(){
	
	$("#preview_comment").hide();
	
	$("#c_submit").click(function(){
		
		//	hide the button
		//$(this).hide();
		
		//	display the 'loading' gif;
		$("#mycommentspan").html('<img src="'+TemplateP+'/images/ajax-loader.gif" alt="Loading" id="loading" />').show();

		//	get the info
		var userId = $("#c_user_id").val();
		var videoId	= $("#c_vid").val();
		var commentTxt = $("#c_comment_txt").val();
		var uName = $("#c_username").val();
		var captchaCode = $("#captcha").val();
		
		if(userId == 0)
		{
			// guest comment
			$.post( MELODYURL2+"/comment.php", { username: uName, captcha: captchaCode, vid: videoId, user_id: userId, comment_txt: commentTxt },
   					function(data){
						if(data.cond == true)
						{
							$("#myform").slideUp("normal", function() {
								$("#mycommentspan").text(data.msg).show();
								
								//	preview comment
								if(data.preview == true)
								{
									$comment = data.html;
									$comment = $comment.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />');
									$("#preview_comment").html($comment).fadeIn(700);
								}
							});
						}
						else if(data.cond == false)
						{
							$("#c_submit").show();
						 	$("#mycommentspan").text(data.msg).show();									
						}
						
   					},"json"
				 );
		}
		else if(userId > 0)
		{
			// user comment
			$.post( MELODYURL2+"/comment.php", { vid: videoId, user_id: userId, comment_txt: commentTxt },
   					function(data){
						
						if(data.cond == true)
						{
							$("#myform").slideUp("normal", function() {  
								$("#mycommentspan").text(data.msg).show();
								
								//	preview comment
								if(data.preview == true)
								{
									$("#preview_comment").html(data.html).fadeIn(700);
								}
							});
							
						}
						else if(data.cond == false)
						{
							$("#c_submit").show();
						 	$("#mycommentspan").text(data.msg).show();
						}
						
   					},"json"
				 );
		}
      return false;

	});	
});