<!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>Miro 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, showDownloads, showWatchableItems, showNewItems, allItemsMode, allItems, watchableItems, matchingItems, newItems, itemSort, itemSortWatchable, itemSortDownloading, notDeleted

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

    # 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 and feed.getAutoDownloadMode() == 'off') or feed.getURL().startswith('dtv:directoryfeed')

    if len(newItems) == 0 or not showNewItems:
        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 feed
    if len(newSearch) == 0:
        feed.setInlineSearchTerm(None)
    else:
        feed.setInlineSearchTerm(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, feed.inlineSearchTerm))
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">
            <div>
                <img id="main-icon" src="***feed.getThumbnail()***" alt=""/>
	    </div>
            <div class="main-titlebar-right" t:hideIf="feed.getURL().startswith('dtv:directoryfeed')">
                <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>

        <div class="main-titlebar-left">
            <div t:updateForView="thisFeedView">
            <h1 id="main-title" t:replace="feed.getTitle()" />
            <div id="auto-download">
                <div t:hideIf="feed.getURL().startswith('dtv:directoryfeed')">
                    <div class="auto-download">
                        <span id="auto-label" i18n:translate="">AUTO DOWNLOAD</span>
                        <div class="select-box-left" onclick="showSelectBoxMenu('auto-download-menu');">
                        <div class="select-box-right">
                        <div class="select-box-mid" t:hideIf="feed is None">
                            <span t:replace="feed.getAutoDownloadMode()" />
                        </div>
                        </div>
                        </div>
                        
                        <br clear="all" />
                        <ul id="auto-download-menu" class="select-box-menu" t:hideIf="feed is None">
                            <li onclick="return eventURL('action:setAutoDownloadMode?mode=all&amp;feed=@@@feed.getID()@@@');" ><a href="#"><strong>ALL</strong> - Get all videos</a></li>
                            <li onclick="return eventURL('action:setAutoDownloadMode?mode=new&amp;feed=@@@feed.getID()@@@');" ><a href="#"><strong>NEW</strong> - Get only new videos</a></li>
                            <li onclick="return eventURL('action:setAutoDownloadMode?mode=off&amp;feed=@@@feed.getID()@@@');" ><a href="#"><strong>OFF</strong> - Don't auto-download videos</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div id="search-box-container" t:hideIf="'feed' in localvars and feed.getURL() == 'dtv:search'">
                <div t:showIf="feed.inlineSearchTerm is None">
                    <input id="search-box" type="search" onfocus="onSearchFocus(this)" onblur="endEditSearch()" value="***_('Find')***" />
                </div>
                <div t:showIf="feed.inlineSearchTerm is not None">
                    <input id="search-box" type="search" onfocus="onSearchFocus(this)" onblur="endEditSearch()" value="***feed.inlineSearchTerm***" searching="1"/>
                </div>
            </div>
            </div>
            <div t:updateForView="matchingItems">
            <div t:hideIf="feed.inlineSearchTerm 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?id=@@@feed.getID()@@@');">Save Search</a>
                </div>
                </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>
