var FriendStatus = 
{
    view: null,
    init: function()
    {
        this.observe();
    },
    
    observe: function(parent_el)
    {
        var elements;
        var selector = 'a.friend_status_update';
        if (typeof(parent_el) == 'undefined')
            elements = $$(selector);
        else
            elements = parent_el.select(selector)
            
        elements.invoke('observe', 'click', this.update.bind(this));
    },
    
    update: function(e)
    {
        var el = Event.element(e);
        var a = ('A' === el.nodeName) ? el : el.up('a');

        var container = a.up('li.friend');
        if (!container)
            container = a.up('div.user_profile');

        var friend_status_p = container.down('p.friend_status');
        var friend_status_icon = friend_status_p.down('span.silk_icon');
        var old_friend_status, action;
        if (typeof(friend_status_icon) == 'undefined')
            old_friend_status = 'none';
        else if (friend_status_icon.hasClassName('friend'))
            old_friend_status = 'friend';
        else if (friend_status_icon.hasClassName('mutual_friend'))
            old_friend_status = 'mutual_friend';
        else if (friend_status_icon.hasClassName('fan'))
            old_friend_status = 'fan';
        action = a.hasClassName('add') ? 'add' : 'remove';   
            
        var options = 
        {
            method: 'get',
            onSuccess: function(t) 
            {
                var response = t.responseJSON;
                if ('success' == response.status)
                {
                    if ('add' === action)
                    {                       
                        this.incrementNumFriends('friends');
                        if ('fan' === old_friend_status)
                            this.incrementNumFriends('mutual_friends');

                        a.href = a.href.replace('ajax-add-friend', 'ajax-remove-friend')                          
                        a.removeClassName('add').addClassName('remove').update('<span class="silk_icon delete"></span> Remove as friend');
                        
                        if ('fan' === old_friend_status)
                        {
                            friend_status_p.update('<span class="silk_icon mutual_friend"></span> Mutual friend');
                        }
                        else
                        {
                            friend_status_p.update('<span class="silk_icon friend"></span> Friend');
                        }
                    }
                    else
                    {                       
                        this.decrementNumFriends('friends');
                        if ('mutual_friend' === old_friend_status)
                            this.decrementNumFriends('mutual_friends');

                        a.href = a.href.replace('ajax-remove-friend', 'ajax-add-friend')
                                
                        if ('friends' === this.view || 'mutual_friends' === this.view)
                        {
                            container.remove();

                            if ($$('ul.friends-list li').length == 0)
                            {
                                var message;
                                switch (this.view)
                                {
                                    case null:
                                    case 'friends':
                                        message = 'You have not added any friends yet.';
                                        break;
                        
                                    case 'mutual_friends':
                                        message = 'You do not have any mutual friends.';
                                        break;
                                }
                                
                                $$('ul.friends-list')[0].update('<p>' + message + '</p>');
                            }
                        }
                        else
                        {
                            a.removeClassName('remove').addClassName('add').update('<span class="silk_icon add"></span>  Add friend');
                            if ('mutual_friend' === old_friend_status)
                            {
                                friend_status_p.update('<span class="silk_icon fan"></span> Fan');
                            }
                            else
                            {
                                friend_status_p.update();
                            }
                        }
                    }
                }
                else
                {
                    alert(response.status_message);
                }
            }.bind(this)
        };

        new Ajax.Request(a.href, options);
        Event.stop(e);
    },
       
    incrementNumFriends: function(friend_type)
    {
       this.updateNumFriends(1, friend_type);
    },
    
    decrementNumFriends: function(friend_type)
    {
        this.updateNumFriends(-1, friend_type);
    },
    
    updateNumFriends: function(adjustment, friend_type)
    {           
        $$('span.num_' + friend_type).each(function (el)
        {
            var num_friends = Number(el.innerHTML);
            el.update(num_friends + adjustment);
        });
    }
}
