<%pre>
#include <vdr/channels.h>
#include <vdr/i18n.h>
#include <vdr/keys.h>
#include "setup.h"
#include "tools.h"

using namespace std;
using namespace vdrlive;

</%pre>
<%args>
	tChannelID channel;
	string async;
</%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");

	pageTitle = tr("VLC: live video stream");

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

	ReadLock channelsLock(Channels);

	if (!channelsLock) {
		throw HtmlError(tr("Couldn't aquire access to channels, please try again later."));
	}
	Channel = Channels.GetByChannelID(channel);
	if (Channel == 0) {
		throw HtmlError( tr("Couldn't find channel or no channels available. Maybe you mistyped your request?") );
	}
</%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" ?>
				});
			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>
	if ( Channel != 0 ) {
		int streamdevPort = LiveSetup().GetStreamdevPort();
#ifdef TNTVERS7
		string server = request.getHost();
		server = server.substr(0, server.rfind(':'));
#else
		string server = request.getServerIp();
#endif
		string videourl = string("http://") + server + ":" + lexical_cast<string,int>(streamdevPort) + "/" + LiveSetup().GetStreamdevType() + "/" + *Channel->GetChannelID().ToString();
</%cpp>
			<embed type="application/x-vlc-plugin" 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") $>: <$ videourl $></p><!-- -->
</div>
</%def>