jQuery(function() {
	$("div#comments span.paging").live("click", function(event){
		event.preventDefault();
		var values = $(this).attr("name").split("/");
		var id = values[1];
		var count = values[2];
		var _this = $(this)
		
		$.post("/comment/paging", {target: id, count:count}, function(value) {
			_this.replaceWith(value)
			$("div#comments div.new_comments").remove();
        });
		
    });
	
	$(".comment .delete").live("click", function(event){
		id = $(this).attr("name");
		var parent = $(this).parents(".comment");
		
		if(confirm("Are you sure you want to delete this comment?")){
			$.post("/comment/delete", {id:id}, function(value) {
				parent.remove();
			});
		}
		event.preventDefault();
	});
	
	$(".comment .remove").live("click", function(event){
		id = $(this).attr("name");
		var parent = $(this).parents(".comment");
		
		if(confirm("Are you sure you want to delete this comment?")){
			$.post("/comment/remove", {id:id}, function(value) {
				parent.slideUp(function(){
					$(this).remove()
				});
			});
		}
		event.preventDefault();
	});
	
	$(".expand_redacted").live("click", function(event){
		$(this).removeClass("expand_redacted");
		$(this).addClass("contract_redacted");
		$(this).parent().siblings(".redacted").slideDown("fast");
	})
	
	$(".contract_redacted").live("click", function(event){
		$(this).removeClass("contract_redacted");
		$(this).addClass("expand_redacted");
		$(this).parent().siblings(".redacted").slideUp("fast")
	})
	
	
	
	$("form.add_comment").submit(function(event){
        var target = $(this).children("input[name=target]").val();
        var body = $(this).children("textarea").val();
        
		if(body != ''){
			$.post("/comment/submit", {target: target, body: body}, function(value) {
				
				$("div#comments").append(value);
				$("form.add_comment").children("textarea").val("");
				$("form.add_comment .reply_preview").html("");
				$("form.add_comment .reply_preview").hide();
				
				$("div#comments").siblings("h2").removeClass("lite");
				$("div#comments").siblings("h2").html("Add Your Comment");
			});
		}
		else{
			alert("Comment was blank");
		}
        event.preventDefault();
    });
	
	$("a.quote").live("click", function(event){
		event.preventDefault();
		var id = $(this).attr("name");
		var username = $(this).parent().attr("name");
		
		$.post("/comment/quote", {id: id, username: username}, function(value) {
			var tx = $("form#reply").children("textarea");
			if(tx.val() == ''){
				tx.val(value);
			}
			else{
				tx.val(tx.val() + "\n\n" + value);
			}			
			markdown2html($("div.reply_preview"), value)
			
			$.scrollTo('textarea[name=comment]')
			$('textarea[name=comment]').focus()
		});
	});
	
	$("a.edit").live("click", function(event){
		var _this = $(this);
		var body = $(this).parents(".comment").children(".clip");
		var id = $(this).attr("name");
		$.post("/forum/edit", {id: id}, function(value){			
			body.html(value);
						
			var form = body.children("form.edit_post");
			var cancel = form.children("div").children("input[name=cancel]");
			var textarea = _this.parents(".comment").children(".clip").children("form").children("textarea[name=edit_post]");
			var edit_preview = _this.parents(".comment").children(".clip").children("div.edit_preview");
			
			textarea.unbind().keyup(function(event){
				markdown2html(edit_preview, $(this).val())
			});
			
			form.unbind().submit(function(event){
				_this = $(this);
				var text = textarea.val();
				var id = $(this).children("input").val();
				
				if(text == ""){
					alert("Thats no fun to read.");
					event.preventDefault();
					return;
				}
				
				$.post("/comment/update", {id: id, body: text}, function(v1) {
					body.html(v1);
				});
				
				event.preventDefault();
			});
			
			cancel.unbind().click(function(event){
				var id = $(this).parent().siblings("input").val();
				
				$.post("/comment/comment", {id: id}, function(v2) {
					body.html(v2);
				});
				
				event.preventDefault();
			});
			
		});
		event.preventDefault();
	});
	
	
	$("textarea[name=comment]").keyup(function(event){
		markdown2html($("div.reply_preview"), $(this).stripHtml())
				
		$.scrollTo('textarea[name=comment]')
	});
	$("textarea[name=comment]").focus(function(event){
		markdown2html($("div.reply_preview"), $(this).stripHtml())
				
		$.scrollTo('textarea[name=comment]')
	});
});


