// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie("mpl").sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	var id = document.getElementById(typ);
	if(typ == "load" && Math.round(pr1) <= 99 ){ id.innerHTML = Math.round(pr1)+"%"; }
	else if(typ == "load" && Math.round(pr1) > 99 ) { id.innerHTML = ""; }
	else if(typ == "time" && Math.round(pr1) < 10){ id.innerHTML = "00:0"+Math.round(pr1); }
	else if(typ == "time" && Math.round(pr1) > 9 && Math.round(pr1) <= 59){ id.innerHTML = "00:"+Math.round(pr1); }
	else if(typ == "time" && Math.round(pr1) > 59 && Math.round(pr1) <= 119){pr3=pr1-60;if(pr3<10){pr3="0"+pr3;}id.innerHTML="01:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 119 && Math.round(pr1) <= 179){pr3=pr1-120;if(pr3<10){pr3="0"+pr3;}id.innerHTML="02:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 179 && Math.round(pr1) <= 239){pr3=pr1-180;if(pr3<10){pr3="0"+pr3;}id.innerHTML="03:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 239 && Math.round(pr1) <= 299){pr3=pr1-240;if(pr3<10){pr3="0"+pr3;}id.innerHTML="04:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 299 && Math.round(pr1) <= 359){pr3=pr1-300;if(pr3<10){pr3="0"+pr3;}id.innerHTML="05:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 359 && Math.round(pr1) <= 419){pr3=pr1-360;if(pr3<10){pr3="0"+pr3;}id.innerHTML="06:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 419 && Math.round(pr1) <= 479){pr3=pr1-420;if(pr3<10){pr3="0"+pr3;}id.innerHTML="07:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 479 && Math.round(pr1) <= 539){pr3=pr1-480;if(pr3<10){pr3="0"+pr3;}id.innerHTML="08:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 539 && Math.round(pr1) <= 599){pr3=pr1-540;if(pr3<10){pr3="0"+pr3;}id.innerHTML="09:"+pr3;}
	else if(typ == "time" && Math.round(pr1) > 599 && Math.round(pr1) <= 659){pr3=pr1-600;if(pr3<10){pr3="0"+pr3;}id.innerHTML="10:"+pr3;}
	else { id.innerHTML = Math.round(pr1); }
	//pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
};

// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie("mpl").loadFile(obj); };
function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }
function removeItem(idx) { thisMovie("mpl").removeItem(idx); }
function getItemData(idx) {
	var obj = thisMovie("mpl").itemData(idx);
	var nodes = "";
	for(var i in obj) { /*
		if(i=='link'){
			document.getElementById('plien').innerHTML = 'Visiter sa page';
			document.getElementById('plien').href = obj[i];
		}else if(i=='title'){
			document.getElementById('ptitre').innerHTML = obj[i];
		}else if(i=='author'){
			document.getElementById('pblog').innerHTML = obj[i];
		}else if(i=='image'){
			document.getElementById('pimg').src = obj[i];
		}else if(i=='id' && obj[i]!=""){
			document.getElementById('pajout').innerHTML = 'Ajouter à ma playlist';
			document.getElementById('pajout').href = 'javascript:addPlaylist('+obj[i]+')';
		}
			
		// nodes += "<li>"+i+": "+obj[i]+"</li>"; 
	*/}
	// document.getElementById("data").innerHTML = nodes;
};

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

function addPlaylist(idFile)
{
	if(document.getElementById("statut")){
		texte = file('/inc/ajax/verifPlaylist.php?id_file=' + idFile);
	
		if (texte.indexOf("true") != -1){
			document.getElementById("statut").innerHTML = "<img src='/img/picto/ok.gif' width='15' height='15' align='absmiddle'/> Ajouté !";
		} else if (texte.indexOf("equal") != -1){
			document.getElementById("statut").innerHTML = "<img src='/img/picto/ko.gif' width='15' height='15' align='absmiddle'/> Déjà existant.";			
		} else if (texte.indexOf("false") != -1){
			document.getElementById("statut").innerHTML = "<img src='/img/picto/ko.gif' width='15' height='15' align='absmiddle'/> <a href='javascript:;' OnClick='window.open(\"/html/identification.php\",\"Identifier\",\"resizable=no,scrollbars=no,width=300,height=300,top=0,left=0\");'>Identifiez-vous</a>";
		} else {
			document.getElementById("statut").innerHTML = "<img src='/img/picto/ko.gif' width='15' height='15' align='absmiddle'/> Une erreur est survenue.";
		}
		
		/* Vu qu'on efface le resultat avec un Fade, on est obligé de le rafficher
		Par une fonction scriptaculous, ici avec un appear */
		Effect.Appear('statut');
		
		/* On efface le resultat au bout de 3 secondes */
		effacer("3000","statut");
	}
}
function file(fichier)
{	
	if(window.XMLHttpRequest) // FIREFOX
	  xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
	  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
	  return(false); 
	xhr_object.open("GET", fichier, false); 
	xhr_object.send(null); 
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);

}

function Annule()
{
	clearTimeout(a);
}

function effacer(Duree,elementid)
{
	/* On lance un effet de scriptaculous, qui doit donc être mis sur la page */
	string="Effect.Fade('"+ elementid +"');";
	a = setTimeout(string,Duree);
}