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

	// returns the Y position of the div
	gy: function (d) {
		gy = d.offsetTop
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop
		return gy
	},

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

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

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

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

	// 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(d){
		i_y = window.innerHeight || document.documentElement.clientHeight;
		i_x = window.innerWidth || document.documentElement.clientWidth;
		h_y=document.body.scrollHeight;
		h_x=document.body.scrollWidth;
		a_y = Scroller.scrollTop();
                                a_x = Scroller.scrollLeft();

                                if(d>a_y)
	                if(h_y-d>i_y)
		a_y+=Math.ceil((d-a_y)/Scroller.speed)
		else
		a_y+=Math.ceil((d-a_y-(h_y-d))/Scroller.speed)
		else
		a_y = a_y+(d-a_y)/Scroller.speed;

		if(a_y==d)
		if(d>a_x)
		if(h_x-d>i_x)
		a_x+=Math.ceil((d-a_x)/Scroller.speed)
		else
		a_x+=Math.ceil((d-a_x-(h_x-d))/Scroller.speed)
		else
		a_x = a_x+(d-a_x)/Scroller.speed;
                                
		window.scrollTo(a_x,a_y)
	  	if((a_x==d || Scroller.offsetLeft==a_x)&&(a_y==d || Scroller.offsetTop==a_y))clearInterval(Scroller.interval)

                                Scroller.offsetLeft=a_x
                                Scroller.offsetTop=a_y

	},
	// 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(0)',10);

						}
					}
				}
	      	}
		}
	}
}
// invoke the initializer of the scroller
Scroller.init();