/*
	____®---------------------------------------------------®
	|##/   Fonctions pour le playeur multimédia ainsi que  /|
	|#/    pour l'assistant de configuration interactif.  /#|
	|/          - Ioan Sameli - 30/07/2003 -             /##|
	®---------------------------------------------------®¯¯¯¯
*/

//*******************************************************//
//      Fonctions pour la détection des paramètres       //
//*******************************************************//

	function cuireBiscuit(indicateur, valeur) {
	// Sauve un cookie chez le client.
	// Passer en paramètre le nom du cookie(indicateur) et sa valeur(valeur).
	// Ioan Sameli, 2003
		var expire = 1000*60*60*24*90; // 3 mois, en millisecondes
		var maintenant = new Date();
		var temps = new Date(maintenant.getTime() + expire);
		document.cookie = indicateur+"="+valeur+"; expires="+temps.toGMTString()+";";
		}

	function mangerBiscuit_old(leQuel){
	// Petite fonction à laquelle on donne le nom d'un biscuit, et elle en retourne la valeur.
	// Vieille version pourrie
	// Ioan Sameli, 2003
		if(!document.cookie) return(null);
		for(var i=0;i<99;i++){
			// IdentStart est le début de l'identifieur.
			// IdentEnd est la fin de l'identifieur, avant le "=" qui le sépare de sa valeur.
			var IdentStart = document.cookie.indexOf(";",IdentEnd)+1;
			var IdentEnd = document.cookie.indexOf("=",IdentEnd+1);

			// Si c'est le premier, il n'y a pas de ";", sinon, il y a un ";" et un espace.
			if (IdentStart > IdentEnd) IdentStart = 0;
			else IdentStart++;

			// Si on a fait le tour de tous les cookies, on arrète.
			if (IdentStart > IdentEnd) return null;

			// On check si la clé actuelle est celle qu'on cherche. Si oui, on la retourne et fini terminé !
			if(document.cookie.substring(IdentStart,IdentEnd)==leQuel){
				// On check la ";" suivante, pour savoir ou s'arrète la valeur recherchée.
				ValueEnd = document.cookie.indexOf(";",IdentEnd);

				// Si c'est la dernière valeur, alors il n'y a pas de ";" et on va jusqu'à la fin
				if(ValueEnd < IdentEnd)ValueEnd = document.cookie.length;

				// retour de la valeur, qui est logiquement comprise entre IdentEnd+1 (à cause du "=") et ValueEnd :
				return(document.cookie.substring(IdentEnd+1,ValueEnd));
				}
			}
		}

	function mangerBiscuit(leQuel){
	// Petite fonction à laquelle on donne le nom d'un biscuit, elle le mange et retourne la valeur qu'il contenait.
	// Ioan Sameli, 2003
		if(!document.cookie) return('pas de cookie');
		var biscuits   = document.cookie;
		if (biscuits != ""){
			liste = biscuits.split(";");
			for (i=0;i<=liste.length-1;i++){
				temp = liste[i].split("=");
				if(temp[0].replace(/\s/,'')==leQuel) return(temp[1]);
				}
			return null;
			}
		else{
			return null;
			}
		}

	function getURLParam(leQuel){
	// récupère le paramètre demandé dans l'url
	// on peut passer en deuxième paramètre une autre URL (si par exemple on est dans des frames)
	// Ioan Sameli, 2003
		var url, args=getURLParam.arguments;

		if(args.length != 1) url = args[1];
		else url = window.location.search;

		if (url!=''){
			url=url.substring(1,url.length);
			liste=url.split("&");
			for (i=0;i<=liste.length-1;i++){
				temp=liste[i].split('=');
				if(temp[0]==leQuel)return(temp[1]);
				}
			return(null);
			}
		else return(null);
		}

	function realCheat(){
		cuireBiscuit('realcheat',1);
		if(realCheat.arguments.length==0)history.back();
		}

	function setFormSelection(RadioToCheck){
		// preSelect un des deux bouton radios gros/petit player.
		btnRadio = document.getElementById('playerMode'+RadioToCheck);
		btnRadio.checked=true;
		}
	function saveSettings(){
	// Enregistre les préférences du user dans des biscuits, pour les manger plus tard.
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;
	safari='';
	if (checkIt('msie')) browser = "Internet Explorer"
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('opera')) browser = "Opera"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "unknown";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('win')) OS = "Windows"
		else if (checkIt('mac')) OS = "Mac"
		else OS = "unknown";
	}

	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}
	if (OS=='Windows'){
		switch (browser){
			case "Internet Explorer":
				myBrowser='IE';
			break;
			case "Netscape Naviguator":
				myBrowser='NS';
			break;
			case "Opera":
				myBrowser='NS';
			break;
			default:
				myBrowser='NS';
			break;
		}
	}else if (OS=='Mac'){
		myBrowser='NS';
		if (browser=='Safari'){
			safari='true';
		}
	}else{
		myBrowser='NS';
	}


		btnRadio1 = document.getElementById('playerMode1');
		btnRadio2 = document.getElementById('playerMode2');
		btnRadio3 = document.getElementById('playerMode3');
		if(myBrowser=='NS'){
			cuireBiscuit('netscapeCompatible',1);
			if(safari=='true'){
				cuireBiscuit('safari',1);
			}else{
				cuireBiscuit('safari','');
			}
		}else{
		    cuireBiscuit('netscapeCompatible','');
		}


		//chkBox = document.getElementById('netscapeCompatible1');
		//if(chkBox.checked) cuireBiscuit('netscapeCompatible',1);
		//else cuireBiscuit('netscapeCompatible','');

		if(btnRadio1.checked) tmpValue=1;
		else if(btnRadio2.checked) tmpValue=2;
		else tmpValue=3;

		cuireBiscuit('playerMode',tmpValue);
		cuireBiscuit('all_Ok',1);
		}

	function gotoPlayer50(){
		if(arguments.length)saveSettings();

		var tmpSizeParam = '';

		if(currentVideoFormat > 1 && mangerBiscuit('playerMode') > 1){
			if(mangerBiscuit('playerMode')==3)tmpSizeParam = '&format=450';
			else tmpSizeParam = '&format=160';
			}

		if(getURLParam('vid')){
			tmpSizeParam = tmpSizeParam + '&vid='+getURLParam('vid');
			}

		tmpHref = 'index.html?siteSect=500080&bcid='+currentBroadcastID+tmpSizeParam;
		if(getURLParam('openwin')) self.location.href=tmpHref;
		else{
			open_win(tmpHref,'',340,450,false);
			ForceCloseWin();

		}
	}
	function gotoPlayerNouvo(){
		if(arguments.length)saveSettings();

		var tmpSizeParam = '1';

		if(currentVideoFormat > 1 && mangerBiscuit('playerMode') > 1){
			if(mangerBiscuit('playerMode')==3)tmpSizeParam = '3';
			else tmpSizeParam = '2';
			}

		tmpHref = getURLParam('nouvo_video_url');
		if (tmpHref.replace('$format$','$format$')){
			tmpHref = tmpHref.replace('$format$',tmpSizeParam)
		}
		self.location.href= 'http://'+tmpHref;
	}
	function gotoPlayerArchives(){
		if(arguments.length)saveSettings();

		var tmpSizeParam = '1';

		if(currentVideoFormat > 1 && mangerBiscuit('playerMode') > 1){
			if(mangerBiscuit('playerMode')==3)tmpSizeParam = '3';
			else tmpSizeParam = '2';
			}

		tmpHref = getURLParam('archives_video_url');
		if (tmpHref.replace('$format$','$format$')){
			tmpHref = tmpHref.replace('$format$',tmpSizeParam)
		}
		self.location.href= 'http://'+tmpHref;
	}
	function gotoPlayer(){
	// fonction qui récupère tous les paramètre dans les biscottes et tout, les fout en vrac dans l'URL et redirect enfin vers le player désiré...
	// Ioan Sameli, 2003
		if(arguments.length)saveSettings();

		// Si les préfèrences sont grand format et si la vidéo est dispo en grand format, on met l'adresse du bigplayer, sinon celle du petit.
		var tmpSizeParam = '';

		// Si on a spécifié un fichier précis, on vérifie que tout est ok et on le repasse plus loin...
		// On passe aussi l'autre fichier au cas oû le gars veut switcher entre [grand format/petit format]
		if(currentVideoFormat > 1 && mangerBiscuit('playerMode') > 1){
			if(mangerBiscuit('playerMode')==3)tmpSizeParam = '&format=450';
			else tmpSizeParam = '&format=160';
			}

		if(getURLParam('vid')){
			tmpSizeParam = tmpSizeParam + '&vid='+getURLParam('vid');
			}

		tmpHref = 'index.html?siteSect=500002&bcid='+currentBroadcastID+tmpSizeParam;

		// Si ça doit être compatible avec netscape parce que le gars a un ordi pourri :
		if(mangerBiscuit('netscapeCompatible')){
			tmpHref = tmpHref+'&netscapeCompatible=true';
			}
		if(mangerBiscuit('safari')){
			tmpHref = tmpHref+'&safari=true';
			}

		// si le paramètre est présent, on le repasse plus loin...
		if(getURLParam('logo')){
			tmpHref = tmpHref+'&logo='+getURLParam('logo');
			}

		if(getURLParam('openwin')) self.location.href=tmpHref;
		else{
			open_win(tmpHref,'',691,400,false);
			ForceCloseWin();
			}
		}

	// Determines si un Real Player superieur au G2 Gold est disponible
	// basé sur le détecteur disponible sur http://1ppl.free.fr/index_real.html
	// retourne 2 si ce n'est pas internet explorer
	// Ioan Sameli, 2003
	function getReal() {
		if (testRealActiveX())return true;
		if (checkPlugIn("real"))return 2;
		if(mangerBiscuit('realcheat')){
			if(document.all)return true;
			return 2;
			}
		return false;
		}

	///// ForceCloseWin 	=> fermer une fenetre sans message de confirmation
	function ForceCloseWin (){
		win = top;
		win.opener = top;
		win.close ();
		}

	// Essaie de créer un ActiveX Real et d'en récuperer la version, ne marche que sous IE
	function testRealActiveX(){
	// Ioan Sameli, 2003

		try {
			testObject = new ActiveXObject("rmocx.RealPlayer G2 Control.1");

			// Since G2 (Real 6), all Real player version numbers begin by 6
			embedVersion = testObject.GetVersionInfo();

			// First version supported : 6.0.6.131 = G2 Gold
			versionArray = embedVersion.split(".");
			conditionA = versionArray[0]>=6;
			conditionB = versionArray[1]>=0;
			conditionC = versionArray[2]>=6;
			conditionD = versionArray[3]>=131;

			if (conditionA && conditionB && conditionC && conditionD)return true;

			return false;
			}
		catch(e) {
			return false;
			}
		}


	// recherche dans la collection de plugins avec les strings passés en paramètres
	// Par exemple : "Real","Flash", etc... ne fonctionne point sous IE, donc si ça marche on est forcément sous autre chose....
	// Ioan Sameli, 2003
	function checkPlugIn() {

		// si le navigateur (IE notamment) ne supporte pas cette méthode
		if(! navigator.plugins.length)return false;

		// initialisation des variables
		allFound = false;
	    plugInsCollection = navigator.plugins;

		// Pour chaque plugin trouvé, on regarde si il contient tous les arguments passés à la fonction
		for (i=0;i<plugInsCollection.length;i++) {

			// On passe tout en majuscule pour ne pas être embêté par des bêtises
	        plugInDescription = " " + (plugInsCollection[i].description.toUpperCase());
			plugInName = " " + (plugInsCollection[i].name.toUpperCase());

			// on cherche chaque arguments
			for (j=0;j<arguments.length;j++) {
				if (plugInDescription.indexOf(" " + arguments[j].toUpperCase())!=-1 || plugInName.indexOf(" " + arguments[j].toUpperCase())!=-1) {
					allFound = true;
					}
				else{
					if(allFound!=true)allFound = false;
					}
				}
			if (allFound)return true;
	   	 	}
		return false;
		}


	function upsize() {
		// aller en double taille

		document.getElementById('btn1x').className = 'bar';
		document.getElementById('btn2x').className = 'bars';

		if(theFormat=='Medium'){
			//adslUpsize();
			document.video.height=360;
			document.video.width=480;
			//window.top.resizeTo(811,495);
			document.getElementById('summary').height = document.getElementById('tedep').scrollHeight-66;
			if (hideShowActive == 0 && !mangerBiscuit('netscapeCompatible')){
				window.resizeTo(document.getElementById('mainTable').scrollWidth-463,document.getElementById('mainTable').scrollHeight+31);
			}else{
				window.resizeTo(document.getElementById('mainTable').scrollWidth+12,document.getElementById('mainTable').scrollHeight+31);
			}
		}
		else{
			document.video.height=240;
			document.video.width=320;
			//window.top.resizeTo(811,495);
			document.getElementById('summary').height = document.getElementById('tedep').scrollHeight-66;
			if (hideShowActive == 0 && !mangerBiscuit('netscapeCompatible')){
				window.resizeTo(document.getElementById('mainTable').scrollWidth-463,document.getElementById('mainTable').scrollHeight+31);
			}else{
				window.resizeTo(document.getElementById('mainTable').scrollWidth+12,document.getElementById('mainTable').scrollHeight+31);
			}
			}
		}

	function downsize(){
		// aller en taille normale
		document.getElementById('btn1x').className = 'bars';
		document.getElementById('btn2x').className = 'bar';
		if(theFormat!='Medium'){
			document.video.height=144;
			document.video.width=192;
			}
		else{
			document.video.height=240;
			document.video.width=320;
			}
		document.getElementById('summary').height = document.getElementById('tedep').scrollHeight-66;
		if (hideShowActive == 0 && !mangerBiscuit('netscapeCompatible')){
			window.resizeTo(document.getElementById('mainTable').scrollWidth-463,document.getElementById('mainTable').scrollHeight+31);
		}else{
			window.resizeTo(document.getElementById('mainTable').scrollWidth+12,document.getElementById('mainTable').scrollHeight+31);
		}
		}

	function invertFilesInURL(){
		//retourne la current URL du player avec juste les paramètres "otherfile" et "file" intervertis (enfin, leurs valeurs, quoi...)
		// Si il n'y a pas de paramètre "otherfile" dans l'url, la fonction renvoie null.
		// Ioan Sameli, 2003

		if(getURLParam('otherfile')){
			var tmpURL = 'index.html?siteSect=500002&bcid='+getURLParam('bcid',window.top.location.search)+'&file='+getURLParam('otherfile',window.top.location.search)+'&otherfile='+getURLParam('file',window.top.location.search);
			return tmpURL;
			}
		else return null;
		}

  /**********************************/
 /*** Fonctions plus utilisées (?) */
/**********************************/

	function goBigPlayer(){
		// ouverture player haut débit 2x
		alert('goBigPlayer : Fonctions plus utilisée');
		window.top.resizeTo(643,820);
		window.top.navigate("player_adsl2x.html");
		}
	function go56k(){
		// ouverture player bas débit
		alert('go56k : Fonctions plus utilisée');
		window.top.resizeTo(691,400);
		window.top.navigate("player_56k.html");
		}
	function goAdsl(){
		// On va vers le player en grand.
		alert('goAdsl : Fonctions plus utilisée');
		if(getURLParam('big')){
			window.top.resizeTo(821,495);
			window.top.navigate(window.top.location.search+"&format=160");
			}
		else{
			alert("cette séquence n'est pas disponible en grand format.");
			}
		}