function replyTo(id){
	if($('#reply_to_comment_div')!=undefined) $('#reply_to_comment_div').remove();
	
	$('#comment_holder_'+id).show();
	$('#comment_holder_'+id).attr("innerHTML",
		"<div id='reply_to_comment_div' class='replyBox'>"+
//		"<input type='text' id='comment_reply_title' />"+
		"Post your reply here...<br>"+
		"<textarea id='comment_reply_body' rows='5'></textarea>"+
		
		"<input type='button' onclick='javascript:postNewComment("+id+",\""+posts_model+"\");' value='SUBMIT YOUR COMMENT' class='comment_submit_button'/>"+
		"</div>"+
		$('#comment_holder_'+id).html());
}

function deleteNode(id){

	$.post("/posts/deleteComment", 
		{comment_id: id,
		lesson_id: posts_previous_id}, 
		function(data) {
		  //$("#debug").text(data); 
		 // populateComments(posts_previous_res,posts_previous_content_div);
		 populatePostsTree(posts_previous_id, posts_model, posts_previous_content_div,init_options);
		});
	

	
}


var posts_previous_id='';
var posts_previous_content_div='';
var init_options;
var posts_model="";

function postNewComment(id,posts_model){

	$.post("/posts/postComment", 
		{input: $("#comment_reply_body").val(),
		parent_id: id,
		model: posts_model,
		lesson_id: posts_previous_id}, 
		function(data) {
		  //$("#debug").text(data); 
		 // populateComments(posts_previous_res,posts_previous_content_div);
		 populatePostsTree(posts_previous_id, posts_model, posts_previous_content_div,init_options);
		});
	
}


function populatePostsTree(id,model,target,options){
	
	document.getElementById(target).innerHTML='';
	posts_model = model;
	posts_previous_id=id;
	init_options = options;
	posts_previous_content_div=target;
		
	 $.ajaxSetup ({ cache: false}); 
	
     $.getJSON("/posts/getTreeJson/"+id,
        function(res){
          populateComments(res,'#'+target, options);
        });
}

function toggleVisibility(id){
	$('#'+id).toggle();
}



function populateComments(res,div,options,count){

	if(count==undefined) count = 0;
	
	if(count==0){
		
		cclass = "replyHolderMain"
	} 
	else cclass =  "replyHolder";
	
	$("<div/>")
		.attr("innerHTML", "")
		.attr("id","comment_holder_"+res.data.id)
		.attr("class",cclass)
		.appendTo(div);
	
	$.each(res.children, function(i,item){

/*
		$("<div/>")
		.attr("innerHTML", "<div>"+item.data.title+" - "+item.data.user+"<h5>"+item.data.body+"</h5></div>")
		.attr("id","comment_"+item.data.id)
		.attr("class",cclass)
		.appendTo(div);
	*/


		
			var str =

			"<div id='comment_"+item.data.id+"' class='commentBox'>"+
				"<div style='padding-bottom:8px;'>" /*+item.data.title+" - "*/ +
				"<h5>"+item.data.user+" - <span class='smallDate'>"+item.data.date+"</span></h5><div class='commentBody'>"+item.data.body+"</div></div>";
				
			if(item.children.length>0)
				str+= "<a href='javascript:toggleVisibility(\"comment_holder_"+item.data.id+"\")'><img src='/img/toggle.gif'/></a> "; //+
				//item.children.length+" Replies ";
			if(options.reply)	str+=" <a href='javascript:replyTo("+item.data.id+")'><img src='/img/postreply.gif'/></a>";
			if(options.admin)	str+=" <a href='javascript:deleteNode("+item.data.id+")'>Delete</a>";
			str+="</div> <div id='line'></div>";
			
		document.getElementById("comment_holder_"+res.data.id).innerHTML+=str;	
		
		var c=count;
        c++;
        	
		populateComments(item,"#comment_holder_"+res.data.id,options,c);
      

    });
    
    if(count==0 && options.reply){
    	
    	//$(div).attr("innerHTML",

    	$(div).html($(div).html()+"<div id='reply_to_comment_div_main' class='replyBox' style='display:block; color:#333'>"+
	//		"<input type='text' id='comment_reply_title' />"+
			"Post your comment here...<br>"+
			"<textarea id='comment_reply_body' rows='5'></textarea>"+
			"<input type='button' onclick='javascript:postNewComment("+res.data.id+",\""+posts_model+"\");' value='SUBMIT YOUR COMMENT' class='comment_submit_button'/>"+
			"</div>");
			
    }
}
 /*
  $(document).ready(function(){
  
     $.getJSON("/posts/getTreeJson/1",
        function(res){
          populateComments(res,"#comments",{reply:false});
        });
  });
  */
  