<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      eventCookie="@@@eventCookie@@@" dtvPlatform="@@@dtvPlatform@@@">

<head>
    <base href="***feed.getBaseHref()***" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Democracy Channel View</title>

    <link href="resource:css/main.css" rel="stylesheet" type="text/css" />
    <t:include filename="dynamic.js" />
    <t:include filename="settings.js" />
    <script type="text/javascript" src="resource:templates/osxdnd.js" />
    <t:execOnLoad><![CDATA[
#print templateState
global feed, searchTerm, showDownloads, showWatchableItems, showNewItems, allItemsMode, allItems, watchableItems, matchingItems, newItems, itemSort, itemSortWatchable, itemSortDownloading, notDeleted

def _setFeed(kargs):
    global feed, allItemsMode, showDownloads, showNewItems, showWatchableItems, searchTerm

    searchTerm = None

    # The main section
    allItemsMode = False
    
    # Initial states of views in this template
    showDownloads = False
    showNewItems = True
    showWatchableItems = True

    feed = views.feeds.getObjectByID(int(kargs['id']))
    filters.switchNewItemsChannel(feed)
    sorts.switchUnwatchedFirstChannel(feed)

def _updateView ():
    global showNewItems, allItemsMode

    showNewItems = (len(watchableItems) == 0) or len(newItems) > 0 or feed.getURL().startswith('dtv:directoryfeed')

    if len(newItems) == 0:
        allItemsMode = True
        matchingItems.recomputeFilter(newItems)

    feed.updateIcons()

def reInit(*args, **kargs):
    global feed, allItems, showDownloads, showWatchableItems, showNewItems, allItemsMode, watchableItems, matchingItems, newItems, itemSort, itemSortWatchable, itemSortDownloading

    if feed.idExists():
        feed.markAsViewed()

    #print "reinitting %s" % repr(kargs)
    _setFeed(kargs)
    itemSort = feed.itemSort
    itemSortDownloading = feed.itemSortDownloading
    itemSortWatchable = feed.itemSortWatchable
    newItems.sortFunc = feed.itemSort.sort
    downloadingItems.sortFunc = feed.itemSortDownloading.sort
    watchableItems.sortFunc = feed.itemSortWatchable.sort

    allItems.changeIndexValue(indexes.itemsByFeed, int(kargs['id']))
    views.feeds.recomputeFilter(thisFeedView)

    _updateView()

def showDownloadsFilter(x):
    return showDownloads

def showWatchableFilter(x):
    return showWatchableItems

def showNewFilter(x):
    isNew = filters.newItems(x)
    # need to make call newItems() so that the filter remembers if the item
    # was flagged as new or not.
    return showNewItems and (allItemsMode or isNew)

def updateSearchString(newSearch):
    global searchTerm
    if len(newSearch) == 0:
        searchTerm = None
    else:
        searchTerm = newSearch
    allItems.recomputeFilters()

def toggleDownloadsView(handle):
    global showDownloads
    showDownloads = not showDownloads
    allDownloadingItems.recomputeFilter(downloadingItems)
    handle.forceUpdate()

def toggleWatchableView(handle):
    global showWatchableItems
    showWatchableItems = not showWatchableItems
    allWatchableItems.recomputeFilter(watchableItems)
    handle.forceUpdate()

def toggleNewItemsView(handle):
    global showNewItems
    showNewItems = not showNewItems
    matchingItems.recomputeFilter(newItems)
    handle.forceUpdate()

def toggleAllItemsMode(handle):
    global allItemsMode
    allItemsMode = not allItemsMode
    matchingItems.recomputeFilter(newItems)
    handle.forceUpdate()

def setSortBy(by, which, handle):
    if which == 'main':
      feed.itemSort.setSortBy(by)
      matchingItems.recomputeSort(newItems)
    elif which == 'downloading':
      feed.itemSortDownloading.setSortBy(by)
      allDownloadingItems.recomputeSort(downloadingItems)
    else:
      feed.itemSortWatchable.setSortBy(by)
      allWatchableItems.recomputeSort(watchableItems)
    handle.forceUpdate()

_setFeed(kargs)

allItems = views.items.filterWithIndex(indexes.itemsByFeed, feed.getID())
notDeleted = allItems.filter(filters.notDeleted)
matchingItems = notDeleted.filter(lambda x: filters.matchingItems(x, searchTerm))
newItems = matchingItems.filter(showNewFilter, sortFunc=feed.itemSort.sort, resort=True)

allDownloadingItems = matchingItems.filter(filters.downloadingOrPausedItems)
downloadingItems = allDownloadingItems.filter(showDownloadsFilter,sortFunc=feed.itemSortDownloading.sort,resort=True)
allWatchableItems = matchingItems.filter(filters.watchableItems)
watchableItems = allWatchableItems.filter(showWatchableFilter,sortFunc=feed.itemSortWatchable.sort,resort=True)

thisFeedView = views.feeds.filter(lambda x: x is feed)
itemSort = feed.itemSort
itemSortWatchable = feed.itemSortWatchable
itemSortDownloading = feed.itemSortDownloading

def allItemsHaveState(view, state):
    for item in view:
        if item.getState() != state:
            return False
    return True
def allItemsPaused(view):
    return allItemsHaveState(view, 'paused')
def allItemsDownloading(view):
    return allItemsHaveState(view, 'downloading')

_updateView()

isFolder = False
]]>
    </t:execOnLoad>
    <t:execOnUnload><![CDATA[
matchingItems.unlink()
thisFeedView.unlink()
notDeleted.unlink()
allItems.unlink()
if feed.idExists():
    feed.markAsViewed()
]]>
    </t:execOnUnload>

</head>

<body onkeydown="sendKeyToSearchBox(event);">
<div id="feed-settings">
  <div t:updateForView="thisFeedView">
    <t:includeTemplate filename="feed-settings" />
  </div>
</div>
    
<!-- TITLE BAR ************************************************************ -->

<div id="main-titlebar">
        <div t:updateForView="thisFeedView">
          <img id="main-icon" src="***feed.getThumbnail()***" alt=""/>
        </div>
        <div class="main-titlebar-right" t:hideIf="feed.getURL().startswith('dtv:directoryfeed')" t:updateHideOnView="thisFeedView">
            <ul>
                <li t:hideIf="feed.getLink() == ''">
                    <a href="***feed.getLink()***" class="round-button-left black">
                    <div class="round-button-right black">
                    <div class="round-button-content black">
                    <span i18n:translate="">VISIT WEBSITE</span>
                    </div>
                    </div>
                    </a>
                </li>
                <li>
                    <a href="#" onclick="return recommendChannel('@@@feed.getTitle()@@@', '@@@feed.getURL()@@@');" class="round-button-left black">
                    <div class="round-button-right black">
                    <div class="round-button-content black">
                    <span i18n:translate="">SEND TO FRIEND</span>
                    </div>
                    </div>
                    </a>
                </li>
                <li>
                    <a href="#" onclick="return showSettings();" class="round-button-left black">
                    <div class="round-button-right black">
                    <div class="round-button-content black">
                    <span i18n:translate="">SETTINGS</span>
                    </div>
                    </div>
                    </a>
                </li>
            </ul>
        </div>

        <div t:updateForView="thisFeedView">
            <h1 id="main-title" t:replace="feed.getTitle()" />
        </div>

        <div id="search-box-container" t:hideIf="'feed' in localvars and feed.getURL() == 'dtv:search'">
            <input id="search-box" type="search" onfocus="onSearchFocus(this)" onblur="endEditSearch()" value="***_('Find in channel')***"/>
        </div>
        <div t:updateForView="matchingItems">
        <div t:hideIf="searchTerm is None or feed.getURL().startswith('dtv:directoryfeed')">
            <div class="white-button-left save-search-channel">
            <div class="white-button-right">
            <div class="white-button-middle">
                <a href="#" onclick="return eventURL('action:addChannelSearchFeed?term=@@@searchTerm@@@&amp;id=@@@feed.getID()@@@');">Save Search</a>
            </div>
            </div>
            </div>
        </div>
        </div>
</div>
<!-- / TITLE BAR -->

<!-- CHANNEL CONTAINER **************************************************** -->
<div id="main-container">
    <div class="main-container-scraping" t:showIf="feed.isScraped() and feed.isUpdating()" t:updateHideOnView="thisFeedView">
    <div class="scraping-indicator">
      <img align="left" src="resource:images/scraping-indicator-left.gif" />
      <img align="right" src="resource:images/scraping-indicator-right.gif" />
      <div class="scraping-content" i18n:translate="">Looking for videos 
      at this URL</div>
    </div>
    </div>
    <div class="hide-channel-title">
        <t:includeTemplate filename="channel-content" />
    </div>
</div>
<!-- / CHANNEL ITEMS LIST -->

</body>
</html>
