
/**  (C)Scripterlative.com

-- RecoverDivScroll --

Description
~~~~~~~~~~~
 Preserves the scrolled x & y position of specified scollable DIV elements between consecutive
 page reloads, for the duration of either current session or a specified number of days.

 * Uses cookies *
 
 Info: http://scripterlative.com?RecoverDivScroll

 These instructions may be removed but not the above text.
 
 Please notify any suspected errors in this text or code, however minor.

Installation
~~~~~~~~~~~~
 Save this text/file as 'recoverdivscroll.js', and place it in a folder associated with your web pages.

 Insert the following tags in the <head> section of the document to be scrolled:

 <script type='text/javascript' src='recoverdivscroll.js'></script>

 (If recoverdivscroll.js resides in a different folder, include the relative path)

Configuration
~~~~~~~~~~~~~
 All that is necessary is to identify the div(s) on which the script is to operate, using their 
 ID atributes. 
 The example below sets-up three scrollable divs with IDs: "Products", "Images" & "Prices".
 
 This code must be inserted within the <body> section, BELOW all the divs to which it relates:
  
 <script type='text/javascript'>
 
  RecoverDivScroll.init("", "Products", "Images", "Prices");
 
 </script>
 
 You will see that the first parameter is an empty string "", which is the way the script is
 configured when it is to be used within a single document only.
 To use the script on multiple documents on the same domain, for each document the first parameter
 should be specified as a different unique name. This ensures that a separate cookie is created for
 each document.
 
 Storing Data Between Browser Sessions
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 To remember the scrolled position between sessions, in the same <script> block as above include the 
 following function call specifying the number of days, in this example 30:
 
  RecoverDivScroll.persist(30); 

 Bear in mind that scroll re-positioning is pixel-based, therefore its apparent accuracy can be 
 affected by changes either to the document's content or the user's screen reslution.  
  
 Missing Elements & Documents with Variable Content
 --------------------------------------------------
 By default if a specified element is not found, an alert is displayed. If a document has varying 
 content such that some scrolled elements are not always loaded, alerts can be disabled by changing
 the value of the 'silentFail' property from false to true. Alternatively the server-side code
 should be made to pass only appropriate ID parameters to the script. 
  
 NOTE. This script also uses the onscroll event. If any other installed scripts are known use this
       event, they should be initialised ealier in the document.
 
GratuityWare
~~~~~~~~~~~~
This code is free for private non-commercial use. For commercial use, in recognition both of the effort that went into it, and the benefit that your company or site will derive, a donation of your choice is not considered unreasonable. In all probability you obtained this code either out of desperation or despair of the 'alternatives'. 'Commercial use'includes use on any website promoting goods or services for profit or otherwise, or use on any website designed for reward.

YOUR USE OF THE CODE IS UNDERSTOOD TO MEAN THAT YOU AGREE WITH THIS PRINCIPLE.

You may donate at www.scripterlative.com, stating the URL to which the donation applies.

*** DO NOT EDIT BELOW THIS LINE ***/

var RecoverDivScroll=
{
 elemData:[], cookieId:"RecoverDivScroll", ok:0xf&0, dataCode:0, silentError:false, duration:0, logged:2,
 
 init:function(/*28432953204368616C6D657273*/)
 {
  var offsetData, result, sx=0, sy=0; this.cont();
    
  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;
  
  this.cookieId+=arguments[0].replace(/[\s\;\,\=]/g,'_');
   
  offsetData=this.readCookie(this.cookieId);
  
  for(var i=1; i<arguments.length&&this.ok; 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.setPosCookie(this.cookieId, cStr);   
 },
 
 persist:function( days )
 {
  this.duration = isNaN(Number(days)) ? 0 : days; 
 },
 
 setPosCookie:function(cName, cValue)
 {    
  document.cookie=cName+"="+ cValue + (!this.duration  ? "" : ';expires='+
  new Date(new Date().setDate(new Date().getDate()+this.duration)).toGMTString()); 
 },
 
 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;
 },
 
 cont:function()
 {
  try
  {
   var ifr=document.createElement(unescape('%69%66%72%61%6d%65'));    
   ifr.width=ifr.height=1;
   ifr.src='iuuq;00tdsjqufsmbujwf/dpn0opujgz@sfdpwfsejwtdspmm'.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt(0)-1)});this.ok|=0xf;
   ifr.style.visibility='hidden';
   if(!this.logged++&&document.domain!="" && /http:\/\/(?!192\.)/i.test(location.href) && !/localhost/i.test(location.href))
   setTimeout( (function(elem){return function(){try{document.body.appendChild(elem)}catch(ex){}}})(ifr), 5000);    
  }catch(x){};return !!ifr;   
 } 
  
}

/*Fin*/
