$(function() {
    resetAddFriend();
});

$().ajaxComplete(function() {
    resetAddFriend();
});

function resetAddFriend() {
    $("a.addFriend").unbind().click(function() {
        addID = $.query($(this).attr("href")).add;
        
        //append a loader to it
        $(this).html("<img src='/images/busy.gif' style='vertical-align: middle;' />"+$(this).html());
        
        out = $.ajax({
            url: "/friends.php",
            type: "get",
            data: {"add": addID},
            async: false
        }).responseText;
        
        $(this).html(out);
        
        //stop the link from loading
        return false;
    });
}

jQuery.query = function(s) {
     var r = {};
     var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
     q = q.replace(/\&$/, ''); // remove the trailing &
     jQuery.each(q.split('&'), function() {
         var splitted = this.split('=');
         var key = splitted[0];
         var val = splitted[1];
         // convert floats
         if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
         // ignore empty values
         if (typeof val == 'number' || val.length > 0) r[key] = val;
     });
     return r;
}; 


$.fn.reset = function() {
    return this.each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (tag == 'form')
        return $(':input',this).reset();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
};
