$(function() {
	// edit comment
	$(".editComment").click(function() {
	    var id = $(this).attr('name');
	    $("#"+id).slideDown();
	});
	
	// submit the label name
	$(".edit_ok").click(function() {
	    var type= $('#type').html();
	    if (type == 'lesson') {
	    	var slug = $('#slug').html();
	    	var url = "/lessons/"+slug+"/editcomment"; 
	    } else if (type == 'post') {
	    	var url = "/community/conversations/editcomment"; 
	    } else if (type == 'glossary') {
	    	var url = "/resources/glossary/editcomment";
	    } else {
	    	var url = "/resources/grammar/editcomment";
	    }
	    var id = $(this).parent().attr("id");
	    var content = tinyMCE.getInstanceById('comment_list_'+id).getContent();
	    if (!content.length) {
	        alert('comment can not be null');
	        return false;
	    }    
	    $(this).val('updating...').attr('disabled', true);
	    $('.edit_cancel').attr('disabled', true);
	    $.ajax({
	         type: "POST",
	         url: url,
	         data : "id="+id+"&content="+encodeURIComponent(content),
	         success: function(msg) {
	            if (msg == "succeed") {
	                $('.edit_ok').val('save').attr('disabled', false);
	                $('.edit_cancel').attr('disabled', false);
	                window.location.reload();
	            } else {
	                $("#"+id).html("Edit Failed, Please Reload This Page!");
	            }
	         }
	      });
	
	});
	
	// cancel the input field
	$(".edit_cancel").click(function() {
	    $(this).parent().hide('slow');
	});
	
	// subscribe comments
	$("#subscribe").click(function() {
		var status = 0;
		if ($("#subscribe").attr("checked")) {
			var status = 1;
		}
		
		var post_id = $("#pid").val();
		
		$.get("/community/conversations/update-subscribe", { status: status, post_id: post_id },
		function(data){
		    $("#updated").fadeIn();
		    $("#updated").fadeOut();
		}); 
	});
	
	// subscribe lesson comments
	$("#subscribe-l").click(function() {
		var status = 0;
		if ($("#subscribe-l").attr("checked")) {
			var status = 1;
		}
		
		var v3_id = $("#pid").val();
		var name = $("#username").html();
		
		$.get("/"+name+"/lessons/update-subscribe", { status: status, v3_id: v3_id },
		function(data){
			$("#updated").fadeIn();
		    $("#updated").fadeOut();
		}); 
	});
});