var axPlayer=false;
// Browser detection
var agt = navigator.userAgent.toLowerCase();

// Browser version
// Warning: On IE5, these return 4, we are using is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_fireFox = (agt.indexOf('firefox') != -1);
var is_opera = (agt.indexOf('opera') != -1);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);

// Platform detection
var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
var is_mac    = (agt.indexOf("mac")!=-1);
var is_vista    = (agt.indexOf("windows nt 6.0")!=-1);

var os;
var browser;
var wmplayer;
var tech = "wmp";

if (is_win==true){
	os="win";
	tech = "wmp";	
} else if (is_mac==true){
	os="mac";
	tech = "flip4mac";
} else {
	os="other";
	tech = "vlc";
};

if (is_ie5_5up==true){
	browser="ie";
} else if (is_gecko==true ){
	browser="nn";
} else if (is_nav6up==true){
	broswer="nn";
} else if (!is_nav6up && !is_gecko){
	browser="other";
};

if (browser == "nn" && is_fireFox){
	browser = "ff";
};
if (is_opera){ 
	browser = "op";
};


///////////////////////////////////////
var seekBar;
var statustimer;

function initSeek(start,playerId){
	seekBar = new Control.Slider('handleSeek',
								 'seekDiv',
								 { alignY:-5, 
								   sliderValue:start, 
								   onSlide:function(v) { DoPause(playerId);},
								   ///attention vlc//////////
								   onChange:function(v){ if (tech == "wmp") {if ($(playerId).playState == 2 && tech == "wmp") seek(v,playerId); } else if (tech == "vlc") {  if ($(playerId).input.state == 2 ) seek(v,playerId);}; }
								  }
			);
};

function seek(v,playerId){
	if (tech == "wmp") {
	///attention vlc//////////
		newPos = parseInt(v*$(playerId).currentmedia.duration);
		if (!isNaN(newPos) && (newPos != '')){
			$(playerId).controls.currentposition = newPos;
		};
		DoPlay(playerId);
	} else if (tech == "vlc")  {
		newPos = parseInt(v*$(playerId).input.length/1000);
		if (!isNaN(newPos) && (newPos != '')){
			$(playerId).input.time  = newPos*1000;
		};
		DoPlay(playerId);
	}
	return;
};

function setSeekPosition(v){
	seekBar.setValue(v);
};

function ShowPosition(wait){
	if (tech == "wmp") {
		if($('video_Player').playState==3){
			//$('positionDiv').innerHTML=" : "+$('video_Player').controls.currentPositionString+" mn";
			v=$('video_Player').controls.currentPosition/$('video_Player').currentmedia.duration;
			setSeekPosition(v);
		};
		statustimer=setTimeout("ShowPosition("+wait+")", wait);
	} else if (tech == "vlc")  {
		if($('video_Player').input.state ==3){
			//$('positionDiv').innerHTML=" : "+$('video_Player').controls.currentPositionString+" mn";
			v=$('video_Player').input.time/$('video_Player').input.length;
			setSeekPosition(v);
		};
		statustimer=setTimeout("ShowPosition("+wait+")", wait);
		fireStatus('video_Player','');
	}
};

////////////////////////////////////////////////
function rollout(id,img,type) {
	$(id).src = 'img/' + img  + '.'+type;
};
function rollover(id,img,type) {
	$(id).src = 'img/' + img  + '_on.'+type;
};

function makeOff(id,img,type){
	$(id).src = 'img/' + img  + '_off.'+type;
};

function DoPlay(playerId){
	if (tech == "wmp") {
		$(playerId).controls.play();
	} else if (tech == "vlc") {
		$(playerId).playlist.play();
	}
};

function DoFullVideo(playerId){
	if (tech == "wmp") {
		if($(playerId).playState==3){
			alert("Vous allez passer en mode plein écran\nAppuyez sur 'echapp' pour revenir en mode normal");
			$(playerId).fullScreen = 'true';
		};
	} else if (tech == "vlc") {
		$(playerId).video.toggleFullscreen();
	};
};

function DoStop(playerId){
	if (tech == "vlc"){
		$(playerId).playlist.stop();
	} else if (tech == "wmp") {
		$(playerId).controls.stop();
	} else if (tech == "flip4mac") {
		$(playerId).Stop();		
	};
};

function DoPause(playerId){
	if (tech == "vlc"){
		$(playerId).playlist.togglePause();
	} else if (tech == "wmp") {
		$(playerId).controls.pause();
	} else if (tech == "flip4mac") {
		$(playerId).Pause();		
	};
};

function SetVolume(vol,playerId){
		vol=parseFloat(vol);
		if(!isNaN(vol)) myVolume=parseFloat(vol);	
	if (tech == "wmp") {	
		$(playerId).settings.volume=myVolume;
	} else if (tech == "vlc") {
		myVolume=myVolume*2;
		$(playerId).audio.volume(myVolume); 
	};
};

function GetVolume(playerId){
	if (tech == "wmp") {
		return $(playerId).settings.volume;
	} else if (tech == "vlc") {
		return $(playerId).audio.volume();
	}
};

function StartPlayer(queryString,playerId){
	queryString = "http://www.tvsciences.com/web/video.php?media=WinMedia&fln=" + queryString;
		if (tech == "wmp") {
			$(playerId).Url=queryString;
		} else if (tech == "vlc") {
		    $('VideoDiv').innerHTML=getHTMvlc(playerId,queryString);
			var wait = 1000;
			statustimer=setTimeout("ShowPosition("+wait+")", wait);	
		} else if (tech == "flip4mac") {			
			$('VideoDiv').innerHTML=getHTMLFlip4Mac(playerId,queryString);			
		};
};

function ShowDuration(){
	if (tech == "wmp") {	
		$('durationDiv').innerHTML="durée du film : "+$('video_Player').currentmedia.durationString;+" mn";
	} else if (tech == "vlc") {
		$('durationDiv').innerHTML="durée du film : "+$('video_Player').currentmedia.durationString;+" mn";		
	};
};


var currentItemId=0;

function ShowCurrentItem(){
	$('currentMovieDiv').innerHTML=$('video_Player').currentMedia.name;
	id=$('video_Player').currentMedia.getItemInfo('ID');
	if(id!=currentItemId){
		if(currentItemId!=0 && currentItemId!=undefined && $('item_'+currentItemId)){
			$('item_'+currentItemId).removeClassName('item_memo')
			$('item_'+currentItemId).addClassName('item');
		};
		$('item_'+id).addClassName('item_memo');
		currentItemId=id;
		changeAdds();
	};
};

function fireStatus(playerId,newState){
if (tech == "wmp") {	
	switch(newState){
		case 0: $('BP').innerHTML="";
		break;
		case 1: $('BP').innerHTML="Stop";
		break;
		case 2: $('BP').innerHTML="Pause";
		break;
		case 3: {
			$('BP').innerHTML="Lecture";
			//ShowCurrentItem();
			//ShowDuration();
			//ShowPosition(1000);
		};
		break;
		case 4: $('BP').innerHTML="Avance rapide";
		break;
		case 5: $('BP').innerHTML="Retour rapide";
		break;
		case 7: $('BP').innerHTML="Attente...";
		break;
		case 8: $('BP').innerHTML="Lecture terminée";
		break;
		case 9: $('BP').innerHTML="Préparation...";
		break;
		case 10: $('BP').innerHTML="Lecteur prêt";
		break;
	};
	 	if (newState == 0 || newState == 1 || newState == 7 || newState == 10 ) {
		$("VideoDiv").style.visibility = 'hidden';
		$("Player_pub").style.visibility = 'visible';
	} else {
		$("VideoDiv").style.visibility = 'visible';
		$("Player_pub").style.visibility = 'hidden';
	};
} else if (tech == "vlc") {
		//vlc.input.state : current state of the input chain given as enumeration (IDLE/CLOSE=0, OPENING=1, BUFFERING=2, PLAYING=3, PAUSED=4, STOPPING=5, ERROR=6)
		var _newState = $(playerId).input.state;
		switch(_newState){
			case 0: $('statusDiv').innerHTML="Close";
			break;
			case 1: $('statusDiv').innerHTML="Préparation...";
			break;
			case 2: $('statusDiv').innerHTML="Buffering...";
			break;
			case 3: {
				$('statusDiv').innerHTML="Lecture";
				//ShowCurrentItem();
				//ShowDuration();
				//ShowPosition(1000);
			};
			break;
			case 4: $('statusDiv').innerHTML="Pause";
			break;
			case 5: $('statusDiv').innerHTML="Arrêter";
			break;
			case 6: $('statusDiv').innerHTML="Error";
			break;
		};
		 if (newState == 0 || newState == 1 || newState == 5 || newState == 6 ) {
		$("VideoDiv").style.visibility = 'hidden';
		$("Player_pub").style.visibility = 'visible';
	} else {
		$("VideoDiv").style.visibility = 'visible';
		$("Player_pub").style.visibility = 'hidden';
	};
}	


};

function initPlayer(playerId){
  if(!is_mac) {
	getPlayer(playerId);
	initSeek('0',playerId);
	ShowPosition(1000);
  } else {
	$('divBarVideo').style.display="none";
	$('seekFond').style.display="none";
	$('seekDiv').style.display="none";
	$('volume').style.display="none";
 }	
};

function checkFlip4Mac() {
	var haveFlip4Mac = false;
	for (var i = 0; i < navigator.plugins.length; i++) {
		if (navigator.plugins[i].name.indexOf("Flip4Mac") != -1) {
			haveFlip4Mac = true; 
			break;
		}
	}
	return haveFlip4Mac;
} 

function detectWMP(){
	var wmpInfo = {
		installed: false,
		scriptable: false,
		type: null,
		versionInfo: null
	};
	//var wmp64 = "MediaPlayer.MediaPlayer.1";
	var wmp7 = "WMPlayer.OCX";
	if((window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1) || window.GeckoActiveXObject) {
		wmpInfo.type = "ActiveX";
		var player = createActiveXObject(wmp7);
		if(player) {
			wmpInfo.installed = true;
			wmpInfo.scriptable = true;
			wmpInfo.versionInfo = player.versionInfo;		
		}
	} else if(navigator.mimeTypes) {		
		wmpInfo.type = "NetscapePlugin";
		var player = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
		if(player) {	
			wmpInfo.installed = true;
			wmpInfo.scriptable = false;
			wmpInfo.versionInfo = "PluginVersion";
		};
	};
	return wmpInfo;
};

function createActiveXObject(id){
	var error;
	var control = null;

	try  {
		if (window.ActiveXObject)    {
			control = new ActiveXObject(id);
		}  else if (window.GeckoActiveXObject)    {
			control = new GeckoActiveXObject(id);
		}
	}  catch (error)  {
		;
	};
	return control;
};

function installAxFF15() { 
	xpi={'Mozilla Firefox 1.5 ActiveX Plug-in':'http://www.iol.ie/~locka/mozilla/mozactivex-ff-15.xpi'}; InstallTrigger.install(xpi); 
};

function installAxFF() { goURL("http://www.iol.ie/~locka/mozilla/plugin.htm");}; 


function getPlayer(PlayerId){
if(!is_mac) {		
	var detectionResults = detectWMP();

	if (detectionResults.installed ) {
		axPlayer=true;
		if (tech == "wmp" ) {			
			$('VideoDiv').innerHTML=getHTMLForActiveX(PlayerId);
		} else  if (tech == "vlc" ) {			
			$('VideoDiv').innerHTML=getHTMvlc(PlayerId,'');
		}
	} else  {
		axPlayer=false;
		var buff="Ce site privilégie l'utilisation du player Windows Media, en mode ActiveX.\n\n";
		installStr= detectionResults.installed ? 'est installé' : 'n\'est pas installé';
		buff+="Détection  : " + detectionResults.type + " " + installStr + "\n\n";
		buff+="Pour installer le composant, utilisez la rubrique d'aide.La procédure et les ressources d'installation y est disponible.\n\n Si vous desirez être redirigé vers la procédure d'installation cliquez sur OK.\n" + agt;
		alert(buff);
		if ( browser == 'ff') {
			var version = agt.substring(agt.indexOf("firefox\/")+8);
			if(parseFloat(version) >= 1.5) {
					installAxFF15();					
			} else {
					installAxFF();					
			};
		};
		$('divBarVideo').style.display="none";
		$('seekFond').style.display="none";
		$('seekDiv').style.display="none";
		$('volume').style.display="none";
	};
} else {
		if(!checkFlip4Mac()) {
			axPlayer=false;
			$('VideoDiv').innerHTML ="<div style='position:relative;left:15px;top:120px;z-index:10'><a href='http://www.flip4mac.com/wmv_download.htm' target='_blank'>";
			$('VideoDiv').innerHTML +="Pour lire ce média, veuillez télécharger Flip4Mac.</a></div>";
			
		} else {
			axPlayer=true;
			$('VideoDiv').innerHTML=getHTMLFlip4Mac(PlayerId,'');
		}
}	
};

function getHTMLFlip4Mac(playerId,url) {
	var clid;
	var params = "";
	var LineEnd="\n";

	clid = "CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6";
	cbase = "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=7,0,0,1954";
	params = " <param name=\"stretchToFit\" VALUE=\"true\">" +LineEnd;
	params += " <param name=\"uiMode\" VALUE=\"none\">" + LineEnd;
	params += " <param name=\"url\" VALUE=\"\">" + LineEnd;

var t = "<object ID='"+playerId+"' width='320' height='260' " + LineEnd +
	" classid=\""+clid+"\" " + LineEnd +
	" codebase= " + cbase + LineEnd +
	" standby=\"Loading Microsoft Windows Media Player components...\"" +
	" type=\"application/x-oleobject\">" + LineEnd +
	" <param name=\"ShowControls\" value=\"false\">" + LineEnd +
	" <param name=\"EnableContextMenu\" value=\"false\">" + LineEnd +
	" <param name='AutoStart' value='true'>" + LineEnd +
	" <param name='src' value='" + url + "'>" + LineEnd + 
	" <param name='volume' value='100'>" + LineEnd +
	" <param name='WindowlessVideo' value='true'>" + LineEnd +
	" <param name='ShowStatusBar' value='true'>" + LineEnd + params;
	t += "<EMBED type='application/x-mplayer2' name='"+playerId+"' id='"+playerId+"'  pluginspage='http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin' " + LineEnd ;
	t += " width='320' height='260' " + LineEnd;
	t += " autoStart='true' autoStart='true' stretchToFit='true' SRC='" + url + "' AutoSize='false' enableContextMenu='false' ShowControls='true' ShowAudioControls='true' ShowTracker='true' uiMode='true' ></EMBED>" + LineEnd +"</object>";
		return t;
}

function getHTMLForActiveX(playerId) {
	var clid;
	var params = "";
	var LineEnd="\n";

	clid = "CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6";
	cbase = "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=7,0,0,1954";
	params = " <param name=\"stretchToFit\" VALUE=\"true\">" +LineEnd;
	params += " <param name=\"uiMode\" VALUE=\"none\">" + LineEnd;
	params += " <param name=\"url\" VALUE=\"\">" + LineEnd;

	var t = "<object ID='"+playerId+"' width='400' height='300' " + LineEnd +
	" classid=\""+clid+"\" " + LineEnd +
	" codebase= " + cbase + LineEnd +
	" standby=\"Loading Microsoft Windows Media Player components...\"" +
	" type=\"application/x-oleobject\">" + LineEnd +
	" <param name=\"ShowControls\" value=\"false\">" + LineEnd +
	" <param name=\"EnableContextMenu\" value=\"false\">" + LineEnd +
	" <param name='AutoStart' value='true'>" + LineEnd +
	" <param name='volume' value='100'>" + LineEnd +
	" <param name='WindowlessVideo' value='true'>" + LineEnd +
	" <param name='ShowStatusBar' value='false'>" + LineEnd + params;
		t += "<EMBED type='application/x-mplayer2' name='"+playerId+"' id='"+playerId+"'  pluginspage='http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin' " + LineEnd ;
		t += " width='400' height='300' " + LineEnd;
		t += " autoStart='false' stretchToFit='true' AutoSize='false' enableContextMenu='false' ShowControls='false' ShowAudioControls='false' ShowTracker='true' uiMode='none' ></EMBED>" + LineEnd +"</object>";
	//alert(t);
	return t;
};


function getHTMvlc(playerId,url) {
		var LineEnd="\n";
		var t = "<embed id='" + playerId + "' name='" + playerId + "' pluginspage='http://www.videolan.org' type='application/x-vlc-plugin' progid='VideoLAN.VLCPlugin.2' width='400' height='300' MRL='"+url+"'></embed>";
		//alert(t);
		return t;
};
	
function getHTMLelse(playerId,movie) {
	if(movie!=undefined) {
		
		var LineEnd="\n";
		var t;
		t = "<EMBED type='application/x-mplayer2' name='"+playerId+"' id='"+playerId+"'  pluginspage='http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin' " + LineEnd ;
		t += " width='400' height='300' " + LineEnd;
		t += " src='"+movie+"' " + LineEnd;
		t += " autoStart='true' stretchToFit='true' AutoSize='false' enableContextMenu='false' ShowControls='false' ShowAudioControls='false' ShowTracker='true' uiMode='none' ></EMBED>" + LineEnd;
		//alert(t);
		return t;
	};
};