// JavaScript Document
/*
Book.prototype = new Item; 
function Book(id, price, title) { 
this.super = Item; 
this.super(id, price); 
this.title = title; 
}*/
var idObjet = 0;

Player.prototype = new Opacity;
function Player(id, dir_img, tab_img, opacity){
	//this.id=id;
	this.dir_img=dir_img;
	this.tab_img=tab_img;
	this.tab_alt;
	this.id_alt;
	this.loop=true;
	this.duree_fondu=1;
	this.imgCourante=0;
	if(opacity==undefined) opacity=1;

	this.superClass = Opacity; 
	this.superClass(id, opacity);
	
	this.idDivButtons;
	this.buttonPlay;
	this.buttonPause;
	this.buttonStop;
	this.idInfo;
	this.idLoading;
	this.loadingText;
	
	idObjet++; 
 	this.idRef = "Player" + idObjet; 
  	window[this.idRef] = this;  
}

Player.prototype.initAlt = function (tab_alt, id_alt){
	this.tab_alt=tab_alt;
	if(id_alt!=undefined) this.id_alt=id_alt;
}


Player.prototype.verifChargement = function (id){
	var i=0;
	if(id==undefined) id=this.id;
	var el = document.getElementById(id);
    var img = el.getElementsByTagName("img")[0];
		
	if(document.getElementById(img.id).complete == true ){
		if(this.chargementFini)	this.chargementFini();
	}
	else{
		if(document.getElementById(img.id).complete == undefined){
			if(this.chargementFini) {
				var localThis = this;
				img.onload = function(){localThis.chargementFini()};
			}
		}
		else{
			var localThis = this;
			this.timeout[this.timeout.length]=window.setTimeout(function(){localThis.verifChargement()}, 10);
		}
	}
}


Player.prototype.writeImg = function(pos_img){ //& this.tab_alt[pos_img]!=''
	var alt=' alt="" ';
	if(this.tab_alt!=undefined){
		if(this.tab_alt.length>0) alt=' alt="'+this.tab_alt[pos_img]+'" ';
	}
	var img='<img id="img'+this.id+pos_img+'"'+alt+'src="'+this.dir_img+this.tab_img[pos_img]+'" />';
	document.getElementById(this.id).innerHTML=document.getElementById(this.id).innerHTML+img;
}

Player.prototype.writeText = function(text, id){
	if(id==undefined) id=this.id;
	if(document.getElementById(id)) document.getElementById(id).innerHTML=document.getElementById(id).innerHTML+text;	
}

Player.prototype.play = function(delai, pas, loop, duree_fondu,numImg){
	if(loop!=undefined) this.loop=loop;
	if(duree_fondu!=undefined) this.duree_fondu=duree_fondu;
	
	this.reset();
	if(this.tab_alt!=undefined){
		if(this.id_alt!=undefined) {
			this.reset(this.id_alt);
			this.writeText(this.tab_alt[this.imgCourante], this.id_alt);
		}
		else this.writeText(this.tab_alt[this.imgCourante]+'<br />');
	}
	this.writeImg(this.imgCourante);
	
	
	this.chargementFini=function() { 
		this.playImg(delai, pas); 
		if(this.idLoading!=undefined) this.removeLoading();
	}
	if(this.idLoading!=undefined) this.addLoading();
	this.verifChargement();
	this.fonduRetourFini= function(){
		this.switchImg(numImg);
	}
}


Player.prototype.playImg = function(delai, pas, numImg){
	if(this.tab_img.length>1 && ((((this.imgCourante+1) < this.tab_img.length) && this.loop==false) || this.loop==true) && this.global_play==true) {
		//if(numImg!=undefined) this.playImg=function(){this.switchImg(numImg);}
		this.fondu_aller_retour(delai, pas);
		if(delai==null) this.fonduAllerFini=function(){this.pause();}
	}
	else {
		if(this.global_play==true) this.fonduAllerFini='';
		this.global_play=true;
		//if(delai==null) this.fonduAllerFini=function(){this.switchImg(numImg);}
		/*if(numImg!=undefined) this.fonduAllerFini=function(){this.switchImg(numImg);};*/
		this.fondu(pas)
		//if(numImg!=undefined) this.fonduRetourFini=function(){this.switchImg(numImg);}
	}
}

Player.prototype.reset = function(id){
	if(id==undefined) id=this.id;
	document.getElementById(id).innerHTML='';	
}

Player.prototype.switchImg = function(numImg){
	if(numImg==undefined || numImg>=this.tab_img.length || numImg<0){
		if(this.tab_img.length >(this.imgCourante+1)){
			this.imgCourante++;
		}
		else{
			this.imgCourante=0;
		}
	}
	else this.imgCourante=numImg;
	this.play(this.delai, this.pas);
	if(this.idInfo!=undefined) this.info(this.idInfo);
}

Player.prototype.pause = function(){
	this.global_play=false;
	this.clearAllTimeout();
	//this.setOpacity(1);
}

Player.prototype.stop = function(){
	this.clearAllTimeout();
	//this.global_play=false;
	this.reset();
	if(this.id_alt!=undefined) this.reset(this.id_alt);
	this.imgCourante=0;
	this.writeImg(this.imgCourante);
	this.setOpacity(0);
}

Player.prototype.initLoading = function(id,text){
	if(text==undefined) text='<img src="'+this.dir_img+'loading.gif" alt="" />';
	this.loadingText=text;
	this.idLoading=id;
	text='<span id="__'+this.id+'loading_player__"></span>';
	this.writeText(text, id);
}

Player.prototype.addLoading = function(){
	this.writeText(this.loadingText, '__'+this.id+'loading_player__');
	//document.getElementById(this.id).parentNode.innerHTML=text+document.getElementById(this.id).parentNode.innerHTML;
}

Player.prototype.removeLoading = function(){
	this.reset('__'+this.id+'loading_player__');
}

Player.prototype.info = function(id){
	this.idInfo=id;
	this.reset(id);
	this.writeText('Image '+(this.imgCourante+1)+' sur '+this.tab_img.length, id);
}

Player.prototype.initButtons = function(id){
	this.idDivButtons=id;
}

Player.prototype.initButtonPlayPause = function(url_play, url_pause){
	var textPlay;
	var textPause;
	if(url_play==undefined) textPlay='Lecture';
	else textPlay='<img alt="Lecture" src="'+url_play+'" />';
	
	if(url_play==undefined) textPause='Pause';
	else textPause='<img alt="Pause" src="'+url_pause+'" />';
	
	this.buttonPlay='<a id="'+this.id+'__butplay__" href="javascript:void(0);" onclick="window[\''+this.idRef+'\'].setPlay();" title="Lecture">'+textPlay+'</a>';
	this.buttonPause='<a href="javascript:void(0);" title="Pause" onclick="window[\''+this.idRef+'\'].setPause();">'+textPause+'</a>';
	this.writeText('&nbsp;<span id="__'+this.id+'buttonplaypause__"></span>&nbsp;', this.idDivButtons);
}

Player.prototype.addButtonPlay = function(){
	this.reset('__'+this.id+'buttonplaypause__');
	this.writeText(this.buttonPlay, '__'+this.id+'buttonplaypause__');
}

Player.prototype.addButtonPause = function(){
	this.reset('__'+this.id+'buttonplaypause__');
	this.writeText(this.buttonPause, '__'+this.id+'buttonplaypause__');
}

Player.prototype.setPlay = function(){
	this.addButtonPause();
	if(this.imgCourante==0 && this.id_alt!=undefined) {
			this.reset(this.id_alt);
			this.writeText(this.tab_alt[this.imgCourante], this.id_alt);
	}
	this.playImg(this.delai, this.pas);
}

Player.prototype.setPause = function(){
	this.addButtonPlay();
	this.pause();
}

Player.prototype.initButtonStop = function(url){
	var textStop;
	if(url==undefined) textStop='Stop';
	else textStop='<img alt="Stop" src="'+url+'" />';

	this.buttonStop='<a id="'+this.id+'__butstop__" href="javascript:void(0);" onclick="window[\''+this.idRef+'\'].setStop();" title="Stop">'+textStop+'</a>';
	this.writeText('&nbsp;<span id="__'+this.id+'buttonstop__"></span>&nbsp;', this.idDivButtons);
}

Player.prototype.addButtonStop = function(){
	this.reset('__'+this.id+'buttonstop__');
	this.writeText(this.buttonStop, '__'+this.id+'buttonstop__');
}

Player.prototype.setStop = function(){
	this.global_play=true;
	this.stop();
	this.addButtonPlay();
}


Player.prototype.initMini = function(dir_mini, id, text_before_b, text_after_b,text_before, text_after){
	if(document.getElementById(id)) {
		if(text_before_b==undefined) text_before_b='';
		if(text_after_b==undefined) text_after_b='';
		if(text_before==undefined) text_before='';
		if(text_after==undefined) text_after='';
		
		var alt='';
		var inner='';
		var img='';
		for(var i=0; i<this.tab_img.length; i++){
			if(this.tab_alt!=undefined) alt=' '+this.tab_alt[i];
			if(dir_mini=='' || dir_mini==undefined) img=i;
			else img='<img id="__mini'+i+'__" alt="Voir l\'image'+alt+'" src="'+dir_mini+this.tab_img[i]+'" />';
			inner+=text_before;
			if(this.tab_img.length>1) inner+='<a href="javascript:void(0);" onclick="window[\''+this.idRef+'\'].goTo('+i+')">';
			inner+=img;
			if(this.tab_img.length>1) inner+='</a>';
			inner+=text_after;
		}
		document.getElementById(id).innerHTML=text_before_b+inner+text_after_b;
	}
}


Player.prototype.goTo = function(numImg){
	this.fonduRetourFini='';
	this.clearAllTimeout();
	this.global_play=true;
	var localPas=global_pas;
	if(this.idLoading!=undefined) this.removeLoading();
	
	this.fondu (0.1, 0, this.opa, 'max');
	this.fonduAllerFini= function(){
		this.pas=localPas;
		this.switchImg(numImg); 
	}
}

Player.prototype.initPrec = function(id_prec){
	if(document.getElementById(id_prec)){
		var localThis=this;
		document.getElementById(id_prec).onclick=function(){
			image=localThis.imgCourante;
			if(image!=0){image--;}
			else{image=localThis.tab_img.length-1;}
			localThis.goTo(image);
			return false;
		}
	}
}

Player.prototype.initSuiv = function(id_prec){
	if(document.getElementById(id_prec)){
		var localThis=this;
		document.getElementById(id_prec).onclick=function(){
			image=localThis.imgCourante;
			if(image!=((localThis.tab_img.length)-1)){image++;}
			else{image=0;}
			localThis.goTo(image);
			return false;
		}
	}
}


Player.prototype.debug = function(id){
document.getElementById(id).innerHTML='global play:'+this.global_play+' &nbsp; opacity:'+this.opa+' &nbsp; pas:'+this.pas+' imgcourante:'+(this.imgCourante+1)+' &nbsp; delai:'+this.delai+' &nbsp;<br />opacite_depart:'+this.o_depart+' &nbsp; opamin:'+this.o_min+' &nbsp; opamax:'+this.o_max;
var localThis = this;
window.setTimeout(function(){localThis.debug(id)}, 0);

}
