var MyBB_Thread = {
	qeCache: new Array(),

	deletePost: function(pid) {
		if(!confirm("Are you sure you want to delete this post?")) return;

		var form = document.createElement('form');
		form.setAttribute('method', 'post');
		form.setAttribute('action', 'editpost.php?action=quickdelete');
		form.setAttribute('style', 'display:none;');

		var input = document.createElement('input');
		input.setAttribute('name', 'pid');
		input.setAttribute('type', 'hidden');
		input.setAttribute('value', pid);
		form.appendChild(input);

		if(my_post_key) {
			var input = document.createElement('input');
			input.setAttribute('name', 'my_post_key');
			input.setAttribute('type', 'hidden');
			input.setAttribute('value', my_post_key);
			form.appendChild(input);
		}

		document.getElementsByTagName('body')[0].appendChild(form);
		form.submit();
	},

	reportPost: function(pid) {
		MyBB.popupWindow('report.php?pid='+pid, 'reportPost', 400, 300);
	},

	quickEdit: function(pid) { },
	quickEditLoaded: function(request, pid) { },
	quickEditSave: function(pid) { },
	quickEditCancel: function(pid) { },
	quickEditSaved: function(request, pid) { }
};

function quickquote(pid) {
	var sel = getCurrentSelection();
	// IE6 might return a selection from within the textarea, which is useless and results in dodgy nesting
	if(sel && sel != '' && sel.indexOf('[quote') == -1) {
		var postdata = sel;
	} else {
		var postdata = document.getElementById('qqmsg' + pid).value;
	}
	var username = document.getElementById('qqusr' + pid).value;
	var messageElement = document.getElementsByName('message')[0];
	insertAtCurrentSelection("[quote=" + username + "]" + postdata + "[/quote]\r\n", messageElement);
}

function insertAtCurrentSelection(text, element) {
	// Inserts at the current selection and moves caret
	if(element.selectionStart !== undefined && element.selectionEnd !== undefined) {
		var value = element.value;
		var startString = value.substring(0, element.selectionStart);
		var endString = value.substring(element.selectionEnd, value.length);
		var newSelPos = element.selectionEnd + text.length;
		element.value = startString + text + endString;
		element.focus();
		element.setSelectionRange(newSelPos, newSelPos);
	}
	// IE doesn't have per-input selection, only document.selection, so we cannot insert at the correct position :(
	else {
		element.value += text;
		element.focus();
	}
}

function getCurrentSelection() {
	// Good browsers
	if(window.getSelection) {
		return window.getSelection().toString();
	}
	// IE
	if(document.selection && document.selection.type == 'Text' && document.selection.createRange) {
		return document.selection.createRange().text;
	}
	return false;
}
