
/*
 * page just loaded? then call init ( ) to highlight the target entry.
 */
function initAnchors ( )
{
	selectAnchor ( );
	addLinksToParagraphsWithNamedAnchors ( );	
	addUnselectLinkToBody ( );
}

function selectAnchor ()
{
	hash = window.location.hash;
	if (hash == "") return; 
	anchorName = hash.substring (1);
	if (window.location.pathname != "/streitschrift/" && window.location.pathname != "/vorurteile.html") return;
	anchors = document.getElementsByName(anchorName);
	if (!anchors) return;
	anchor = anchors[0];
	if (!anchor) return;
	p = anchor.parentNode;
	if (!p) return;
	select ( p );	
	window.setTimeout("moveQuestionSlightlyAwayFromBorder()", 2);	
}

function moveQuestionSlightlyAwayFromBorder ( )
{
	window.scrollBy (0, -60);
}

function select ( p )
{
	unselect ( );
	lastSelected = p;
	/*
	 * wait a millisecond because body may have clicked (which unselects)
	 */
	window.setTimeout("selectNow()", 1);	
}

function selectNow ( )
{
	if (!lastSelected) return;
	lastSelected.style.backgroundColor = "#eeee44";
}

function unselect ( )
{
	if (!lastSelected) return;
	lastSelected.style.backgroundColor = "#ffffff";
}

var anchorNames;
var lastSelected;

function addUnselectLinkToBody ( )
{
	body = document.getElementsByTagName("body")[0];
	body.onclick = function ()
	{
		loc = window.location;
		url = loc.protocol + "//" + loc.host + loc.pathname;
		unselect ( );
	};		
}

var xPos;
var yPos;

function addLinksToParagraphsWithNamedAnchors ( )
{
	anchors = document.getElementsByTagName("a");
	anchorNames = new Array(anchors.length);
	for (var i = 0; i < anchors.length; ++i) 
	{
		anchor = anchors[i];
		if (!anchor.name) continue;
		if (anchor.name == "up") continue;
		p = anchor.parentNode;
		if (!p) continue;
		if (p.tagName != "P") return;
		p.style.fontWeight = "bold"; /* makes text in <p> bold! */
		p.style.marginTop = "4px";
		p.style.padding = "5px";
		p.style.marginLeft = "-5px";
		p.style.marginRight = "5px";
		p.id = i;
		anchorNames[i] = anchor.name;
		p.onclick = function ()
		{
			loc = window.location;
			url = loc.protocol + "//" + loc.host + loc.pathname + "#" + anchorNames[this.id];
			xPos = window.pageXOffset;
			yPos = window.pageYOffset;
			window.location = url;
			select (this);
			window.scrollTo (xPos, yPos);
		};
	}	
}
