/**
* Javascriptfunktionen für eine Thread
*/
var thread;
function Thread() {

	this.url = server + "/forum/bookmarks.php?ajax=info";

	this.xslt = new Xslt();
	this.xPath = new XPath();
	this.cnt = -1;
	//this.xslt.init("Xsl/AJAX/manageThread.php");

	/**
	* Ergebniss des Ajax-Requests wird behandelt
	* Statische Funktion
	*/
	this.update = function() {
		if(thread.xmlHttpReq.readyState == 4) {
			if (thread.xmlHttpReq.responseXML && thread.xmlHttpReq.responseXML.documentElement && thread.xmlHttpReq.responseXML.documentElement.firstChild) {
				
				// Thread ist als bookmark gespeichert worden
				if (thread.xmlHttpReq.responseXML.documentElement.firstChild.nodeName == "bookmark") {
					// Ausblenden des Bookmark Popup
					if (document.getElementById("bookmark[viewgeneraldiscussion][0]")) {
						document.getElementById("bookmark[viewgeneraldiscussion][0]").style.display = "none";
					}
					if (document.getElementById("bookmark[viewwrongentry][0]")) {
						document.getElementById("bookmark[viewwrongentry][0]").style.display = "none";
					}
					if (document.getElementById("bookmark[viewunsolvedquery][0]")) {
						document.getElementById("bookmark[viewunsolvedquery][0]").style.display = "none";
					}
				}
				
				if (thread.xmlHttpReq.responseXML.documentElement.firstChild.nodeName == "count") {
					// Der Seitentitle wird angepasst
					var cnt = parseInt(thread.xmlHttpReq.responseXML.documentElement.firstChild.firstChild.data);
					if (!cnt || cnt <= 0) {
						cnt = 0;
					}
					
					// Falls es die Bookmarkseite ist wird diese erneut geladen
					if (thread.cnt >= 0 && (window.location.href.search(/manageThread/) != -1) && thread.cnt != cnt) {
						location.reload();
					}
					thread.cnt = cnt;
					
					var str = "LEO Forum ";
					// Seitentitle wird geupdatet
					if (cnt > 0) {
						if (cnt == 1) {
							str += cnt + " Faden mit neuen Antworten. "; 
						}
						else {
							str += cnt + " Fäden mit neuen Antworten. "; 
						}
					}

					
					// Mitteilungen
					cnt = parseInt(thread.xmlHttpReq.responseXML.documentElement.firstChild.nextSibling.firstChild.data);
					if (!cnt || cnt <= 0) {
						cnt = 0;
					}
					
					// Seitentitle wird geupdatet
					if (cnt > 0) {
						if (cnt == 1) {
							str += cnt + " neue Mitteilung."; 
						}
						else {
							str += cnt + " neue Mitteilungen."; 
						}
					}
					document.title = str;
				}
			}
		}
	}

	/**
	* Thread wird gebookmarkt
	*/
	this.createBookmark = function(event) {
		this.url = server + "/forum/manageThread.php?ajax=info";
		this.startPostRequest(postBackString(event, "bookmark"));
		this.url = server + "/forum/bookmarks.php?ajax=info";
	}
	
	/**
	* Thread wird gebookmarkt
	*/
	this.refresh = function() {
		var str = encodeURI("ajax=info&postBack=managethread&value[action]=count&value[object]=notificationlisting&lang=" + lang +"&lp=" +lp);
		this.startPostRequest(str);
	}
	
	/**
	* Thread wird gebookmarkt
	*/
	this.suggest = function(event, field) {
		var str = encodeURI("ajax=info&postBack=alphabeticindex&value[action]=search&value[object]=threadlisting&lang=" + lang +"&lp=" +lp+"&text=") + encodeURIComponent(field.value);
		this.url = server + "/forum/alphabeticindex.php?ajax=info";
		this.startPostRequest(str);
		this.url = server + "/forum/bookmarks.php?ajax=info";
	}
}
Thread.prototype = new Ajax();

function Preview(idforum) {
	this.idforum = idforum;
	this.option = new Option();
	this.xslt = new Xslt();
	this.xPath = new XPath();
	this.url = "";
	this.preview = true;
}
Preview.prototype = new Ajax();

Preview.prototype.setData = function(page, limit, orderBy) {
	this.option.page = page;
	this.option.limit = limit;
	this.option.orderBy = orderBy;
}

Preview.prototype.loadPage = function(page) {
	if (page) {
		this.option.page = page;
	}
	else {
		this.option.page = 1;
	}
	window.location.href = this.url + "?idForum=" + this.idforum + "&lp=" + lp + "&lang=" + lang + encodeURI(this.option.toString());
}

Preview.prototype.suggest = function(event, node) {
	// Position wird überprüft
	/*if (node.parentNode != document.getElementById("suggestions").parentNode) {
		var sug = document.getElementById("suggestions").cloneNode(true);
		document.getElementById("suggestions").parentNode.removeChild(document.getElementById("suggestions"));
		node.parentNode.appendChild(sug);
	}*/
	document.getElementById("suggestions").style.top = (getPosition(node).y + 25) + "px";
	document.getElementById("suggestions").style.left = (getPosition(node).x + 5) + "px";
	if (node.value.search(/((^|\s)ich\s|(^|\s)hallo|(^|\s)suche\s|(^|\s)hilfe|(^|\s)bitte|(^|\s)dringend|(^|\s)brauche)/i) >= 0) {
			document.getElementById("suggestions").innerHTML = "";
			document.getElementById("suggestions").innerHTML = "<div class='warning'><img width='32' height='32' alt='warning' src='/trainer/img/warning_32x32.png'/>Suchen sie wirklich die Übersetzung für \"<i>"+ node.value+"</i>\"?<br/>Bitte <b>nur</b> den Begriff für den eine Übersetzung gesucht wird eingeben.</div>";
			document.getElementById("suggestions").style.display = "block";
	}
	else {
		if (event.keyCode == 32 || (node.value.length > 3 && node.value.length % 3 == 0 && this.preview)) {
			var str = encodeURI("ajax=info&postBack=preview"+this.type+"&value[action]=search&value[object]=threadlisting&lang=" + lang +"&lp=" +lp+"&text=") + "idForum=" + this.idforum +"&weight=0.8"+ encodeURI(this.option.toString());
			this.startPostRequest(str);
		}
		else if(node.value.length == 0) {
			document.getElementById("suggestions").style.display = "none";
			this.preview = true;
		}
	}
}

Preview.prototype.closeSuggestion = function() {
	window.setTimeout("closeSuggestion()", 500);
}

closeSuggestion = function() {
	document.getElementById("suggestions").style.display = "none";
}

Preview.prototype.renderThreads = function(xmlDoc) {
	this.xPath.evaluate("//" + this.type, xmlDoc);
	if (this.xPath.iterateNext()) {
		// Liste enthält Ergebnisse
		this.xslt.setParameter("mode", "view");
		var result = this.xslt.transformToDocument(xmlDoc);
		this.xslt.setParameter("mode", "");
		
		document.getElementById("suggestions").innerHTML = "";
		appendXSLTResult(document.getElementById("suggestions"), result);
		document.getElementById("suggestions").style.display = "block";
		
		if (xmlDoc.getElementsByTagName(this.type).length < 10) {
			this.preview = false;
		}
	}
}

/**
* Ergebniss des Ajax-Requests wird behandelt
* Statische Funktion
*/
Preview.prototype.update = function() {
	if(preview.xmlHttpReq.readyState == 4) {
		if (preview.xmlHttpReq.responseXML 
			&& preview.xmlHttpReq.responseXML.documentElement 
			&& preview.xmlHttpReq.responseXML.documentElement.firstChild) {
			
			// Threads als Vorschläge zurückgekommen
			if (preview.xmlHttpReq.responseXML.documentElement.firstChild.nodeName == preview.type+"listing") {
				preview.renderThreads(preview.xmlHttpReq.responseXML);
			}
			
		}
	}
}
Preview.prototype.keyUp = function(evt, node) {
	if (evt.keyCode == 13) {
		this.loadPage();
		cancelEvent(evt);
	}
}

function PreviewSearch() {
	Preview.call(this, null);
}
PreviewSearch.prototype = new Preview();

PreviewSearch.prototype.loadPage = function(page) {
	document.alphabeticindex.submit();
}