function edit(id) {
	var p = $("#post-box-"+id);
	if (!p) {
		return false;
	}
	p.html('Loading...').load('/forum/edit/'+id);
}

function reply(id) {
	if (id) {
		if ($("#reply-box-" + id).length > 0) {
			$("#reply-box-" + id).toggle();;
			if (!$("#reply-box-" + id).is('hide')) {
				$("#reply-box-" + id).find('textarea').focus();
				if (silentquot) {
					var text = '<blockquote>wrote by ' + $("#post-"+id).find(".username").text() + "\n";
					text += $("#post-"+id).find(".post-message").text()+'</blockquote>';
					setTimeout(function(){
						$("#reply-box-" + id).find('textarea').val(text);
					},500);
				}
			};
		}
		else {
			$("#reply-box-all").clone(true)
				.insertAfter($("#post-" + id))
				.attr('id', 'reply-box-' + id)
				.removeClass('hide')
				.find("form").attr("id", "reply-form" + id)
				.find("input[name='post_id']").val(id);
			//$("#reply-form-" + id).uniform({widget: 'white'});
			$("#reply-form" + id).uniform({widget: 'white', callback: function(result, status) {
				if (result.success) {
					window.location.reload();
					if (result.cmd == 'redirect') {
						//window.location = result.data;
					}
				}
			}});
			$("#reply-form" + id + ' .cancel').click(function() {
				reply(id);
			});
			$("#reply-form" + id).find('textarea').focus();
			if (silentquot) {
				var text = '<blockquote>wrote by ' + $("#post-"+id).find(".username").text() + "\n";
				text += $("#post-"+id).find(".post-message").text()+'</blockquote>';
				setTimeout(function(){
					$("#reply-box-" + id).find('textarea').val(text);
				},500);
			};
		}
		silentquot = 1;
	}
	else {
		// reply topic
		// clone another one with first post id
		// trigger last reply button
		silentquot = 0;
		$("span.reply-link:last a").click();
		//$("#reply-box-all").removeClass("hide");
		//$("#reply-box-all").toggleClass("hide");
	}
}

var silentquot = 1;
$(function() {
	//$("form").uniform({widget: 'white'});
	$(".uniform").uniform({widget: 'white'});
	$(".uniform.newtopic").click(function() {
		$("#main-box").toggleClass('hide');
		$("#posting-form-box").toggleClass('hide');
		if (!$("#posting-form-box").is('hide')) {
			$("#posting-form-box").find("input[type='text']:first").focus();
		}
	});
	$(".uniform.login").click(function() {
		//window.location = '/user/?redirect=' + window.location.href;
		redirect();
	});
	
	// topic
	$("#posting-form").uniform({callback: function(result, status) {
		if (result.success) {
			if (result.cmd == 'redirect') {
				window.location = result.data;
			}
		}
	}});
	
	// reply
	$(".reply-button").click(function() {
		reply(0);
		//$("#reply-box-all textarea").focus();
	});
	$(".mcp-button").click(function() {
		window.location = "/forum/mcp/" + $(this).parents('dd').attr('rel');
	});
	

});
