
var HexConverter = {
	hexDigits : '0123456789ABCDEF',
	
	dec2hex : function( dec )
	{
		return( this.hexDigits[ dec >> 4 ] + this.hexDigits[ dec & 15 ] );
	},
	
	hex2dec : function( hex )
	{
		return( parseInt( hex, 16 ) )
	}
}

//This function from Quirksmode (Thanks PPK - http://www.quirksmode.org)
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function goToAnchor(a)
{
	var href = a.href.split(/#/);
	var id = href[1];
	var targetEl = document.getElementById(id);

	return scrollTo(targetEl);
}

function scrollTo(el)
{
	var pos = findPos(el);
	return doScroll(pos[0], el);
}

function doScroll(dest, el)
{
	var current = document.documentElement.scrollTop;
	if(!current && current != 0)
		current = document.scrollTop;
	if (!current && current != 0)
	{
		return true;
	}
	var next = Math.min(dest, current + (dest - current) / 2 + 5);

	if(next >= dest)
	{
		window.scrollBy(0, next - current);

		if(el)
		{
			document.location = "#" + el.id;
			highlight(el);
		}
		
		return false;
	}
	else
	{
		window.scrollBy(0, next - current);
		setTimeout(function() { doScroll(dest, el); }, 40);
	}
	return false;
}

var HighlightSteps = 5;

function highlight(el)
{
	el.style.backgroundColor = "#FFFFFF";
	doHighlight(el, 0);
}

function doHighlight(el, step)
{
	if(step >= HighlightSteps)
	{
		doUnHighlight(el, step);
		return;
	}

	el.style.backgroundColor = "#FFFF" + HexConverter.dec2hex((HighlightSteps - step) / HighlightSteps * 256);

	setTimeout(function() { doHighlight(el, step + 1); }, 40);
}

function doUnHighlight(el, step)
{
	if(step <= 0)
	{
		el.style.backgroundColor = "";
		return;
	}
	window.status = HexConverter.dec2hex(HighlightSteps - step);
	el.style.backgroundColor = "#FFFF" + HexConverter.dec2hex((HighlightSteps - step) / HighlightSteps * 256);
	setTimeout(function() { doUnHighlight(el, step - 1); }, 40);
}