var timer = new Timer();
var scrolling;
var ss_STEPS = 25;

function FixAllLinks()
{
	var as = document.getElementsByTagName("a");
	for (var i = 0; i < as.length; i++)
	{
		var a = as[i];
		var test;
		if (a)
		{
			test = true;
		}
		else
		{
			test = false;
		}
		
		// link in breadcrumbs throwing exception for some reason
		// temp try catch to ignore exception
		try
		{
			// If the link is internal to the page (begins in #)
			// then attach the smoothScroll function as an onclick
			// event handler
			if (a.hash && a.hash.length > 0 && a.search == location.search && (a.pathname == location.pathname || "/" + a.pathname == location.pathname))
			{
				addEvent(a, "click", smoothScroll);
			}
		}
		catch (e)
		{
		}
	}
}

function smoothScroll(e)
{
	if (e)
	{
		target = e.target || e.srcElement;
	}
	
	// loop through the target's parents until the anchor tag is found
	while (target)
	{
		if (target.nodeName.toLowerCase() == "a")
		{
			break;
		}
		
		target = target.parentNode;		
	}
	// if no anchor is found then return
	if (!target)
	{
		return;
	}
	
	// strip the hast off the hash (first character)
	var anchorTarget = target.hash.substr(1);
	var desty;

	// if target is blank then move to the top of the page. otherwise try to find anchor with matching name
	if (anchorTarget != "top")
	{
		var destinationElement = document.getElementById(anchorTarget);
		
		// If we didn't find a destination, give up and let the browser do
		// its thing
		if (!destinationElement)
		{
			return true;
		}
		
		// Find the destination's position
		desty = destinationElement.offsetTop;
		
		var thisNode = destinationElement;
		
		while (thisNode.offsetParent && (thisNode.offsetParent != document.body))
		{
			thisNode = thisNode.offsetParent;
			desty += thisNode.offsetTop;
		}
	}
	else
	{
		desty = 0;
	}

	// Stop any current scrolling
	timer.clearTimeout(scrolling);
		
	var currentY = GetCurrentYPos();
		
	var stepSize = parseInt((desty - currentY) / ss_STEPS);
	scrolling = timer.setTimeout("ScrollWindow", 10, stepSize, desty, anchorTarget, currentY);
	cancelEvent(e, true);
}

function ScrollWindow(stepSize, destY, anchorTarget, currentY)
{
	// use absolute values to it doesn't matter wether the page is asc or desc
	if (Math.abs(stepSize) < Math.abs(destY - currentY))
	{
		window.scrollTo(0, currentY + stepSize);
	}
	
	var newY = GetCurrentYPos();
	
	// if page didn't scroll either because it has reached the target
	// or it has hit the bottom of the page..
	if (currentY == newY)
	{
		window.scrollTo(0, destY);
		// to the target directly so the URL's right
		location.hash = anchorTarget;
//		var lblTarget = document.getElementById("lblTarget");
//		if (anchorTarget != "top")
//		{
//			lblTarget.innerHTML = "# " + anchorTarget;
//		}
//		else
//		{
//			lblTarget.innerHTML = "";
//		}
	}
	else
	{
		scrolling = timer.setTimeout("ScrollWindow", 10, stepSize, destY, anchorTarget, newY);
	}
}

function GetCurrentYPos()
{
	if (document.body && document.body.scrollTop)
	{
		return document.body.scrollTop;
	}
	if (document.documentElement && document.documentElement.scrollTop)
	{
		return document.documentElement.scrollTop;
	}
	if (window.pageYOffset)
	{
		return window.pageYOffset;
	}
	return 0;
}

addEvent(window, "load", FixAllLinks);