/*
	USAGE:
		this.commentsBox = new CommentsBoxClass();
		this.commentsBox.id = this.id + 'CommentsBox';
		this.commentsBox.name = this.name + '.commentsBox';
		this.commentsBox.maxCount = 12;
		this.commentsBox.type = 'photo'; // Type of the object that this Comment-Box is attached to
		this.commentsBox.objectId = 'FDJFJDJF56DF55D54'; // PublicID of the Object that this Comment-Box is attached to.
		this.commentsBox.init();
*/

function CommentsBoxClass () {
	// Initialise Variables
	
	var defaultCommentString = 'Dein Kommentar...';
	
	var commentCounter = 0;
	
	var pagesCounter;
	var currentPage = 1;
	
	var commentListBackground = 'even';
	
	// Initialization
	this.init = function() {
		// Prepare HTML for CommentBox
		htmlString = "<div id='" + this.id + "CommentsBox' class='CommentsBox'>";		
		// Add HtmlString to CommentBox
		document.getElementById(this.id).innerHTML = htmlString;
		
		this.loadComments(1);
	}
	
	// Load Comments from WebServer
	this.loadComments = function(page) {
		mousePointerLabel.clear();
		
		var output = new Object();
		output.objectType = this.type;
		output.objectId = this.objectId;
		output.page = page;
		output.maxCount = this.maxCount;
		outputString = JSONstring.make(output);

		jsonPostData('jsApi/comments/get', escape(outputString), this.name + '.loadCommentsInt');		
	}
	
	// Recieve Comments from Webserver
	this.loadCommentsInt = function(Object) {
		var displayErrorMessage = false;
		document.getElementById(this.id + "CommentsBox").innerHTML = '';

		if(!Object) {
			displayErrorMessage = true;
		} else {
			if(Object["counter"]) {
				if(Object["counter"] > 0) {
					if(Object["counter"] > this.maxCount) {
						pagesCounter = Math.ceil(Object["counter"]/this.maxCount);
					} else {
						pagesCounter = 1;
					}
				} else {
					displayErrorMessage = true;
				}					
			}
			
			if(Object["status"]) {
				if(Object["status"] == 'ok') {
				
					for (z1=0; z1 < Object["comments"].length; z1++) {
						
						var text = Object["comments"][z1]["Text"].replace(/\n/g,"<br />");
						
						if(!Object["comments"][z1]["UserInfo"]["ImagePath"]) {
							Object["comments"][z1]["UserInfo"]["ImagePath"] = '/images/noPhotoThumbnail.jpg';
						}
						
						comment = document.createElement("DIV");
						comment.id = this.id + 'Comment' + z1;
						comment.className = 'comment';
						
						commentBackground = document.createElement("DIV");
						commentBackground.className = commentListBackground;
						commentBackground.innerHTML = "<table><tr><td valign='top' style='width: 60px;'><div id='" + this.id + z1 + "commentContentLeft'></div></td><td valign='top' style='padding: 3px;'><div id='" + this.id + z1 + "commentContentRight' class='commentContent'></div></td></tr></table>";
						
						commentOptionsContainer = document.createElement("DIV");			
						commentOptionsContainer.className = 'commentOptionsContainer';			
						
						if(Object["comments"][z1]["IsDeletable"]) {						
							deleteCommentIcon = document.createElement("IMG");
							deleteCommentIcon.src = '/images/style/icons/comments/deleteOnBright.gif';
							deleteCommentIcon.id = this.id + 'deleteCommentIcon' + z1;
							deleteCommentIcon.className = 'deleteCommentIcon';
							eval("deleteCommentIcon.onmouseover = function(event) { this.className = 'deleteCommentIcon hovered'; mousePointerLabel.display(event, 'Kommentar l&ouml;schen'); };");
							eval("deleteCommentIcon.onmouseout = function() { this.className = 'deleteCommentIcon'; mousePointerLabel.clear(); };");
							eval("deleteCommentIcon.onclick = function() { " + this.name + ".deleteComment('" + Object["comments"][z1]["PublicID"] + "'); };");
						}
						
						reportCommentIcon = document.createElement("IMG");
						reportCommentIcon.src = '/images/style/icons/comments/reportOnBright.gif';						
						
						reportCommentIcon.id = this.id + 'reportCommentIcon' + z1;
						reportCommentIcon.className = 'reportCommentIcon';
						eval("reportCommentIcon.onmouseover = function(event) { this.className = 'reportCommentIcon hovered'; mousePointerLabel.display(event, 'Kommentar melden'); };");
						eval("reportCommentIcon.onmouseout = function() { this.className = 'reportCommentIcon'; mousePointerLabel.clear(); };");
						eval("reportCommentIcon.onclick = function() { " + this.name + ".reportComment('" + Object["comments"][z1]["PublicID"] + "'); };");						

						if(Object["comments"][z1]["IsDeletable"]) {								
							commentOptionsContainer.appendChild(deleteCommentIcon);
						}							
						commentOptionsContainer.appendChild(reportCommentIcon);							
						
						commentBackground.appendChild(commentOptionsContainer);

						comment.appendChild(commentBackground);
						
						document.getElementById(this.id + 'CommentsBox').appendChild(comment);
						
						var userImage = document.createElement("IMG");
						userImage.className = 'userImage';
						userImage.src = Object["comments"][z1]["UserInfo"]["ImagePath"];
						eval("userImage.onclick = function() { document.location.href = '/user/" + Object["comments"][z1]["UserInfo"]["URLName"] + "';};");
						eval("userImage.onmouseover = function(event) { this.className = 'userImage hovered'; mousePointerLabel.display(event, '" + Object["comments"][z1]["UserInfo"]["Name"] + "');};");
						eval("userImage.onmouseout = function() { this.className = 'userImage'; mousePointerLabel.clear(); };");
						
						var userName = document.createElement("DIV");
						userName.className = 'userName';
						userName.innerHTML = Object["comments"][z1]["UserInfo"]["Name"] + ':';
						eval("userName.onclick = function() { document.location.href = '/user/" + Object["comments"][z1]["UserInfo"]["URLName"] + "';};");						
						eval("userName.onmouseover = function() { this.className = 'userName hovered'; };");
						eval("userName.onmouseout = function() { this.className = 'userName'; };");

						var commentDate = document.createElement("DIV");
						commentDate.className = 'commentDate';
						commentDate.innerHTML = Object["comments"][z1]["Date"]["Day"] + '. ' + Object["comments"][z1]["Date"]["MonthShort"] + " '" + Object["comments"][z1]["Date"]["YearShort"];						
						
						var commentText = document.createElement("DIV");
						commentText.className = 'commentText';
						commentText.innerHTML = text;

						document.getElementById(this.id + z1 + "commentContentLeft").appendChild(userImage);						
						document.getElementById(this.id + z1 + "commentContentRight").appendChild(userName);						
						document.getElementById(this.id + z1 + "commentContentRight").appendChild(commentDate);
						document.getElementById(this.id + z1 + "commentContentRight").appendChild(commentText);						
		
						if(commentListBackground == 'even') {
							commentListBackground = 'uneven';
						} else {
							commentListBackground = 'even';
						}
					}
				} else {
					displayErrorMessage = true;
				}			
				
				var commentsScrollbar = document.createElement("DIV");
				commentsScrollbar.className = 'commentsScrollBar';				
				
				commentsScrollbarText = document.createElement("DIV");
				commentsScrollbarText.className = 'commentsScrollbarText';
				commentsScrollbarText.innerHTML = 'Seite ' + currentPage + ' von ' + pagesCounter;
					
				var scrollLeftIcon = document.createElement("IMG");
				scrollLeftIcon.src = '/images/style/arrows/greyArrowLeftActive.gif';
				if(currentPage > 1) {
					eval("scrollLeftIcon.onclick = function() { " + this.name + ".gotoPage(" + Math.ceil(currentPage - 1) + "); }; ");
					scrollLeftIcon.className = 'scrollLeftIcon';
				} else {
					scrollLeftIcon.className = 'scrollLeftIcon inactive';
				}
					
				var scrollRightIcon = document.createElement("IMG");
				scrollRightIcon.src = '/images/style/arrows/greyArrowRightActive.gif';
				if(currentPage < pagesCounter) {
					eval("scrollRightIcon.onclick = function() { " + this.name + ".gotoPage(" + Math.ceil(currentPage + 1) + "); }; ");
					scrollRightIcon.className = 'scrollRightIcon';
				} else {
					scrollRightIcon.className = 'scrollRightIcon inactive';					
				}
					
				commentsScrollbar.appendChild(commentsScrollbarText);	
				commentsScrollbar.appendChild(scrollLeftIcon);					
				commentsScrollbar.appendChild(scrollRightIcon);

				if(!displayErrorMessage) {
					document.getElementById(this.id + 'CommentsBox').appendChild(commentsScrollbar);
				}
			}
		}
		
		if(displayErrorMessage) {
			var noCommentsMessageContainer = document.createElement("DIV");
			noCommentsMessageContainer.className = 'noCommentsMessageContainer';
			
			var noCommentsIcon = document.createElement("IMG");
			noCommentsIcon.src = '/images/style/icons/commentsOnBright.gif';			
			noCommentsIcon.className = 'noComentsIcon';
			
			var noCommentsMessage = document.createElement("DIV");
			noCommentsMessage.className = 'noCommentsMessage';
			if(this.type == 'user') {
				noCommentsMessage.innerHTML = 'Es wurden noch keine Eintr&auml;ge geschrieben.';				
			} else {
				noCommentsMessage.innerHTML = 'Es wurden noch keine Kommentare abgegeben.';		
			}
			noCommentsMessageContainer.appendChild(noCommentsIcon);
			noCommentsMessageContainer.appendChild(noCommentsMessage);
			
			document.getElementById(this.id + "CommentsBox").appendChild(noCommentsMessageContainer);
		}
	}
	
	this.deleteComment = function(id) {
		var output = new Object();
		output.commentId = id;
		outputString = JSONstring.make(output);

		jsonPostData('jsApi/comments/delete', escape(outputString), this.name + '.deleteCommentInt');	
	}
	
	this.deleteCommentInt = function(Object) {
		this.loadComments(1);
	}
	
	this.reportComment = function(id) {
		document.location.href = '/reports/send.php?type=comment&id=' + id;
	}		
	
	// Display Write-Comment-Form
	this.writeComment = function() {
		if(userLoggedIn) {
			htmlString = "<div class='commentWriteBox' id='" + this.id + "writeCommentBox'><div class='commentCloseLink'><a href='JavaScript:" + this.name + ".init();'>X</a></div><h3>Kommentar schreiben</h3>";
			htmlString = htmlString + "<div class='info'>Gebe deinen Kommentar ein und klicke auf 'Weiter'.</div>";
			htmlString = htmlString + "<div class='writeBoxContent'><form accept-charset='utf-8' action='JavaScript: " + this.name + ".sendComment();'><textarea id='" + this.id + "CommentString' onFocus='clearInputField(this.id, \"" + defaultCommentString + "\");' >" + defaultCommentString + "</textarea><br /><input onClick='" + this.name + ".init();' type='button' class='cancelButton' value='Abbrechen'><input id='" + this.id + "SubmitButton' class='defaultButton' type='submit' value='Speichern &raquo;'></form></div>";
			htmlString = htmlString + "</div>";
	
			document.getElementById(this.id + 'CommentsBox').innerHTML = htmlString;
		} else {
			this.messageWindow = new MessageWindowClass();
			this.messageWindow.className = 'photoUploadMessageWindow';
			this.messageWindow.title = 'Bitte melde dich an...';
			this.messageWindow.message = 'Du musst dich anmelden um einen Kommentar schreiben zu k&ouml;nnen. <br /><br /><li><a href="/login/">Zur Anmeldung &raquo;</a></li>';
			this.messageWindow.name = this.name + '.messageWindow';
			this.messageWindow.displayCurtain = false;
			this.messageWindow.display();				
		}
	}
	
	// Send Comment to Webserver
	this.sendComment = function() {
		// Disable 'Save'-Button
		document.getElementById(this.id + "SubmitButton").disabled = true;
		
		// Initiliase Variables
		var comment = document.getElementById(this.id + "CommentString").value;
			
		if(comment.length > 0 && comment != defaultCommentString) {		
			var output = new Object();
			output.type = this.type;
			output.objectId = this.objectId;
			output.comment = encodeURIComponent(comment);
			
			outputString = JSONstring.make(output);

			jsonPostData('jsApi/comments/save', escape(outputString), this.name + '.init');
		} else {
			this.init();
		}	
	}
	
	this.gotoPage = function(page) {
		if(page <= pagesCounter) {
			currentPage = page;
			this.loadComments(page);
		}
	}
}

function commentsCommentsjs() {}
