function loadImage(id) {
	
	jQuery.ajax({
		type: "post",
		url: "/wp-content/themes/pixeled/random_image.php",
		data: {cmd:'loadImage', id:id},
		beforeSend: function() {
			jQuery(".imageGallery").addClass("loading");
			jQuery(".imageContainer").fadeTo(500, 0);
		},
		success: function(html) {
			var newContent = "<input class=\"postId\" name=\"newId\" type=\"hidden\" value=\"" + 0 + "\" />" + html;
			jQuery(".imageContainer").html(newContent);
			jQuery(".imageGallery").removeClass("loading");
			jQuery(".imageContainer").fadeTo(500, 1);
		}
	});
}

function loadNextImage() {
	
	jQuery.ajax({
		type: "post",
		url: "/wp-content/themes/pixeled/random_image.php",
		data: {cmd:'nextImage'},
		beforeSend: function() {
			jQuery(".imageGallery").addClass("loading");
			jQuery(".imageContainer").fadeTo(500, 0);
		},
		success: function(html) {
			var newContent = "<input class=\"postId\" name=\"newId\" type=\"hidden\" value=\"" + 0 + "\" />" + html;
			jQuery(".imageContainer").html(newContent);
			jQuery(".imageGallery").removeClass("loading");
			jQuery(".imageContainer").fadeTo(500, 1);
		}
	});
}

function loadPrevImage() {

	jQuery.ajax({
		type: "post",
		url: "/wp-content/themes/pixeled/random_image.php",
		data: {cmd:'prevImage'},
		beforeSend: function() {
			jQuery(".imageGallery").addClass("loading");
			jQuery(".imageContainer").fadeTo(500, 0);
		},
		success: function(html) {
			var newContent = "<input class=\"postId\" name=\"newId\" type=\"hidden\" value=\"" + 0 + "\" />" + html;
			jQuery(".imageContainer").html(newContent);
			jQuery(".imageGallery").removeClass("loading");
			jQuery(".imageContainer").fadeTo(500, 1);
		}
	});
}

function loadRandomImage() {

	jQuery.ajax({
		type: "post",
		url: "/wp-content/themes/pixeled/random_image.php",
		data: {cmd:'randImage'},
		beforeSend: function() {
			jQuery(".imageGallery").addClass("loading");
			jQuery(".imageContainer").fadeTo(500, 0);
		},
		success: function(html) {
			var newContent = "<input class=\"postId\" name=\"newId\" type=\"hidden\" value=\"" + 0 + "\" />" + html;
			jQuery(".imageContainer").html(newContent);
			jQuery(".imageGallery").removeClass("loading");
			jQuery(".imageContainer").fadeTo(500, 1);
		}
	});
}

function voteUp(id) {
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'voteUpImg', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			jQuery(".rating").html(content);
		}
	});
}

function voteDown(id) {
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'voteDownImg', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			jQuery(".rating").html(content);
		}
	});
}

function voteUpImgComment(id) {
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'voteUpImgComment', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			jQuery("#image-comment-" + id + " .imgCommentRating").html(content);
		}
	});
}

function voteDownImgComment(id) {
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'voteDownImgComment', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			jQuery("#image-comment-" + id + " .imgCommentRating").html(content);
		}
	});
}

function approveImgComment(id, commentContainer) {
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'approveImgComment', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			jQuery("#image-comment-" + id + " .approveImgComment").html(content);
		}
	});
}

function deleteImgComment(id, container) {
	
	var commentContainer = jQuery(container).parent().parent();
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'deleteImgComment', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			if(content.indexOf("Deleted") != -1) {
				if(content.indexOf("Thread Deleted") != -1) {
					commentContainer = jQuery(container).parentsUntil("li.comment-level-1").parent();
				}
				jQuery(commentContainer).css("opacity","0.5");
				jQuery(commentContainer).before("<li class='deletedComment'>" + content + "</li>");
				var deletedCommentFiller = jQuery(commentContainer).parent().find("li.deletedComment");
				setTimeout(function() {
					jQuery(commentContainer).fadeOut().remove();
					jQuery(deletedCommentFiller).remove();
				}, 5000);
			} else {
				jQuery(commentContainer).find("p.author:first").text("[deleted]");
				jQuery(commentContainer).find("p.content:first").text("[deleted]");
				jQuery(commentContainer).before("<li class='deletedComment'>" + content + "</li>");
				var deletedCommentFiller = jQuery(commentContainer).parent().find("li.deletedComment");
				setTimeout(function() {
					jQuery(deletedCommentFiller).fadeOut().remove();
				}, 5000);
			}
		}
	});
}

function forceDeleteImgComment(id, container) {
	
	var commentContainer = jQuery(container).parent().parent();
	
	jQuery.ajax({
		type: "post",
		url: "/wac/ajax.php",
		data: {cmd: 'forceDeleteImgComment', id: id},
		beforeSend: function() {
			
		},
		success: function(content) {
			console.log(content);
			jQuery(commentContainer).css("opacity","0.5");
			jQuery(commentContainer).before("<li class='deletedComment'>" + content + "</li>");
			var deletedCommentFiller = jQuery(commentContainer).parent().find("li.deletedComment");
			setTimeout(function() {
				jQuery(commentContainer).fadeOut().remove();
				jQuery(deletedCommentFiller).remove();
			}, 5000);
		}
	});
}

function replyToImgComment(id, replyButton) {
	
	/* Get the id of the image the comment will pertain to */
	var imageId = jQuery("#submit-new-comment-form input[name='comment_image_id']").val();
	
	/* Get the user's id */
	var userId = jQuery("#submit-new-comment-form input[name='user_id']").val();
	
	/* Get the redirect url */
	var redirect = jQuery("#submit-new-comment-form input[name='redirect']").val();
	
	/* Create the reply code */
	var replyCode = "<div class='replyToImgCommentContainer'><h3>Reply to Comment</h3><form method='post' action='/wac/comments.php'><input type='hidden' value='" + id + "' name='comment_parent_id' /><input type='hidden' value='" + imageId + "' name='comment_image_id' /><input type='hidden' value='" + userId + "' name='user_id' /><input type='hidden' value='" + redirect + "' name='redirect' />";
	
	/* Figure out where to put the reply code */
	var replyContainer = jQuery(replyButton).parent();
	
	/* Figure out if the reply code should include a name field */
	if(userId == 0) {
		replyCode = replyCode + "<div class='commentAuthorContainer'><label for='comment_author'>Name</label><input type='text' name='comment_author' /></div>";
	}
	replyCode = replyCode + "<p><textarea name='content'></textarea></p><p><input type='submit' value='Submit Comment' /></form></div>";
	
	/* Hide the reply button */
	jQuery(replyButton).hide();
	
	/* Replace the reply link with the reply code */
	jQuery(replyContainer).after(replyCode);
}

jQuery(document).ready(function() {
	
	jQuery("a.submitNewCommentLink").bind("click", function(event) {
		
		jQuery(".submitCommentContainer").slideDown();
	});
})
