/* Javascript
 * For: Adding comments to a Profile's wall
 */
    
/* Javascript
 * Use: Attaches an onSubmit event to all form.comment-form
 *      elements in the content div.  When the event fires, it grabs 
 *      All data from the form and submits it to the server using 
 *      an AJAX POST request.
 */
/*Mousumi Changed the selector to form.comment-form from div#content in next line*/
//$(document).ready(function(){
	$("form.comment-form").submit(function(ele) {
		if ( $("textarea#comment").val() != "") {
			$("#error-comment").text('');
			$("#error-comment").hide();
			var form = $(ele.target);
			var fieldset = form.children("fieldset");
			var b = $(fieldset.children("textarea.commentbx")).val();
			$(fieldset.children("div").children("button#submit")).attr("disabled", "true");
			$(fieldset.children("div").children("button#submit")).addClass("disabled");
			$(fieldset.children("div").children("button#submit")).after("<span class='ajax-spinner'><img src='/static/images/loading-white.gif' alt='' style='vertical-align: middle;' /></span>");
			$.post("/users/"+name+"/wall/save/", {body: b, username: name}, function(data) { parseWall(data, fieldset); }, "json");
			return false;
		}
	});
//});
/* Javascript
 * Use: Parses the response data sent back as a result of the AJAX request made by
 *      the onSubmit event firing on a comment form.  If it is a new comment, it is
 *      appended to the ul#comments list.  If it is a reply, we check to see if the 
 *      parent already has children present in the source.  If so, we append the new
 *      reply to the list of existing replies.  If not we wrap the new comment in 
 *      <ul> elements to create our nested list of comments.
 *      
 *      If the response has error data, it is displayed instead.
 */
function parseWall(data, fieldset) {
	var comment = data['body'];
	var user = data['post_user'];
	var avatar = data['avatar'];
	var whoswall = data['username'];
	var comment_id = data['comment_id'];

	$(fieldset.children("div").children("button#submit")).removeAttr("disabled");
    $(fieldset.children("div").children("button#submit")).removeClass("disabled");
    $(fieldset.children("div").children("button#submit").next("span.ajax-spinner")).remove();
	$(fieldset.children("div").children("button#submit")).blur();
	if (comment != undefined) {
	    $(fieldset).children("textarea.commentbx").val("");
	    
		var walls = Number($("span.walls-num").html());
		var tmp = String(walls + 1);
	    $("span.walls-num").fadeOut("slow", function() { $(this).html(tmp); $(this).fadeIn("slow"); });
	
		var html = "";
		// if there is already a active wall block make that inactive first
		$(".active-wall").removeClass('active-wall');
		html += "<li class='clearfix comment-entry active-wall' id='comment-"+comment_id+"'>";
    	html += "<div class='comment-meta'>";
    	html += "<a style='float:left;' href='/users/"+user+"/'>";
    	html += "<img class='photo' src="+avatar+"></a>";
    	html += "<div class='comment-author'>";
    	html += "<a href='/users/"+user+"/' class='s'><em style='font-weight:bold;'>"+user+"</em></a>";
    	html += "<span style='display:block;'>Just Now</span></div></div>";
    	html += "<div class='comment-block'><p>"+comment.replace(/\n/g, "")+"</p>";
		
		// if comment is on ther's wall show wall-to-wall link
		if(whoswall != user)
			html += "<div id='wall-actions'><a href='/users/"+whoswall+"/wall/?chat="+user+"'><img src='/static/images/icon-wall-convo.gif'/> <span>Wall-to-Wall</span></a>";
		// add the delete link
		html += "<span class='comment-delete'><a id='comment-"+comment_id+"' class='comment-delete' href='javascript:void(0)'> <img src='/static/images/icon-wall-delete.gif'/> Delete</a></span></div>";		
    	
		html += "</div>";
		html += "<div class='clearfix'></div>";
    	html += "</li>";
    	html += "</ul>";
        if ($("div.orange-note")) { $("div.orange-note").remove(); }
        $("#comment-success").append("<div class='orange-note'><em>"+data['status']+"</em></div>");
        $("#comment-success").show();
        
		$("ul#start").prepend(html);
		$("textarea#comment").val('');
		
		//increment wall counter
		var walls = Number($("span#walls-num").html());
		var tmp = String(walls + 1);
		$("span#walls-num").fadeOut("slow", function() { $(this).html(tmp); $(this).fadeIn("slow"); });
		
		userbar_message('action', 'wall_post');
	}
	else {
		alert('Empty Comment!');
	}
}

/* Javascript
 * Use: Attaches another onClick event to comment links (similar to one above).
 *      This time, we're looking for the delete link and if it is pressed, we 
 *      submit a POST request to the server to delete the comment.
 */
$("ul.comments-wrap").click(function(ele) {
   	if($(ele.target).hasClass('comment-delete')) {
       	var answer = confirm("Are you sure you want to delete this wall post? This cannot be undone.");
       	if (answer) {
       		var comment_id = $(ele.target).attr("id");
       		var tmp = comment_id.split("-");
       		var comment_id = tmp.pop();
       		$.post("/users/"+name+"/wall/delete/", {comment_id:comment_id}, parseWallDelete, "json");
       	}
       return false;
   } 
});

/* Javascript
 * Use: After a wall post is deleted, we need to update the wall post counter in the tab.
 */
function parseWallDelete(data) {
	var status = data['status'];
	var comment_id = data['comment_id'];
	var walls = Number($("span#walls-num").html());
	var tmp = String(walls - 1);
    $("span#walls-num").fadeOut("slow", function() { $(this).html(tmp); $(this).fadeIn("slow"); });
	$("li#comment-"+comment_id+"> div.comment-block").html("<em>"+status+"</em>");
}
