// Progressive Scroll To (11-November-2010)
// by Vic Phillips http://www.vicsjavascripts.org.uk



// Functional Code(1.33K) - NO NEED to Change

// parameter 0 = the scroll duration in milli seconds. (default = 2000)
// parameter 1 = (optional) the type of progression, 'sin', 'cos' or 'liner'.                (string, default = 'liner')
//                 'sin' progression starts fast and ends slow.
//                 'cos' progression starts slow and ends fast.
//
function zxcScrollTo(ms,c){
 this.mS=ms||2000;
 this.to=null;
 this.c=typeof(c)=='string'?c.charAt(0).toLowerCase():this.c?this.c:'';
}

zxcScrollTo.prototype={

// parameter 0 = number = the new scroll x position or string = ID name of the anchor element.
// parameter 1 = number = the new scroll y position or string = ID name of the anchor element. (default = parameter 0)
 ScrollTo:function(x,y){
  y=typeof(y)=='undefined'?x:y;
  x=typeof(x)=='number'?x:this.pos(document.getElementById(x))[0];
  y=typeof(y)=='number'?y:this.pos(document.getElementById(y||x))[1];
  if (typeof(x)=='number'&&typeof(y)=='number'){
   clearTimeout(this.to);
   this.srttime=new Date().getTime();
   var scroll=this.scroll();
   this.data=[[scroll[0],x],[scroll[1],y]];
   this.inc=Math.PI/(2*this.mS);
   this.cng();
  }
 },

 cng:function(){
  var ms=new Date().getTime()-this.srttime,scroll=[],z0,d;
  for (z0=0;z0<2;z0++){
   d=this.data[z0][1]-this.data[z0][0];
   scroll[z0]=Math.floor(this.c=='s'?d*Math.sin(this.inc*ms)+this.data[z0][0]:this.c=='c'?this.data[z0][1]-d*Math.cos(this.inc*ms):d/this.mS*ms+this.data[z0][0])
  }
  window.scrollTo(scroll[0],scroll[1]);
  if (ms<this.mS){
   this.to=setTimeout(function(oop){ return function(){oop.cng(); } }(this), 10);
  }
  else {
   window.scrollTo(this.data[0][1],this.data[1][1]);
  }
 },

 pos:function(obj){
  var rtn=[0,0];
  while(obj){
   rtn[0]+=obj.offsetLeft;
   rtn[1]+=obj.offsetTop;
   obj=obj.offsetParent;
  }
  return rtn;
 },


 scroll:function(){
  if (window.innerHeight) return [window.pageXOffset,window.pageYOffset];
  else if (document.documentElement.clientHeight) return [document.documentElement.scrollLeft,document.documentElement.scrollTop];
  return [document.body.scrollLeft,document.body.scrollTop];
 }

}

var S=new zxcScrollTo(1000);
