<%pre>

#include <setup.h>
#include <tools.h>
#include <users.h>

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
	tChannelID channel;
	string async;
	string recid;
</%args>
<%session scope="global">
	bool logged_in(false);
</%session>
<%request scope="page">
	cChannel* Channel;
</%request>
<%include>page_init.eh</%include>
<%cpp>
	if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");
	if (!cUser::CurrentUserHasRightTo(UR_STARTREPLAY))
		throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );

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

    // the availabilty of Channel signals that we will do live tv streaming.
	Channel = 0;
	if (recid.empty()) {
		pageTitle = tr("VLC: live video stream");
#if VDRVERSNUM >= 20301
		LOCK_CHANNELS_READ;
		Channel = (cChannel *)Channels->GetByChannelID(channel);
#else
		ReadLock channelsLock(Channels);

		if (!channelsLock) {
			throw HtmlError(tr("Couldn't aquire access to channels, please try again later."));
		}
		Channel = Channels.GetByChannelID(channel);
#endif
		if (Channel == 0) {
			throw HtmlError( tr("Couldn't find channel or no channels available. Maybe you mistyped your request?") );
		}
	}
	else {
		pageTitle = tr("VLC: play recording");
	}
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR-Live - <$ pageTitle $></title>
		<& pageelems.stylesheets &>
<%cpp>
	if (!asyncReq) {
</%cpp>
		<& pageelems.ajax_js &>
<%cpp>
	}
	else {
</%cpp>
		<script type="text/javascript" src="js/mootools/mootools.v1.11.js"></script>
<%cpp>
	}
</%cpp>
		<script type="text/javascript" src="js/live/vlc.js"></script>
		<script type="text/javascript"><!--
			var vlcControl = new VLC("video1", {
					<? !asyncReq ? "autoresize: false," ?>
					<? !Channel ? "playRecording: true," ?>
						offset: 5
				});
			vlcControl.addEvent('toggle', function(id, state){
					var text = null;
					switch (id) {
					case "TogglePlay":
						text = state ? "<$ tr("Stop") $>" : "<$ tr("Play") $>";
						break;
					case "ToggleMute":
						text = state ? "<$ tr("Sound on") $>" : "<$ tr("Sound off") $>";
						break;
					}
					if (text)
						$(id).setHTML(text);
				});
		--></script>
	</head>
	<body>
<%cpp>
	if (!asyncReq) {
</%cpp>
		<& pageelems.logo &>
		<& menu active="vlc" &>
		<div class="inhalt">
<%cpp>
	}
</%cpp>
<%cpp>
	string server = request.getHost();
	server = server.substr(0, server.rfind(':'));
	string videourl;
	if (Channel != 0) {
		int streamdevPort = LiveSetup().GetStreamdevPort();
		videourl = string("http://") + server + ":" + lexical_cast<string,int>(streamdevPort) + "/" + LiveSetup().GetStreamdevType() + "/" + *Channel->GetChannelID().ToString();
	}
	else {
		videourl = string("http://") + server + ":" + lexical_cast<string,int>(LiveSetup().GetServerPort()) + "/recstream.html?recid=" + recid;
	}
</%cpp>
			<embed type="application/x-vlc-plugin" pluginspage="http://www.videolan.org/" version="VideoLAN.VLCPlugin.2" id="video1" name="video1" autoplay="yes" loop="no" width="720" height="576" target="<$ videourl $>" />
			<& vlc.controls videourl=(videourl) asyncReq=(asyncReq) &>
<%cpp>
	if (!asyncReq) {
</%cpp>
		</div>
<%cpp>
	}
</%cpp>
	</body>
</html>
<%include>page_exit.eh</%include>

<%def controls>
<%args>
	string videourl;
	bool asyncReq;
</%args>
<div id="vlcControls">
	<button id="TogglePlay" type="button" class="green"><$ tr("Stop") $></button>
	<button id="ToggleMute" type="button" class="red"><$ tr("Sound off") $></button>
	<button id="FullScreen" type="button" class="blue"><$ tr("Fullscreen") $></button>
<%cpp>
	if (asyncReq) {
</%cpp>
	<button id="Close" type="button" class="yellow"><$ tr("Close") $></button>
<%cpp>
	}
</%cpp>
<!-- --><p><$ tr("VLC media URL") $>: <span class="bold"><a href="<$ videourl $>"><$ videourl $></a></span></p><!-- -->
</div>
</%def>