


var RecoverDivScroll=
{
 elemData:[], cookieId:"RecoverDivScroll", dataCode:0, silentError:false, logged:0,

 init:function()
 {
  var offsetData, result, sx=0, sy=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;

  try {
    this.cookieId+=arguments[0].replace(/[\s\;\,]/g,'_');
  } catch(e){}

  offsetData=this.readCookie(this.cookieId);

  for(var i=1; i<arguments.length; i++)
  {
    if( (result=offsetData.match(new RegExp(arguments[i]+'=x:(\\d+)\\|y:(\\d+)'))) )
     try
     {
      with(document.getElementById(arguments[i]))
      {
       scrollLeft=Number(result[1]);
       scrollTop=Number(result[2]);
      }
     }
     catch(e){};

    try
    {
     var divRef=document.getElementById(arguments[i]);

     this.addToHandler(divRef,'onscroll', (function(id){return function(){RecoverDivScroll.setTimer(id)}})(divRef.id));

     this.elemData[divRef.id]={elem:divRef,timer:null,x:0,y:0};

     this.record(arguments[i]);
    }
    catch(e)
    {
     if(!this.silentError)
      alert('Element with id: "'+arguments[i]+'" was not present at the instant the script executed. (Case must match)');
    }
  }

 },

 setTimer:function(ref)
 {
  clearTimeout(this.elemData[ref].timer);
  this.elemData[ref].timer = setTimeout( (function(r){return function(){RecoverDivScroll.record(r);}})(ref), 250);
 },

 reset:function()
 {
  clearTimeout(this.timer);
  this.timer=setTimeout(function(){RecoverDivScroll.record();}, 50);
 },

 record:function(ref)
 {
  var cStr;

  this.getScrollData(ref);

  if( (cStr=this.readCookie(this.cookieId)).match(ref) )
   cStr=cStr.replace( new RegExp(ref+"=[^,]*,?"), "" );

  cStr+=(cStr.length&&cStr.charAt(cStr.length-1)!=','?',':'') + ref +"=x:"+this.elemData[ref].x+"|y:"+this.elemData[ref].y;

  this.setTempCookie(this.cookieId, cStr);
 },

 setTempCookie:function(cName, cValue)
 {
  document.cookie=cName+"="+cValue;//+';path=/';
 },

 readCookie:function(cookieName)
 {
  var cValue="";

  if(typeof document.cookie!='undefined')
   cValue=(cValue=document.cookie.match(new RegExp("(^|;|\\s)"+cookieName+'=([^;]+);?'))) ? cValue[2] : "";

  return cValue;
 },

 getScrollData:function(ref)
 {
  this.elemData[ref].x=this.elemData[ref].elem.scrollLeft;
  this.elemData[ref].y=this.elemData[ref].elem.scrollTop;
 },

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


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