
/*
Script by Gibney-Enterprises

This saves the position the document is currently scrolled to in a cookie
if you visit the document again it restores the poition
Currently only one value per domain is saved
*/

function getCookieValue(cookieName)
{
 var value=null;
 if(document.cookie != "") 
 {
  cookieName=cookieName+"=";
  
  var start=document.cookie.indexOf(cookieName);
  if(start>=0) 
  {
   start=start+cookieName.length;
   
   var end=document.cookie.indexOf(";", start);
   if(end<0) end=document.cookie.length;
   
   value=document.cookie.substring(start,end);
   value=unescape(value);
  }
 }
 return value;
}

function setCookie(cookieName,cookieValue, expires, path, domain, secure)
{
 var cookie=cookieName+"="+escape(cookieValue)+";";
 if (expires) cookie+=" expires="+expires+";";
 if (path)    cookie+=" path="+path+";";
 if (domain)  cookie+=" domain="+domain+";";
 if (secure)  cookie+=" secure;";
 document.cookie=cookie;
}

function getPage()
{
 if ("undefined"!=typeof(mg_savescroll_forced_page)) return mg_savescroll_forced_page;
 
 var name=window.location.pathname;
 // var indexpos=name.indexOf('index.php');
 // if (indexpos==name.length-9) name=name.substring(0,name.length-9);
 
 if (name.substring(name.length-9)=='index.php') name=name.substring(0,name.length-9);
 
 name=escape(name);
 
 return name;
}

function saveScrollPos()
{
 var page=getPage();
 
 var scrollPos=0;
 
 var             scrollPos=document.documentElement.scrollTop; // ie and firefox
 if (!scrollPos) scrollPos=document.body.scrollTop;            // Chrome and Safari
 if (!scrollPos) scrollPos=window.pageYOffset;                 // IPad
 
 setCookie('mg_savescroll',page+'|'+scrollPos,false,'/');
}

function restoreScrollPos()
{
 var page=getPage();
 var scrollPos=null;
 
 // is there a cookie?
 var cookie=getCookieValue('mg_savescroll');
 if (cookie==null) return;
 
 // is it for this page?
 var pos=cookie.indexOf(page);
 if (pos!=0) return;
 
 // get the saved position
 var pos=cookie.indexOf("|");
 if (pos>0) scrollPos=cookie.substr(pos+1);
 
 // if we have a saved position scroll there...
 if (scrollPos!=null) window.scrollTo(0, scrollPos);
}

// document.onmousedown=saveScrollPos;
document.onclick=saveScrollPos;
window.onload=restoreScrollPos;
