class Main:
    def __init__( self, params ):
        self.client = NineMSNVideo()
        handle = int(sys.argv[1])
        section = params['id'][0]
        page = 1
        if 'page' in params:
          page = int(params['page'][0])
        
        def get_play_url(url):
          url_base = sys.argv[0]
          url_params = {'action': 'play', 'page_url': url}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        def get_page_url(page):
          url_base = sys.argv[0]
          url_params = {'action': 'section', 'id': section, 'page': page}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        for video in self.client.get_videos_for_section(section, page):
          li = xbmcgui.ListItem("{0} - {1}".format(video.show, video.name), thumbnailImage=video.image)
          li.setProperty('IsPlayable', 'true')
          # Set the image if we have one
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_play_url(video.url))

        # And tack on the "Next page" option all the time; should really check if there *is* a next page first
        # Instead, let's just clamp it to 10 pages
        if section != 'mostpopular' and page != 10:
          li = xbmcgui.ListItem("Next Page")
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_page_url(page+1), isFolder=True)
        
        xbmcplugin.addSortMethod( handle=handle, sortMethod=xbmcplugin.SORT_METHOD_NONE )
        xbmcplugin.setContent( handle=handle, content='tvshows' )
        xbmcplugin.endOfDirectory( handle=handle, succeeded=1 )