function emoticon(tresc) {
	tresc = '' + tresc + '';
	if (document.shoutbox2.tresc.createTextRange && document.shoutbox2.tresc.caretPos) {
		var caretPos = document.shoutbox2.tresc.caretPos;
		caretPos.tresc = caretPos.tresc.charAt(caretPos.tresc.length - 1) == ' ' ? tresc + ' ' : tresc;
		document.shoutbox2.tresc.focus();
	} else {
		document.shoutbox2.tresc.value  += tresc;
		document.shoutbox2.tresc.focus();
	}
}

function showInfo(element, text, color, time) {
	var oldColor = element.style.color;
	element.style.color = color;
	element.innerHTML = text;
	setTimeout(function() {
			element.style.color = oldColor;
			element.innerHTML = "";
		}, time);
}

function shoutboxService(action, id) {
	var info = $("content_info");
	var sponDiv = $("shoutbox_entries");
	var res = new KamAjax();
	
	if (action == "add") {
		var name = document.shoutbox2.imie.value;
		var txt = document.shoutbox2.tresc.value;
		
		if (name != "" && txt != "") {
			info.innerHTML = "Prosze czekać...";
			res.getPlainText("panel/services/shoutbox_service.php?cmd=" + action + "&imie=" + name 
							 + "&text=" + txt, 
					function(xhr) {
						if (xhr.responseText == "ok") {
							document.shoutbox2.imie.value = "";
							document.shoutbox2.tresc.value = "";
							shoutboxService("get");
							showInfo(info, "Dodano do ramówki spontanicznej!", "#009900", 10000);	
						} else if (xhr.responseText == "error") {
							showInfo(info, "Wystąpi3 b3ąd podczas dodawania do ramówki!", "#ff0000", 10000);
						}
						
					} );
		} else showInfo(info, "Uzupełnij dane!", "#ff0000", 10000);
			
	} else if (action == "get") {
		sponDiv.innerHTML = "";
		info.innerHTML = "Prosze czekać...";
		res.getPlainText("panel/services/shoutbox_service.php?cmd=" + action, 
				function(xhr) {
					if (xhr.responseText != "") {
						var sponArr = (xhr.responseText).split("#");
						sponArr.pop();
						
						for (var key in sponArr) {
							var spon = sponArr[key].split("|");
							var div = document.createElement("div");
							var h3 = document.createElement("h3");
							var span = document.createElement("span");
							var li = document.createElement("li");
							var p = document.createElement("p");
							var hr = document.createElement("hr");
							
							div.id = "entry_" + spon[2];
							li.style.fontWeight = "bold";
							li.className ="btn_small";
							//li.setAttribute("onclick", "shoutboxService('del', " + spon[2] + ");");
							addEvent(li, "click", function() {
											shoutboxService('del', spon[2]);
										});
							li.innerHTML = "X";
							p.innerHTML = spon[1];
							
							span.appendChild(li);
							span.appendChild(h3);
							h3.innerHTML += spon[0];
							div.appendChild(span);
							div.appendChild(p);
							div.appendChild(hr);
							sponDiv.appendChild(div);
						}
					} else sponDiv.innerHTML = "<p style=\"font-weight: bold;\">Brak spontanów...</p>";
					info.innerHTML = "";
				});
	} else if (action == "del") {
		info.innerHTML = "Prosze czekać...";
		res.getPlainText("panel/services/shoutbox_service.php?cmd=" + action + "&id=" + id, 
				function(xhr) {
					if (xhr.responseText == "ok") {
						var delEntry = $("entry_" + id);
						sponDiv.removeChild(delEntry);
						showInfo(info, "Usunięto spontana!", "#009900", 10000);
					}
					
				});
	}
}

function showShout() {
	var sponDiv = $("shoutbox_entries");
	if (sponDiv.innerHTML == "") shoutboxService("get");
	else sponDiv.innerHTML = "";
}

