//---------------------------------------------------------
//  Nom Document : lg_scroll_div.js
//  Auteur       : L.Gabriel d'apres G.Ferraz (gf_scroll_div.js)
//---------------------------------------------------------
// OUTILS /////////////////////////////
//---------------------------------------------
function Add_Event( obj_, event_, func_, mode_){
  if( obj_.addEventListener)
    obj_.addEventListener( event_, func_, mode_? mode_:false);
  else
    obj_.attachEvent( 'on'+event_, func_);
}
//----------------------
function GetScrollPage(){
  var Left;
  var Top;
  var DocRef;

  if( window.innerWidth){
    with( window){
      Left   = pageXOffset;
      Top    = pageYOffset;
    }
  }
  else{ // Cas Explorer a part
    if( document.documentElement && document.documentElement.clientWidth)
      DocRef = document.documentElement;
    else
      DocRef = document.body;

    with( DocRef){
      Left   = scrollLeft;
      Top    = scrollTop;
    }
  }
  return({top:Top, left:Left});
}
//---------------------------
function ObjGetPosition(obj_){
  var PosX = 0;
  var PosY = 0;
  //-- suivant type en parametre
  if( typeof(obj_)=='object')
    var Obj  = obj_;
  else
    var Obj  = document.getElementById( obj_);
  //-- Si l'objet existe
  if( Obj){
    //-- Recup. Position Objet
    PosX = Obj.offsetLeft;
    PosY = Obj.offsetTop;
    //-- Si propriete existe
    if( Obj.offsetParent){
      //-- Tant qu'un parent existe
      while( Obj = Obj.offsetParent){
        if( Obj.offsetParent){ // on ne prend pas le BODY
          //-- Ajout position Parent
          PosX += Obj.offsetLeft;
          PosY += Obj.offsetTop;
        }
      }
    }
  }
  //-- Retour des positions
  return({left:PosX, top:PosY});
}
//-------------------------------------
// MENU FLOTTANT //////////////////////
//-------------------------------------
var IdTimer_1;
var IdTimer_2;
var O_DivScroll;
var Rapport = 1.0/10.0;  // On divise par 20
var Mini = 2* Rapport;
//---------------------------
function DIV_dblScroll( id1_, id2_, idRef_){
  //-- initialisation 1er objet à scroller
  var Obj = document.getElementById( idRef_);
  this.refObj = Obj;
  if( Obj){
	  var Pos   = ObjGetPosition( idRef_);
	  this.refPosX = Pos.left;
	  this.refPosY = Pos.top;
  }

  Obj = document.getElementById( id1_);
  this.fObj = Obj;
  if( Obj){
    Obj.style.position = "absolute"; // IMPERATIF
    //-- Recup position de depart
    var Pos   = ObjGetPosition( id1_);
    this.fPosX = Pos.left;
    this.fPosY = Pos.top;
    this.fDebX = this.fPosX;
    this.fDebY = this.fPosY;
    this.fNewX = 0;
    this.fNewY = 0;
    this.fMove = DIV_fDeplace;
  }
  //-- initialisation 2eme objet à scroller
  Obj = document.getElementById( id2_);
  this.sObj = Obj;
  if( Obj){
    Obj.style.position = "absolute"; // IMPERATIF
    //-- Recup position de depart
    var Pos   = ObjGetPosition( id2_);
    this.sPosX = Pos.left;
    this.sPosY = Pos.top;
    this.sDebX = this.sPosX;
    this.sDebY = this.sPosY;
    this.sNewX = 0;
    this.sNewY = 0;
    this.sMove = DIV_sDeplace;
  }
}
//---------------------------
function DIV_sDeplace( x_, y_){
  if( arguments[0] != null){
    this.sPosX = x_;
    this.sObj.style.left = parseInt(x_) +"px";
  }
  if( arguments[1] != null){
    this.sPosY = y_;
    this.sObj.style.top  = parseInt(y_) +"px";
  }
}
//---------------------------
function DIV_fDeplace( x_, y_){
  if( arguments[0] != null){
    this.fPosX = x_;
    this.fObj.style.left = parseInt(x_) +"px";
  }
  if( arguments[1] != null){
    this.fPosY = y_;
    this.fObj.style.top  = parseInt(y_) +"px";
  }
}
//---------------------------
function DIV_dblReplace( x1_, y1_, x2_, y2_){
  //-- Calcul Delta deplacement
  var Delta_X1 = (x1_ -O_DivScroll.fPosX) *Rapport;
  var Delta_Y1 = (y1_ -O_DivScroll.fPosY) *Rapport;
  var Delta_X2 = (x2_ -O_DivScroll.sPosX) *Rapport;
  var Delta_Y2 = (y2_ -O_DivScroll.sPosY) *Rapport;
  //-- Test si fin deplacement
  if( 
	  (( (Delta_Y1 < Mini)&&( Delta_Y1 > -Mini) ) && ( ( Delta_X1 < Mini)&&( Delta_X1 > -Mini) )) &&
	  (( (Delta_Y2 < Mini)&&( Delta_Y2 > -Mini) ) && ( ( Delta_X2 < Mini)&&( Delta_X2 > -Mini) ))
	){
    clearInterval( IdTimer_1);
	}


	if( ( (Delta_Y1 < Mini)&&( Delta_Y1 > -Mini) ) && ( ( Delta_X1 < Mini)&&( Delta_X1 > -Mini) ) ){
		O_DivScroll.fMove( x1_, y1_);
	}
	else{
		O_DivScroll.fMove( O_DivScroll.fPosX +Delta_X1, O_DivScroll.fPosY +Delta_Y1);
	}



	if( ( (Delta_Y2 < Mini)&&( Delta_Y2 > -Mini) ) && ( ( Delta_X2 < Mini)&&( Delta_X2 > -Mini) ) ){
		O_DivScroll.sMove( x2_, y2_);
	}
	else{
		O_DivScroll.sMove( O_DivScroll.sPosX +Delta_X2, O_DivScroll.sPosY +Delta_Y2);
	}

}
//-----------------------
function DIV_CheckDblScroll( refID_){
  var Scroll  = GetScrollPage();
  //-- New position  du menu
  //O_DivScroll.fNewX = Scroll.left +O_DivScroll.fDebX; -- ancien calcul de deplacement horizontal Obj 1
  //O_DivScroll.sNewX = Scroll.left +O_DivScroll.sDebX; -- ancien calcul de deplacement horizontal Obj 2

  var cPos = ObjGetPosition(refID_);
  O_DivScroll.fNewX = cPos.left+(O_DivScroll.fDebX-O_DivScroll.refPosX);
  O_DivScroll.sNewX = cPos.left+(O_DivScroll.sDebX-O_DivScroll.refPosX);

  //-- calcul de deplacement vertical Obj 1
  if(Scroll.top>O_DivScroll.fDebY){
	O_DivScroll.fNewY=Scroll.top+5;
  }else{
	O_DivScroll.fNewY=O_DivScroll.fDebY;
  }

  //-- calcul de deplacement vertical Obj 2
  if(Scroll.top>O_DivScroll.sDebY){
	O_DivScroll.sNewY=Scroll.top+5;
  }else{
	O_DivScroll.sNewY=O_DivScroll.sDebY;
  }
  //-- Si pas la bonne Position
  if(( O_DivScroll.fPosY != O_DivScroll.fNewY)||( O_DivScroll.fPosX != O_DivScroll.fNewX)||( O_DivScroll.sPosY != O_DivScroll.sNewY)||( O_DivScroll.sPosX != O_DivScroll.sNewX)){
    //-- Clear l'encours
    clearInterval( IdTimer_1);
    IdTimer_1 = setInterval("DIV_dblReplace(" + O_DivScroll.fNewX +"," + O_DivScroll.fNewY +"," +O_DivScroll.sNewX+"," +O_DivScroll.sNewY+")", 5);
  }
  return( true);
}
//-----------------------
function DIV_InitDblScroll(){
  //-- Recup position Objet
  O_DivScroll  = new DIV_dblScroll('DIV_MOVE_1','DIV_MOVE_2','Cadre-Blanc');
  //-- Lance inspection si existe
  if( O_DivScroll.fObj||O_DivScroll.sObj)
    IdTimer_2 = setInterval('DIV_CheckDblScroll(\'Cadre-Blanc\')',100);
}
//========================================
Add_Event( window, 'load', DIV_InitDblScroll);
//-- EOF --

