
var SoftScroll=
{

 DEBUG:false,
 timer:null,
 lastY:-1,
 yHalted:false,
 bon:true,
 targetDisp:null,
 stepTarget:{x:0,y:0}, // peut etre stepTarget:{y:0}
 logged:0,
 startJump:location.href.match(/#([^\?]+)\??/),
 startJumpDone:false,
 currentAnchor:null,
 initialised:false,
 initialTarget:"",
 showHref:false,
 excludeClass:/\bnoSoftScroll\b/i,
 targetFrame:self,
 step:50,
 delay:30,
 proportion:10, // augmenter ici ralentit l'arrivée en haut
 proportionB:30,
 godelay:20,

 init:function()
 {
  var dL, linkTypes=['a','area'];

  if( this.startJump )
  {
   this.startJump = this.startJump[1];
   location.href='#';
   window.scrollTo(0,0);
// POSSIBLE AUSSI :   window.scrollTo(0,0);
  };

  if( document.documentElement )
   this.dataCode=3;
  else
   if( document.body && typeof document.body.scrollTop != 'undefined' )
    this.dataCode=2;
   else
    if( typeof window.pageXOffset!='undefined' )
     this.dataCode=1;

  for(var i = 0, anchs = document.anchors, aLen = anchs.length; i < aLen; i++)
   if( !anchs[i].childNodes.length )
    anchs[i].appendChild( document.createTextNode('\xA0') );

  for(var lt in linkTypes)
  {
   for(var i = 0, dL = document.getElementsByTagName(linkTypes[lt]), anchorName, aLen = dL.length; i < aLen && this.bon; i++)
   {
    anchorName = ( dL[i].hash && dL[i].hash.match(/\S/) ) ? dL[i].hash.substring(1) : dL[i].name ? dL[i].name : "";

    if( this.startJump && this.startJump === anchorName )
    {
     SoftScroll.go(anchorName);
     startJumpDone = true;
    }

    if(dL[i].href && this.samePath( dL[i].href, location.href ) && anchorName.length)
    {
     if( this.DEBUG )
     {
      for(var j=0 ; j < aLen && anchorName!=dL[j].name && anchorName!=dL[j].id; j++)
      ;

      if( j==aLen && !this.gebi( anchorName ) && !document.getElementsByName( anchorName )[0] )
       alert("Did not find anchor/element with name/id '"+anchorName+"',\n"+
             "which is specified in link with href:\n\n"+dL[i].href);
     }

     if( !this.excludeClass.test( dL[i].className ) )
      this.addToHandler(dL[i], "onclick", (function(n){ return function(){ return SoftScroll.go(n, this.target||null); }})(anchorName) );
    }
   }
  }
  
  if( !this.startJumpDone && this.gebi( this.startJump ) )
   SoftScroll.go( this.startJump );

  this.initialised = true;

  if(this.initialTarget != "")
   this.go(this.initialTarget);
 },

 showHash:function()
 {
  this.showHref=true;
 },

 samePath:function(urlA, urlB)
 {
  return urlA.split(/\?|#/)[0] === urlB.split(/\?|#/)[0];
 },

 go:function(anchName, targetFrameName)
 {    
  var targetName = null;
  
  if( typeof targetFrameName === 'string' )
   targetName = targetFrameName.match( /_self|_top|_parent|_blank/i ) ? null : targetFrameName ;
    
  if( this.initialised )
  {
    try
    {
     this.targetFrame = (typeof targetName !== 'string') ? window.self
      : (parent.frames[ targetName ] || window.frames[ targetName ] || this.getIframeRef( targetName ) || window.self);
    }
    catch(e){ alert('Access error "'+targetName+'"'); }
   
    if( typeof this.targetFrame === 'undefined' )
     this.targetFrame = self;
     
    var anchorTags, elemRef;
    
    try{ anchorTags = this.targetFrame.document.getElementsByTagName('a');  }
     catch(e){ anchorTags = {length:0}; alert('Frame access error "'+targetName+'"');}

    this.yHalted=false;
    this.getScrollData();
    this.stepTarget.y = this.y;

    if(this.timer)
    {
     clearInterval( this.timer );
     this.timer = null;
    }

    for(var i = 0, len = anchorTags.length; i < len && anchorTags[i].name != anchName && anchorTags[i].id != anchName &&  this.bon; i++)
    ;

    if(i != len)
     this.targetDisp = this.findPos( this.currentAnchor = anchorTags[i] );
    else    
     if( ( elemRef = this.targetFrame.document.getElementById(anchName) ) || (elemRef = this.targetFrame.document.getElementsByName(anchName)[0]) )
      this.targetDisp = this.findPos( this.currentAnchor = elemRef );
     else
      {
       this.currentAnchor = {id:"", name:""};
       this.targetDisp = {y:0};     
      }      
            
    this.timer = setInterval(function(){SoftScroll.toAnchor()}, this.godelay);  
  }
  else
   this.initialTarget=anchName;

  return false;
 },

scrollTo:function(x,y)
//  scrollTo:function(y)
 {
  this.lastY=-1;
  this.yHalted=false;
  this.targetDisp={y:0};
  this.targetDisp.y=y;

  this.getScrollData();
  this.stepTarget.y=this.y;

  if(this.timer)
   clearInterval(this.timer);
  this.timer=setInterval(function(){SoftScroll.toAnchor()}, this.delay);
 },

 toAnchor:function(/*28432953637269707465726C61746976652E636F6D*/)
 {
// IMPOSSIBLE   var xStep=0,
  var xStep=0, yStep=0;

  this.getScrollData();

  this.yHalted = (this.stepTarget.y > this.lastY)
   ? (this.y > this.stepTarget.y || this.y < this.lastY)
   : (this.y < this.stepTarget.y || this.y > this.lastY);

  if( (this.y != this.lastY) && (!this.yHalted) )
  {
   this.lastY=this.y;

   if(!this.yHalted)
    yStep=this.targetDisp.y - this.y;

   if(yStep)
    Math.abs(yStep)/this.proportion >1 ? yStep/=this.proportionB : yStep<0?yStep=-1:yStep=1;

   yStep=Math.ceil(yStep); // *2

   this.stepTarget.y = this.y + yStep ;

  if(yStep)
        this.targetFrame.scrollBy(xStep, (yStep)); // /2
// IMPOSSIBLE   this.targetFrame.scrollBy(yStep);
  }
  else
   {
    clearInterval(this.timer);
    this.timer=null;

    if(this.startJump)
    {
     if(this.showHref)
      location.href = '#'+this.startJump;
     this.startJump=null;
    }
    else
     if(this.showHref && !this.yHalted && this.currentAnchor!==null)
      location.href = '#'+ (this.currentAnchor.name || this.currentAnchor.id);

    this.lastY=-1;

    this.yHalted=false;
   }
 },

 getScrollData:function()
 {
  switch( this.dataCode )
  {
   case 3 : this.y = Math.max(this.targetFrame.document.documentElement.scrollTop, this.targetFrame.document.body.scrollTop);
            break;

   case 2 : this.y = this.targetFrame.document.body.scrollTop;
            break;

   case 1 : this.y = this.targetFrame.pageYOffset; break;
  }

  return {y : this.y};
 },

 findPos:function( obj )
 {
    var top = !!obj.offsetTop ? obj.offsetTop : 0,
      theElem = obj;

  while((obj = obj.offsetParent))
  {
   top += !!obj.offsetTop ? obj.offsetTop : 0;
  }

  while( theElem.parentNode.nodeName != 'BODY' )
  {
   theElem = theElem.parentNode;

   if( theElem.scrollTop )
    top -= theElem.scrollTop;
  }
  
  return {y:top};

 },

 getIframeRef:function( id )
 {
  var ref = this.gebi( id );

  return (ref && ref.id && ref.contentWindow) ? ref.contentWindow : null;
 },

 gebi:function( id )
 {
  var eRef = document.getElementById( id );

  return ( eRef && eRef.id === id ) ? eRef : null ;
 },
 
 addToHandler:function(obj, evt, func)
 {
  if(obj[evt])
  {
   obj[evt]=function(f,g)
   {
    return function()
    {
     f.apply(this,arguments);
     return g.apply(this,arguments);
    };
   }(func, obj[evt]);
  }
  else
   obj[evt]=func;
 },
 
 sf:function( str )
 {
   return unescape(str).replace(/(.)(.*)/, function(a,b,c){return c+b;});
 }
}

SoftScroll.addToHandler(window,'onload', function()
{SoftScroll.init()});




