<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.load.gallery");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	$mAlbumId = $p["albumId"];
	$a["htmlContent"] = null;
	
	$mCount = 0;
	$mPicturesQuery = $systemExecution->runQuery("SELECT * FROM web_images WHERE image_album_id = '%s'", $mAlbumId);
	$mImageCount = 0;
	$mAlbumName = "{album.name}";
	$mImageCounter = 0;
	while($mImageRow = $mPicturesQuery->fetch_assoc()) {
		$mImageLikeCount = $systemExecution->runQuery("SELECT COUNT(id) AS likeCount FROM web_likes WHERE image_id = '%s'", $mImageRow["id"])->fetch_object()->likeCount;
		$mImageCommentCount = $systemExecution->runQuery("SELECT COUNT(id) AS commentCount FROM web_comments WHERE image_id = '%s'", $mImageRow["id"])->fetch_object()->commentCount;
		$mCount = $systemExecution->runQuery("SELECT COUNT(id) AS timelineCount FROM web_timeline WHERE post_image_id = '%s'", $mImageRow["id"])->fetch_object()->timelineCount;
		$mAlbumName = $systemExecution->runQuery("SELECT album_name FROM web_albums WHERE id = '%s'", $mAlbumId)->fetch_object()->album_name;
		
		if($mCount == 1) {
			$mTimeAgo = $systemExecution->runQuery("SELECT post_submit_date FROM web_timeline WHERE post_image_id = '%s'", $mImageRow["id"])->fetch_object()->post_submit_date;
			$mPostUserId = $systemExecution->runQuery("SELECT post_user_id FROM web_timeline WHERE post_image_id = '%s'", $mImageRow["id"])->fetch_object()->post_user_id;
			$mPrivacy = $systemExecution->runQuery("SELECT post_privacy FROM web_timeline WHERE post_image_id = '%s'", $mImageRow["id"])->fetch_object()->post_privacy;
		
			$mImageCount++;
			if($mImageCount == 1)
				$a["htmlContent"] .= "<div class=\"row descRow\">";
				
			$mImageHash = $mImageRow["image_hash"];
			$mImagePath = 'system.uploads/'.$mImageHash;
			$mOwnUserId = $systemHandler->loadHandler("USER")->mUserId;
			$mOkayGiven = false;
			
			switch($mPrivacy) {
				case 1: // everyone
					$mOkayGiven = true;
				break;
				
				case 2: //friends
					if($mPostUserId == $mOwnUserId || $systemExecution->runQuery("SELECT COUNT(id) AS friendCount FROM web_friends WHERE (user_one_id = '%s' AND user_two_id = '%s') OR (user_one_id = '%s' AND user_two_id = '%s')", array($mOwnUserId, $mPostUserId, $mPostUserId, $mOwnUserId))->fetch_object()->friendCount > 0)
						$mOkayGiven = true;
				break;
				
				case 3: // me
					if($mPostUserId == $mOwnUserId)
						$mOkayGiven = true;
				break;
			}
			if($mOkayGiven) {
				$mImageCounter++;
				$a["htmlContent"] .= '<div class="col-md-4 col-xs-12">
										<figure data-parameter="'.$mImageRow["id"].'" class="galleryElement">
										<div data-location="galleryPicture-'.$mImageRow["id"].'" data-parameter="'.$mImagePath.'" class="galleryPicture" style="background-image: url(\''.$mImagePath.'\')"></div>
										<div class="galleryOverlay">
										<div class="galleryElementInfo">
										<span class="albumName">
											'.$mAlbumName.'
										</span>
										<span class="imageCount">
											<span class="likeCount">
												<i class="glyphicon glyphicon-thumbs-up font14"></i> '.$mImageLikeCount.'
											</span>
											<span class="commentCount">
												<i class="glyphicon glyphicon-comment font14"></i> '.$mImageCommentCount.'
											</span>
											<span class="timeAgo">
												'.$systemCore->timeAgo($mTimeAgo).'
											</span>
										</span>
									</div>
								</div>
							</figure>
						</div>';
			}
			if($mImageCount % 3 == 0) {
				$a["htmlContent"] .= "</div>";
				$mImageCount = 0;
			}
		} else continue;
	}
	
	if($mImageCounter == 0)
		$a["htmlContent"] .= "<center><h4>".$l->getValue("currentlynopictures")."</h4></center>";
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
	?>