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

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
	int channel = -1;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%request scope="page">
	cChannel* Channel;
</%request>
<%include>page_init.eh</%include>
<{
if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");
}>
<%cpp>
	pageTitle = tr("Schedule");

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

	ReadLock channelsLock( Channels );
	if ( !channelsLock )
		throw HtmlError( tr("Couldn't aquire access to channels, please try again later.") );

	// cChannel* Channel; (see %request above)
	if ( channel > 0 )
		Channel = Channels.GetByNumber( channel );
	else
		Channel = Channels.Get( Channels.GetNextNormal( -1 ) );

	if ( Channel == 0 )
		throw HtmlError( tr("Couldn't find channel or no channels available. Maybe you mistyped your request?") );

	cSchedule const* Schedule = schedules->GetSchedule( Channel );
	if ( Schedule == 0 )
		throw HtmlError( tr("No schedules available for this channel.") );
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR Live - <$ pageTitle $></title>
		<link rel="stylesheet" type="text/css" href="styles.css" />
		<& tooltip.javascript var=("domTT_styleClass") value=("domTTepg") &>
		<& pageelems.ajax_js &>
	</head>
	<body onload="<& pageelems.infobox_start_update &>">
		<& pageelems.logo &>
		<& menu active=("schedule") component=("schedule.channel_selection")>
		<div class="inhalt">
			<table class="schedule" cellspacing="0" cellpadding="0">
<{
				EpgEvents epgEvents;

				bool active_line = false;
				string current_day = "";
				const cEvent* PresentEvent = Schedule->GetPresentEvent();
				time_t now = time(NULL) - ::Setup.EPGLinger * 60;
				tChannelID channel_id(Channel->GetChannelID());
				int evntNr = 0;
				for (const cEvent *Event = Schedule->Events()->First(); Event; Event = Schedule->Events()->Next(Event)) {
					if (Event->EndTime() <= now && Event != PresentEvent)
						continue;

					string evntId("eventId_");
					evntId += lexical_cast<std::string, int>(++evntNr);
					EpgEventPtr epgEvent(new EpgEvent(evntId, Event, Channel->Name()));
					epgEvents.push_back(epgEvent);

					//active_line = !active_line;
					string title(Event->Title() ? Event->Title() : "");
					string short_description(Event->ShortText() ? Event->ShortText() : "");
					string description(Event->Description() ? Event->Description() : "");
					string start(Event->StartTime() ? FormatDateTime(tr("%I:%M %p"), Event->StartTime()) : "");
					string end(Event->EndTime() ? FormatDateTime(tr("%I:%M %p"), Event->EndTime()) : "");
					string day(Event->StartTime() ? FormatDateTime(tr("%A, %b %d %Y"), Event->StartTime()) : "");
					tEventID event = Event->EventID();
					ostringstream os;
      					os << Event->EventID();
					string strEventID = os.str();
					bool truncated = false;
					if (current_day != day) {
						if (current_day != "") {
}>
						<tr>
							<td colspan="5" style="border-top: 1px solid black; border-bottom: none; background-image: none">&nbsp;</td></tr>
						</tr>
<{
						}
}>
						<tr>
							<td class="day" colspan="5"><div class="boxheader"><div><div><$ day $></div></div></div></td>
						</tr>
%					current_day = day;
%					}
					<tr class="<? active_line ? "active" ?>">
						<td style="border-left: 1px solid black"><& pageelems.event_timer channelid=(channel_id) eventid=(event) &></td>
						<td><$ start $> - <$ end $></td>
						<td>
%							if(Event == PresentEvent) {
							<font color="#0000FF">
% 							}
							<div class="more"<& tooltip.hint text=(StringEscapeAndBreak(StringWordTruncate(description, 300, truncated)) + string("<br />") + string(tr("Click to view details."))) &><& tooltip.display domId=(epgEvent->Id()) &>><strong><$ title $></strong></div><$ short_description $><br />
%							if(Event == PresentEvent) {
							</font>
%							}
						</td>
						<td style="border-right: 1px solid black">&nbsp;</td>
					</tr>
%				}
%				if (Schedule->Events()->Count() > 0) {
						<tr>
							<td colspan="4" style="border-top: 1px solid black; background-image: none; border-bottom: none">&nbsp;</td>
						</tr>
%				}
			</table>
		</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 channel_selection>
<form name="channels" id="channels"><span><$ tr("Show schedule of channel") $>: </span><& channels_widget name=("channel") selected=(Channel ? *Channel->GetChannelID().ToString() : "") onchange=("document.forms.channels.submit()") &></form>
</%def>