<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.timeline.commentload");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	$systemSmilies = $systemHandler->loadHandler("SMILIES");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mContainerId = $p["containerId"];
	$mOutputType = isset($p["outputType"]) ? false : true;
	$mCommentIndex = isset($_POST["containerIndex"]) ? $p["containerIndex"] : 3;
	$a["commentHTML"] = null;
	$mUserId = $systemHandler->loadHandler("SESSION")->loadSession("USER");
	$mCommentLimit = $systemHandler->loadHandler("CONFIG")->mCommentsDisplay;
	
	$containerCommentsCount = $systemExecution->runQuery("SELECT COUNT(id) AS commentsCount FROM web_comments WHERE container_id = '%s'", $mContainerId)->fetch_object()->commentsCount;
		
		$a["commentHTML"] = "";
		if($containerCommentsCount > 0) {
			$mCommentsQuery = $systemExecution->runQuery("SELECT * FROM web_comments WHERE container_id = '%s' ORDER BY id DESC LIMIT %s,%s", array($mContainerId, $mCommentIndex, $mCommentLimit));
			
			if($containerCommentsCount > $mCommentLimit && $containerCommentsCount > ($mCommentIndex + $mCommentLimit)) {
				if($mOutputType)
					$a["commentHTML"] .= "<div data-fancy=\"false\" data-event=\"load-comments\" data-parameter=\"".$mContainerId."\" class=\"panel-footer loadMoreComments\" style=\"border-bottom-right-radius: 0px; border-bottom-left-radius: 0px\">
												<a>
													<strong><i class=\"glyphicon glyphicon-refresh font10\"></i> ".$l->getValue("display")." ".($containerCommentsCount - ($mCommentIndex + $mCommentLimit))." ".$l->getValue("morecomments")."</strong>
												</a>
											</div>";
				else
					$a["commentHTML"] .= "<div data-fancy=\"true\" data-event=\"load-comments\" data-parameter=\"".$mContainerId."\" class=\"panel-footer loadMoreComments\" style=\"border-bottom-right-radius: 0px; border-bottom-left-radius: 0px\">
												<a>
													<strong><i class=\"glyphicon glyphicon-refresh font10\"></i> ".$l->getValue("display")." ".($containerCommentsCount - ($mCommentIndex + $mCommentLimit))." ".$l->getValue("morecomments")."</strong>
												</a>
											</div>";
			}
			
			$mCommentsArray = array();
			while($mRow = $mCommentsQuery->fetch_assoc()) {
				$mCachedUser = new systemUSER($mRow["user_id"]);
				
				$commentLikeType = $systemExecution->runQuery("SELECT COUNT(id) AS likeCount FROM web_likes WHERE user_id = '%s' AND comment_id = '%s'", array($systemHandler->loadHandler("USER")->mUserId, $mRow["id"]))->fetch_object()->likeCount > 0 ? $l->getValue("unlikethis") : $l->getValue("likethis");
				
				$mCommentContainerOwnerId = $systemExecution->runQuery("SELECT web_timeline.post_user_id FROM web_comments, web_timeline WHERE web_timeline.id = web_comments.container_id AND web_comments.id = '%s'", $mRow["id"])->fetch_object()->post_user_id;
				$mCommentLikeCount = $systemExecution->runQuery("SELECT COUNT(id) AS commentLikeCount FROM web_likes WHERE comment_id = '%s'", $mRow["id"])->fetch_object()->commentLikeCount;
				$mCommentPrivileges = $mCommentContainerOwnerId == $mUserId || $mRow["user_id"] == $mUserId;
				$mOwnComment = $mRow["user_id"] == $mUserId;
				$mCommentHTML = "";
				
				if($mOutputType)
					$mCommentHTML = "<div container-id=\"".$mRow["container_id"]."\" id=\"container-comment-row-".$mRow["id"]."\" class=\"panel-footer\" style=\"border-bottom-right-radius: 0px; border-bottom-left-radius: 0px\">
											<div class=\"row\">
												<div class=\"col-md-1 hidden-xs hidden-sm\">
													<a href=\"page.profile.php?profile-id=".$mCachedUser->mUserId."\"><img class=\"timeline-profilepicture-comment\" src=\"".$mCachedUser->mProfilePicture."\"></a>
												</div>
													
												<div class=\"col-md-11 col-xs-12 commentText\">
													<div class=\"mainCommentText\">
														<strong><a class=\"profilePopOver\" href=\"page.profile.php?profile-id=".$mCachedUser->mUserId."\">".$mCachedUser->mFullname."</a></strong>
														<span location-id=\"".$mRow["id"]."\" class=\"".($mOwnComment ? "comment-text" : "")."\" id=\"commentLocation-".$mRow["id"]."\">
															".$systemSmilies->initializeSmilies(strlen($mRow["comment_text"]) < 150 ? htmlspecialchars($mRow["comment_text"]) : substr(htmlspecialchars($mRow["comment_text"]), 0, 150).".. · ", true)."
															".(strlen($mRow["comment_text"]) > 150 ? "<a href=\"javascript:;\" data-event=\"display-more-comment\" data-parameter=\"".$mRow["id"]."\" style=\"font-size: 12px\">".$l->getValue("displaymore")."</a>" : null)."
														</span>
														
														<div class=\"commentTime\">
															<span class=\"mutedText\">".$systemCore->timeAgo($mRow["submit_date"])."</span>
																 · 
															<a href=\"javascript:;\" data-type=\"comment\" data-parameter=\"".$mRow["id"]."\" data-event=\"report\">".$l->getValue("report")."</a>
																 · 
															<a href=\"javascript:;\" comment-id=\"".$mRow["id"]."\" class=\"commentLikeEvent\">".$commentLikeType."</a>
																· 
															<a href=\"javascript:;\" class=\"display-likes comment-like-count-".$mRow["id"]."\" comment-id=\"".$mRow["id"]."\" display-likes-type=\"comment\">
																<i class=\"glyphicon glyphicon-thumbs-up\" style=\"font-size: 11px\"></i>
															</a>
 															<a href=\"javascript:;\" id=\"comment-like-count-".$mRow["id"]."\" class=\"display-likes comment-like-count-".$mRow["id"]."\" comment-id=\"".$mRow["id"]."\" display-likes-type=\"comment\">
																".$mCommentLikeCount."
															</a>
														</div>
													</div>
													
													".($mCommentPrivileges ? "
														<div class=\"removeCommentRight\">
																<a href=\"javascript:;\" comment-id=\"".$mRow["id"]."\" class=\"removeComment\">
																	<i class=\"glyphicon glyphicon-remove\"></i>
																</a>
														</div>" : null)."
												</div>
											</div>
										</div>";
				else
					$mCommentHTML = "<li class=\"list-group-item\" comment-row-id=\"".$mRow["id"]."\" container-id=\"".$mRow["container_id"]."\">
										<img src=\"".$mCachedUser->mProfilePicture."\" class=\"fancyCommentPicture\" />
										<div class=\"commentContentArea\">
											<a href=\"page.profile.php?profile-id=".$mCachedUser->mUserId."\">
												".$mCachedUser->mFullname."
											</a>
											<span location-id=\"".$mRow["id"]."\" class=\"".($mOwnComment ? "comment-text" : "")."\" id=\"commentLocation-".$mRow["id"]."\">
												".$systemSmilies->initializeSmilies(strlen($mRow["comment_text"]) < 150 ? htmlspecialchars($mRow["comment_text"]) : substr(htmlspecialchars($mRow["comment_text"]), 0, 150).".. · ", true)."
												".(strlen($mRow["comment_text"]) > 150 ? "<a href=\"javascript:;\" data-event=\"display-more-comment\" data-parameter=\"".$mRow["id"]."\" style=\"font-size: 12px\">".$l->getValue("displaymore")."</a>" : null)."
											</span>
											<div class=\"commentTime smallerFont\">
												<span class=\"mutedText\">".$systemCore->timeAgo($mRow["submit_date"])."</span>
																 · 
												<a href=\"javascript:;\" data-type=\"comment\" data-parameter=\"".$mRow["id"]."\" data-event=\"report\"><i class=\"glyphicon glyphicon-bullhorn font9\"></i></a>
																 · 
												<a href=\"javascript:;\" comment-id=\"".$mRow["id"]."\" class=\"removeComment\">
													<i class=\"glyphicon glyphicon-remove font9\"></i>
												</a>
													<br />
												<a href=\"javascript:;\" comment-id=\"".$mRow["id"]."\" class=\"commentLikeEvent\">".$commentLikeType."</a>
																· 
												<a href=\"javascript:;\" class=\"display-likes comment-like-count-".$mRow["id"]."\" comment-id=\"".$mRow["id"]."\" display-likes-type=\"comment\">
													<i class=\"glyphicon glyphicon-thumbs-up font9\" style=\"font-size: 11px\"></i>
												</a>
 												<a href=\"javascript:;\" id=\"comment-like-count-".$mRow["id"]."\" class=\"display-likes comment-like-count-".$mRow["id"]."\" comment-id=\"".$mRow["id"]."\" display-likes-type=\"comment\">
													".$mCommentLikeCount."
												</a>
											</div>
										</div>
									</li>";
				array_push($mCommentsArray, $mCommentHTML);
			}
			$a["commentCount"] = count($mCommentsArray);
			for($i = count($mCommentsArray); $i > 0; $i--)
				$a["commentHTML"] .= $mCommentsArray[$i - 1];
		}
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
	?>
