/*
 * Copyright (c) 2010 Riccardo Cagnasso
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
*/

jquery_mo4r_handler=function(options, target){
    var defaults={
        after_init:function(){},
        generate_html: function(response){return response;},
        after_insert: function(){},
        url: '',
        get_page: function(options, callback, error_callback){
            return $.ajax({
                type: 'POST',
                datatype: 'html',
                url: options.url,
                data: {page: options.current_page, perpage: options.perpage},
                success: function(response){
                    callback(response);
                },
                error: function(){
                    error_callback();
                }
            });
        },
        mo4r_label: 'visualizza altri',
        mo4r_button: $('<button/>').button(),
        nomore_message: $('<div/>').addClass('nomore').html('Non ci sono altri articoli da visualizzare al momento'),
        perpage: 4,
        current_page: 1
    };
    options=$.extend({}, defaults, options);
    var obj={
        init:function(){
            var that=this;
            var span=$('<span/>');
            target.data('holder', span);
            target.append(span);
            target.append(options.mo4r_button.html(options.mo4r_label).click(function(){
                that.mo4r();
            }));
            options.after_init();
        },
        mo4r: function(){
            var that=this;
            options.get_page(options, function(response){
                html=options.generate_html(response);
                that.append_mo4r(html);
                options.current_page++;
                options.after_insert(); 
            }, function(){
                target.data('holder')
                    .html(options.nomore_message);
                options.mo4r_button.css('display', 'none');
            });
        },
        append_mo4r:function(html){
            new_span=$('<span />');
            target.data('holder')
                .html(html)
                .append(new_span);
            target.data('holder', new_span);
        }
    };
    obj.init();
    return obj;
};

$(function(){
    jQuery.fn.mo4r=function(action, args){
        this.each(function(){
            var target=$(this);
            var mo4r_handler=target.data('mo4r_handler');
            switch(action){
                case 'mo4r':
                    mo4r_handler.mo4r();
                    break;
                default:
                    mo4r_handler=jquery_mo4r_handler(action, target);
                    target.data('mo4r_handler', mo4r_handler);
                    break;
            }
        });
        return this;
    };
});
