/***

Preserves scrolled x & y position between consecutive page reloads in the same session.

[Uses session cookies]

Usage: 
 
 RecoverScroll.init(); (Must be called from within onload handler)
 
For use on multiple pages, specify a unique name on each page:

 RecoverScroll.init("homePage");

***/

var RecoverScroll=/*28432953204368616C6D657273*/
{
 timer:null, x:0, y:0,cookieId:"RecoverScroll", dataCode:0, DEBUG:false,
 
 init:function(pageName)
 {
  var offsetData;
    
  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;
  
  if(pageName)
   this.cookieId=pageName;
   
  if((offsetData=this.readCookie(this.cookieId))!="" 
     && (offsetData=offsetData.split('|')).length==4 
     && !isNaN(offsetData[1]) && !isNaN(offsetData[3]))
  {   
   window.scrollTo(offsetData[1],offsetData[3]);
   if(this.DEBUG)
    document.title=offsetData;
  }  
  
  this.record();
  this.addToHandler(window,'onscroll',function(){RecoverScroll.reset()});   
 },
 
 reset:function()
 {
  clearTimeout(this.timer);
  this.timer=setTimeout("RecoverScroll.record()",500);
 },
 
 record:function()
 {
  var cStr;  
  
  this.getScrollData();  
  
  this.setTempCookie(this.cookieId, cStr='x|'+this.x+'|y|'+this.y); 
  
  if(this.DEBUG)
   window.status=cStr;
 },
 
 setTempCookie:function(cName, cValue)
 {    
  document.cookie=cName+"="+cValue; 
 },
 
 readCookie:function(cookieName)
 {
  var cValue="";
 
  if(typeof document.cookie!='undefined')
   cValue=(cValue=document.cookie.match(new RegExp(cookieName+'=([^;]+);?'))) ? cValue[1] : "";
  
  return cValue;
 },
 
 getScrollData:function()
 {
  switch( this.dataCode )
  {
   case 3 : this.x = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
            this.y = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
            break;

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

   case 1 : this.x = window.pageXOffset; this.y = window.pageYOffset; break;
  }
 },

 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;
 }
}

/*Fin*/
