// 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, width_img, height_img, opacity){
	//this.id=id;
	Player.prototype = new Opacity;
	this.dir_img=dir_img;
	this.tab_img=tab_img;
	this.width_img=width_img;
	this.height_img=height_img;
	this.tab_alt;
	this.id_alt;
	this.tab_lien;
	this.loop=true;
	this.duree_fondu=1;
	this.imgCourante=0;
	if(opacity==undefined) opacity=1;
	this.id_div_loupe="loupe__diapo__";

	this.superClass = Opacity; 
	this.superClass(id, opacity);
	
	this.idDivButtons;
	this.buttonPlay;
	this.buttonPause;
	this.buttonStop;
	this.idInfo;
	this.idLoading;
	this.loadingText;
	this.nameBorderMini;
	this.typeBorderMini;
	this.zoom=false;
	this.zoomDossier;
	this.urlClose;

	
	if(document.all) {
		document.getElementById(this.id).style.zoom=1; /// pr que opacité marche sous IE
	}
	
	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.initLien = function (tab_lien){
	this.tab_lien=tab_lien;
}


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 onclick='';
	if(this.zoom==true){
		var image=this.tab_img[this.imgCourante];
		var width=this.width_img[this.imgCourante];
		var height=this.height_img[this.imgCourante];
		/*onclick="openDiv(\''+this.zoomDossier+image+'\', \''+this.urlClose+'\')"*/
			onclick=' style="cursor:pointer; height:'+height+'px;width:'+width+'px;" class="mid" ';
	}
	if(this.tab_lien!=undefined){
		/* onclick="window.open(\''+this.tab_lien[pos_img]+'\');"*/
		onclick=' style="cursor:pointer; height:'+height+'px;width:'+width+'px;"  class="mid"';
	}
	
	var img='<img'+onclick+' 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]);
	}
	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();this.addBorderMini(this.imgCourante);}}
		else{
			this.fonduAllerFini2=this.fonduAllerFini;
			this.fonduAllerFini=function(){
				this.fonduAllerFini2();
				this.addBorderMini(this.imgCourante);
			}
		}
	}
	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)+' / '+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,long,haut){
	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='';
		if(this.tab_img.length>1){
			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;
				if((i+1)%4==0 && i!=0) inner+='</tr><tr align="center" valign="middle">';
			}
			document.getElementById(id).innerHTML=text_before_b+inner+text_after_b;
		}
	}
}

Player.prototype.initBorderMini = function(name, type){
	this.nameBorderMini=name;
	this.typeBorderMini=type;
	this.addBorderMini(0);
}
Player.prototype.addBorderMini = function(numImg){
	if(this.nameBorderMini!=undefined) {
		if(document.getElementById('__mini'+numImg+'__')){
			var d=document.getElementById('__mini'+numImg+'__').parentNode.parentNode;
			switch(this.typeBorderMini){
				case 'id' :
					for(var i=0; i<this.tab_img.length; i++){
						var d2=document.getElementById('__mini'+i+'__').parentNode.parentNode;
						d2.id='';
					}
					d.id=this.nameBorderMini;
				break;
						
				case 'class' :
					for(var i=0; i<this.tab_img.length; i++){
						var d2=document.getElementById('__mini'+i+'__').parentNode.parentNode;
						d2.className='';
					}
					d.className=this.nameBorderMini;
				break;
			}
		}
	}	
}

Player.prototype.initLoupe = function(dossier_img, id_div){
	var niouDiv=document.createElement("div");
	if(id_div==undefined)id_div=this.id_div_loupe;
	else{this.id_div_loupe=id_div;}
	niouDiv.id=id_div;
	niouDiv.style.position="absolute";
	niouDiv.style.visibility="hidden";
	document.body.appendChild(niouDiv);
	var localThis=this;
	
	var jsLib=new SuperClass();
	jsLib.addEvent(document, "mousemove", 
		function(event){
			var tab_coord=new Array();
			tab_coord=jsLib.getMouseCoords(event);
			var x=tab_coord[0];
			var y=tab_coord[1];
			
			tab_coord=jsLib.getScrollXY();
			x+=tab_coord[0];
			y+=tab_coord[1];
			
			var niouImg=document.createElement("img");
			niouImg.src=dossier_img+localThis.tab_img[localThis.imgCourante];
			jsLib.verifChargementImg(niouImg, function(){
				var daImg=document.getElementById(localThis.id).getElementsByTagName("img")[0];
				
				var pos_x=jsLib.calculeOffsetLeft(daImg);
				var pos_y=jsLib.calculeOffsetTop(daImg);
				
				
				if(x<pos_x || x>pos_x+daImg.width || y<pos_y || y>pos_y+daImg.height) {niouDiv.style.visibility="hidden";}
				else{		
					var ratio=niouImg.width/daImg.width;	
					niouDiv.style.backgroundImage="url("+dossier_img+localThis.tab_img[localThis.imgCourante]+")";
				
					niouDiv.style.visibility="visible";
					var w=niouDiv.offsetWidth;
					var h=niouDiv.offsetHeight;
					w=Math.round(w/2);
					h=Math.round(h/2);
					
					var pos_x=x-pos_x;
					var pos_y=y-pos_y;
					
					
					niouDiv.style.backgroundPosition = ((pos_x-(w/ratio))*(-ratio))+"px "+((pos_y-(h/ratio))*(-ratio))+"px";				
					
					niouDiv.style.top=y-h+"px";
					niouDiv.style.left=x-w+"px";
								}
			});
			
			
		}
	);
	
	jsLib.addEvent(document.getElementById(this.id), "mouseover", function(){niouDiv.style.visibility="visible";});
	//jsLib.addEvent(document.getElementById(this.id).parentNode, "mouseout", function(){niouDiv.style.visibility="hidden";});
	
	
}

Player.prototype.goTo = function(numImg){
	//this.fonduAllerFini='';
	this.fonduRetourFini='';
	this.clearAllTimeout();
	this.global_play=true;
	var localPas=global_pas;
	if(this.idLoading!=undefined) this.removeLoading();
	
	//if(this.opa==0) this.opa=1;
	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.initZoom = function(id_lien, url_dossier_img, urlImgClose){
	this.zoom=true;
	this.zoomDossier=url_dossier_img;
	this.urlClose=urlImgClose;
	if(document.getElementById(id_lien)){
		var localThis=this;
		document.getElementById(id_lien).onclick=function(){
			image=localThis.tab_img[localThis.imgCourante];
			openDiv(url_dossier_img+image, urlImgClose);
			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);

}