/*--------------------------------------------------------------------------
 *  Smooth Scroller Script, version 1.0.1
 *  (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
 *
 *  For details, please check the website : http://dezinerfolio.com/
 *
/*--------------------------------------------------------------------------*/

Scroller = {
	// control the speed of the scroller.
	// dont change it here directly, please use Scroller.speed=50;
	speed:5,

	// returns the X position of the div
	gx: function (dx) {
		gx = dx.offsetLeft
		if (dx.offsetParent) while (dx = dx.offsetParent) gx += dx.offsetLeft
		return gx
	},
	// returns the Y position of the div
	gy: function (dy) {
		gy = dy.offsetTop
		if (dy.offsetParent) while (dy = dy.offsetParent) gy += dy.offsetTop
		return gy
	},

	// returns the current scroll position
	scrollLeft: function (){
		body=document.body
	    dx=document.documentElement
	    if (body && body.scrollLeft) return body.scrollLeft
	    if (dx && dx.scrollLeft) return dx.scrollLeft
	    if (window.pageXOffset) return window.pageXOffset
	    return 0
	},
	scrollTop: function (){
		body=document.body
	    dy=document.documentElement
	    if (body && body.scrollTop) return body.scrollTop
	    if (dy && dy.scrollTop) return dy.scrollTop
	    if (window.pageYOffset) return window.pageYOffset
	    return 0
	},

	// attach an event for an element
	// (element, type, function)
	add: function(event, body, dx, dy) {
	    if (event.addEventListener) return event.addEventListener(body, dx, dy, false)
	    if (event.attachEvent) return event.attachEvent('on'+body, dx)
	    if (event.attachEvent) return event.attachEvent('on'+body, dy)
	},

	// kill an event of an element
	end: function(e){
		if (window.event) {
			window.event.cancelBubble = true
			window.event.returnValue = false
      		return;
    	}
	    if (e.preventDefault && e.stopPropagation) {
	      e.preventDefault()
	      e.stopPropagation()
	    }
	},
	
	// move the scroll bar to the particular div.
	scroll: function(dx,dy){
		//alert(dx+', '+dy);
		i = window.innerWidth || document.documentElement.clientWidth;
		j = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollWidth;
		k=document.body.scrollHeight;
		a = Scroller.scrollLeft()
		b = Scroller.scrollTop()
		if(dx>a)
			//if(h-dx>i)
				a+=Math.ceil((dx-a)/Scroller.speed)
			//else
				//a+=Math.ceil((dx-a-(h-dx))/Scroller.speed)
		else
			a = a+(dx-a)/Scroller.speed;
		if(dy>b)
			//if(k-dy>j)
				b+=Math.ceil((dy-b)/Scroller.speed)
			//else
			//	b+=Math.ceil((dy-b-(k-dy))/Scroller.speed)
		else
			b = b+(dy-b)/Scroller.speed;
		window.scrollTo(a,b)
	  	if((a==dx || Scroller.offsetLeft==a)&&(b==dy || Scroller.offsetTop==b))clearInterval(Scroller.interval)
	  	Scroller.offsetLeft=a
	  	Scroller.offsetTop=b
	},
	// initializer that adds the renderer to the onload function of the window
	init: function(){
		Scroller.add(window,'load', Scroller.render)
	},

	// this method extracts all the anchors and validates then as # and attaches the events.
	render: function(){
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll
	    for (i=0;i<a.length;i++) {
	      l = a[i];
	      if(l.href && l.href.indexOf('#') != -1 && ((l.pathname==location.pathname) || ('/'+l.pathname==location.pathname)) ){
	      	Scroller.add(l,'click',Scroller.end)
	      		l.onclick = function(){
	      			Scroller.end(this);
		        	l=this.hash.substr(1);
		        	 a = document.getElementsByTagName('a');
				     for (i=0;i<a.length;i++) {
				     	if(a[i].name == l){
				     		clearInterval(Scroller.interval);
				     		Scroller.interval=setInterval('Scroller.scroll('+Scroller.gx(a[i])+','+Scroller.gy(a[i])+')',10);
						}
					}
				}
	      	}
		}
	}
}
// invoke the initializer of the scroller
Scroller.init();


/*------------------------------------------------------------
 *						END OF CODE
/*-----------------------------------------------------------*/
