<%pre>
#include <vdr/plugin.h>
#include <vdr/channels.h>
#include <vdr/epg.h>
#include <vdr/config.h>
#include <vdr/i18n.h>
#include "setup.h"
#include "tools.h"
#include "epg_events.h"

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
type = "now";
string attime;
string fixtime;

</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%request scope="page">
string current_type;
string current_attime;
string current_fixtime;
string current_displaytime;
</%request>

<%include>page_init.eh</%include>
<{
if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");


cSchedulesLock schedulesLock;
const cSchedules* Schedules = cSchedules::Schedules(schedulesLock);

string head;
time_t seektime = 0;
string displaytime;

current_type = type;
current_attime = attime;
current_fixtime = fixtime;

if (type == "now") {
	head = tr("What's running at")+string(" ")+FormatDateTime(tr("%I:%M %p"), time(0));
} else if (type == "next") {
	head = tr("What's on next?");
} else if (type == "at") {
	if (attime != "")
		displaytime = ExpandTimeString(attime);
	else if (fixtime != "")
		displaytime = ExpandTimeString(fixtime);
	current_displaytime = displaytime;

	seektime = GetTimeT(displaytime);
	if (seektime - time(0) + 3600 < 0) // if wanted time is past more then 1h, then use tomorrow
		seektime += SECSINDAY;
	head = tr("What's running at") + string(" ") + FormatDateTime(tr("%I:%M %p"), seektime) +string(" (") +FormatDateTime(tr("%a, %b %d"), seektime) + string(")");
}

}>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR-Live - <$ head $></title>
		<link rel="stylesheet" type="text/css" href="styles.css" />
		<& tooltip.javascript var=("domTT_styleClass") value=("domTTepg") &>
		<& pageelems.ajax_js &>
		<script type="text/javascript"><!--
		function showtime(selection)
		{
			if (selection.options[selection.selectedIndex].value != "")
				window.location.href = "whats_on.html?type=at&fixtime=" + selection.options[selection.selectedIndex].value;
		}
		function showspectime(selection)
		{
			if (selection.value != "")
				window.location.href = "whats_on.html?type=at&attime=" + selection.value;
		}
		//--></script>
	</head>
	<body onload="<& pageelems.infobox_start_update &>">
		<& pageelems.logo &>
		<& menu active=("whats_on") component=("whats_on.whats_on_actions")>
		<div class="inhalt">
<{
	EpgEvents epgEvents;

	ReadLock channelsLock( Channels );
	if (channelsLock) {
		int evntNr = 0;
		for (cChannel *Channel = Channels.First(); Channel && Channel->Number() <= LiveSetup().GetLastChannel(); Channel = Channels.Next(Channel)) {
			if (!Channel->GroupSep()) {
				const cSchedule *Schedule = Schedules->GetSchedule(Channel);
				if (Schedule) {				
					const cEvent *Event = NULL;
					if (type == "now")
						Event = Schedule->GetPresentEvent();
					else if (type == "next")
						Event = Schedule->GetFollowingEvent();
					else if (type == "at")
						Event = Schedule->GetEventAround(seektime);
					
						if (Event) {
							string evntId("eventId_");
							evntId += lexical_cast<std::string, int>(++evntNr);
							EpgEventPtr epgEvent(new EpgEvent(evntId, Event, Channel->Name()));
							epgEvents.push_back(epgEvent);
							tChannelID channel_id(Channel->GetChannelID());
							tEventID event = Event->EventID();

							bool truncated = false;
							string description();
}>
			<div class="event">
				<div class="station">
					<div><div><div><a href="schedule.html?channel=<$ Channel->Number() $>" <& tooltip.hint text=(tr("View the schedule of this channel")) &>><$ (epgEvent->Caption()) $></a></div></div></div>
				</div>
				<div class="content">
					<div class="tools">
						<& pageelems.event_timer channelid=(channel_id) eventid=(event) &>
%							if (type == "now") {
						<& pageelems.ajax_action_href action="switch_channel" tip=(tr("Switch to this channel.")) param=(channel_id) image="zap.png" alt="" &>
%							}
%						if (LiveSetup().HaveEPGSearch()) {
						<a href="searchresults.html?searchplain=<$ StringEscapeAndBreak(epgEvent->Title()) $>"><img src="/search.png" border="0" alt="" <& tooltip.hint text=(tr("Search for repeats.")) &>></img></a>
						<a href="http://akas.imdb.com/Tsearch?title=<$ StringUrlEncode(epgEvent->Title()) $>"><img src="imdb.png" border="0" alt="" <& tooltip.hint text=(tr("Find more at the Internet Movie Database.")) &>></img></a>
%						}
					</div>
				 	<div>
						<div class="info"><$ (epgEvent->StartTime(tr("%I:%M %p"))) $> - <$ (epgEvent->EndTime(tr("%I:%M %p"))) $></div>
%							if (epgEvent->Elapsed() >= 0) {
						<div class="progress"><div><& pageelems.progressbar progress=(epgEvent->Elapsed()) &></div></div>
%							}
						<div class="title"><$ (epgEvent->Title()) $></div>
						<div class="short"><$ (epgEvent->ShortDescr()) $></div>
						<div class="description"><$ (StringWordTruncate(epgEvent->LongDescr(), 150, truncated)) $></div>
						<div class="more"<& tooltip.hint text=(StringEscapeAndBreak(StringWordTruncate(epgEvent->LongDescr(), 300, truncated)) + string("<br />") + string(tr("Click to view details."))) &><& tooltip.display domId=(epgEvent->Id()) &>><$ (string(tr("more")) + string(" ...")) $></div>
				 	</div>
				</div>
			</div>
<{
					}
				}
			}
		}
	}
}>
		</div>
		<div class="epg_data" style="display: none;">
<{
	// create hidden div for the tooltip hints.
	for (vector<EpgEventPtr>::iterator i = epgEvents.begin(); i != epgEvents.end(); ++i) {
		EpgEventPtr epg = *i;
}>
		<& pageelems.epg_tt_box boxId=(epg->Id()) caption=(epg->Caption()) time=(epg->StartTime(tr("%I:%M %p")) + string(" - ") + epg->EndTime(tr("%I:%M %p"))) title=(epg->Title()) short_descr=(epg->ShortDescr()) long_descr=(epg->LongDescr()) elapsed=(epg->Elapsed()) &>
<{
	}
}>
		</div>

	</body>
</html>
<%include>page_exit.eh</%include>

<%def whats_on_actions>
<a class="<? current_type == "now" ? "active" ?>" href="whats_on.html?type=now" id="nowhref"><$ tr("Now") $></a>
<span class="sep">|</span>
<a class="<? current_type == "next" ? "active" ?>" href="whats_on.html?type=next" id="nexthref"><$ tr("Next") $></a>
<span class="sep">|</span>
<span><$ tr("What's on") $></span>
<select name="userdeftimes" size="1" id="userdeftimes" onchange="showtime(this)">
	<option value="0"/>
<{
	vector< string > parts = StringSplit( LiveSetup().GetTimes(), ';' );
	vector< string >::const_iterator part = parts.begin();
	for ( int i = 0; part != parts.end(); ++i, ++part ) {
}>
	<option <{ reply.out() << ( (current_fixtime != "" && *part == current_displaytime) ? "selected=\"selected\"" : "" ); }> value="<$ *part $>"> <$ tr("at") + string(" ") + *part $></option>
<{
	}
}>
</select>
<span class="sep">|</span>
<span><$ tr("at") $></span>
<input type="text" size="6" name="spectime" id="spectime" value = "<? current_attime != "" ? current_displaytime ?>" onchange="showspectime(this)"/></td>
</%def>