<%pre>

#include <stdext.h>
#include <exception.h>
#include <setup.h>
#include <tools.h>
#include <epg_events.h>
#include <recman.h>

#if VDRVERSNUM < 20301
namespace vdrlive {
	class SchedulesLock
	{
		public:
			SchedulesLock() : m_schedulesLock() {}

			operator cSchedulesLock& () { return m_schedulesLock; }

		private:
			SchedulesLock(SchedulesLock const &schedulesLock) {}

			cSchedulesLock m_schedulesLock;
	};

	typedef stdext::shared_ptr<SchedulesLock> SchedulesLockPtr;
}
#endif

using namespace vdrlive;

</%pre>
<%args>
	std::string epgid;
	std::string async;
</%args>
<%session scope="global">
	bool logged_in(false);
</%session>
<%request scope="global">
	EpgInfoPtr epgEvent;
	std::string epgImage;
</%request>
<%include>page_init.eh</%include>
<%cpp>
	if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");

	pageTitle = tr("Electronic program guide information");

	bool ajaxReq = !async.empty() && (lexical_cast<int>(async) != 0);

	bool aboutBox = false;

#if VDRVERSNUM >= 20301
	bool isEvent = false;
#else
	// One of these get initialized when needed. When freed by getting
	// out of scope they will release (if initialized) important
	// Semaphores/Locks.
	SchedulesLockPtr schedulesLockPtr;
#endif
	RecordingsManagerPtr recordings;
	epgImage.clear();

	if (!epgid.empty()) {

		const std::string recording_s("recording_");
		const std::string event_s("event_");
		const std::string aboutbox("aboutBox");

		// check for recording:
		if (epgid.compare(0, recording_s.length(), recording_s) == 0) {
			recordings = LiveRecordingsManager();
			const cRecording *recording = recordings->GetByMd5Hash(epgid);
			if (recording == 0) {
				throw HtmlError(tr("Couldn't find recording or no recordings available"));
			}
			epgEvent = EpgEvents::CreateEpgInfo(epgid, recording);
			epgImage = EpgEvents::PosterTvscraper(NULL, recording);
		}
		// check for event:
		else if (epgid.compare(0, event_s.length(), event_s) == 0) {
#if VDRVERSNUM >= 20301
			/* Need to lock here channels also, because CreateEpgInfo will lock
			 * it also and this will result in a wrong lock order */
			LOCK_CHANNELS_READ;
			LOCK_SCHEDULES_READ;
			epgEvent = EpgEvents::CreateEpgInfo(epgid, Schedules);
			isEvent = true;
#else
			schedulesLockPtr = SchedulesLockPtr(new SchedulesLock);
			if (!schedulesLockPtr) {
				throw HtmlError(tr("Error aquiring schedules lock"));
			}
			const cSchedules* schedules = cSchedules::Schedules(*schedulesLockPtr);
			if (!schedules) {
				throw HtmlError(tr("Error aquiring schedules"));
			}
			epgEvent = EpgEvents::CreateEpgInfo(epgid, schedules);
#endif
			const cEvent* event = epgEvent->Event();
			epgImage = EpgEvents::PosterTvscraper(event, NULL);
		}
		// check for aboutbox:
		else if (epgid.compare(0, aboutbox.length(), aboutbox) == 0) {
			aboutBox = true;
		}
	}
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR-Live - <$ pageTitle $></title>
<%cpp>
	if (!ajaxReq) {
</%cpp>
		<& pageelems.stylesheets &>
		<& pageelems.ajax_js &>
<%cpp>
	}
</%cpp>
	</head>
	<body onload="adjustHeader()" onresize="adjustHeader()">
<%cpp>
	if (!ajaxReq) {
</%cpp>
		<& pageelems.logo &>
		<& menu &>
<%cpp>
	}
</%cpp>
		<div class="inhalt">
<%cpp>
	if (epgEvent) {
		std::string start(epgEvent->StartTime("%a,") + std::string(" ")
				+ epgEvent->StartTime(tr("%b %d %y")) + std::string(" ")
				+ epgEvent->StartTime(tr("%I:%M %p")));
		std::string tools_component;
		if (recordings) {
			tools_component = epgEvent->Archived().empty() ? "recordings.rec_tools" : "recordings.archived_disc" ;
		}
#if VDRVERSNUM >= 20301
		if (isEvent)
#else
		if (schedulesLockPtr)
#endif
			tools_component = "epginfo.epgTools";

</%cpp>
		<& pageelems.epg_tt_box boxId=(epgEvent->Id()) caption=(epgEvent->Caption()) tools_comp=(tools_component) time=(start) title=(epgEvent->Title()) short_descr=(epgEvent->ShortDescr()) long_descr=(epgEvent->LongDescr()) archived=(epgEvent->Archived()) elapsed=(epgEvent->Elapsed()) filename=(epgEvent->FileName()) epgImage=(epgImage) &>
<%cpp>
	}
	if (aboutBox) {
</%cpp>
		<& pageelems.about_tt_box &>
<%cpp>
	}
</%cpp>
		</div>
	</body>
</html>

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

<# ---------------------------------------------------------------------- #>

<%def epgTools>
<%args>
std::string id;
std::string title;
int detail;
</%args>
<& pageelems.epg_tool_box detail=(detail) epgid=(id) title=(title) startTime=(epgEvent->GetStartTime()) endTime=(epgEvent->GetEndTime()) &>
</%def>
