$j( function() {
    
    var total_pages = $j('#pages div').length;
    
    if(total_pages > 1) {
        
        $j('#pages').after('<div id="page_num"></div><a href="#" id="page_next" class="page_nav" title="Next page">&raquo;</a><a href="#" id="page_prev" class="page_nav" title="Previous page">&laquo;</a>');
        
    }
    
    $j('#pages').cycle({
        fx: 'scrollHorz',
        next: '#page_next',
        prev: '#page_prev',
        timeout: 0,
        before: function() {
            
            var current_page = parseInt($j('#pages div').index(this))+1;

            $j('#page_num').html(current_page +'/'+ total_pages);                    
            
        },
        after: function(curr, next, opts) {
            
            var current_page = opts.currSlide;
            
            $j('#page_prev')[current_page == 0 ? 'hide' : 'show']();
            $j('#page_next')[current_page == opts.slideCount - 1 ? 'hide' : 'show']();
            
        }
    });
                
});