(function($) {
  $.fn.mrMore = function(options){

    var defaultOptions = {
      "errorMessage": ""
    };
    
    var local = {

      "options": $.extend(defaultOptions, options),

      "onSuccess": function(data, textStatus, jqXHR){

        // remove li.more
        $('li.more', local.options.container).remove();

        // inject html
        local.options.container.append(data);
      },

      "onError": function(jqXHR, textStatus, errorThrown){

        // hide loader
        $('div.loader', $(this.context).parent()).hide();

        // show button
        $(this.context).show();

        // display error
        if (local.options.errorMessage != '')
        {
          $.notice.display(local.options.errorMessage, 'error');
        }
      }
    };

    return this.each(function(index, element){

      // bind click
      jQuery(element).click(function(event){
        event.preventDefault();

        // show loader
        $('div.loader', jQuery(this).parent()).show();
        
        // hide button
        $(this).hide();

        // load
        $.ajax({
          "context":  $(element),
          "url":      $(element).attr('href'),
          "dataType": "html",
          "success":  local.onSuccess,
          "error":    local.onError
        });

      });
    });
  }
})(jQuery);

