(function($) {

$.fn.contactToggle = function(options) {
    var options = $.extend({
         messages:{
            on: gettext('Remove from network'),
            off: gettext('Add to network')
        },
        urls: { 
            on:'/you/contacts/remove/#$1/', 
            off: '/you/contacts/add/#$1/' 
        }
    }, options);
    
    
    return this.each(function() {
        var state = !$(this).hasClass('nw-add'),
            href = $(this).attr('href'),
            starid = href.replace(/[^0-9]+/g,'');
            
        
        $(this).click(function() {
            $.post(
                (state ? options.urls.on : options.urls.off).replace(/#\$1/g,starid), {},
                function(d) {
                    if (d.success) {
                        var a = $('a[href="'+href+'"]'), container = $('span',a);
                        state = !state;
                        message_engine.alert('', d.message);
                        container.html(state ? options.messages.on : options.messages.off);
                        if (state) {
                            a.addClass('nw-add');
                        } else {
                            a.removeClass('nw-add');
                        }
                    }
                }, "json" );
            return false;
        });
    });
}})(jQuery);

