/* jquery.goatstone.pager.js
 */

$.widget("ui.pager", {
    options: {
        currentPage:1,
        totalPages:13,
        startPage:1,
        pagerWidth:7  
    },
    _create: function() {
        $this = this;
    },
    generateMenu: function(config){  
        $this = this;
        if(config){
            this.options.totalPages = config.totalPages;
            this.options.pagerWidth = config.pagerWidth;    
        }

        $this.element.find( 'div').remove();
        var j = Math.min($this.options.startPage-1 + $this.options.pagerWidth, $this.options.totalPages );
        
        $this.element.append($('<div data-page="prev"><</div>')) 
        for (var i = $this.options.startPage; i <= j; i++) {
            var elem = "";
            if(i <= $this.options.totalPages ){
                elem = $('<div data-page="'+i+'">')
                elem.text(i)
            }
            $this.element.append( elem)
        }        
        $this.element.append($('<div data-page="next">></div>'));
        
        $this.element.find('div').click(function( e ){
            var pageData =  $(this).data('page');
            
            if( typeof pageData === 'undefined'){
                return;
            }
            if( pageData === 'next'){
                $this.options.currentPage++;
                if($this.options.currentPage >= $this.options.startPage + $this.options.pagerWidth){
                    $this.options.startPage = $this.options.currentPage;
                    $this.generateMenu( );
                }
            }else if( pageData === 'prev'){
                $this.options.currentPage--;
                if($this.options.currentPage < $this.options.startPage){
                    $this.options.startPage = $this.options.currentPage - $this.options.pagerWidth +1
                    $this.generateMenu( );
                }
            }
            else{
                $this.options.currentPage =  pageData;
            }
            $this.element.trigger('setPage', $this.options.currentPage);
            $this.setPager();
        }).addClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only");
        this.setPager()
    },
    // setPagerStyle
    setPager: function(){
        $this = this;
        $this.element.find('div').removeClass('current')
        .css({
            'visibility':'visible'
        } );
        // previous arrow
        if($this.options.currentPage===1){
            $this.element.find('div:eq(0)').css({
                'visibility':'hidden'
            } );
        }
        $($this.element.find('div[data-page='+$this.options.currentPage+']')).addClass(  'current');
        // next arrow
        if( $this.options.currentPage  === $this.options.totalPages ){
            $this.element.find('div').last().css({
                'visibility':'hidden'
            } );
        }
    },
    getStartIndex: function(){
        var currIndex = 1;
        currIndex = (( $this.options.currentPage-1) * 3) + 1;
        return currIndex;         
    },
    destroy: function() {
        $.Widget.prototype.destroy.apply(this, arguments); // default destroy
    }
});

