var timer;

function initWebcastPlayer(file) {
	if (LinkWMPPlugin())
		$('webcastPlayer').innerHTML = '<div id="helpPlay">\n' +
					                   '    <a href="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx" target="_blank">T&eacute;l&eacute;chargez l\'extension Windows Media Player pour Firefox</a><br />\n' +
                        			   '	<i>(instructions en anglais)</i>\n' +
				                       '</div>\n';
	else {
		$('webcastPlayer').innerHTML = getPlayer('', '', '','EdemoPlay', 'EdemoPlay', '400', '300', 'none', 'true', 'false', false, '../images', file);
	}
}

function getPlayer(pre, inter, post, id, name, width, height, uimode, autostart, strechToFit, simpleSkin, channel, Url) {

	var playerCode = pre;

	if(window.ie)
		playerCode = '<object id="'+id+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'+width+'" height="'+height+'">';
	else
		playerCode = '<object id="'+id+'" type="application/x-ms-wmp" data="'+Url+'" width="'+width+'" height="'+height+'">';

	playerCode += '<param name="URL" value="'+Url+'" />';
	playerCode += '<param name="uiMode" value="'+uimode+'">';
	if(window.ie)
		playerCode += '<param name="windowlessVideo" value="true">';
	else
		playerCode += '<param name="windowlessVideo" value="false">';
	playerCode += '<param name="enableContextMenu" value="true">';
	playerCode += '<param name="strechToFit" value="'+strechToFit+'">';
	playerCode += '<param name="currentPosition" value="0">';
	playerCode += '<param name="autostart" value="true" />';
	playerCode += '</object>';
	playerCode += post + getStatusBarCode(simpleSkin, channel);

	return playerCode;
}

function wmpMediaChange(newState)  {
}

var playstateValues = new Array("Undefined","Stopped","Paused","Playing","Scan Forward","Scan Reverse","Buffering","Waiting","Media Ended","Transitioning","Ready","Reconnecting");

function wmpPlayStateChange(newState)  {

	if(newState == 6 || newState == 7 || newState == 11)
	{
		$('webcastPlayerStatus').setText(playstateValues[newState]+"...");
		$clear(timer);
	}

	if(newState == 3)
	{
		$('webcastPlayerStatus').setText("");
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('play.png', 'pause.png'));

		$clear(timer);
		if($('area') && $('knob'))
		{
			mySlide = new Slider($('area'), $('knob'), {
											steps: Math.round($('EdemoPlay').controls.currentItem.duration),
											onChange: function(step){
												seekTo(step);
											}
			});

			if($('EdemoPlay').controls.currentPosition != 0)
				mySlide.set(Math.round($('EdemoPlay').controls.currentPosition));

			timer = updateProgressBar.periodical(Math.round((Math.round($('EdemoPlay').controls.currentItem.duration) / $('area').getStyle('width').toInt()) * 1000) );
		}
	}
	if(newState == 1)
		$('webcastPlayerStatus').setText("");
}

function updateProgressBar()
{
	if($('EdemoPlay').playState == 3 &&  $('knob').getStyle('left').toInt() <= $('area').getStyle('width').toInt() )
		$('knob').setStyle('left', $('knob').getStyle('left').toInt()+1);
}


function VisionMute()
{
	try
	{
      if (!$('EdemoPlay').settings.mute) {
    	     $('EdemoPlay').settings.mute = true;
    	     $('muteButton').setProperty('src', $('muteButton').getProperty('src').replace('On', 'Off'));
	  }else{
             $('EdemoPlay').settings.mute = false;
             $('muteButton').setProperty('src', $('muteButton').getProperty('src').replace('Off', 'On'));
      }
	}
	catch (ex){}
}

function getStatusBarCode(simpleSkin, channel)
{
	var statusBarCode;

	if(simpleSkin)
	{
		statusBarCode = '<div id="visionPlayStatusBar">'
						+'<div id="webcastPlayerStatus" style="float:left;background:url(images/'+channel+'/player/home/leftSpacer.png) no-repeat;"><img src="images/'+channel+'/player/home/leftSpacer.png" border="0" style="float:left;" /></div><a href="javascript:startPlay();" style="float:left;"><img src="images/'+channel+'/player/home/play.png" border="0" id="playPauseButton" /></a><a href="javascript:wmpstop();" style="float:left;"><img src="images/'+channel+'/player/home/stop.png" border="0"/></a>'
						+'</div>';

	}
	else
	{
		statusBarCode = '<div id="visionPlayStatusBar">'
						+'<a href="javascript:startPlay();" style="float:left;"><img src="images/'+channel+'/player/pause.png" border="0" id="playPauseButton" /></a><a href="javascript:wmpstop();" style="float:left;"><img src="images/'+channel+'/player/stop.png" border="0"/></a><img src="images/'+channel+'/player/barLeft.png" border="0" style="float:left;"/>'
						+'<div id="area" style="float:left;"><span id="webcastPlayerStatus"></span>'
						+'<div id="knob" style="float:left;"></div>'
						+'</div>'
						+'<img src="images/'+channel+'/player/barRight.png" border="0" style="float:left;"/><a href="javascript:VisionMute();" style="float:left;"><img src="images/'+channel+'/player/muteOn.png" border="0" id="muteButton"/></a><a href="#" onclick="javascript:return VisionSetFullScreen();" style="float:left;"><img src="images/'+channel+'/player/fullscreen.png" border="0"/></a>'
						+'</div>';
	}

	return statusBarCode;
}


function startPlay(){
	if( $('playPauseButton').getProperty('src').test('pause.png') )
	{
		$('EdemoPlay').controls.pause();
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('pause.png', 'play.png'));
	}
	else
	{
		$('EdemoPlay').controls.play();
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('play.png', 'pause.png'));
	}
}

function wmpstop(){
	$clear(timer);
	$('webcastPlayerStatus').setText("");
	if($('knob'))
		$('knob').setStyle('left', 0);
	$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('pause.png', 'play.png'));
	$('EdemoPlay').controls.stop();
}

function seekTo(steps)
{
	$('EdemoPlay').controls.currentPosition = steps;
	$('EdemoPlay').controls.play();
}

var fullscreenTimer;

function VisionSetFullScreen()
{
	if ($('EdemoPlay').playState.toInt() == 3)
	{
		if(window.ie)
		{
			$('EdemoPlay').uiMode = 'full';
			$('EdemoPlay').fullScreen='true';
			fullscreenTimer = manageFullScreen.periodical(500);
		}
		else {
			window.alert('Le mode plein ecran ne fonctionne que sous Internet Explorer');
		}
	}
	return false;
}

function manageFullScreen() {
	if(!$('EdemoPlay').fullScreen && $('EdemoPlay').uiMode != 'none') {
		$('EdemoPlay').uiMode = 'none';
		$clear(fullscreenTimer);
	}
}

function LinkWMPPlugin()
{
	var lLink = false;

	if (navigator.platform == "Win32")
	{
		if(window.gecko)
		{
			lLink = true;
			for (var i1 = 0 ; i1 < navigator.plugins.length ; i1++)
			{
				var n = navigator.plugins[i1].name;
				if ( n != null && n.test("Windows Media") && n.test("Microsoft"))
				{ lLink = false; break; }
			}
		}
	}

	return lLink;
}
