﻿var oFlashPlayer = null;
var bPlayerChat = false;
var bPlayerVideos = false;
var bPlayerReplay = false;
var bPlayerMini = false;
var aChannels = new Array();
var aPrgms = new Array();
var nChannelID = 0;
var aTracks = new Array();
var aTracks2 = new Array();
var aReplays = new Array();
var aControlsBars = new Array();
var nNbControlsBars = 0;
var bReplaysLoaded = false;
var nShownTrack = -1;
var nCurrentTrack = -1;
var dtReference = new Date();

var sVideoDomain = 'video.radiocontact.be';

function Player_PlayVideo(nVideoId)
{
	document.getElementById('PlayerVideo').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="swf_237206" align=""> \
<param name="movie" value="http://' + sVideoDomain  + '/GED_vp/00230000/237200/237206.swf?VideoID=' + nVideoId + '&sXitiRoot=Videos_a_revoir&bembed=1&bAutoStart=0&sDummyPath=" /> \
<param name="quality" value="high" /> \
<param name="allowFullScreen" value="true" /> \
<param name="allowScriptAccess" value="always" /> \
<param name="wmode" value="transparent" /> \
<embed src="http://' + sVideoDomain + '/GED_vp/00230000/237200/237206.swf?VideoID=' + nVideoId + '&sXitiRoot=Videos_a_revoir&bembed=1&bAutoStart=0&sDummyPath=" quality="high" allowFullScreen="true" allowScriptAccess="always" wmode="transparent" width="100%" height="100%" name="swf_237206" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed> \
</object>';
}

function Player_LoadVideos()
{
	if(document.getElementById('PlayerVideosList').innerHTML.length > 0) return;

	try
	{
		sVideosrss = aPrgms.prgm[0].videosrss;
		if(sVideosrss.length == 0) sVideosrss = aChannels.channel[nChannelID - 1].videosrss;

		var aXslParams = new Array();
		aXslParams["param1"] = "1";

		var output = CWS_XslTransform(sVideosrss, "/PlayersRadio/Videos.xsl", "PlayerVideosList", aXslParams);

		oScrollingBars.aScrollingBars[0].onContentChanged();

		Player_PlayVideo(CWS_GetFirstChild(document.getElementById('PlayerVideosList')).getAttribute('videoid'));
	}
	catch(e) {alert(e);}
}

function Player_BuyTrack()
{
	if(nShownTrack >= 0)
	{
		sHtml = '';

		if(aTracks.track[nShownTrack].itunes.length > 0)
		{
			sHtml += '<a class="itunes" href="javascript:" onclick="window.open(aTracks.track[nShownTrack].itunes); return false;">ITUNES</a>';
		}

		if(aTracks.track[nShownTrack].starzik.length > 0)
		{
			sHtml += '<a class="starzik" href="javascript:" onclick="window.open(aTracks.track[nShownTrack].starzik); return false;">STARZIK</a>';
		}

		if(sHtml.length > 0)
		{
			document.getElementById('buymenu').innerHTML = sHtml;
			document.getElementById('buymenu').style.display = 'block';
		}

	}
}

function CRTL_AddToFavoritesCallback(sResponse)
{
	//alert(sResponse);
	
	if(CRTL_HandleResponse(sResponse))
	{
		
	}
}

function Player_FavoriteTrack()
{
	if(nShownTrack >= 0)
	{
		script = document.createElement('script');
	    	script.type = 'text/javascript';
	    	script.src  = 'http://www.rtl.be/moncompte/RID_Id.asmx?action=addtofavorites&js=CRTL_AddToFavoritesCallback(\'{0}\');&sSESSION=' + CRTL_SessionID + '&type=track&id=' + aTracks.track[nShownTrack].id;
	    	document.getElementsByTagName('head')[0].appendChild(script);
	}
}

function TogglePlayerChat()
{
	bPlayerChat = !bPlayerChat;
	if (bPlayerChat)
	{
		bPlayerVideos = false;
		bPlayerReplay = false;
	}
	SetPlayerState();
}

function TogglePlayerVideos()
{
	bPlayerVideos = !bPlayerVideos;
	if (bPlayerVideos)
	{
		bPlayerChat = false;
		bPlayerReplay = false;

		SetPlayerState();
		Player_LoadVideos();
	}
	else SetPlayerState();
}

function ToggleReplays(d)
{
	for (i = 0; i < 7; i++)
	{
		document.getElementById('ReplaysPrgm_' + i).style.display = (i == d ? 'block' : 'none');
		document.getElementById('PRDA_' + i).className = (i == d ? 'selected' : document.getElementById('PRDA_' + i).getAttribute('defclass'));
	}
}

function ReplayReplay(d, p)
{
	sMp3 = aReplays.day[d].mp3url;
	nStart = aReplays.day[d].prgm[p].timeoffset;

//	alert(sMp3 + '?start=' + nStart);
	if(oFlashPlayer)
	{
		oFlashPlayer.switchToReplay(sMp3 + '?start=' + Math.floor(nStart * 0.97425), 1, aReplays.day[d].prgm[p].title);
		oFlashPlayer.doPlay();
	}
}	

function TranslateDay(sDayname)
{
	if(sDayname == 'Monday') return 'Lundi';
	if(sDayname == 'Tuesday') return 'Mardi';
	if(sDayname == 'Wednesday') return 'Mercredi';
	if(sDayname == 'Thursday') return 'Jeudi';
	if(sDayname == 'Friday') return 'Vendredi';
	if(sDayname == 'Saturday') return 'Samedi';
	if(sDayname == 'Sunday') return 'Dimanche';
}

function TogglePlayerReplay()
{
	bPlayerReplay = !bPlayerReplay;

	if (bPlayerReplay)
	{
		bPlayerChat = false;
		bPlayerVideos = false;

		if (!bReplaysLoaded)
		{
			bReplaysLoaded = true;

			document.getElementById('PlayerReplay').style.display = bPlayerReplay ? 'block' : 'none';


			aReplays = eval('(' + CWS_HttpRequest('/PlayersRadio/Replays_' + nChannelID + '.json?ts=' + new Date().getTime()) + ')');
		
			sHtmlDays = '';
			
			for (d = 0; d < aReplays.day.length; d++)
			{
				sHtmlDays += '<a id="PRDA_' + d + '" ' + (d == 0 ? 'defclass="first" ' : (d == 6 ? 'defclass="last" ' : '')) + 'href="javascript:" onclick="ToggleReplays(' + d + ');"><span class="Big">' + (d == 0 ? 'Aujourd\'hui' : TranslateDay(aReplays.day[d].dayname)) + '</span><br>' + aReplays.day[d].daydate + '</a>';

				sHtmlPrgms = '';
				
				for (p = 0; p < aReplays.day[d].prgm.length; p++)
				{
					if(d == 0)
					{
						aReplays.day[d].prgm[p].dtStop = new Date(
							parseInt(aReplays.day[d].prgm[p].timestop.substr(0, 4), 10),
							parseInt(aReplays.day[d].prgm[p].timestop.substr(5, 2), 10) - 1,
							parseInt(aReplays.day[d].prgm[p].timestop.substr(8, 2), 10),
							parseInt(aReplays.day[d].prgm[p].timestop.substr(11, 2), 10),
							parseInt(aReplays.day[d].prgm[p].timestop.substr(14, 2), 10),
							parseInt(aReplays.day[d].prgm[p].timestop.substr(17, 2), 10)
						);

						if(aReplays.day[d].prgm[p].dtStop < dtReference) sHtmlPrgms += '<div class="ReplaysPrgmItem' + (p % 2 == 0 ? '' : ' RPI2') + '"><span class="Hour">' + aReplays.day[d].prgm[p].start + '</span><span class="Title">' + aReplays.day[d].prgm[p].title + '</span><a class="ReplaysShareBut" href="javascript:" onclick="ReplayShare(); return false;"> </a><a class="ReplaysReplayBut" href="javascript:" onclick="Player_Relive(' + d + ', ' + p + '); return false;"> </a></div>';
					}
					else sHtmlPrgms += '<div class="ReplaysPrgmItem' + (p % 2 == 0 ? '' : ' RPI2') + '"><span class="Hour">' + aReplays.day[d].prgm[p].start + '</span><span class="Title">' + aReplays.day[d].prgm[p].title + '</span><a class="ReplaysShareBut" href="javascript:" onclick="ReplayShare(); return false;"> </a><a class="ReplaysReplayBut" href="javascript:" onclick="ReplayReplay(' + d + ', ' + p + '); return false;"> </a></div>';
				}

				document.getElementById('ReplaysPrgmItems_' + d).innerHTML = sHtmlPrgms;

				document.getElementById('ReplaysPrgm_' + d).style.display = 'block';
				oScrollingBars.aScrollingBars[d + 1].onContentChanged();
			}

			document.getElementById('ReplaysDays').innerHTML = sHtmlDays;

			ToggleReplays(0);
		}
	}

	SetPlayerState();
}

function SetPlayerState()
{
	document.getElementById('PlayerChat').style.display = bPlayerChat ? 'block' : 'none';
	document.getElementById('PlayerVideos').style.display = bPlayerVideos ? 'block' : 'none';
	document.getElementById('PlayerReplay').style.display = bPlayerReplay ? 'block' : 'none';

	oDiv = document.getElementById('PlayerBottom');
	oDiv.style.display = (bPlayerChat | bPlayerVideos | bPlayerReplay) ? 'block' : 'none';

	oA = document.getElementById('PlayerChatBut');
	oA.className = bPlayerChat ? 'but Selected' : 'but';

	oA = document.getElementById('PlayerVideosBut');
	oA.className = bPlayerVideos ? 'but Selected' : 'but';

	oA = document.getElementById('PlayerReplayBut');
	oA.className = bPlayerReplay ? 'but Selected' : 'but';

	oA = document.getElementById('PlayerExpandBut');
	oA.className = bPlayerMini ? '' : 'Selected';
}

function TogglePlayerSize()
{
	bPlayerMini = !bPlayerMini;

	oDiv = document.getElementById('PlayerTop');
	oDiv.className = bPlayerMini ? 'Mini' : '';

	SetPlayerState();
}

function SwitchToContact()
{
	SetChannel(1);

	document.getElementById('PlayerEmissionTitle').innerHTML = aPrgms.prgm[0].title;
	document.getElementById('PlayerEmissionTime').innerHTML = aPrgms.prgm[0].start + ' - ' + aPrgms.prgm[0].stop;
	
	aControlsBars[0].UpdateLimits(aPrgms.prgm[0].dtStart, aPrgms.prgm[0].dtStop);

	SetPlayerState();
}

function SwitchToMint()
{
	SetChannel(2);

	aControlsBars[0].UpdateLimits(aPrgms.prgm[0].dtStart, aPrgms.prgm[0].dtStop);

	SetPlayerState();
}

function SwitchToRNB()
{
	SetChannel(3);

	aControlsBars[0].UpdateLimits(aPrgms.prgm[0].dtStart, aPrgms.prgm[0].dtStop);

	SetPlayerState();
}

function GetChannelsParams()
{
	aChannels = eval('(' + CWS_HttpRequest('/PlayersRadio/Channels.json?ts=' + new Date().getTime()) + ')');
}

function GetPrgmParams()
{
	aPrgms = eval('(' + CWS_HttpRequest('/PlayersRadio/Prgm_' + nChannelID + '.json?ts=' + new Date().getTime()) + ')');

	document.getElementById('PlayerEmissionTitle').innerHTML = aPrgms.prgm[0].title;//animator;
	document.getElementById('PlayerEmissionTime').innerHTML = aPrgms.prgm[0].start + ' - ' + aPrgms.prgm[0].stop;

	aPrgms.prgm[0].dtRef = dtReference;

	aPrgms.prgm[0].dtStart = new Date(
		parseInt(aPrgms.prgm[0].timestart.substr(0, 4), 10),
		parseInt(aPrgms.prgm[0].timestart.substr(5, 2), 10) - 1,
		parseInt(aPrgms.prgm[0].timestart.substr(8, 2), 10),
		parseInt(aPrgms.prgm[0].timestart.substr(11, 2), 10),
		parseInt(aPrgms.prgm[0].timestart.substr(14, 2), 10),
		parseInt(aPrgms.prgm[0].timestart.substr(17, 2), 10)
	);

	aPrgms.prgm[0].dtStop = new Date(
		parseInt(aPrgms.prgm[0].timestop.substr(0, 4), 10),
		parseInt(aPrgms.prgm[0].timestop.substr(5, 2), 10) - 1,
		parseInt(aPrgms.prgm[0].timestop.substr(8, 2), 10),
		parseInt(aPrgms.prgm[0].timestop.substr(11, 2), 10),
		parseInt(aPrgms.prgm[0].timestop.substr(14, 2), 10),
		parseInt(aPrgms.prgm[0].timestop.substr(17, 2), 10)
	);

	if(aPrgms.prgm[0].dtStop.getTime() < aPrgms.prgm[0].dtStart.getTime()) aPrgms.prgm[0].dtStop.setTime(aPrgms.prgm[0].dtStop.getTime() + (24 * 3600 * 1000));

	try
	{
		oFlashPlayer.SetXiti(
			aChannels.channel[nChannelID - 1].xtdomain,
			aChannels.channel[nChannelID - 1].xtsite,
			aChannels.channel[nChannelID - 1].channel,
			aChannels.channel[nChannelID - 1].xtsite2,
			aPrgms.prgm[0].title
		);
	}
	catch(e) {}
}

function Player_ReLiveTrack(t)
{
	nSeek = aTracks.track[t].dtDiffusion.getTime() - (new Date().getTime() - 86400000);
	nSeek = Math.floor(nSeek / 1000.0);// - 15;
	if(nChannelID == 1) nSeek -= 15;

	if(oFlashPlayer)
	{
		oFlashPlayer.switchToReLive(aPrgms.prgm[0].title, aTracks.track[t].title, aTracks.track[t].artist);
		oFlashPlayer.doSeek(nSeek);
	}

	ShowTrack(t);

	aControlsBars[0].UpdateTellers();
}

function Player_Relive(d, p)
{
	sDateTime = aReplays.day[d].prgm[p].timestart;

	var dtSeek = new Date(
		parseInt(sDateTime.substr(0, 4), 10),
		parseInt(sDateTime.substr(5, 2), 10) - 1,
		parseInt(sDateTime.substr(8, 2), 10),
		parseInt(sDateTime.substr(11, 2), 10),
		parseInt(sDateTime.substr(14, 2), 10),
		parseInt(sDateTime.substr(17, 2), 10)
	);

	var nSeek = dtSeek.getTime() - (new Date().getTime() - 86400000);
	nSeek = Math.floor(nSeek / 1000.0);// - 15;

	if (nSeek >= 86400)
	{
		oFlashPlayer.switchToLive();
		oFlashPlayer.doPlay();

		ShowTrack(aTracks.track.length - 1);
	}
	else
	{
		oFlashPlayer.switchToReLive(aReplays.day[d].prgm[p].title, '', '');
		oFlashPlayer.doSeek(nSeek);
	}
}

function ShowTrack(t)
{
	if (nShownTrack != t)
	{
		nShownTrack = t;

		document.getElementById('CurrentTrackCover').src = aTracks.track[t].cover;
		document.getElementById('CurrentTrackTitle').innerHTML = aTracks.track[t].title;
		document.getElementById('CurrentTrackArtist').innerHTML = aTracks.track[t].artist;
	}
}

function CloseVideo()
{
	document.getElementById('PlayerLiveVideoFlash').innerHTML = '';
	document.getElementById('PlayerLiveVideo').style.display = 'none';
	document.getElementById('PlayerTop').style.height = ((nChannelID == 1) ? '382px' : '352px');
}

function SwitchToVideo()
{
	if(document.getElementById('PlayerLiveVideo').style.display != 'block')
	{
		nVideoId = aPrgms.prgm[0].videoid;
		if(nVideoId == 0) nVideoId = aChannels.channel[nChannelID - 1].videoid;

		if(nVideoId == 0) return;

		if(oFlashPlayer) oFlashPlayer.doStop();

		document.getElementById('PlayerLiveVideo').style.display = 'block';
		//document.getElementById('PlayerTop').style.height = '600px';//document.getElementById('PlayerLiveVideo').style.height;

		document.getElementById('PlayerLiveVideoFlash').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="swf_237206b" align=""> \
<param name="movie" value="http://' + sVideoDomain  + '/GED_vp/00230000/237200/237206.swf?VideoID=' + nVideoId + '&bembed=1&bAutoStart=0&sDummyPath=" /> \
<param name="quality" value="high" /> \
<param name="allowFullScreen" value="true" /> \
<param name="allowScriptAccess" value="always" /> \
<param name="wmode" value="transparent" /> \
<embed src="http://' + sVideoDomain + '/GED_vp/00230000/237200/237206.swf?VideoID=' + nVideoId + '&bembed=1&bAutoStart=0&sDummyPath=" quality="high" allowFullScreen="true" allowScriptAccess="always" wmode="transparent" width="100%" height="100%" name="swf_237206b" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed> \
</object>';
	}
	else
	{
		CloseVideo();
	}
}

function GetPlaylist()
{
	var sHtmlPlaylist = '';

	aTracks = eval('(' + CWS_HttpRequest('/PlayersRadio/PlayList_' + nChannelID + '.json?ts=' + new Date().getTime()) + ')');

	document.getElementById('PlayerPlaylistItems').innerHTML = '';

	for (t = 0; t < aTracks.track.length; t++)
	{
		aTracks.track[t].dtDiffusion = new Date(
			parseInt(aTracks.track[t].diffusion.substr(0, 4), 10),
			parseInt(aTracks.track[t].diffusion.substr(5, 2), 10) - 1,
			parseInt(aTracks.track[t].diffusion.substr(8, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(11, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(14, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(17, 2), 10)
		);

		sHtmlPlaylist += '<div class="PlayerPlaylistItem"><a href="javascript:" onclick="Player_ReLiveTrack(' + t + '); return false;"><img src="' + aTracks.track[t].cover + '"></a><br><span class="PlayerPlaylistItemArtist">' + aTracks.track[t].artist + '</span><br><span class="PlayerPlaylistItemTitle">' + aTracks.track[t].title + '</span></div>';
	}

	if (t > 0)
	{
		if(oFlashPlayer && oFlashPlayer.IsLive()) ShowTrack(t - 1);
	}

	document.getElementById('PlayerPlaylistItems').innerHTML = sHtmlPlaylist;

	oScrollingPanes.aScrollingPanes[0].onContentChanged();
	oScrollingPanes.aScrollingPanes[0].GotoPage(oScrollingPanes.aScrollingPanes[0].nNbPages - 4)
}

function RefreshCurrentTrack()
{
	aTracks2 = eval('(' + CWS_HttpRequest('/PlayersRadio/PlayList_' + nChannelID + '.json?ts=' + new Date().getTime()) + ')');

	if (aTracks2.prgm[0].timestart != aPrgms.prgm[0].timestart)
	{
		if(aTracks2.prgm[0].videorss != aPrgms.prgm[0].videorss) document.getElementById('PlayerVideosList').innerHTML = '';

		GetPrgmParams();
		document.getElementById('PlayerEmissionTitle').innerHTML = aPrgms.prgm[0].title;//animator;
		document.getElementById('PlayerEmissionTime').innerHTML = aPrgms.prgm[0].start + ' - ' + aPrgms.prgm[0].stop;
		aControlsBars[0].UpdateLimits(aPrgms.prgm[0].dtStart, aPrgms.prgm[0].dtStop);
		aTracks = new Array();
	}

	for (t = aTracks.track.length; t < aTracks2.track.length; t++)
	{
		aTracks.track[t] = aTracks2.track[t];

		aTracks.track[t].dtDiffusion = new Date(
			parseInt(aTracks.track[t].diffusion.substr(0, 4), 10),
			parseInt(aTracks.track[t].diffusion.substr(5, 2), 10) - 1,
			parseInt(aTracks.track[t].diffusion.substr(8, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(11, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(14, 2), 10),
			parseInt(aTracks.track[t].diffusion.substr(17, 2), 10)
		);

		sHtmlPlaylist = '<div class="PlayerPlaylistItem"><a href="javascript:" onclick="Player_ReLiveTrack(' + t + '); return false;"><img src="' + aTracks.track[t].cover + '"></a><br><span class="PlayerPlaylistItemArtist">' + aTracks.track[t].artist + '</span><br><span class="PlayerPlaylistItemTitle">' + aTracks.track[t].title + '</span></div>';
		document.getElementById('PlayerPlaylistItems').innerHTML += sHtmlPlaylist;

		if (oFlashPlayer && oFlashPlayer.IsLive())
		{
			ShowTrack(t);
			oScrollingPanes.aScrollingPanes[0].onContentChanged();
			oScrollingPanes.aScrollingPanes[0].GotoPage(oScrollingPanes.aScrollingPanes[0].nNbPages - 4);
		}
	}

	// Update tellers
//	aControlsBars[0].UpdateTellers();
}

function GetTimeRef()
{
	var sTimeRef = CWS_HttpRequest('/MOD_Tools.asmx?action=gettimeref&ts=' + new Date().getTime());

	dtReference = new Date(
		parseInt(sTimeRef.substr(0, 4), 10),
		parseInt(sTimeRef.substr(5, 2), 10) - 1,
		parseInt(sTimeRef.substr(8, 2), 10),
		parseInt(sTimeRef.substr(11, 2), 10),
		parseInt(sTimeRef.substr(14, 2), 10),
		parseInt(sTimeRef.substr(17, 2), 10)
	);
}

function AlertFromFlash(s)
{
	alert(s);
}

function SetChannel(nChannel)
{
	if(nChannelID == nChannel) return;

	nChannelID = nChannel;

	if(nChannelID == 4) sVideoDomain = 'video.belrtl.be';
	else sVideoDomain = 'video.radiocontact.be';

	if(nChannelID == 2) 
	{
		oDiv = document.getElementById('Player');
		oDiv.className = 'Mint';
	}
	else if(nChannelID == 3) 
	{
		oDiv = document.getElementById('Player');
		oDiv.className = 'RNB';
	}
	else
	{
		oDiv = document.getElementById('Player');
		oDiv.className = '';
	}

	GetTimeRef();
	GetPrgmParams();

	try
	{
		oFlashPlayer.doReset(
			aChannels.channel[nChannelID - 1].livestream,
			aChannels.channel[nChannelID - 1].replaystream,
			"",
			aChannels.channel[nChannelID - 1].webcam,
			aChannels.channel[nChannelID - 1].videostream,
			aChannels.channel[nChannelID - 1].imageurl,
			aChannels.channel[nChannelID - 1].vpshares,
			aChannels.channel[nChannelID - 1].vptagslive,
			aChannels.channel[nChannelID - 1].vptagsreplay,
			aChannels.channel[nChannelID - 1].seekfactor
		);
	}
	catch (e)
	{
	}

	GetPlaylist();
}

var oTimeout = null;
function InitPlayer()
{
	document.getElementById('PlayerStripe').style.display = 'block';

	if (oTimeout) window.clearTimeout(oTimeout);
	if (!bFlashPlayerIsReady || !oTimeout)
	{
		oTimeout = window.setTimeout(InitPlayer, 1000);
		return;
	}
	oTimeout = null;

	oFlashPlayer = getFlashMovieObject('RadioPlayer');

	GetChannelsParams();
	SetPlayerState();
//	SetChannel(1);
	if(nDefaultChannelID) SetChannel(nDefaultChannelID);
	else SetChannel(1);

	AddControlsBar(document.getElementById('PlayerTop'), 'PlayerControls0', dtReference, aPrgms.prgm[0].dtStart, aPrgms.prgm[0].dtStop);

	window.setInterval(RefreshCurrentTrack, 20000);

	try
	{
		if(bForceMini)
		{
			bPlayerMini = false;
			TogglePlayerSize();
		}
	}
	catch(e){}
}

function AddControlsBar(oContainer, sId, dtRef, dtStart, dtStop)
{
	aControlsBars[nNbControlsBars] = new ControlsBar(nNbControlsBars, sId, dtRef, dtStart, dtStop);

	oContainer.appendChild(aControlsBars[nNbControlsBars].oBar);

	return aControlsBars[nNbControlsBars++];
}

function ControlsBar(nIndex, sId, dtRef, dtStart, dtStop)
{
	this.dtInit = new Date();
	this.nTimeShift = this.dtInit.getTime() - dtRef.getTime();

	this.dtRef = dtRef;
	this.dtStart = dtStart;
	this.dtStop = dtStop;
	this.nTimeTotal = this.dtStop.getTime() - this.dtStart.getTime();

	this.nDuration = Math.floor((dtStop.getTime() - dtStart.getTime()) / 1000.0);
	this.nStartOffset = Math.floor((this.dtInit.getTime() + this.nTimeShift - dtStart.getTime()) / 1000.0);

	this.bSeeking = false;
	this.nStartMouseX = 0;
	this.bBackToLive = false;

	this.bAlert = false;

	this.nSeekOffset = 0;
	this.nMaxSeekOffset = 0;
	
	this.nAnimCount = 0;

	// Bar
	this.oBar = document.createElement('DIV');
	this.oBar.setAttribute('index', nIndex);
	this.oBar.className = 'PlayerControlsBar';
	if (sId.length > 0) this.oBar.setAttribute('id', sId);

	// Seekbar
	this.oSeekBar = document.createElement('DIV');
	this.oSeekBar.className = 'PlayerControlsSeekBar';
	this.oBar.appendChild(this.oSeekBar);

	// Replaybar
	this.oReplayBar = document.createElement('DIV');
	this.oReplayBar.className = 'PlayerControlsReplayBar';
	this.oBar.appendChild(this.oReplayBar);

	// Seek but
	this.oSeek = document.createElement('INPUT');
	this.oSeek.setAttribute('index', nIndex);
	this.oSeek.setAttribute('type', 'button');
	this.oSeek.className = 'PlayerControlsSeek';
	this.oBar.appendChild(this.oSeek);

	// Seek time
	this.oSeektime = document.createElement('SPAN');
	this.oSeektime.setAttribute('index', nIndex);
	this.oSeektime.className = 'PlayerControlsSeekTime';
	this.oSeekBar.appendChild(this.oSeektime);

	// Start / Stop
	this.oStart = document.createElement('SPAN');
	this.oStart.className = 'PlayerControlsStart';
	this.oBar.appendChild(this.oStart);
	this.oStop = document.createElement('SPAN');
	this.oStop.className = 'PlayerControlsStop';
	this.oBar.appendChild(this.oStop);
	this.oStart.innerHTML = dtStart.getHours() + 'h' + (dtStart.getMinutes() < 10 ? '0': '') + dtStart.getMinutes();
	this.oStop.innerHTML = dtStop.getHours() + 'h' + (dtStop.getMinutes() < 10 ? '0' : '') + dtStop.getMinutes();

	// Play / Pause but
	this.oPlayPause = document.createElement('INPUT');
	this.oPlayPause.setAttribute('type', 'button');
	this.oPlayPause.className = 'PlayerControlsPlayPauseBut';
	this.PlayPause_onClick = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		oFlashPlayer.doPlay();
		oSrc.className = 'PlayerControlsPlayPauseBut' + (oFlashPlayer.IsPlay() ? ' Playing' : '');
	}
	this.oPlayPause.onclick = this.PlayPause_onClick;
	this.oBar.appendChild(this.oPlayPause);

	// Mute but
	this.oMute = document.createElement('INPUT');
	this.oMute.setAttribute('type', 'button');
	this.oMute.className = 'PlayerControlsMuteBut';
	this.Mute_onClick = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		oFlashPlayer.doMute();
		oSrc.className = 'PlayerControlsMuteBut' + (oFlashPlayer.IsMuted() ? ' Muted' : '');
	}
	this.oMute.onclick = this.Mute_onClick;
	this.oBar.appendChild(this.oMute);

	// Volume slider
	this.oVolume = document.createElement('DIV');
	this.oVolume.className = 'VolumeSlider';
	this.onVolumeChange = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		v = evt.clientY - getElementCoords(oSrc).top - 25;
		if(v < 0) v = 0;
		v = 100 - v * 2;

		oFlashPlayer.setVolume(v);

		oSrc.className = 'VolumeSlider vol' + Math.floor(v / 10);
		window.status = v;
	}
	this.onVolumeStart = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		while (oSrc != null && (oSrc.nodeName != 'DIV' || oSrc.className != 'PlayerControlsBar'))
		{
			oSrc = oSrc.parentNode;
		}
		if (!oSrc) return;

		oControlBar = aControlsBars[oSrc.getAttribute('index')];

		oControlBar.oVolume.style.display = 'block';
	}
	this.onVolumeEnd = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		oSrc.style.display = 'none';
	}
	this.oVolume.onmousemove = this.onVolumeChange;
	this.oVolume.onmouseout = this.onVolumeEnd;
	this.oVolume.onclick = this.onVolumeEnd;
	this.oMute.onmouseover = this.onVolumeStart;
	this.oBar.appendChild(this.oVolume);

	// Direct / Replay Indicator
	this.oDirectReplay = document.createElement('SPAN');
	this.oDirectReplay.className = 'PlayerControlsDirectReplay';
	this.oBar.appendChild(this.oDirectReplay);

	this.UpdateLimits = function(dtStart, dtStop)
	{
	//	this.dtInit = new Date();
	//	this.nTimeShift = this.dtInit.getTime() - dtRef.getTime();
	//	this.dtRef = dtRef;

		this.dtStart = dtStart;
		this.dtStop = dtStop;
		this.nTimeTotal = dtStop.getTime() - dtStart.getTime();

		this.nDuration = Math.floor((dtStop.getTime() - dtStart.getTime()) / 1000.0);
		this.nStartOffset = Math.floor((new Date().getTime() + this.nTimeShift - dtStart.getTime()) / 1000.0);

		this.oStart.innerHTML = dtStart.getHours() + 'h' + (dtStart.getMinutes() < 10 ? '0': '') + dtStart.getMinutes();
		this.oStop.innerHTML = dtStop.getHours() + 'h' + (dtStop.getMinutes() < 10 ? '0' : '') + dtStop.getMinutes();
	}

	this.UpdateTellers = function ()
	{
		this.oSeekBar.style.width = (Math.floor(this.nMaxSeekOffset)) + 'px';
		
		if(!oFlashPlayer) return;

		if (oFlashPlayer.IsLive())
		{
			this.nSeekOffset = this.nMaxSeekOffset;

			this.oSeek.style.left = Math.floor(88.0 + this.nMaxSeekOffset) + 'px';
			
			this.oDirectReplay.className = 'PlayerControlsDirectReplay';
			this.oReplayBar.style.display = 'none';
		}
		else if (oFlashPlayer.IsReLive())
		{
			dtReLive = new Date();
			dtReLive.setTime(Math.floor(new Date().getTime() - 86400000 + (oFlashPlayer.GetReLiveCurrent() * 1000)));

			if(dtReLive.getTime() < this.dtStart.getTime())
			{
				dtReLive = dtStart;
			}
			else if(dtReLive.getTime() > this.dtStop.getTime())
			{
				dtReLive = dtStop;
			}
			else
			{
				this.nSeekOffset = Math.floor( (dtReLive.getTime() - this.dtStart.getTime()) / (this.dtStop.getTime() - this.dtStart.getTime()) * 320.0);
					
				this.oSeek.style.left = (this.nSeekOffset + 88) + 'px';
				this.oReplayBar.style.width = this.nSeekOffset + 'px';
				this.oReplayBar.style.display = 'block';
			}


			this.oDirectReplay.className = 'PlayerControlsDirectReplay replay';
		}

		this.oPlayPause.className = 'PlayerControlsPlayPauseBut' + (oFlashPlayer.IsPlay() ? ' Playing' : '');
	}

	this.reset = function (dtRef, dtStart, dtStop, bLive, oFlashPlayer)
	{
	}

	this.MouseUp = function (_Self)
	{
		if (_Self.bSeeking)
		{
			_Self.bSeeking = false;

			oControlBar.DoSeek();
		}
	}

	this.Animate = function (_Self, fElapdseTime)
	{
		var nOffset = _Self.nStartOffset + Math.floor((new Date().getTime() - _Self.dtInit.getTime()) / 1000.0);
		_Self.nMaxSeekOffset = Math.floor((nOffset / _Self.nDuration) * 320.0);

		if (_Self.bSeeking)
		{
			nDelta = CWS_curPosX - _Self.nStartMouseX;

			_Self.nSeekOffset += nDelta;

			if (_Self.nSeekOffset >= 0 && _Self.nSeekOffset <= _Self.nMaxSeekOffset) _Self.nStartMouseX = CWS_curPosX;

			if (_Self.nSeekOffset < 0) _Self.nSeekOffset = 0;
			if (_Self.nSeekOffset >= _Self.nMaxSeekOffset)
			{
				_Self.nSeekOffset = _Self.nMaxSeekOffset;
				_Self.bBackToLive = true;
			}
			else _Self.bBackToLive = false;

			_Self.oSeek.style.left = Math.floor(88.0 + _Self.nSeekOffset) + 'px';

			dtSeek = new Date();
			dtSeek.setTime(_Self.dtStart.getTime() + Math.floor(_Self.nTimeTotal / 320.0 * _Self.nSeekOffset));

			_Self.oSeektime.innerHTML = dtSeek.getHours() + 'h' + (dtSeek.getMinutes() < 10 ? '0' : '') + dtSeek.getMinutes();
			_Self.oSeektime.style.left = Math.floor(_Self.nSeekOffset - 21) + 'px';

			if(!_Self.bBackToLive)
			{
				_Self.oReplayBar.style.width = _Self.nSeekOffset + 'px';
				_Self.oReplayBar.style.display = 'block';
			}
			else _Self.oReplayBar.style.display = 'none';
		}
		else
		{
		//	_Self.nAnimCount ++;
			if(_Self.nAnimCount % 4 == 0) _Self.UpdateTellers();
		}

		_Self.nAnimCount ++;
		//window.status = '' + _Self.nAnimCount;
	}

	this.onSeekStart = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		while (oSrc != null && (oSrc.nodeName != 'INPUT' || oSrc.className != 'PlayerControlsSeek'))
		{
			oSrc = oSrc.parentNode;
		}
		if (!oSrc) return;

		oControlBar = aControlsBars[oSrc.getAttribute('index')];

		oControlBar.bSeeking = true;
		oControlBar.nStartMouseX = CWS_curPosX;

		oControlBar.oSeektime.style.display = 'block';

		return CWS_CancelBubble(evt);
	}

	this.onSeekStop = function (evt)
	{
		var oSrc = null;

		if (!evt) oSrc = window.event.srcElement;
		else oSrc = evt.target;

		while (oSrc != null && (oSrc.nodeName != 'INPUT' || oSrc.className != 'PlayerControlsSeek'))
		{
			oSrc = oSrc.parentNode;
		}
		if (!oSrc) return;

		oControlBar = aControlsBars[oSrc.getAttribute('index')];
		oControlBar.oSeektime.style.display = 'none';

		if (oControlBar.bSeeking)
		{
			oControlBar.bSeeking = false;

			oControlBar.DoSeek();
		}

		return CWS_CancelBubble(evt);
	}

	this.DoSeek = function ()
	{
		this.bReplay = true;

		dtSeek = new Date();
		dtSeek.setTime(this.dtStart.getTime() + Math.floor(this.nTimeTotal / 320.0 * this.nSeekOffset));

		nSeek = dtSeek.getTime() - (new Date().getTime() - 86400000);
		nSeek = Math.floor(nSeek / 1000.0);// - 15;

		if (nSeek >= 86400)
		{
			this.bBackToLive = true;
		}

		if (this.bBackToLive)
		{
			oFlashPlayer.switchToLive();
			oFlashPlayer.doPlay();

			ShowTrack(aTracks.track.length - 1);
		}
		else
		{
			oFlashPlayer.switchToReLive(aPrgms.prgm[0].title, '', '');
			oFlashPlayer.doSeek(nSeek);
		}

		oControlBar.oSeektime.style.display = 'none';

	//	this.UpdateTellers();
	}

	this.oSeek.onmousedown = this.onSeekStart;
	this.oSeek.onmouseup = this.onSeekStop;

	oTimersHandler.AddTimer(this);
}

CWS_sOnLoad += "InitPlayer();";

