/**
 * Makes the named part of an "A" html element external, so that it can be
 * styled independently.
 *
 * Also provides the required styling.
 * 
 * This is required, in order to have "#whatsoever" named links to jump at the
 * appropriate position (or they'll be covered by the graphicall header).
 * 
 * This is the best I could come with so far.
 * 
 * Note:
 * 	 The script is intended to be includded in the header of the page, 
 *   prefferably at the end of the 'link' style inclusions list.
 * Claudius Tiberiu Iacob <claudius.iacob@gmail.com>
 * 2008/09/06
 * GNU License v.2
 */

// Internet Explorer 6 runs a legacy layout, so no fix required for it. 
var isMSIE6OrLess = false /*@cc_on || @_jscript_version < 5.7 @*/;
if (!isMSIE6OrLess) {
	
	// Everything should be customized here:
	// [params]
	var graphHeadHeight = 160;
	// [/params]
	
	// Stage 1: ensure that, if we are currently jumping to a named anchor,
	// 		    we arrive at the appropriate vertical position.
	var styleAddition = '\n' +
			'<style type="text/css">\n\t' +
				'a[name], a[id] {\n\t\t' +
					'position: relative;\n\t\t' +
					'padding-top: ' + graphHeadHeight + 'px;\n\t' +
				'}\n' +
			'</style>\n';
	document.writeln (styleAddition);
	
	// Stage 2: the helper CSS code above has lot of side effects on the
	// 			page links; therefor, we create helper named links, place them
	// 			in front of real links, and transfer information from original
	// 			"id" and/or "name" attributes. 
	// 
	// 			Our precious CSS styles should now affect our helper links 
	// 			instead, and since their position is pretty much the same as
	// 			the original, we're cool if we jump trough the page. 
	var haveBodyLoaded = false;
	function checkDomReady () {
		while (!(haveBodyLoaded = document.getElementsByTagName('body')[0])) {
			window.setTimeout (checkDomReady, 10);
			return;
		}
		splitNamedAnchors ();	
	}
	function splitNamedAnchors () {
		var allAnchors = document.getElementsByTagName('a');
		for (var i=0; i<allAnchors.length; i++) {
			var anchor = allAnchors [i];
			var ancName = anchor.getAttribute("name");
			var ancId = anchor.getAttribute("id");
			if (ancId || ancName) {
				var helper = document.createElement('a');
				if (ancName) {
					helper.setAttribute("name", ancName);
					anchor.removeAttribute ("name");
				}
				if (ancId) {
					helper.setAttribute ("id", ancId);
					anchor.removeAttribute ("id");
				}
				anchor.parentNode.insertBefore (helper, anchor);
			}	
		}
	}
	// RUN:
	checkDomReady ();	
}

