
ShoutBox.prototype.getLastMessages = ShoutBox_getLastMessages;
ShoutBox.prototype.getForm = ShoutBox_getForm;
ShoutBox.prototype.parseResponse = ShoutBox_parseResponse;
ShoutBox.prototype.shout = ShoutBox_shout;
ShoutBox.prototype.resetForm = ShoutBox_resetForm;
ShoutBox.prototype.showElement = ShoutBox_showElement;
ShoutBox.prototype.insertEmoticon = ShoutBox_insertEmoticon;
ShoutBox.prototype.storeCaret = ShoutBox_storeCaret;


function ShoutBox()
{
	this.getLastMessages();
	this.getForm();
	window.setInterval("shoutbox.getLastMessages()", 10000);
}

function ShoutBox_getLastMessages()
{
	httpRequest = new HttpRequest();
	httpRequest.successCallback = this.parseResponse;
	httpRequest.url = "/shoutbox/shoutbox.php";
	httpRequest.setPostParameter("action", "getLastMessages");
	httpRequest.post();
}

function ShoutBox_getForm()
{
	httpRequest = new HttpRequest();
	httpRequest.successCallback = this.parseResponse;
	httpRequest.url = "/shoutbox/shoutbox.php";
	httpRequest.setPostParameter("action", "getForm");
	httpRequest.post();
}

function ShoutBox_shout()
{
	httpRequest = new HttpRequest();
	httpRequest.successCallback = this.parseResponse;
	httpRequest.url = "/shoutbox/shoutbox.php";
	httpRequest.setPostParameter("action", "addMessage");
	httpRequest.setFormElementsAsPostParameters("shoutboxForm");
	httpRequest.post();
}

function ShoutBox_parseResponse(hr)
{
	var root = hr.responseXML.getElementsByTagName("elements").item(0);
	if (root.hasChildNodes()) {
		for (var i = 0; i < root.childNodes.length; i++) {
			var el = root.childNodes.item(i);
			if (el == undefined) continue;
			if (el.nodeType != 1) continue;
			var id = el.getAttribute("id");
			if (id == undefined) continue;
			var del = document.getElementById(id);
			if (del == undefined) continue;
			var className = el.getAttribute("className");
			if (className != undefined) del.className = className;
			var display = el.getAttribute("display");
			if (display != undefined) del.style.display = display;
			var content = el.firstChild.nodeValue;
			if (content != undefined) del.innerHTML = content;
		}
	}
}

function ShoutBox_resetForm()
{
	document.forms["shoutboxForm"].elements["nickname"].value = "";
	document.forms["shoutboxForm"].elements["message"].value = "";
}

function ShoutBox_showElement(element, show)
{
	var el = document.getElementById(element);
	if (el != undefined) {
		var display = "none";
		if (show == 1 || (show == -1 && el.style.display != "block")) display = "block";
		el.style.display = display;
	}
}

function ShoutBox_insertEmoticon(emoticon)
{
	var el = document.getElementById("messageInputText");
	if (el == undefined) return;

//	emoticon += " ";
	if (el.createTextRange && el.caretPos) {
		var caretPos = el.caretPos;
		caretPos.text = emoticon;
		document.selection.empty();
		el.focus();
		storeCaret(el);
	}
	else if (el.selectionStart != undefined && el.selectionEnd != undefined) {
		var text = el.value.substring(0, el.selectionStart) + emoticon + el.value.substring(el.selectionEnd);
		el.value = text;
	}
	else {
		el.value += emoticon;
	}
	this.showElement("emoticons", 0);
	el.focus();
}

function ShoutBox_storeCaret(el)
{
	if (el.type != "text") return;
	if (el.createTextRange) el.caretPos = document.selection.createRange().duplicate();
}

