
function delete_thread()
{
	return confirm("Are you sure? If you delete this thread, all posts will be lost.");
}

function delete_post()
{
	return confirm("Are you sure you want to delete this post?");
}

function swap_class(o, c1, c2)
{
	o.removeClass(c1); o.addClass(c2);
}

function edit_post(id, t)
{
	if(t)
	{
		swap_class($("div#pb_"+id), "p_body", "ph_body");
		swap_class($("#pe_"+id), "peh_body", "pev_body");
		swap_class($("#es_"+id), "es_h", "es_v");
		swap_class($("#ec_"+id), "ec_h", "ec_v");
	}
	else
	{
		swap_class($("div#pb_"+id), "ph_body", "p_body");
		swap_class($("#pe_"+id), "pev_body", "peh_body");
		swap_class($("#es_"+id), "es_v", "es_h");
		swap_class($("#ec_"+id), "ec_v", "ec_h");
	}
}

var currentInput = null;

function insertAtCursor(myField, myValue) {
	//IE support
	myField.focus();
	if (document.selection) {
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
					+ myValue
					+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

$(function(){
	$(".del_thread").click(function() { return delete_thread(); });
	$("#f_delthread").click(function() { return delete_thread(); });
	$(".del_post").click(function() { return delete_post(); });
	$(".edit_post").click(function() {var id=this.id; var idp=id.split("_");
			currentInput = $("#pe_"+idp[1])[0];
			edit_post(idp[1], true);
			return false;
		});
	$(".es_h").click(function(){
		var id=this.id; var idp=id.split("_");
		var body=$("#pe_"+idp[1]).val();
		$.post("/sb/ajax/edit_post.sha", {post: idp[1], body: body},
			function(xml) {
				var new_body_pre=$("body", xml).text();
				var new_body=$("fb", xml).text();
				$("#pe_"+idp[1]).html(new_body_pre);
				$("div#pb_"+idp[1]).html(new_body);
			});
		edit_post(idp[1], false);
		currentInput = $("textarea#body")[0];
		});
	$(".ec_h").click(function(){
		var id=this.id; var idp=id.split("_");
		edit_post(idp[1], false);
		currentInput = $("textarea#body")[0];
		});
	$("a.ins_sm").click(function(){
		var id=this.id;
		//alert("currentinput: "+currentInput);
		insertAtCursor(currentInput, id);
		return false;
		});
	$("#t_type").change(function(){
		var tid = $("#threadid").val();
		var v = $("#t_type option:selected").val();
		$.post("/sb/ajax/edit_thread.sha", {thread: tid, type: v},
			function(xml) {
				$("#bt_mod_res").html("Type changed.");
			});
		});
	$("#t_type").val($("#special").val());
	currentInput = $("textarea#body")[0];
});


