friend =  {	
	init: function() {

	},
	
	// friend acceptance methods
	friendAccept: function(friendID,trigger) {
		trigger.onclick = function() {
			return false;
		}
		util.setSaving();
		new Ajax.Request('/friends/ajax/friend_accept/', {
			method: 'post',
			parameters:  {
				friendID: friendID
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	friendDecline: function(friendID) {
		util.setSaving();
		new Ajax.Request('/friends/ajax/friend_decline/', {
			method: 'post',
			parameters:  {
				friendID: friendID
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	friendDelete: function(friendID,friendName,obj) {
		if (!confirm('Are you sure you permanantly remove' + friendName + ' from your friends?')){ return; }
		new Effect.Fade(obj.parentNode,{duration: 1});
		var removeTimer = window.setTimeout(function(){
			obj.parentNode.remove();			
			friend.resetFriends();
		},1000);
		new Ajax.Request('/friends/ajax/friend_delete/', {
			method: 'post',
			parameters: {
				friendID: friendID
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	
	// friend page management methods
	manageFriends: function() {
		if ($$('.delete-friend').length != 0) {return;}
		$('friend-list').select('a').each(function(linkAt) {
			linkAt.href = '#';
			linkAt.onclick = function() {
				return false;
			}
		});
		$('friend-list').select('li').each(function(itemAt) {
			var friendName = itemAt.select('span')[0].innerHTML;
			var deleteLink = new Element('a',{
				href: '#',
				onclick: 'friend.friendDelete(\''+ itemAt.id +'\',\''+ friendName +'\',this)',
				className: 'delete-friend'
			}).update('delete');
			itemAt.appendChild(deleteLink);
		});
		Sortable.create('friend-list',{
			ghosting:true,
			constraint:false,
			hoverclass: 'hover',
			overlap: 'horizontal',
			onUpdate: function() {
				friend.resetFriends();
				$$('button.save-button')[0].addClassName('show');
			}
		});
	},
	resetFriends: function() {
		$('friend-list').select('li').each(function(itemAt,index) {
			if (index+1 <= 6) {
				itemAt.addClassName('top');
			} else {
				itemAt.removeClassName('top');
			}
		});
	},
	saveOrder: function() {
		var tempMssg = {
			"header": "Saving Friends sort order",
			"bodyContent": "<div class=\"loading\"><p>Saving</p><div>"
		}
		util.talkbox($H(tempMssg).toJSON());
		var listValues = [];
		$('friend-list').select('li').each(function(itemAt){
			listValues.push(itemAt.id)
		});
		new Ajax.Request('/friends/ajax/save_order/', {
			method: 'post',
			parameters: {
				friendList: listValues.toString()
			},
			onSuccess: function(t) {
				var reply = $H(t.responseText.evalJSON()).toObject();
				$('talkHeader').update(reply.header);
				$('talkTarget').update(reply.bodyContent);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
		listValues.toString();
	}
	
}

//document.observe('dom:loaded',friend.init);