/**
 * Controls the Album Widget found at the top right of the Image View Page
 */
function AlbumWidgetPages()
{
	this.ajaxURL = "/ajax/album_widget_pages.php";
	this.user_sid = 0;
	this.album_sid = 0;
	this.image_hash = 0;
	this.page = 0;
	this.pagelimit = 0;

	/**
	 * Load in information to be used
	 */
	this.loadData = function(user_sid, album_sid, image_hash, page, pagelimit)
	{
		this.user_sid = user_sid;
		this.album_sid = album_sid;
		this.image_hash = image_hash;
		this.page = page;
		this.pagelimit = pagelimit;
	};

	/**
	 * Grabs the previous set of Images in this Album
	 */
	this.prev = function()
	{
		if (this.page > 1)
		{
			this.loadAjaxLoader();
			this.page--;
			$.post(this.ajaxURL,
				{ user_sid: this.user_sid, album_sid: this.album_sid, image_hash: this.image_hash, page: this.page },
				function(data)
				{
					$(".album_widget").children(".middle").html(data);
				});
		}
	};
	
	/**
	 * Grabs the next set of Images in this Album
	 */
	this.next = function()
	{
		if (this.page < this.pagelimit)
		{
			this.loadAjaxLoader();
			this.page++;
			$.post(this.ajaxURL,
				{ user_sid: this.user_sid, album_sid: this.album_sid, image_hash: this.image_hash, page: this.page },
				function(data)
				{
					$(".album_widget").children(".middle").html(data);
				});
		}
	};
	
	/**
	 * Loads the Ajax loader gif until results come back
	 */
	this.loadAjaxLoader = function()
	{
		$(".album_widget").children(".middle").html("<img src='/images/ajax-loader.gif' style='margin-left: 112px; margin-top: 112px;' />");
	};
};
var AlbumWidgetPages = new AlbumWidgetPages();

/**
 * Image Favorites
 */
function ImageFavorite()
{
	this.ajaxURL = "/ajax/image_favorite.php";
	
	/**
	 * Favorites/Un-Favorites this Image
	 * 
	 * @param text image_hash
	 */
	this.favorite = function(image_hash)
	{
		$.post(this.ajaxURL,
			{ type: "favorite", hash: image_hash },
			function(data)
			{
				if (data.error == 0)
				{
					if (data.isFavorited)
					{
						$(".image_"+image_hash+"_favorite").removeClass("false is_false").addClass("true is_true");
					} else {
						$(".image_"+image_hash+"_favorite").removeClass("true is_true").addClass("false is_false");
					}
				} else {
					alert(data.html);
				}
			}, "json");
	};
	
	/**
	 * Unfavorites the Image by the User
	 * 
	 * @param text image_hash
	 * @param int page
	 */
	this.user_unfavorite = function(image_hash, page)
	{
		if (isNaN(page) || page == "") page = 1;
		
		$("#me_favorite_image_"+image_hash).fadeOut(400,
			function()
			{
				var container = $("#me_favorite_image_"+image_hash).parent();
				$("#me_favorite_image_"+image_hash).remove();
				
				$.post(ImageFavorite.ajaxURL,
					{ type: "user_unfavorite", hash: image_hash, page: page },
					function(data)
					{
						if (data.error == 0)
						{
							var div = $(document.createElement("div"));
							div.html(data.html);
							container.append(div);
							div.fadeIn(400);
						} else {
							alert(data.html);
						}
					}, "json");
			});
	};
}
var ImageFavorite = new ImageFavorite();

/**
 * Does a special animation of loading some more Images
 */
function ImageResults()
{
	this.ajaxURL = "/ajax/image_results.php";
	this.container_id = "#image_results";
	this.page = 0;
	this.isLoading = false;
	
	/**
	 * Load a new Page
	 * 
	 * @param int page
	 * @param text type
	 */
	this.loadPage = function(page, type)
	{
		if (!this.isLoading)
		{
			//Fade out
			$(this.container_id).find(".result").fadeOut(400);
			this.isLoading = true;
			location.hash = "page"+page;
			
			setTimeout(function()
			{
				//Set a div with a fix height
				$(ImageResults.container_id).find("tbody").find("td").html("<div style='height: 660px; position: relative;'></div>");
				
				//Get all the new Images
				$.post(ImageResults.ajaxURL,
					{ type: type, page: page },
					function(data)
					{
						//Replace the Pagination
						var div = $(document.createElement("div"));
						div.html(data.html);
						div = div.find(".zata_pagination:first").html();
						$(".zata_pagination").html(div);
						
						ImageResults.loadImages(data.images);
						ImageResults.isLoading = false;
					}, "json");
			}, 350);
		}
	};
	
	/**
	 * Handles the Special Animation
	 * 
	 * @param array data
	 */
	this.loadImages = function(data)
	{
		var display = $(this.container_id).find("div");
		var div = null;
		
		//Animate
		var j=0;
		for(var i=0,n=data.length; i<n; i++)
		{
			//Position in the center
			div = $(document.createElement("div"));
			div.addClass("result");
			div.css({
				position: "absolute",
				left: "380px",
				top: "220px"
			});
			div.html(data[i]);
			
			//Append the Image
			display.append(div);
			
			//Animate from the inside out
			div.animate({left: (190 * (i % 5))+"px", top: (220 * j)+"px"}, 800);
			
			//Row check
			if ((i+1) % 5 == 0) j++;
		}
		$(this.container_id).find("tbody").find("td").children("div").append("<div class='clear'></div>");
	};
};
var ImageResults = new ImageResults();

/**
 * Image Comments
 */
function Comments()
{
	this.ajaxURL = "/ajax/comments.php";
	this.image_hash = "";
	this.count = 0;
	this.max = 10;
	this.page = 1;
	
	this.isViewingMore = false;
	
	/**
	 * Adds a Comment
	 */
	this.addComment = function()
	{
		var com = $("#comment_text");
		
		if (com.val().length !== 0)
		{
			$.post(this.ajaxURL,
				{ hash: this.image_hash, type: "add", text: com.val() },
				function(data)
				{
					if (data.error == 0)
					{
						Comments.count++;
						com.val("");
						
						$("#comments_log_none").remove();
						var div = $(document.createElement("div"));
						div.html(data.html);
						div.hide();
						
						$("#comments").prepend(div);
						div.slideDown(400);
						//$("#comment_"+data.sid).focus();
						
						//Delete the last comment if over 10
						if (Comments.count > Comments.max && Comments.page == 1)
						{
							$(".comment:last").slideUp(400);
							$("#comment_viewmore").css("display", "block");
							
							setTimeout(function()
							{
								$(".comment:last").remove();
							}, 401);
						}
					} else {
						alert(data.reasons);
					}
				}, "json");
		}
	};
	
	/**
	 * Deletes the Comment, only the original Author, and an Admin can delete a Comment
	 */
	this.deleteComment = function(sid)
	{
		$.post(this.ajaxURL,
			{ hash: this.image_hash, type: "del", sid: sid },
			function(data)
			{
				if (data.error == 0)
				{
					Comments.count--;
					
					if (Comments.count == 0)
					{
						var div = $(document.createElement("div"));
						div.attr("id", "comments_log_none");
						div.html("No Comments Yet");
						div.hide();
						
						$("#comments").append(div);
						div.slideDown(400);
					}
					
					$("#comment_"+sid).slideUp(400);
					
					setTimeout(function()
					{
						$("#comment_"+sid).remove();
					}, 401);
				} else {
					alert(data.reasons);
				}
			}, "json");
	};
	
	/**
	 * Displays more Comments
	 */
	this.viewmore = function()
	{
		if (!this.isViewingMore)
		{
			this.isViewingMore = true;
			this.page++;
			
			//See if the show more needs to go away
			if (this.page == Math.ceil(this.count / this.max))
			{
				$("#comment_viewmore").slideUp(400);
			}
			
			$.post(this.ajaxURL,
				{ hash: this.image_hash, type: "viewmore", page: this.page },
				function(data)
				{
					if (data.error == 0)
					{
						Comments.isViewingMore = false;
						
						var div = $(document.createElement("div"));
						div.html(data.html);
						div.hide();
						
						$("#comments").append(div);
						div.slideDown(400);
					}
				}, "json");
		}
	};
};
var Comments = new Comments();

/**
 * Changes the Safe Viewing type for the visitor|user. Then redirects them to the same page
 * 
 * @param text type
 */
function SafeViewing(type)
{
	$.post("/ajax/safeviewing.php",
		{ type: type },
		function()
		{
			window.location = window.location;
		});
};

//Hidden Easter Egg
var iHasASecretKeys=new Array(38,38,40,40,37,39,37,39,66,65,13);var iHasASecretKeyIndex=0;$().ready(function(){$("html").keyup(function(e){if (e.keyCode==iHasASecretKeys[iHasASecretKeyIndex]){iHasASecretKeyIndex++;if (iHasASecretKeyIndex>=iHasASecretKeys.length){$("img").draggable();}}else {iHasASecretKeyIndex=0;}});});