# Copyright (C) 2008  Red Hat, Inc. All rights reserved.
#
# This copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the GNU
# General Public License v.2.  This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.  You should have
# received a copy of the GNU General Public License along with this program; if
# not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks that are
# incorporated in the source code or documentation are not subject to the GNU
# General Public License and may only be used or replicated with the express
# permission of Red Hat, Inc.
#
# Author(s): Luke Macken <lmacken@redhat.com>

from tg import url
from tw.api import Widget, JSLink, CSSLink, js_function, js_callback
from tw.jquery import jquery_js, jQuery

myfedora_js = JSLink(link='/javascript/myfedora.js')
myfedora_ui_js = JSLink(link='/javascript/myfedora.ui.js')
myfedora_extensions_js = JSLink(link='/javascript/myfedora.extensions.js')

fedora_css = CSSLink(link='https://fedoraproject.org/static/css/fedora.css')
myfedora_appchrome_css = CSSLink(link='/css/application-chrome.css')
myfedora_branding_css = CSSLink(link='/css/myfedora-branding.css')

class GlobalResourceInjectionWidget(Widget):
    javascript=[myfedora_js]
    css=[fedora_css,
         myfedora_appchrome_css, 
         myfedora_branding_css]
    
class PagerWidget(Widget):
    template = "genshi:myfedora.widgets.templates.pager"
    params = ['page', 'last_page', 'show', 'parent_dom_id']
    show = 10
    page = 1
    
    def update_params(self, d):
        super(PagerWidget, self).update_params(d)
        
        page = int(d['page'])
        last_page = int(d['last_page'])
        
        # how many of the main set do we show
        show = int(d['show'])
        
        parent_id = d['parent_dom_id']
        
        front_set = []
        back_set = []
        main_set = []
        
        fmax_block_num = float(last_page) / show
        max_block_num = int(fmax_block_num)
        if (fmax_block_num - max_block_num) > 0:
            max_block_num += 1
        
        # figure out main set
        block_num = page / show
        print page, '/', show, '=', block_num
        start = block_num * show + 1
        last_in_set = start + show
        if last_in_set >= last_page:
            start = last_page - show + 1
            last_in_set = last_page + 1
            block_num = max_block_num
            
        main_set = range(start, last_in_set)
        
        # do we need a front set
        if block_num > 0:
            front_set = [1]
        
        # do we need a back set
        if block_num < max_block_num:
            back_set = [last_page]
            
        prev_page = None
        if page > 1:
            prev_page = page - 1
            
        next_page = None
        if page < last_page:
            next_page = page + 1
            
        d.update({'front_set': front_set,
                  'back_set': back_set,
                  'main_set': main_set,
                  'prev_page': prev_page,
                  'next_page': next_page,
                  'parent_id': parent_id,
                  'page': page,
                  'last_page': page,
                  'show': show})
        
@@ -1960,32 +1960,80 @@
  int(d%5B'page'%5D)%0A
+        if page %3C 1:%0A            page = 1%0A     %0A
         last_pag
@@ -2048,32 +2048,32 @@
 d%5B'last_page'%5D)%0A
-
         %0A       
@@ -2282,17 +2282,16 @@
         
-f
 max_bloc
@@ -2302,14 +2302,8 @@
 m = 
-float(
 last
@@ -2311,17 +2311,16 @@
 page
-)
  / show%0A
     
@@ -2319,132 +2319,8 @@
 how%0A
-        max_block_num = int(fmax_block_num)%0A        if (fmax_block_num - max_block_num) %3E 0:%0A            max_block_num += 1%0A
     
@@ -2398,45 +2398,8 @@
     
-print page, '/', show, '=', block_num
 %0A   
@@ -2496,32 +2496,32 @@
 t %3E= last_page:%0A
-
             star
@@ -2537,32 +2537,86 @@
 page - show + 1%0A
+            if start %3C 1:%0A                start = 1;%0A%0A
             last

fix pager