markdown2html = function(element, text){
	if(text == ''){
		element.hide("slow");
		return;
	}
	converter = new Showdown.converter();
	html = converter.makeHtml(text);
	
	html = "<div>\n\t" + html + "\n</div>";
	
	element.html(html);
	element.show();
	
	return true
}

jQuery(function() {
	
	$("#forum .delete_thread").click(function(event){
		url = $(this).attr("href");
		if(confirm("Are you sure you want to delete this thread?")){
			window.location = url
		}
		event.preventDefault();
	})
	
	$("#forum .delete_comment").click(function(event){
		url = $(this).attr("name");
		if(confirm("Are you sure you want to delete this comment?")){
			$.post(url, {}, function(value) {
				if(value == "refresh"){
					window.location.reload()
				}
				else{
					window.location = value;	
				};
			});
		}
		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")
	})
	
	$("#forum .lock").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/lock/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	$("#forum .unlock").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/unlock/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	
	$("#forum .block").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/block/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	$("#forum .unblock").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/unblock/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	
	$("#forum .sticky").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/sticky/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	$("#forum .unstick").click(function(event){
		id = $(this).attr("href");
		$.post("/forum/unstick/"+id, {}, function(value) {
			window.location.reload()
		});
		event.preventDefault();
	})
	
	$("a.quote").click(function(event){
		var id = $(this).html();
		var username = $(this).attr("id");
		
		$.post("/forum/quote", {id: id, username: username}, function(value) {
			
			if($("div#reply").children("form").children("textarea").val() == ''){
				$("div#reply").children("form").children("textarea").val(value);
			}
			else{
				$("div#reply").children("form").children("textarea").val($("div#reply").children("form").children("textarea").val() + "\n\n" + value);
				
			}
			
			markdown2html($("div.reply_preview"), value)
			
			$.scrollTo('textarea[name=body]')
			$('textarea[name=body]').focus()
		});
	});
	
	$("a.edit").click(function(event){
		var _this = $(this);
		var body = $(this).parent().siblings(".body").children(".c");
		var id = $(this).attr("href");
		
		$.post("/forum/edit", {id: id}, function(value){			
			body.html(value);
						
			var form = $("form.edit_post");
			var cancel = $("form.edit_post").children("div").children("input[name=cancel]");
			var textarea = _this.parent().siblings(".body").children(".c").children("form").children("textarea[name=edit_post]");
			var edit_preview = _this.parent().siblings(".body").children(".c").children("div.edit_preview");
			
			textarea.unbind().keyup(function(event){
				markdown2html(edit_preview, $(this).val())
			});
			
			form.unbind().submit(function(event){
				var _this = $(this);
				var text = $(this).children("textarea").val();
				var id = $(this).children("input").val();
				
				if(text == ""){
					alert("Thats no fun to read.");
					event.preventDefault();
					return;
				}
				
				$.post("/forum/update", {id: id, body: text}, function(v1) {
					_this.parent(".c").html(v1);
				});
				
				event.preventDefault();
			});
			
			cancel.unbind().click(function(event){
				var _this = $(this);
				var id = $(this).parent().siblings("input").val();
				
				$.post("/forum/comment", {id: id}, function(v2) {
					_this.parent().parent("form").parent(".c").html(v2);
				});
				
				event.preventDefault();
			});
			
		});
		event.preventDefault();
	});
	
	$("textarea[name=body]").keyup(function(event){
		markdown2html($("div.reply_preview"), $(this).stripHtml())
				
		$.scrollTo('textarea[name=body]')
	});
	$("textarea[name=body]").focus(function(event){
		markdown2html($("div.reply_preview"), $(this).stripHtml())
				
		$.scrollTo('textarea[name=body]')
	});
});
