
	var edit_itemType;
	var edit_itemID;
	var edit_oldText;
	var edit_newText;
	var edit_oldCategory;
	var edit_newCategory;
	
	
	function updateEditLink(postID)
	{
		$.get("getposteditable.php", { post: postID },
			function(resp)
			{
				resp = $.trim(resp);
				if ( resp ) { $("#post"+postID+"authorTools").html(resp); }
				else 
				{ 
					$("#post"+postID+"authorTools").fadeOut(500); 
					clearInterval( eval('editupdate'+postID) );
				}
			}
		);

	}
	
	
	function goEdit(itemType,itemID,admin)
	{
		if ( itemType == "post" && !admin )
		{
			$.get("getposteditable.php", { post: itemID },
				function(resp)
				{
					resp = $.trim(resp);
					if ( resp ) { openEdit(itemType,itemID); }
					else 
					{ 
						alert("Sorry, you're too late to edit this post."); 
						$("#post"+itemID+"authorTools").fadeOut(500); 
						clearInterval( eval('editupdate'+itemID) );
					}
				}
			);
		}
		
		else { openEdit(itemType,itemID); }
	}
		
	function openEdit(itemType,itemID)
	{
		$("#edit_itemType").text(itemType);
		edit_itemType = itemType;
		
		$("#edit_itemID").text(itemID);
		edit_itemID = itemID;
		
		currentText = $("#"+itemType+itemID+"text").text();
		edit_oldText = currentText;
		
		$("#edit_input").attr("value", currentText );
		
		if ( edit_itemType == "post" ) 
		{ 
			edit_oldCategory = $("#category"+itemID).text();
			$("#edit_category").attr('value',edit_oldCategory);
			$("#changecategory").css('display','block'); 
		}
		
		
		centerPopup("#editPopup");
		loadPopup("#editPopup");
	}
	
	
	
	function saveEdit()
	{	
		//Process the edited text to remove URLs and crud
		edit_newText = $("#edit_input").attr("value");

		if ( edit_itemType == 'post' ) {
			$.ajax({
				type: "POST",
				url: "processtext.php",
				data: { what: "add_text" , text: urlencode(edit_newText) },
					success: function(resp){
						
						resp = $.trim(resp);
						edit_newText = resp;

						reallySaveEdit();
					}
			});
		}
		else { reallySaveEdit(); }
	}
	
	
	
	function reallySaveEdit()
	{
		
		edit_newCategory = $("#edit_category").attr('value');
		
		//Save it to the DB
		$.ajax({
			type: "POST",
			url: "saveedit.php",
			data: "itemType=" + edit_itemType + "&itemID=" + edit_itemID + "&newText=" + urlencode(edit_newText) + "&category=" + edit_newCategory,
			success: function(resp){
				resp = $.trim(resp);
				
				if ( resp == "done")
				{
					closePopup();
					setTimeout("setNewTextAfterEdit();",500);
				}
				else { alert(resp); }
			}
		});
	}
	
	

	function setNewTextAfterEdit()
	{
		
		edit_newText = edit_newText.replace("amirite?","<strong>amirite?</strong>");
		edit_newText = edit_newText.replace("am I right?","<strong>am I right?</strong>");
		edit_newText = edit_newText.replace("\n","<br>");
		
		if ( edit_newText != edit_oldText)
		{
			$("#"+edit_itemType+edit_itemID+"text").animate( { opacity: 0 }, blinkSpeed);
			$("#"+edit_itemType+edit_itemID+"text").html(edit_newText);
			$("#"+edit_itemType+edit_itemID+"text").animate( { opacity: 100 }, blinkSpeed);
		}
		
		if ( edit_newCategory != edit_oldCategory && edit_itemType == "post")
		{
			$("#category"+edit_itemID).animate( { opacity: 0 }, blinkSpeed);
			$("#category"+edit_itemID).text(edit_newCategory);
			$("#category"+edit_itemID).animate( { opacity: 100 }, blinkSpeed);
		}
	}
	
	
	
	
	
	function deleteComment(commentID)
	{
		if ( confirm("Are you sure you want to delete comment #"+commentID+"?") )
		{	
			$.get("deletecomment.php", { commentID: commentID },
				function(resp){
					resp = $.trim(resp);
					
					if (resp == "done") { $("#comment"+commentID).fadeOut(1000); }
					else { alert(resp); }

  			});
		}
	}
	
	
	function authorDeletePost(postID)
	{
		if ( confirm("Are you sure you want to delete post #"+postID+"?") )
		{	
			$.get("deletepost.php", { postID: postID },
				function(resp){
					
					if (resp == "done") { $("#post"+postID).fadeOut(1000); }
					else { alert(resp); }
	
			});
		}
	}
	