/**
 * Simple jQuery pagination plugin.
 * Free to use, copy, modify...
 * 
 * Note: the navigation inserts into an element with id #paginate-navigation.
 * You change next line:
 *      $('#paginate-navigation').html(navigation_html);
 * to something like this:
 *      $(this).parent().append().html('<div id="my-navigation">'+navigation_html+'</div>');
 * to append automatically after paginated elements.    
 *  
 * @author LIA Ant. <lia.ant@live.com>
 */  
(function($) {

    $.fn.paginate = function ( options ) {
        
        // Hash value
        var hashVal = window.location.hash.substr(1);
        
        // Prevent page number being less than 1
        var current_page = (!hashVal || hashVal < 1) ? 1 : parseInt(hashVal);

        // Settings
        var settings = {
            show_per_page  :  5
        }
        
        var methods = {
            /**
             * Renders HTML markup of navigation links
             */                         
            render : function () { 
                // HTML for navigation
                var navigation_html = '';
                
                // Current
                var current_link = 0;
                
                // Show PREV link if we're not on first page
                if (current_page > 1) {
                    navigation_html = '<a class="paginate_previous_link" href="#'+(current_page - 1)+'">&laquo;</a>';
                }
                
                // Get all links
                while(total_pages >= ++current_link){
                    navigation_html += ' <a class="paginate_page_link';
                    if ( (current_link) == current_page ) {
                        navigation_html += ' paginate_current';
                    }
                    navigation_html += '" href="#' + current_link +'">'+ (current_link) +'</a>';
                }
                
                // Do not show NEXT link if we're on the last page
                if (current_page < total_pages) {
                    navigation_html += ' <a class="paginate_next_link" href="#'+(current_page + 1)+'">&raquo;</a>';
                }
                
                return navigation_html;
            },
            
            /**
             * Shows only that of a page entries
             */                         
            showPage : function (pageNum, obj) {
                obj.hide();
                obj.slice(settings.show_per_page * (pageNum - 1), (settings.show_per_page * (pageNum - 1)) + 5).fadeIn(400);
            }
        }
        
        // Merge our settings with user ones
        if ( options ) {
            $.extend(settings, options);
        }

        // Automatic settings
        var total_entries = this.length;
        var total_pages = Math.ceil(total_entries / settings.show_per_page);
        
        // Prevent page number being more than total 
        if ( current_page > total_pages ) {
            current_page = total_pages;
        }
        
        // Render navigation menu
        var navigation_html = methods.render();
        
        // Page object
        var pages = $(this);
        
        // Show start page
        methods.showPage(current_page, pages);
        
        // Live navigation for switching pages
        $('.paginate_page_link, .paginate_next_link, .paginate_previous_link').live('click', function(){
            $('.paginate_page_link').removeClass('paginate_current');
            var pageN = current_page = parseInt($(this).attr('href').substr(1));
            
            $('.paginate_page_link[href=#'+(pageN)+']').addClass('paginate_current');
            $('#paginate-navigation').html(methods.render());
            methods.showPage(pageN, pages);
        });
        
        // Append navigation after entries
        $('#paginate-navigation').html(navigation_html);
    }

})(jQuery)
