/*--------------------------------------------------------------------------
 *  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:10,

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

	// returns the current scroll position
	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;
	},

	// 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_OLD: function(y){
		i = window.innerHeight || document.documentElement.clientHeight;
		h = document.body.scrollHeight;
		a = Scroller.scrollTop();
		if(y>a)
			if(h-y>i)
				a+=Math.ceil((y-a)/Scroller.speed);
			else
				a+=Math.ceil((y-a-(h-y))/Scroller.speed);
		else
			a = a+(y-a)/Scroller.speed;
		window.scrollTo(0,a)
    if(a==y || Scroller.offsetTop==a) 
      clearInterval(Scroller.interval);
    Scroller.offsetTop=a
	},

	scroll: function(y){
		altura_visivel = window.innerHeight || document.documentElement.clientHeight; 
		altura_pagina = document.body.scrollHeight;
		scroll_actual = Scroller.scrollTop();
    salto = (y-scroll_actual)/ Scroller.speed;
    if( salto < 1 && salto > -1  ) salto = (salto>0)?1:-1;
    scroll_actual+= salto;
		window.scrollTo(0,scroll_actual);
    if( scroll_actual == y || Scroller.offsetTop == scroll_actual ) {
      clearInterval(Scroller.interval);
    }
    Scroller.offsetTop=scroll_actual;
	},

	// 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 (var 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);
          var nome = this.hash.substr(1);
/* PROCURA TODOS OS LINKS COM NAME */
          a = document.getElementsByTagName('a');
          for (var i=0; i < a.length; i++) {
            if(a[i].name == nome){
              clearInterval(Scroller.interval);
              Scroller.interval=setInterval('Scroller.scroll(' + Scroller.get_y( a[i] ) + ')',10);
            }
          }
/* PROCURA O ID */
          var elemento = document.getElementById( nome );
          if(elemento){
            clearInterval(Scroller.interval);
            Scroller.interval=setInterval('Scroller.scroll(' + Scroller.get_y( elemento ) + ')',10);
          }
        }
      }
    }
	}
  
}
// invoke the initializer of the scroller

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