var commentPanel=document.getElementById("commentPanel");
var latestCommentID=0;
var fID=0;
var commentPage=0;
var commentCount=0;
var hasMore=false;
var maxComments=10;



setInterval(poll4Comments, 150000);


function isCompatible(){
	if (document.getElementById && document.createElement) {
		return true;
	}else{
		return false;
	}
}

function initComments(){
	if(isCompatible()){
		latestCommentID=document.form.lid.value;
		fID=document.form.fid.value;
		commentCount=document.form.count.value;
		if(document.form.hasMore.value=="true"){
			hasMore=true;
		}
		var footer1=document.getElementById("comp1");
		footer1.className="c_footer";
		var footer2=document.getElementById("comp2");
		footer2.className="c_footer";
		var addHeadline=document.getElementById("comp3");
		addHeadline.className="c_l_headline";
		var addForm=document.getElementById("comp4");
		addForm.className="addCommentPanel";
		var errorInc=document.getElementById("inc1");
		errorInc.className="hidden";
		checkNavi();
		


	}
	
	
}

function checkNavi(){
	//alert("hasmore:" + hasMore + " commentPage:" + commentPage + " commentCount:" + commentCount);
	var nextTop=document.getElementById("c_f_left_on_top");
	var nextBottom=document.getElementById("c_f_left_on_bottom");
	
	var previousTop=document.getElementById("c_f_right_on_top");
	var previousBottom=document.getElementById("c_f_right_on_bottom");
	if(hasMore){
		previousTop.className="";
		previousBottom.className="";
	}else{
		previousTop.className="hidden";
		previousBottom.className="hidden";
	}
	
	if(commentPage==0){
		nextTop.className="hidden";
		nextBottom.className="hidden";
	}else{
		nextTop.className="";
		nextBottom.className="";
	}
	
}

function commentsVor(){
	if(commentPage>0){
		

		getNextCommentPage(fID, commentPage-1)
	}
	
	
}

function commentsZurueck(){
	if(hasMore){
		

		getPreviousCommentPage(fID, commentPage+1)
	}
	
}

function cleanComments(){
	while(commentPanel.hasChildNodes()){
		commentPanel.removeChild(commentPanel.lastChild);
	}
	commentCount=0;
}



function getNextCommentPage(){
	//alert("getnext fID:" + fID + " commentPage:" + commentPage);
	xajax_getNextComments(fID, commentPage);

}

function getPreviousCommentPage(){
	//alert("getprevious fID:" + fID + " commentPage:" + commentPage);
	xajax_getPreviousComments(fID, commentPage);

}
function poll4Comments(){
	if(commentPage==0){
		xajax_checkComment(fID, latestCommentID);
	}
}



function addFirstComment(id, fid, date, name, headline, text){
	//alert("before add first - commentCount:" + commentCount + " latestCommentID:" + latestCommentID + " hasmore:" + hasMore);
	var newComment = createComment(id, fid, date, name, headline, text);
	



	if(commentPanel.hasChildNodes()){
		commentPanel.insertBefore(newComment, commentPanel.firstChild);
	}else{
		commentPanel.appendChild(newComment);
	}
	
	latestCommentID=id;
	commentCount++;
	//wenn mehr als 10 in der Liste sind - letzten löschen

	if(commentCount>maxComments){
		if(commentPanel.hasChildNodes()){
			while(commentPanel.childNodes.length>maxComments){
				commentPanel.removeChild(commentPanel.lastChild);
				commentCount=commentCount-1;
			}
		}

		//commentCount=maxComments-1;
	}
	
	//alert("after add first - commentCount:" + commentCount + " latestCommentID:" + latestCommentID + " hasmore:" + hasMore);
	
}

function addLastComment(id, fid, date, name, headline, text){
	//alert("before add last - commentCount:" + commentCount + " latestCommentID:" + latestCommentID + " hasmore:" + hasMore);
	var newComment = createComment(id, fid, date, name, headline, text);
	
	var commentPanel=document.getElementById("commentPanel");
	commentPanel.appendChild(newComment);
	
	latestCommentID=id;
	//alert("a:" + commentCount);
	commentCount++;
	//alert("b:" + commentCount);
	//wenn mehr als 10 in der Liste sind - ersten löschen
	if(commentCount>maxComments){
		if(commentPanel.hasChildNodes()){
			while(commentPanel.childNodes.length>maxComments){
				//alert(commentPanel.firstChild.nodeValue);
				commentPanel.removeChild(commentPanel.firstChild);
				commentCount=commentCount-1;
			}
		}
		//commentCount=maxComments-1;
	}
	//alert("b:" + commentCount);
	//alert("after add last - commentCount:" + commentCount + " latestCommentID:" + latestCommentID + " hasmore:" + hasMore);
	
}


function createComment(id, fid, date, name, headline, text){
	name=unescape(name);
	headline=unescape(headline);
	text=unescape(text);
	
	//alert("createComment - id:" + id + " fid:" + fid + " date:" + date + " name:" + name + " headline:" + headline + " text:" + text);
	var newComment = document.createElement("div");
	newComment.className="comment";
	newComment.id="c_" + id;

	var inputId=document.createElement("input");
	inputId.setAttribute("type","hidden");
	inputId.setAttribute("name","id");
	inputId.setAttribute("value",id);
	newComment.appendChild(inputId);

	var inputFid=document.createElement("input");
	inputFid.setAttribute("type","hidden");
	inputFid.setAttribute("name","fid");
	inputFid.setAttribute("value",fid);
	newComment.appendChild(inputFid);

	var headlineDiv=document.createElement("div");
	headlineDiv.className="c_headline";

	var headlineSpan=document.createElement("span");
	headlineSpan.className="c_h_headline";
	var headlineText = document.createTextNode(headline);
	headlineSpan.appendChild(headlineText);
	headlineDiv.appendChild(headlineSpan);

	var nameSpan=document.createElement("span");
	nameSpan.className="c_h_name";
	var nameText = document.createTextNode(name);
	nameSpan.appendChild(nameText);
	headlineDiv.appendChild(nameSpan);

	var dateSpan=document.createElement("span");
	dateSpan.className="c_h_date";
	var dateText = document.createTextNode(date);
	dateSpan.appendChild(dateText);
	headlineDiv.appendChild(dateSpan);

	newComment.appendChild(headlineDiv);

	var textDiv=document.createElement("div");
	textDiv.className="c_text";
	textDiv.innerHTML=text;
	//var newCommentText = document.createTextNode(text);
	//textDiv.appendChild(newCommentText);
	newComment.appendChild(textDiv);
	
	return newComment;
}

function saveComment(){
	var fid=document.form.fid.value;
	var name=escape(document.form.name.value);
	var text=escape(document.form.text.value);
	document.form.text.value="";
	var headline=escape(document.form.headline.value);
	xajax_addComment(fid, name, headline, text);

}

function removeComment(cid){
	 commentView=document.getElementById("c_" + cid);
	commentPanel.removeChild(commentView);
}

function deleteComment(cid, fid){
	xajax_deleteComment(cid, fid);
}

function addtext(text) {
	var txtarea = document.form.text;
	var caretPos = txtarea.caretPos;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value += text;
		txtarea.focus();
	}
}

function setHasMore(){
	hasMore=true;
}

function setHasNoMore(){
	hasMore=false;
}

function decreaseCommentPageNr(){
	commentPage=commentPage-1;
}
function increaseCommentPageNr(){
	commentPage=commentPage+1;
}

initComments();
	