﻿var SiteLifeCommentsObjects = [];
function SiteLifeComments(id, textboxID, listPanelID, articleID, onPageUrl, onPageTitle, perPage, sortOrder)
{
	this.__id = id;
	this.__textboxID = textboxID;
	this.__listPanelID = listPanelID;
	this.__articleKey = new ArticleKey(articleID);
	this.__onPageUrl = (onPageUrl == '' ? document.location.href : onPageUrl);
	this.__onPageTitle = (onPageTitle == '' ? document.title : onPageTitle);
	this.__perPage = perPage;
	this.__sortOrder = sortOrder;

	if (typeof SiteLifeComments.__initialized == 'undefined')
	{
		SiteLifeComments.prototype.__processResponse = function(responseBatch)
		{
			for (var i = 0; i < responseBatch.Responses.length; i++)
			{
				if (responseBatch.Responses[i].CommentPage)
				{
					var cp = responseBatch.Responses[i].CommentPage;
					for (var s = 0; s < SiteLifeCommentsObjects.length; s++)
					{
						if (SiteLifeCommentsObjects[s].__articleKey.ArticleKey.Key == cp.ArticleKey.Key)
						{
							if (responseBatch.Messages
								&& responseBatch.Messages.length
								&& responseBatch.Messages[0].Message != 'ok')
							{
								var commentBox = document.getElementById(SiteLifeCommentsObjects[s].__textboxID);
								if (commentBox)
									commentBox.value += '\r\n' + responseBatch.Messages[0].Message;
							}
							SiteLifeCommentsObjects[s].renderPage(cp);
							break;
						}
					}
				}
			}
		}

		SiteLifeComments.prototype.renderPage = function(commentPage)
		{
			if (commentPage.Comments == null || commentPage.Comments.length == 0)
				return;
			var listPanel = document.getElementById(this.__listPanelID);
			if (listPanel != null)
			{
				if (listPanel.hasChildNodes())
				{
					var p = listPanel.parentNode;
					var nlp = listPanel.cloneNode(false);
					p.replaceChild(nlp, listPanel);
					listPanel = nlp;
				}
				var totPages = Math.ceil(parseInt(commentPage.NumberOfComments) / this.__perPage);
				if (totPages > 1) listPanel.appendChild(this.createPager(parseInt(commentPage.OnPage), totPages, 'commentsTopPager'));
				for (var i = 0; i < commentPage.Comments.length; i++)
				{
					listPanel.appendChild(this.__renderComment(commentPage.Comments[i]));
					if (i % 2 == 1) listPanel.lastChild.className += '_alt';
				}
				if (totPages > 1) listPanel.appendChild(this.createPager(parseInt(commentPage.OnPage), totPages, 'commentsBottomPager'));
			}
		}

		SiteLifeComments.prototype.__renderComment = function(comment)
		{
			if (comment.AbuseReportCount >= 10)
			{
				var commentPanel = document.createElement('div');
				commentPanel.className = 'panel commentPanel';
				commentPanel.appendChild(document.createTextNode('Moderated.'));
				return commentPanel;
			}
			else
			{
				var commentPanel = document.createElement('div');
				commentPanel.className = 'panel commentPanel';
				var avatarPanel = document.createElement('div');
				avatarPanel.className = 'panel avatarPanel';
				var avatarHyperlink = document.createElement('a');
				avatarHyperlink.className = 'hyperLink avatarHyperlink';
				avatarHyperlink.href = Uri.resolveUrl('~/myvehix/mysummary.aspx?uid=' + comment.Author.UserKey.Key).toString();
				var avatarImage = document.createElement('img');
				avatarImage.className = 'img avatarImage';
				avatarImage.src = comment.Author.AvatarPhotoUrl;
				if (comment.Author.PersonaPrivacyMode == 'Private')
				{
					avatarPanel.appendChild(avatarImage);
				}
				else
				{
					avatarHyperlink.appendChild(avatarImage);
					avatarPanel.appendChild(avatarHyperlink);
				}
				commentPanel.appendChild(avatarPanel);

				var commentPostPanel = document.createElement('div');
				commentPostPanel.className = 'panel commentPostPanel';
				var commentAuthorLabel = document.createElement('span');
				commentAuthorLabel.className = 'label commentAuthorLabel';
				if (comment.Author.PersonaPrivacyMode == 'Private')
				{
					commentAuthorLabel.appendChild(document.createTextNode(comment.Author.DisplayName));
				}
				else
				{
					var commentAuthorLink = document.createElement('a');
					commentAuthorLink.className = 'hyperLink commentAuthorLink';
					commentAuthorLink.href = Uri.resolveUrl('~/myvehix/mysummary.aspx?uid=' + comment.Author.UserKey.Key).toString();
					commentAuthorLink.appendChild(document.createTextNode(comment.Author.DisplayName));
					commentAuthorLabel.appendChild(commentAuthorLink);
				}
				commentAuthorLabel.appendChild(document.createTextNode(' wrote:'));
				commentPostPanel.appendChild(commentAuthorLabel);

				var commentBodyLabel = document.createElement('span');
				commentBodyLabel.className = 'label commentBodyLabel';
				commentBodyLabel.appendChild(document.createTextNode(comment.CommentBody));
				commentPostPanel.appendChild(commentBodyLabel);

				var commentPostDateLabel = document.createElement('span');
				commentPostDateLabel.className = 'label commentPostDateLabel';
				commentPostDateLabel.appendChild(document.createTextNode(comment.PostedAtTime));
				commentPostPanel.appendChild(commentPostDateLabel);

				var commentRecommendLabel = document.createElement('span');
				commentRecommendLabel.className = 'label commentRecommendLabel';
				commentRecommendLabel.id = 'recommend:' + comment.CommentKey.Key;
				if (comment.CurrentUserHasRecommended == 'False')
				{
					var commentRecommendHyperLink = document.createElement('a');
					commentRecommendHyperLink.className = 'hyperLink commentRecommendHyperLink SiteLife_Recommend';
					commentRecommendHyperLink.appendChild(document.createTextNode('Recommend (' + comment.NumberOfRecommendations + ')'));
					commentRecommendHyperLink.href = 'javascript:void(0);';
					var title = this.__onPageTitle;
					commentRecommendHyperLink.onclick = function() { return gSiteLife.PostRecommendation('Comment', comment.CommentKey.Key, commentRecommendLabel.id, title); };
					commentRecommendLabel.appendChild(commentRecommendHyperLink);
				}
				else
					commentRecommendLabel.appendChild(document.createTextNode('Recommended (' + comment.NumberOfRecommendations + ')'));

				commentPostPanel.appendChild(commentRecommendLabel);

				var reportAbuseLabel = document.createElement('span');
				reportAbuseLabel.className = 'label reportAbuseLabel';
				reportAbuseLabel.id = 'rpt_' + comment.CommentKey.key;

				if (comment.CurrentUserHasReportedAbuse == 'False')
				{
					var commentAbuseHyperLink = document.createElement('a');
					commentAbuseHyperLink.id = comment.CommentKey.Key + '_RptAbuse';
					commentAbuseHyperLink.className = 'hyperLink commentAbuseHyperLink SiteLife_ReportAbuse';
					commentAbuseHyperLink.onclick = function()
					{
						var requestBatch = new RequestBatch();
						var abuseReport = new ReportAbuseAction(new CommentKey(comment.CommentKey.Key), 'Unknown', 'Unknown');
						requestBatch.AddToRequest(abuseReport);

						var abuseHandler = function(responseBatch)
						{
							if (responseBatch.Messages && responseBatch.Messages[0].Message == 'ok')
								reportAbuseLabel.replaceChild(document.createTextNode('Abuse Reported'), commentAbuseHyperLink);
							else
								alert('Unable to submit abuse report.');
						}
						requestBatch.BeginRequest(sitelife_process, abuseHandler);
					};
					commentAbuseHyperLink.href = 'javascript:void(0);';
					commentAbuseHyperLink.appendChild(document.createTextNode('Report Abuse'));
					reportAbuseLabel.appendChild(commentAbuseHyperLink);
				}
				else
					reportAbuseLabel.appendChild(document.createTextNode('Abuse Reported'));

				commentPostPanel.appendChild(reportAbuseLabel);
				commentPanel.appendChild(commentPostPanel);

				return commentPanel;
			}
		}

		SiteLifeComments.prototype.createPager = function(curpage, totpages, pagerStyle)
		{
			var pagingPanel = document.createElement('div');
			pagingPanel.className = 'panel pagingPanel';
			if (pagerStyle) pagingPanel.className += ' ' + pagerStyle;


			var pagingLabel = document.createElement('span');
			pagingLabel.className = 'span pagingLabel';

			var me = this;

			var createPage = function(p)
			{
				var thisPage = document.createElement('a');
				thisPage.href = 'javascript:void(0);';
				thisPage.onclick = function() { me.__pageComments(p); };
				thisPage.appendChild(document.createTextNode(p));
				return thisPage;
			}

			var first = 1;
			var last = totpages;

			if (totpages > 10)
			{
				if (curpage >= 5)
				{
					pagingLabel.appendChild(createPage(1));
					pagingLabel.appendChild(document.createTextNode(' ... '));

					first = curpage - 4;
					last = curpage + 4;
					if (last > totpages) last = totpages;
				}
				else
					last = 9;
			}

			if (curpage > first)
			{
				var prev = curpage - 1;
				var prevPage = document.createElement('a');
				prevPage.href = 'javascript:void(0);';
				prevPage.onclick = function() { me.__pageComments(prev); };
				prevPage.appendChild(document.createTextNode('< Prev'));
				pagingLabel.appendChild(prevPage);
				pagingLabel.appendChild(document.createTextNode(' '));
			}

			for (var p = first; p <= last; p++)
			{
				if (p == curpage)
					pagingLabel.appendChild(document.createTextNode(p + ' '));
				else
				{
					pagingLabel.appendChild(createPage(p));
					pagingLabel.appendChild(document.createTextNode(' '));
				}
			}

			if (curpage < totpages)
			{
				if (totpages > 10)
				{
					pagingLabel.appendChild(document.createTextNode('... '));
					pagingLabel.appendChild(createPage(totpages));
				}

				var next = curpage + 1;
				var nextPage = document.createElement('a');
				nextPage.href = 'javascript:void(0);';
				nextPage.onclick = function() { me.__pageComments(next); };
				nextPage.appendChild(document.createTextNode('Next >>'));
				pagingLabel.appendChild(nextPage);
			}

			pagingPanel.appendChild(pagingLabel);
			return pagingPanel;
		}

		SiteLifeComments.prototype.postComment = function(btn)
		{
			var commentBox = document.getElementById(this.__textboxID);
			var comment = commentBox.value;
			if (comment == null || comment.length == 0)
			{
				alert('Please porvide a comment first.');
				return;
			}
			commentBox.value = 'your comment has been submitted';
			commentBox.disabled = true;
			btn.disabled = true;

			setTimeout(function() { commentBox.disabled = false; commentBox.value = ''; btn.disabled = false; }, 30000);

			var requestBatch = new RequestBatch();
			var onpurl = new String(this.__onPageUrl);
			var hash = onpurl.indexOf('#', 0);
			if (hash > 0)
				onpurl = onpurl.substring(0, hash);
			hash = 0;
			var ontitle = new String(this.__onPageTitle);
			hash = ontitle.indexOf('#', 0);
			if (hash > 0)
				ontitle = ontitle.substring(0, hash);
			requestBatch.AddToRequest(new CommentAction(new ArticleKey(this.__articleKey.ArticleKey.Key), onpurl.toString(), ontitle.toString(), comment));
			requestBatch.AddToRequest(new CommentPage(new ArticleKey(this.__articleKey.ArticleKey.Key), this.__perPage, 1, this.__sortOrder));

			requestBatch.BeginRequest(sitelife_process, this.__processResponse);
			document.location.replace('#commentsTop_' + this.__id);
		}

		SiteLifeComments.prototype.__pageComments = function(page)
		{
			this.OnPaging();
			var p = (typeof page == 'number' && page > 0 ? page : 1);
			var requestBatch = new RequestBatch();
			requestBatch.AddToRequest(new CommentPage(this.__articleKey, this.__perPage, p, this.__sortOrder));

			requestBatch.BeginRequest(sitelife_process, this.__processResponse);
			if ((typeof page == 'number') && page > 0)
				document.location.replace('#commentsTop_' + this.__id);
		}

		SiteLifeComments.prototype.OnPaging = function() { }

		SiteLifeComments.__initialized = true;
	}

	SiteLifeCommentsObjects.push(this);
}