/*
    name : pagination
    file : jquery.pagination.js
    author : gregory tomlinson
    Dual licensed under the MIT and GPL licenses.
    ///////////////////////////
    ///////////////////////////
    dependencies : jQuery 1.4.2, jquery.pagination.js, jquery.urlParams.js
    ///////////////////////////
    ///////////////////////////
        
        Method: $.urlParamGet('param')

*/

(function($) {
    
    $.fn.pagination = function( options ) {
        // extend the defaults settings
        var el = this, o = $.extend(true, defaults, options),
            pagination, page,
            totalResults,
            perpage;
        
        if(el.length <= 0) return this;
        //page = parseInt(page);
        el.bind('hashList', function(e,data) {
            totalResults = data.total;
            perpage = data.perpage;
            page = data.page;
            pagination = $('<div id="pagination"></div>');
            pagination.appendTo( el );
            
            if( totalResults >= perpage ) {
                pagination.html( drawPagination() );
            }
            pagination.find('a').bind('click',clickEvent);
        });
        
        /*
            1. draw out pagination
            2. attach events
            3. broadcast click events to the world
        
        */
        return this;
        
        function drawPagination() {
            
            // todo, add logic so the page cannot be 0 / doesn't exceed the total results available
            
            var html = '<div class="paginationLinks">';
                    html +=  previousLinkValue( page );
                        html += drawPageNumbers();
                    html += nextLinkValue( page );
                html += '<div class="hr"><hr /></div>';
                html += '</div>';
            
            return html;
        }
        
        function previousLinkValue( page ) {
            var link = "";
            if(page <= 1) link = "<span class='inactivePagination previousPagination'>Previous</span>";
            else if ( page === 2 ) link = '<span class="activePagination previousPagination"><a href="'+ o.baseURL +'" class="previousLinkActive">Previous</a></span>';
            else link = '<span class="activePagination previousPagination"><a href="'+ o.baseURL + 'page/' + (page-1)+'" class="previousLinkActive">Previous</a></span>';
            
            return link;
        }
        
        function nextLinkValue( page ) {
            var link = "";
            if(page > Math.ceil(totalResults/perpage) ) link = "<span class='inactivePagination nextPagination'>Next</span>";
            else link = '<span class="activePagination nextPagination"><a href="'+o.baseURL + 'page/' + (page+1) + '" class="previousLinkActive">Next</a></span>';
            
            return link;
        }
        
        function drawPageNumbers() {
            //console.log(page, perpage, totalResults);
            
            var totalPages = Math.ceil(totalResults / perpage), html = "";
            
            if( totalPages > 6 ) {
                html = drawGreaterThan6Pages( totalPages );
            } else {
                html = drawLessThan6Pages( totalPages );
            }
            
            
            return html;
        }
        
        function drawGreaterThan6Pages(  totalPages ) {
            
            var html = "", active="",
                end = Math.min(page+2, totalPages),
                begin = Math.max( 1, page-2 );
            
            
            // add some numbers onto the display at the beginning
            if(page <= 2 ) end+=2;
            else if(page > 4) {
                html += '<span><a href="'+ o.baseURL + '">1</a></span>';
                html += ' ... ';
            }
            
            // add extra numbers to set
            if(page >= totalPages-2 ) begin-=1;
            
            for(var i=begin; i<=end; i++) {
                active = (page === i) ? "activePaginationLink" : "";
                html += '<span><a class="'+active+'" href="'+o.baseURL + "page/" + i + '">'+i+'</a></span>'
            }
            
            if( page < totalPages-3) {
                html += ' ... ';
                html += '<span><a href="' + o.baseURL + 'page/' + totalPages + '">' + totalPages + '</a></span>';
            }
            
            html += '<span style="display:none;">' + page + " of " + totalPages + '</span>';
            return html;
        
        }
        
        function drawLessThan6Pages( totalPages ) {
            var html = "", active = "", url;
            for(var i=1; i<=totalPages; i++ ) {
                active = (page === i) ? "activePaginationLink" : "";
                url = o.baseURL;
                url += (i===1) ? "" : "page/" + i;
                html += '<span><a class="'+active+'" href="'+ url+ '">'+i+'</a></span>'
            }
            return html;
        }
        
        function clickEvent(e) {
            var q = $.urlParamGet('q')
            if(q) {
                this.href +=  '?q=' + q
            }
        }
    }
    var defaults = {
        url : '',
        baseURL : '/a/search/'
    }, $bod;

})(jQuery);
