$(function(){
$.followership = {};
$.extend($.followership,{
    follow: function(el, id){
      var data = {};data['url'] = '/followerships/create';
      data['data'] = { user_id: id };
      var p = el.parent(); el.attr('disabled', true);
      $.send('json', data, function(dt, tg){
        if (dt.st){
          el.unbind().addClass('following').hover(function(){
            $(this).addClass('unfollow').click(function(){
              $.followership.unfollow($(this), $(this).attr('id'));
            });
          }, function(){
            $(this).removeClass('unfollow').unbind('click');
          });
        }else{  
          $.loginRequire();
        }
        el.removeAttr('disabled');
      }, null, null);
    },
    unfollow: function(el, id){
      var data = {};data['url'] = '/followerships/destroy';
      data['data'] = { user_id: id };
      var p = el.parent(); el.attr('disabled', true);
      $.send('json', data, function(dt, tg){
        if (dt.st){
          el.unbind().removeClass('following unfollow').addClass('follow').unbind('hover').click(function(){
            $.followership.follow(el, id);
          });
        }else{}
        el.removeAttr('disabled');
      }, null, null);
    }

  });
});

