/**
 *  jPagine
 *  v1.0
 *  @author Davide Redaelli
 */
jQuery.fn.jPagine = function(options) {

    var defaults = {
		default_page: 1,
		nb_for_page: 5,
        selector: null, //considers only those children
        zebra: false
	};

    if (this.length > 1) {
        return this.each(function() {
            $(this).jPagine(options);
        });
    }

    this.init = function(options) {
        var opts = jQuery.extend(defaults, options),
            object = jQuery(this),
            nb_total = object.children(opts.selector).size(),
            page = 1,
            j = 0;

       object.children().removeClass('jPagine-element');
       object.children().removeClass('jPagine-element-zebra');

       if(!object.parent().hasClass('jPagine-wrap')){

            object.addClass('jPagine');
            object.wrap('<div class="jPagine-wrap" />');

            $pagination = $('<ul class="jPagine-pagination"></ul>').insertAfter(object);
            $pagination.append('<li><a href="javascript:" rel="1">1</a></li>');

            object.children(opts.selector).each(function(i){
                if(j == opts.nb_for_page){
                    page++;
                    j = 0;
                    $pagination.append('<li><a href="javascript:" rel="'+page+'">'+page+'</a></li>');
                }
                j++;
                $(this).addClass('jPagine-element');
                $(this).addClass('jPagine-page-'+page);
                if(opts.zebra == true && j/2 == Math.round(j/2)){
                    $(this).addClass('jPagine-element-zebra');
                }
            });
            $pagination.children('li').eq(opts.default_page-1).find('a').addClass('jPagine-active');

        }else{

           $pagination=object.next('.jPagine-pagination');
           $pagination.html('<li><a href="javascript:" rel="1" class="jPagine-active">1</a></li>');

           object.children(opts.selector).each(function(i){
                if(j == opts.nb_for_page){
                    page++;
                    j = 0;
                    $pagination.append('<li><a href="javascript:" rel="'+page+'">'+page+'</a></li>');
                }
                j++;

                //remove other jPagine-page class
                 $(this).removeClass(function() {
                    cl=$(this).attr('class');
                    classes=cl.split(' ');
                    for (var i=0;i<classes.length;i++){
                        if (classes[i].indexOf('jPagine-page-')!=-1){
                            //alert(classes[i])
                            return classes[i];
                        }
                    }
                });

                $(this).addClass('jPagine-element');
                $(this).addClass('jPagine-page-'+page);
                if(opts.zebra == true && j/2 == Math.round(j/2)){
                    $(this).addClass('jPagine-element-zebra');
                }
            });

        }

        object.children().hide();

        object.children('.jPagine-element.jPagine-page-'+opts.default_page).show();

        $pagination.children('li').find('a').click(function(){
            var p = $(this).attr('rel');
            object.children().hide();
            object.children('.jPagine-element.jPagine-page-'+p).show();
            $(this).parent().parent().find('a').removeClass('jPagine-active');
            $(this).addClass('jPagine-active');
        });
    }
    //init
    this.init(options);
}
