function trim(string)
{
    a = string.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

function init()
{
	// more posts
	$('#more_post').click(function() {
		var data = $(this).attr('name');
		var val = data.split('|');
		if($('#more_post img').css('display')=='none')
		{
			$('#more_post img').show();
			$.ajax({
			  type: "POST",
			  url: "wall.php",
			  data: "account="+val[0]+"&sender="+val[1]+"&before="+val[2],
			  success: function(data) {
				if(data.search("wall_post")>0) 
				{
					$('#more_post').remove();
					$('#wall_post_list').append(data);
					FB.XFBML.Host.parseDomElement( document.getElementById("wall_post_list") );
					init();
				}
				else
				{
				  alert('Loading failed, try again later');
				}
				$("#wall_loading").hide();
				$("#wall_content").show();
			  },
			  error: function() {
				alert('Loading failed, try again later');
				$("#wall_loading").hide();
				$("#wall_content").show();
			  }
			});
		}
	})
	
	// load full comments
	$('.loadcomments').click(function() {
		var data = $(this).attr('name');
		var val = data.split("|");
		if(val[2]>8)
		{
		   document.location.href = 'wallpost-'+val[1]+'-'+val[0];
		}
		else
		{
		  if($('#commload_'+val[0]).css('display')=='none')
		  {
			$('#commload_'+val[0]).show();
			$.ajax({
			  type: "POST",
			  url: "wall_full_comment.php",
			  data: "parent="+val[0]+"&sender="+val[1],
			  success: function(data) {
				if(data.search("theComment")>0) 
				{
					$('#more_comments_'+val[0]).hide();
					$('#more_comments_'+val[0]).after(data);
					$('#more_comments_'+val[0]).remove();
					init_post();
					FB.XFBML.Host.parseDomTree();
				}
				else
				{
				  alert('Loading failed, try again later');
				}
				$('#commload_'+val[0]).hide();
			  },
			  error: function() {
				alert('Loading failed, try again later');
				$('#commload_'+val[0]).hide();
			  }
			});
		  }
		}
	});
	
	// adding comment
	$('.add_comment').click(function() {
	  if(sender)
	  {
		var id = $(this).attr('accesskey');
		var form = '<div class="comment" id="comm_'+id+'">';
		
		form += '<div class="avatar" id="av_'+id+'">';
		if(avatar=='no_avatar_small.jpg' || fbphoto==1)
		{
			form += '<fb:profile-pic uid="'+senderfb+'" size="square" width="45" height="45" linked="false" facebook-logo="true" /></fb:profile-pic>';
		}
		else
		{
			form += '<img src="images/'+avatar+'" />';
		}
		form += '</div>';
		
		form += '<div class="detail">';
		form += '<textarea name="comment_'+id+'" id="'+id+'" class="post" style="width:545px; height:60px; overflow:auto;" ></textarea>';
		form += '<br><input name="share_'+id+'" id="share_'+id+'" alt="'+id+'" accesskey="'+account+'|'+sender+'|'+id+'" type="button" value="Comment" class="comment_btn"> <img src="images/loading_small.gif" id="commenting_'+id+'" width="16" height="11" align="absmiddle" style="display:none; margin-left:10px;" />';
		form += '</div><div class="clear"></div></div>';
		
		//alert($('#commbox_'+id).attr("alt"));
		if($('#commbox_'+id).attr('alt')==0) 
		{
			$('#comments_'+id).append(form);
			$('#commbox_'+id).attr('alt','1')
			init_comment()
			init_post();
			FB.XFBML.Host.parseDomElement( document.getElementById("av_"+id) );
		}
		$('#'+id).css('width','545px');
		$('#'+id).css('height','60px');
		if($('#'+id).attr('value')=="Write a comment...") $('#'+id).attr('value','');
		$('#'+id).css('color','#333');
		$('#'+id).focus();
		$('#share_'+id).show();
		$('#av_'+id).show();
	  }
	  else
	  {
		alert("Please sign-in to leave a comment");
	  }
	});
}

function init_comment()
{
	// comment
	$(".comment_btn").click(function() {
		var id = $(this).attr('alt');
		var post = $('#'+id).attr('value');
		var theobj = $(this);
		if(!trim(post)) alert('Write something');
		else
		{
		  var values = $(this).attr('accesskey');
		  var val = values.split('|');
		  
		  if($('#commenting_'+id).css('display')=='none')
		  {
			$('#commenting_'+id).show();
			$('#'+id).attr('disabled','disabled');
			$('#share_'+id).attr('disabled','disabled');
			
			$.ajax({
			  type: "POST",
			  url: "wall_comment.php",
			  data: "account="+val[0]+"&sender="+val[1]+"&parent="+val[2]+"&content="+post,
			  success: function(data) {
				if(data.search("comment")>0) 
				{
					$('#comm_'+id).before(data);
					//FB.XFBML.Host.parseDomTree();
					FB.XFBML.Host.parseDomElement( document.getElementById("thePost_"+val[2]) );
					
					if($('#commnum_'+id))
					{
						var num = $('#commnum_'+id).html()-0;
						$('#commnum_'+id).html(num+1);
					}
					
					var parent=id;
					if($('#morecomm_'+parent).attr('name'))
					{
						var dat = $('#morecomm_'+parent).attr('name').split("|");
						dat[2]++;
						newname = dat.join("|");
						$('#morecomm_'+parent).attr('name', newname);
					}
					
					$('#'+id).attr('value',"Write a comment...");
					$('#'+id).css('height','20px');
					$('#'+id).css('color','#888');
					$('#'+id).css('width','600px');
					$('#av_'+id).hide();
					theobj.hide();
					init_post();
				}
				else
				{
				  alert('Posting failed, try again later');
				}
				$('#commenting_'+id).hide();
				$('#'+id).removeAttr('disabled');
				$('#share_'+id).removeAttr('disabled');
			  },
			  error: function() {
				alert('Posting failed, try again later');
				$('#commenting_'+id).hide();
			  }
			});
		  }
		}
	});
}


function DelPost(id)
{
		//var id = $(this).attr('alt');
		var type = $("#del_"+id).attr('title');
		var parent = $("#del_"+id).attr('accesskey');
		var run = 1;
		var post = 0;
		if(type=='Delete Post')
		{
			if(!confirm('All comments will also be deleted')) run=0;
			post = 1;
		}
		else
		{
			if(!confirm('Delete this comment?')) run=0;
		}
		if(run)
		{
			$('#del_'+id).hide();
			$('#godel_'+id).show();
			if(post)
			{
				$('#thePost_'+id).fadeTo('fast', 0.3);
			}
			else
			{
				$('#theComment_'+id).fadeTo('fast', 0.3);
			}
			$.ajax({
			  type: "POST",
			  url: "wall_delete.php",
			  data: "id="+id+"&post="+post,
			  success: function(data) {
					if(post) 
					{
						$('#thePost_'+id).slideUp();
						$('#thePost_'+id).remove();
					}
					else
					{
						$('#theComment_'+id).slideUp();
						$('#theComment_'+id).remove();
						if($('#morecomm_'+parent).attr('name'))
						{
							$('#commnum_'+parent).html( $('#commnum_'+parent).html()-1 );
							var dat = $('#morecomm_'+parent).attr('name').split("|");
							dat[2]--;
							newname = dat.join("|");
							$('#morecomm_'+parent).attr('name', newname);
						}
					}
			  },
			  error: function() {
				alert('Deleting failed, try again later');
				$('#godel_'+id).hide();
				$('#del_'+id).show();
				if(post)
				{
					$('#thePost_'+id).fadeTo('fast', 1);
				}
				else
				{
					$('#theComment_'+id).fadeTo('fast', 1);
				}
			  }
			});
		}
}


	// delete
	/*
	$(".delete").livequery("click",function() {
		var id = $(this).attr('alt');
		var type = $(this).attr('title');
		var parent = $(this).attr('accesskey');
		var run = 1;
		var post = 0;
		if(type=='Delete Post')
		{
			if(!confirm('All comments will also be deleted')) run=0;
			post = 1;
		}
		else
		{
			if(!confirm('Delete this comment?')) run=0;
		}
		if(run)
		{
			$('#del_'+id).hide();
			$('#godel_'+id).show();
			if(post)
			{
				$('#thePost_'+id).fadeTo('fast', 0.3);
			}
			else
			{
				$('#theComment_'+id).fadeTo('fast', 0.3);
			}
			$.ajax({
			  type: "POST",
			  url: "wall_delete.php",
			  data: "id="+id+"&post="+post,
			  success: function(data) {
					if(post) 
					{
						$('#thePost_'+id).slideUp();
						$('#thePost_'+id).remove();
					}
					else
					{
						$('#theComment_'+id).slideUp();
						$('#theComment_'+id).remove();
						if($('#morecomm_'+parent).attr('name'))
						{
							$('#commnum_'+parent).html( $('#commnum_'+parent).html()-1 );
							var dat = $('#morecomm_'+parent).attr('name').split("|");
							dat[2]--;
							newname = dat.join("|");
							$('#morecomm_'+parent).attr('name', newname);
						}
					}
			  },
			  error: function() {
				alert('Deleting failed, try again later');
				$('#godel_'+id).hide();
				$('#del_'+id).show();
				if(post)
				{
					$('#thePost_'+id).fadeTo('fast', 1);
				}
				else
				{
					$('#theComment_'+id).fadeTo('fast', 1);
				}
			  }
			});
		}
	});
	*/


function init_post()
{
	// click on post field
	$(".post").click(function() {
	  if(sender)
	  {
		$(this).css('height','60px');
		$(this).css('color','#333');
		if($(this).attr('value')=="Drop me a line ..." || $(this).attr('value')=="Write a comment...") $(this).attr('value','');
		$('#share_'+$(this).attr('id')).show();
		if($(this).attr('id')!='status') 
		{
			$('#av_'+$(this).attr('id')).show();
			$('#'+$(this).attr('id')).css('width','545px');
		}
	  }
	  else
	  {
		alert("Please sign-in to write on Wall");
	  }
	});
	
	// post field blur
	$(".post").blur(function() {
		if(!$(this).attr('value'))
		{
			$(this).css('height','18px');
			$(this).css('color','#888');
			if($(this).attr('id')=='status') $(this).attr('value',"Drop me a line ...");
			else $(this).attr('value',"Write a comment...");
			$('#share_'+$(this).attr('id')).hide();
			if($(this).attr('id')!='status') 
			{
				$('#av_'+$(this).attr('id')).hide();
				$('#'+$(this).attr('id')).css('width','600px');
			}
		}
	});
}

$(document).ready(function () {
	
	// share
	$(".share_btn").click(function() {
		var id = $(this).attr('alt');
		var post = $('#'+id).attr('value');
		var theobj = $(this);
		if(!trim(post)) alert('Write something');
		else
		{
		  var values = $(this).attr('accesskey');
		  var val = values.split('|');
		  
		  if($('#posting').css('display')=='none')
		  {
			$('#posting').show();
			$('#status').attr('disabled','disabled');
			$('#share_status').attr('disabled','disabled');
			$.ajax({
			  type: "POST",
			  url: "wall_post.php",
			  data: "account="+val[0]+"&sender="+val[1]+"&parent="+val[2]+"&content="+post,
			  success: function(data) {
				if(data.search("wall_post")>0) 
				{
					if(id=='status')
					{
						$('#wall_post_list').prepend(data);
						FB.XFBML.Host.parseDomTree();
					}
					$('#'+id).attr('value',"Drop me a line ...");
					$('#'+id).css('height','20px');
					theobj.hide();
					init_post();
					init();
				}
				else
				{
				  alert('Posting failed, try again later');
				}
				$('#posting').hide();
				$('#status').removeAttr('disabled');
				$('#share_status').removeAttr('disabled');
			  },
			  error: function() {
				alert('Posting failed, try again later');
				$('#posting').hide();
				$('#status').removeAttr('disabled');
				$('#share_status').removeAttr('disabled');
			  }
			});
		  }
		}
	});
	
	init();
	init_post();
});