$(document).ready(function()
{
	//fade in any new posts
	$("#highlightme").hide();
	$("#highlightme").fadeIn("slow");
	
	//process new replies
	$(".reply_submit").live("click", function(){

		//set vals
		var element = $(this);
		var post_id = element.attr("id");
		var wall_user_id = element.attr("wall_user_id");
		var text = $("#textboxcontent"+post_id).val();
		var wall_size = $("#wall_size").attr("class");
		var private_comment = $("#privatecomment"+post_id).val();
		
		if(text == '')
		{
			$("#error"+post_id).html("<span class='custom_error'>請輸入留言</span>");//you must enter text
		}
		else
		{	
			$.ajax({
				type: "POST",
				url: "/includes/ajax_wall_updater.php",
				data: {reply_text: text, post_id: post_id, wall_user_id: wall_user_id, private_comment:private_comment, wall_size: wall_size},
				cache: false,

				success: function(html)
				{
					$("#postbox"+post_id).append(html);
					
					//find the new ID by finding the last div in the parent and then doing a substr on the class to get the ID
					var newly_posted_class = $("#postbox" + post_id + " > div:last-of-type").attr("class");
					var newly_posted_id = newly_posted_class.substr(23);
					
					//fade in the new post
					$(".postid"+newly_posted_id).hide().fadeIn("slow");
					
					//cleanup
					$("#textboxcontent"+post_id).val('');
					$("#error"+post_id).html('');
				
					//user parent() to get back to textbox and the add comment link
					parent_id = element.parent().parent().parent().parent().parent().parent().attr("id").substr(14);
					$(".addcomment"+parent_id).toggle();
				}
			});
		}
		return false;
	});	
	
	
	//process del comment
	$(".del_comment").live("click", function(){

	var answer = confirm("確定刪除此留言?");//are you sure you want to delete this comment?
	if (answer)
	{
		var element = $(this);
		var post_id = element.attr("id").substr(3);
		$.ajax({
			type: "POST",
			url: "/includes/ajax_wall_updater.php",
			data: {del_id: post_id},
			cache: false,

			success: function(msg)
			{
				if(msg != '')
				{
					var label = msg.substr(0,4);
					var del_id = msg.substr(4);

					if(label == 'prnt')
					{
						$("#postbox"+del_id).slideUp("fast");
						$("#addcommentlink"+del_id).slideUp("fast");
						
						$("#postbox"+del_id).find("div").slideUp();
					}
					else if (label == 'chld')
					{
						$(".postid"+del_id).slideUp("fast");
					}
				}
			}
		});
	}
	return false;});
	
	//process del comment
	$(".del_comment").live("mouseover", function(){
	
	$(this).find("img").attr("src", "/skinning/std_art/comment_del_hover.png");
	//alert("hovered");
	return false;});
	
	//process del comment
	$(".del_comment").live("mouseout", function(){
	
	$(this).find("img").attr("src", "/skinning/std_art/comment_del.png");
	
	return false;});
});