// requière tag.js

/*
*Script de gestion de la visionneuse mosaic_report 
*/

// délai du timer du diaporama
var PLAY_DELAY=3;

/*
* Déclaration de l'objet Diaporama
* @author abe
*/
function Diaporama(name,Resolution,auto_resize)
{
   //indice de l'image courante
   this.m_currentImg=0;
   //délai du timer pour diaporama
   this.m_delay=PLAY_DELAY;
   //tableau contenant les images chargées dans la frame de gauche
   this.m_tabImg= document.getElementsByName("img_report");
   //longueur du tableau d'images
   this.m_nbImg=this.m_tabImg.length;
   //variable de stockage du timeout
    this.autoPlay;
    //si isPLay  est true > le diaporama est actif
    this.isPlay=false;
    //nom de la variable (Diaporama_MANAGER)
    this.name=name;
        if(Resolution==null) this.resolution="MEDIUM";
        else this.resolution=Resolution;
 
    this.CUR_ID=1;
    /*
	modif abe 27/10/06 resize auto 
	*/
    this.autoResize=auto_resize;
    
   return this;
}

/**
*  chargement de l'objet Diaporama on charge la première image
*
 */
Diaporama.prototype.start = function ()
{
   this.changeStyle();
   if(this.m_tabImg) this.loadFrame(this.m_tabImg[this.m_currentImg].id);
   return null;
}
/**
*  chargement de l'image suivante
*
 */
Diaporama.prototype.next = function ()
{
   this.m_currentImg++;
   if(this.m_currentImg>=this.m_nbImg) this.m_currentImg=0;
   //chargement de l'image
   this.loadFrame(this.m_tabImg[this.m_currentImg].id);
   if( this.isPlay)       this.autoPlay = setTimeout(this.name + ".next(" + this.m_delay + ")", this.m_delay*1000);
   //changement de style
   return null;
}
/**
*  chargement de l'image précédente
*
 */
Diaporama.prototype.previous = function ()
{
   this.m_currentImg--;
   if(this.m_currentImg<0) this.m_currentImg=this.m_nbImg-1;
   this.loadFrame(this.m_tabImg[this.m_currentImg].id);
   return null;
}
/**
*  lecture du diaporama
*
 */
Diaporama.prototype.play = function ()
{
   this.autoPlay = setTimeout(this.name + ".next(" + this.m_delay + ")", this.m_delay*1000);
   this.isPlay=true;
}
/**
*  arrêt du diaporama
*
 */
Diaporama.prototype.stop = function ()
{
   //lancer next en boucle avec autom_delay
    this.isPlay=false;
    this.autoPlay=null;
}

/**
*  chargement de l'image dans la frame droite
*
 */
Diaporama.prototype.loadFrame = function (id,urlComplete,isApplet)
{  

   if(id!=null) this.CUR_ID=id;
   var elementTitle=document.getElementById(this.CUR_ID).getAttribute("title");
   var elementDesc=document.getElementById(this.CUR_ID).getAttribute("desc");
   var TitleContainer=document.getElementById("TitleContainer");
   if(TitleContainer!=null && elementTitle!="")TitleContainer.innerHTML=elementTitle+" ("+elementDesc+") ";
   /*
   url="servlet/DocumentFileManager?";
   url+="recordId="+RECORD_ID;
   url+="&source=";
   url+="&document="+this.CUR_ID;
   url+="&file=";
   url+="&resolution=HIGH";
   if(urlComplete!=null) url+=urlComplete;
*/
   if(urlComplete!=null)
      urlComplete+="&timeStamp="+new Date().getTime();
   else
      urlComplete="&timeStamp="+new Date().getTime();
   url="servlet/PhotoManager?";
   url+="recordId="+RECORD_ID;
   url+="&idocsId="+this.CUR_ID;
   url+="&resolution="+this.resolution;
   if(urlComplete!=null) url+=urlComplete;
   
   if(!isApplet){
   	/*
	modif abe 27/10/06 resize auto 
	*/
    if(this.autoResize) 
    {
    	var img = new Tag("img").att("src", url)
    	.att("border", 0).att("id","imgtoResize")
    	.att("onLoad","javascript:resizeImg(this);");
    }
    else  var img = new Tag("img").att("src", url).att("border", 0);
    IMG_CONTAINER.innerHTML=img.text();
    if(!document.all && this.autoResize) document.getElementById("imgtoResize").style.display="none";
    }
    else
    {
    	url=urlComplete;
    	url+="&document="+this.CUR_ID;
    var height=this.resize('height') + 80;
      var width=this.resize('width') + 350;
    var iframe = new Tag("iframe").att("src", url).att("class", "iframeDiapo").att("height",height).att("width",width);
        IMG_CONTAINER.innerHTML=iframe.text();
    }
    this.changeStyle();
}

/**
*  chargement de l'image quand clic sur l'iamge par l'utilisateur
*  url: (pru) permet d'effectuer des transformation dès le chargement de l'image
 */
Diaporama.prototype.chooseFrame = function (id,url)
{
   this.loadFrame(id,url);
      for(var i=0;i<this.m_nbImg;i++)
      {
         if (this.m_tabImg[i].id==id) this.m_currentImg=i;
      }
    this.changeStyle();
}

/**
*  chargement du style de l'image sélectionnée
*
 */
Diaporama.prototype.changeStyle= function ()
{
   var cur_img=this.m_tabImg[this.m_currentImg];
   //var text= document.getElementById("text-"+cur_img.id);
   //var container=document.getElementById("container-"+cur_img.id);
   
   //this.moveElementTop(container);
   for(var i=0;i<this.m_nbImg;i++)
   {
   if (document.all) 
   {
   this.m_tabImg[i].setAttribute("className","imgReportNone");
  // document.getElementById("text-"+this.m_tabImg[i].id).setAttribute("className","textLeftContainerOff");
  // document.getElementById("container-"+this.m_tabImg[i].id).setAttribute("className","imageLeftContainerOff");
   }
   else
   {
     this.m_tabImg[i].setAttribute("class","imgReportNone");
 //  document.getElementById("text-"+this.m_tabImg[i].id).setAttribute("class","textLeftContainerOff");
  // document.getElementById("container-"+this.m_tabImg[i].id).setAttribute("class","imageLeftContainerOff");
   }
   }
   //test pour IE pour attribué la classe 
   if (document.all) 
   {
      cur_img.setAttribute("className","imgReport");
     // text.setAttribute("className","textLeftContainerOn");
     // container.setAttribute("className","imageLeftContainerOn");
   }
   else
   {
      cur_img.setAttribute("class","imgReport");
   //   text.setAttribute("class","textLeftContainerOn");
    //  container.setAttribute("class","imageLeftContainerOn");
   }
   return null;
}

/**
*  scroll automatique sur la position de l'image
*
 */
Diaporama.prototype.moveElementTop= function (elem) 
{
		var yPos = elem.offsetTop;
		var tempEl = elem.offsetParent;
		while (tempEl != null) 
      {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
      document.getElementById('Left').scrollTop=yPos-80;
      return null;
}

Diaporama.prototype.resize= function (param) 
{


   //var heightIE = 175;
   //var heightNotIE = 180;
   var heightIE = 225;
   var heightNotIE =230;
  
   var widthIE = 550;
   var widthNotIE =600;

      var height = 0;
      if( typeof( window.innerWidth ) == 'number' )
      {
         //Non-IE
         height = window.innerHeight;
         height = height - heightNotIE;
         
         width = window.innerWidth;
         width = width - widthNotIE;
         
      } 
      else if(   document.documentElement 
             && ( document.documentElement.clientWidth 
             ||   document.documentElement.clientHeight ) ) 
      {
         //IE 6+ in 'standards compliant mode'
         height = document.documentElement.clientHeight;
         height = height - heightIE;
         
         width = document.documentElement.clientWidth;
         width = width - widthIE;
      }
      else if(    document.body 
              && ( document.body.clientWidth 
              ||   document.body.clientHeight ) ) 
      {
         //IE 4 compatible
         height = document.body.clientHeight;
         height = height - heightIE;
         
          width = document.body.clientWidth;
         width = width - widthIE;
         
         
      }
      if (height < 0 ) height = 200;

      //document.getElementById('viewRoot').style.height = height;
      //setTimeout('resize()',200);
if(param=="height") ret=height;
else ret=width;
return ret;
}   

