(function($){
    /**
     * Propiedades
     */
    var settings = {
        loading_text                : 'Cargando usuarios...',
        loading_class               : 'loading',
        limit                       : 10,
        offset                      : 0,
        tipo                        : 0,
        mes                         : 0,
        url                         : '',
        button_class                : 'morecontent',
        list_class                  : 'list',
        query						: ''
    };
    /**
     * Metodos
     */
    var methods = {
        init : function( options ) { 
            return this.each(function(){                
                options = $.extend(true, {}, settings, options);
                methods.save_state.call(this, options);                
                
                if(options.url == ''){
                    $.error( 'Url debe ser definido' );
                }  
                //Elemento boton
                options.boton       = $(this).find('ul.' + options.list_class + ' li .'+ options.button_class);
                //Texto original boton
                options.reset_text  = options.boton.text(); 
                
                //Vinculamos el boton a la carga de resultados
                options.boton.live('click', $.proxy(function(){   
                    options = methods.load_state.call(this);  
                    var offset  = options.offset + options.limit;
                    methods.update.call(this, {offset:offset});
                }, this) );
                //Se accede a la info del listado y se actualiza
                methods.update.call(this, options);
            });            
       
        },       
        load_state : function() {
            return $(this).data('ranking');
        },
        save_state : function(options) {
            $(this).data('ranking', options);
        },        
        loading : function() {
            options = methods.load_state.call(this);  
            options.boton.addClass(options.loading_class).text(options.loading_text);
        },
        unloading : function (){
            options = methods.load_state.call(this);  
            options.boton.removeClass(options.loading_class).text(options.reset_text);
        },
        populate : function(response) {
            options = methods.load_state.call(this);
            $.each(response, $.proxy(function(i, item){
                if(item.premio != false){
                    var datos = '<li><span class="name">'+item.nombre+'</span><span class="puntuacion"><span class="num">'+item.puntuacion+'</span><span class="p">Puntos</span></span><span class="premio"><img src="assets/img/premios/'+item.premio.imagen+'" alt="'+item.premio.nombre+'" alt="'+item.premio.nombre+'" /><span class="premioname"><span>Premio</span><strong>'+item.premio.nombre+'</strong></span></span></li>';			
                } else {
                    var datos = '<li><span class="name">'+item.nombre+'</span><span class="puntuacion"><span class="num">'+item.puntuacion+'</span><span class="p">Puntos</span></span></li>';						
                }
                $(datos).insertBefore($(this).find('ul.' + options.list_class + ' li:last-child'));                
            }, this));           	
            
            if(response.length < options.limit || response.length == false){
            	$(this).find('ul.' + options.list_class + ' li:last-child').fadeOut();                
        	} else {
            	$(this).find('ul.' + options.list_class + ' li:last-child').fadeIn();
            }
            
        },
        clear: function(){
            options = methods.load_state.call(this);
            $(this).find('ul.' + options.list_class + ' li:lt(' + ($(this).find('ul.' + options.list_class + ' li').length - 1) + ')').remove();
        },
        update : function( options ) {
            //Se modifican las propiedades
            opt         = methods.load_state.call(this);            
            options     = $.extend(true, {}, opt, options);
            methods.save_state.call(this, options);
            
            if(options.offset == 0){
                methods.clear.call(this);
            }
            $.ajax({
                beforeSend: $.proxy(methods.loading, this),
                url: options.url,
                type: "POST",
                dataType: 'json',
                data: 'limit=' + options.limit + '&offset=' + options.offset + '&tipo=' + options.tipo + '&mes=' + options.mes + '&query=' + options.query ,
                success: $.proxy(function(response){
                    methods.unloading.call(this);
                    methods.populate.call(this, response);                    
                }, this)
            });
        }
    };
    /**
     * LLamada de metodos
     */
    $.fn.ranking = function( method ) {    
        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        }
        else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        }
        else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.ranking' );
        }
  
    };
 
})(jQuery);
