(function()
{
	var execute = function(e)
	{
		var nodelist = document.getElementsByTagName("a");
		for(var i=0; i<nodelist.length; i++)
		{
			var elm = nodelist[i];
			if(elm.href.match(/([html|\/]\#.+)/))
			{
				var onclick = (function(_elm)
				{
					return function(e)
					{
						var current = location.href.split("#")[0];
						var reference = _elm.href.split("#")[0];
						
						if(current!=reference)
						{
							return;
						}
						if(e.preventDefault) e.preventDefault();
						else event.returnValue = false;
						
						var speed = 0.5;
						var bounds =  Util.getBounds(
							document.getElementById(
								_elm.href.substring(
									_elm.href.indexOf("#")+1
								)
							)
						);
						
						var scrollBottom = document.documentElement.scrollHeight
										   -document.documentElement.clientHeight;
						var destination = (bounds.top<scrollBottom)
										  ?bounds.top
										  :scrollBottom;
						if(destination<0) destination = 0;
						
						var old = null;
						var interval = setInterval(function()
						{
							var current = document.documentElement.scrollTop||document.body.scrollTop;
							var distance = (destination - current);
							
							window.scrollTo(0, current+distance*speed);
							
							if(Math.abs(distance)<(1/speed)||current==destination)
							{
								window.scrollTo(0, destination);
								clearInterval(interval);
							}
							old = current;
							
						}, 33);
					}
					
				})(elm);
				
				EventHandler.addListener(elm, "click", onclick, null);
			}
		}
	}
	EventHandler.addListener(window, "load", execute, null);
})();
