//========================================================
// LIGAEXPERTE Guestbook, copyright Ligaexperte.de
// v 2.1.2
//
// - needs le_std.js (must be included in same HTML)
//========================================================

// globals
var gbMsgCache, gbMsgCacheKeys, gbMailbox, gbSUId, gbUId, gbUName, gbLimit;
var gbLoadingInfo = '<div class="contData2 ac" style="height:92px;"><br /><br />Daten werden geladen<br /><img src="/grafik/waiting.gif" alt="waiting" /><br /></div>';

function gbCallInit()
{
	gbInit(gbUId, gbUName, gbMailbox, gbLimit, gbSUId, true);
}

function gbSetGlobals(userId, userName, mailbox, limit, sUId, form)
{
	// globals
	gbUId = userId;
	gbUName = userName;
	gbMailbox = mailbox;
	gbLimit = limit;
	gbSUId = sUId;
}

function gbInit(userId, userName, mailbox, limit, sUId, form)
{
	gbSetGlobals(userId, userName, mailbox, limit, sUId, form);
	if (userId && mailbox && limit)
	{
		// retrieve msgs
		gbGetMsgs(userId, limit);
		if (document.getElementById)
    {
			var gbList = document.getElementById("gbList");
			gbList.innerHTML = gbLoadingInfo;
    }
		// init form
		if (form)	gbInitForm();
	}
	else
	{
		//alert("gb failure!");
	}
}

function gbUpdateList (msgId)
{
	var message;
	if (msgId > 0)
	{
		// list highlight
		if (document.getElementById)
    {
	  	for (var oi=1; oi <= (document.getElementById("gbList").childNodes.length-2); oi++)
	  	{
				document.getElementById("gbList").childNodes[oi].firstChild.style.backgroundColor = "#e8e8e8";
			}
			if (message = document.getElementById("message" + msgId))
    	{
	  		message.style.backgroundColor = "#f0f0f0";
			}
    }
	}
	else
	{
		msgId = gbMsgCache[0]["id"];
	}
	// show msg
	gbShowMessage(msgId);
	// set read flag
	if (gbMsgCache[gbMsgCacheKeys[msgId]]["gelesen"]==0 && gbMailbox=="in" && gbUId==gbSUId) setMsgReadFlag(msgId);
	// paging
	gbPaging(msgId);
}

function gbShowMessage(id)
{
	if (document.getElementById)
	{
		var html, el, imgPostfix;

		// gbMessage => border class
		el = document.getElementById("gbMsgContainer");
		html = "vat nopad";
		if (gbUId==gbSUId) html += " " + gbMsgCache[gbMsgCacheKeys[id]]["type"] + "Bor";
		else html += " tblBottomBor";
		el.className = html;

		// gbInOutInfo
		if (gbUId==gbSUId)
		{
			el = document.getElementById("gbInOutInfo");
			if (gbMsgCache[gbMsgCacheKeys[id]]["type"]=="public") html = "G&auml;stebuch Eintrag";
			else if (gbMsgCache[gbMsgCacheKeys[id]]["type"]=="private") html = "Private Nachricht";
			else if (gbMsgCache[gbMsgCacheKeys[id]]["type"]=="le_info") html = "Ligaexperte-Info";
			if (gbMailbox=="in") html += " erhalten";
			else if (gbMailbox=="out") html += " gesendet";
			el.innerHTML = html;
			el.className = "white strong " + gbMsgCache[gbMsgCacheKeys[id]]["type"];
		}

		// gbImage
		el = document.getElementById("gbImage");
		if((gbMsgCache[gbMsgCacheKeys[id]]["type"]!="le_info")) {

			if (gbMsgCache[gbMsgCacheKeys[id]]["foto"]=="default_") imgPostfix = "100";
			else imgPostfix = "_thumb";
			html = "<a href=\"/user/" + gbMsgCache[gbMsgCacheKeys[id]]["mod_rewrite_username"] + "/\"><img class=\"gbImg flleft\" src=\"/userfotos/" + gbMsgCache[gbMsgCacheKeys[id]]["foto"] + "" + imgPostfix + ".jpg\" alt=\"";
			if (gbMailbox=="in") html += gbMsgCache[gbMsgCacheKeys[id]]["id_sender"] + "\" /></a>";
			else html += gbMsgCache[gbMsgCacheKeys[id]]["id_empfaenger"] + "\" /></a>";
			el.innerHTML = html;
		}
		else {
		el.innerHTML = "";
		}
		// gbMeta
		el = document.getElementById("gbMeta");
		if (gbMailbox=="in") html = "von";
		else html = "an";
		html += ": <span class=\"strong\">";
		if((gbMsgCache[gbMsgCacheKeys[id]]["type"]!="le_info")) html += "<a href=\"/user/" + gbMsgCache[gbMsgCacheKeys[id]]["mod_rewrite_username"] + "/\">";
		html += gbMsgCache[gbMsgCacheKeys[id]]["username"];
		if((gbMsgCache[gbMsgCacheKeys[id]]["type"]!="le_info")) html += "</a>";
		html += "</span><br /> am: <span class=\"strong\">" + gbMsgCache[gbMsgCacheKeys[id]]["datum"] + "</span>&nbsp";
		if (gbSUId!=0 && gbSUId==gbUId && gbMailbox=="in") html += "<a href=\"/user/" + gbMsgCache[gbMsgCacheKeys[id]]["mod_rewrite_username"] + "/postfach/\" class=\"simple\" onmousemove=\"stdMOver(event, 'Antworten', '', 0)\" onmouseout=\"stdMOut()\"><img class=\"vatb\" src=\"/grafik/icons/message_reply.png\" alt=\"Antworten\" /></a>&nbsp;<a href=\"javascript:gbDeleteMessage(" + gbMsgCache[gbMsgCacheKeys[id]]["id"] + ")\" class=\"simple\" onmousemove=\"stdMOver(event, 'Nachricht l&ouml;schen!', '', 0)\" onmouseout=\"stdMOut()\"><img class=\"vatb\" src=\"/grafik/icons/message_delete.png\" alt=\"Nachricht l&ouml;schen\" /></a><br />";
		el.innerHTML = html;

		// gbMsgTxt
		el = document.getElementById("gbMsgTxt");
		el.innerHTML = gbMsgCache[gbMsgCacheKeys[id]]["text"];
	}
}

function gbPaging(msgId)
{
	var el, h;
	if (document.getElementById)
	{
		el = document.getElementById("gbPaging");
		h = "";
		if ((gbMsgCacheKeys[msgId] - 1) >= 0) h += "<a class=\"simple\" href=\"javascript:gbUpdateList(" + gbMsgCache[(gbMsgCacheKeys[msgId]-1)]["id"] + ")\" onmousemove=\"stdMOver(event, 'vorherige', '', 0)\" onmouseout=\"stdMOut()\"><img src=\"/grafik/icons/resultset_previous.png\" class=\"vatb\" alt=\"vorherige\"  /></a> <a href=\"javascript:gbUpdateList(" + gbMsgCache[(gbMsgCacheKeys[msgId]-1)]["id"] + ")\">vorherige</a>";
		else h += "<img src=\"/grafik/icons/resultset_previous.png\" class=\"vatb\" alt=\"vorherige\"  /> vorherige";
		h += " | ";
		if ((gbMsgCacheKeys[msgId] + 1) < gbMsgCache.length) h += "<a href=\"javascript:gbUpdateList(" + gbMsgCache[(gbMsgCacheKeys[msgId]+1)]["id"] + ")\">n&auml;chste</a> <a class=\"simple\" onmousemove=\"stdMOver(event, 'n&auml;chste', '', 0)\" onmouseout=\"stdMOut()\" href=\"javascript:gbUpdateList(" + gbMsgCache[(gbMsgCacheKeys[msgId]+1)]["id"] + ")\"><img src=\"/grafik/icons/resultset_next.png\" class=\"vatb\" alt=\"n&auml;chste\" /></a>";
		else h += "n&auml;chste <img src=\"/grafik/icons/resultset_next.png\" class=\"vatb\" alt=\"n&auml;chste\"  />";
	}
	el.className = el.ClassName + " tblBottomBor";
	el.innerHTML = h;
}

function gbCacheMsgData(data)
{
	var i, j, p, dStart, dEnd, needleS, needleE, elemData;
	var arrMsgs = data.split("</gbmsg>");
	var arrMsgData = new Array();
	var arrMsgDataKeys = new Object();
	var arrElements = new Array("id", "datum", "id_sender", "id_empfaenger", "foto", "username", "mod_rewrite_username", "text", "gelesen", "type");
	// each msg
	arrMsgs.pop();
	for (i=0; i < arrMsgs.length; i++)
	{
		arrMsgs[i] = arrMsgs[i].replace(/<gbmsg>/, "");
		arrMsgData[i] = new Object(); // generate associative array
		for (p=0; p < arrElements.length; p++)
		{
			needleS = "<" + arrElements[p] + ">";
			dStart = arrMsgs[i].search(needleS) + arrElements[p].length + 2; // +2: < >
			needleE = "<\/" + arrElements[p] + ">";
			dEnd = arrMsgs[i].search(needleE);
			elemData = arrMsgs[i].slice(dStart, dEnd);
			arrMsgData[i][arrElements[p]] = elemData;
			// key
			if (p==0)
			{
				arrMsgDataKeys[elemData] = i;
			}
		}
	}
	gbMsgCache = arrMsgData;
	gbMsgCacheKeys = arrMsgDataKeys;
	gbUpdateList(0);
}

function gbGetMsgs(user_id, limit)
{
	var response = xajax_gbGetMsgs(user_id, gbMailbox, limit);
	return response;
}

function setMsgReadFlag(id)
{
  if (document.getElementById("gbNewEntriesValue"))
  {
		var newMsgsFooter = document.getElementById("gbNewEntriesValue").innerHTML;

		// update db
		if (xajax_setGbMsgRead(id)) gbMsgCache[gbMsgCacheKeys[id]]["gelesen"] = 1;

		// font weight
		document.getElementById("msgMeta" + id).style.fontWeight = "normal";

		// decrease new message display
		document.getElementById("gbNewEntriesValue").innerHTML = newMsgsFooter-1;
		if (newMsgsFooter==1)
		{
			// hide box
			document.getElementById("gbNewEntriesInfoBox").style.display = "none";
		}
	}
}

function gbInitForm (uid, un)
{
  if (document.getElementById)
  {
		var gb = document.getElementById("gbForm");
		gb.innerHTML = gbLoadingInfo;
  }
	// undefined at first call
	if (uid==undefined || un==undefined)
	{
			uid = gbUId;
			un = gbUName;
	}
  xajax_gbForm(uid, un);
}

function gbInsertMessage (userId, senderId, userName, senderName)
{
  // check for empty msg
  var formXmlData = xajax.getFormValues('gbFormFields');
  formXmlData = formXmlData.replace(/<xjxquery><q>/, "");
  formXmlData = formXmlData.replace(/<\/q><\/xjxquery>/, "");

  var formData = formXmlData.split("&");
  var formValues;
  var i=0;
  formValues = formData[i].split("=");

  if (formValues[1]!="")
  {
		// insert to db
		xajax_gbInsertMsg(userId, senderId, userName, senderName, xajax.getFormValues('gbFormFields'));
    if (document.getElementById)
    {
	 	 var gb = document.getElementById("gbForm");
	 	 gb.innerHTML = gbLoadingInfo;
    }
		// init again, but wait some time to complete db insert operation
		window.setTimeout("gbInit(" + userId + ", '" + senderName + "', 'in', " + gbLimit + ", " + gbSUId + ", false)", 200);
  }
  else
  {
    if (document.getElementById)
    {
		if (!document.getElementById("gbInsertInfo"))
		{
			var elem = document.createElement("div");
			var elemTxt = document.createTextNode('Fehler: Deine Nachricht darf nicht leer sein!');
			elem.appendChild(elemTxt);
			var attr = document.createAttribute("class");
			attr.nodeValue = "contInfo highlightBor strong";
			elem.setAttributeNode(attr);
			attr = document.createAttribute("id");
			attr.nodeValue = "gbInsertInfo";
			elem.setAttributeNode(attr);
			var Ausgabebereich = document.getElementById("gbForm");
			Ausgabebereich.appendChild(elem);
		}
	  var gb = document.getElementById("gbText");
	  gb.style.backgroundColor = "#ff0000";
	  gb.style.color = "#ffffff";
	  gb.style.borderColor = "#ff0000 #800000 #800000 #ff0000";
	  gb.focus();
	  gb = document.getElementById("gbInsertInfo");
	  gb.style.color = "#ff0000";
    }
  }
}

function gbDeleteMessage (msgId)
{
  var c = confirm(unescape("Soll dieser Eintrag wirklich gel%F6scht werden?"));
  if (c)
  {
    if (xajax_gbDeleteMsg(msgId)) gbInit(gbUId, gbUName, 'in', gbLimit, gbSUId, false);
  }
}

// register event handler
var gbGlobalInit = leGlobalInitializer.getInstance();
gbGlobalInit.addFunction('gbCallInit');
