// ihm.js : IHM helpers 

/******************************************************************************
 * class sizeMNG dépendant dev.js (codé+compatibilité le 25/05/2011)
 * permet d'enregistré les fonctions à appeller qui dimensionne les éléments graphiques
 * se mettra habituellement dans body.onresize=monSizeMng.resize()
 ******************************************************************************/
function resizeMng(){
  this.canvasWidth=windowWidth() // to customize
  this.canvasHeight=windowHeight() // to customize
  this.tabFunc=[]
  this.add=function(nameFunc){
		this.tabFunc.push(nameFunc)
  }
  this.resize=function(){
    for(nameFunc in this.tabFunc){
      //alert(this.tabFunc[nameFunc]+'()')
      eval(this.tabFunc[nameFunc]+'()') // play func
    }
  }
}

/******************************************************************************
 * fonction dimFontSize() dépendant navigateur (codé+compatibilité le 22/05/2011)
 * dimensionne la police de caractère de idNode 
 ******************************************************************************/
function dimFontSize(idNode,height){
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.fontSize=height+"px"
  }
}

/******************************************************************************
 * fonction redimWindow() dépendant dev.js(getBrowserRender) (codé+compatibilité le 17/02/2011) 
 * dimensionne l'élement idNode à la largeur de l'écran pratique pour fond
 ******************************************************************************/
function redimWindow(idNode){
  if(document.getElementById(idNode)!=undefined){
  	if(getBrowserRender()!='TRIDENT' ){
  		dimNode(idNode,window.innerWidth,window.innerHeight)
  	}else{ // spé TRIDENT (IE like)
      var niaw=document.documentElement.clientWidth
      var niah=document.documentElement.clientHeight
      dimNode(idNode,document.documentElement.clientWidth,document.documentElement.clientHeight)
  	}
	}
}

/******************************************************************************
 * fonction windowWidth() dépendant dev.js(getBrowserRender) (codé+compatibilité le 17/02/2011) 
 * retourne largeur du navigateur en pixel
 ******************************************************************************/
function windowWidth(){
	var res=0
	if(getBrowserRender()!='TRIDENT' ){
		res=window.innerWidth-1
	}else{ // spé TRIDENT (IE like)
		res=document.documentElement.clientWidth
	}
	return res
}

/******************************************************************************
 * fonction windowHeight() dépendant dev.js(getBrowserRender) (codé+compatibilité le 17/02/2011) 
 * retourne hauteur du navigateur en pixel
 ******************************************************************************/
function windowHeight(){
	var res=0
	if(getBrowserRender()!='TRIDENT' ){
		res=window.innerHeight-1
	}else{ // spé TRIDENT (IE like)
		//res=document.documentElement.clientHeight+1
		res=document.documentElement.clientHeight
	}
	return res
}

/******************************************************************************
 * fonction dimNode() dépendant navigateur (codé le 20 décembre 2010) 
 * dimmensionne l'élement idNode avec largeur(width) hauteur(height)
 ******************************************************************************/ 
function dimNode(idNode,width,height){
  if(document.getElementById(idNode)!=undefined){
    if(width>=0){
      document.getElementById(idNode).style.height=height+"px "
    }
    if(height>=0){
      document.getElementById(idNode).style.width=width+"px "
    }
  }
}

/******************************************************************************
 * fonction absPos() dépendant navigateur (codé le 30 septembre 2010) 
 * place l'élement idNode en absolute par rapport son élément parent
 * au coordonnée left,top     
 ******************************************************************************/  
function absPos(idNode,left,top){
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.position="absolute"
    document.getElementById(idNode).style.top=top+"px "
    document.getElementById(idNode).style.left=left+"px "
  }
}

/******************************************************************************
 * fonction absPos() dépendant navigateur (codé le 30 septembre 2010)
 * place l'élement idNode en absolute par rapport son élément parent
 * au coordonnée left,top
 ******************************************************************************/
function relPos(idNode,left,top){
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.position="relative"
    document.getElementById(idNode).style.top=top+"px "
    document.getElementById(idNode).style.left=left+"px "
  }
}

/******************************************************************************
 * fonction image() dépendant navigateur (codé le 30 septembre 2010) 
 * ajuste l'élement idNode à l'image ...
 * de dimension height, width ...
 * qui se trouve dans l'image d'url pathImg ...
 * qui se trouve au coordonnée offSetX, offSetY ...
 ******************************************************************************/    
function image(idNode,width,height,offSetX,offSetY,pathImg){ 
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.background="url('"+pathImg+"') -"+offSetX+"px -"+offSetY+"px";
    document.getElementById(idNode).style.height=height+"px "
    document.getElementById(idNode).style.width=width+"px "
  }
}

/******************************************************************************
 * Classe NodeExShower dépendant navigateur (14/10/2010 - 30/10/2010)
 * permet d'afficher de façon exclusive les nodes entré grace à la fonction 
 * addNode  
 ******************************************************************************/
function NodeExShower(){
  this.tabNode=[] 
  this.add=function(id){
		this.tabNode[id]="bla"
  }
  this.hideAllNode=function(){
    for(id in this.tabNode){
      if(document.getElementById(id)!=undefined){
        document.getElementById(id).style.visibility="hidden"
      }
    }
  }
  this.show=function(id){ // and hide the other
    this.hideAllNode()
    if(document.getElementById(id)!=undefined){
      document.getElementById(id).style.visibility="visible"
    }
  }
} 

/******************************************************************************
 * Classe DirtNodeExShower dépendant navigateur (30/05/2011)
 * permet d'afficher de façon exclusive les nodes entré grace à la fonction
 * addNode grâce à la propriété display (none ou block)
 * un plus crade que celle avec visibility mais qui à bon goût de ne pas
 * faire scrollé l'écran en cas d'overflow
 ******************************************************************************/
function DirtNodeExShower(){
  this.tabNode=[]
  this.add=function(id){
		this.tabNode[id]="bla"
  }
  this.hideAllNode=function(){
    for(id in this.tabNode){
      if(document.getElementById(id)!=undefined){
        document.getElementById(id).style.display="none"
      }
    }
  }
  this.show=function(id){ // and hide the other
    this.hideAllNode()
    if(document.getElementById(id)!=undefined){
      document.getElementById(id).style.display="block"
    }
  }
}

/*******************************************************************************************************
* Classe Animation dépendant navigateur 
* 10/09/2010 : premier jet                                                                             
* 13/09/2010 : mis en classe (compatible firefox et IE8, opéra, safari, chrome) 
* USAGE exemple : var anim1 = new animation(8,30,30,24,'img1','loading.png','anim1');                  
********************************************************************************************************/
function Animation(nbImg,height,width,cadence,idNode,pathImg,varNameInstance){
  this.numImg=0;        // commence sur la première image
  this.nbImg=nbImg;
  this.offsetX=width;   // succession d'images en ligne 
  this.offsetY=0;       // et pas en colonne
  this.id=idNode;
  this.height=height;
  this.width=width;
  this.isAniming=0;
  this.cadence=cadence; // en img/s
  this.pathImg=pathImg;
  this.varNameInstance=varNameInstance;   
                      
  this.nextImg=function(){ // lit les images en ligne
    document.getElementById(this.id).style.background="url('"+this.pathImg+"') -"+this.numImg*this.offsetX+"px -"+this.offsetY+"px";
    this.numImg++;
    if(this.numImg>=this.nbImg){
      this.numImg=0;
    }
  }
  this.initImg=function(){
     document.getElementById(this.id).style.height=height+"px "
     document.getElementById(this.id).style.width=width+"px "  
     this.playPause()
  }
  this.playPause=function(){
    if (this.isAniming){
      this.isAniming=0;
    }else{
      this.isAniming=1;
      this.anim();
    }
  }
  this.anim=function(){
    this.nextImg();
    if(this.isAniming){  // setTimeout(fonction appelable sans this,durée en ms) et 1000ms=1s
      t=setTimeout(this.varNameInstance+'.anim()',1000/this.cadence);  
    }
  }
  this.initImg();  // go init
}

/*******************************************************************************************************
* function hide dépendant navigateur (codé le 09/11/2010 )
********************************************************************************************************/
function hide(idNode){
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.visibility="hidden"
  }
}

/*******************************************************************************************************
* function show dépendant navigateur (codé le 09/11/2010 )
********************************************************************************************************/
function show(idNode){
  if(document.getElementById(idNode)!=undefined){
    document.getElementById(idNode).style.visibility="visible"
  }
}

/*******************************************************************************************************
* function toogle dépendant navigateur (codé le 09/11/2010 )
********************************************************************************************************/
function toogle(idNode){
  if(document.getElementById(idNode)!=undefined){
  	var d=document.getElementById(idNode).style.visibility
  	if(d.search("visible")){
  		document.getElementById(idNode).style.visibility="hidden"
  	}else{
  		document.getElementById(idNode).style.visibility="visible"
  	}
	}
}
