// Copyright (c) James Crompton 2004-2006. License terms at http://home.jcrompton.de/legalese.html

function getParentAttribute( x , attName )	//	Ie didn't like making this a method of Node or Element
{
	return ( x && x.getAttribute && ( x.getAttribute( attName ) ||
		getParentAttribute( x.parentNode , attName ) ) ) ||
		document.getElementsByTagName( "html" )[0].getAttribute( attName ) ;
}
/****************************** Back menu ***************************************
	If you arrive from one of my pages, you get a JavaScript-driven 'Back' button
	If you come from somewhere else, you get the 'Home' link
	If you have no scripting, you get the menu in the html code */

function backmenu( backText , myDomain , backJumps )
{
	backText = backText || "Back\u2026" ;
	myDomain = new RegExp( myDomain || document.domain ) ;
	backJumps = backJumps || 1 ;

	if ( document.referrer )
		var lastDomain = document.referrer.split( /\/+/ )[1] ;
	else
		lastDomain = "" ;

	var oldMenu = document.getElementById( "menu" ) ;
	var newMenu = document.createElement( "div" ) ;
	newMenu.className = "backmenu" ; // setAttribute fails IE6
	newMenu.setAttribute( "id" , "menu" ) ;

	var newLink = newMenu.appendChild( oldMenu.getElementsByTagName( "a" )[0] ) ;
	newLink.removeAttribute( "target" ) ;
	oldMenu.parentNode.replaceChild( newMenu , oldMenu ) ;

	if ( myDomain.test( lastDomain ) )
	{
		newLink.firstChild.nodeValue = backText ;
		newLink.href = "javascript:history.go(-" + backJumps + ")" ;
		newLink.title = newMenu.title = "" ;
		newLink.accesskey = "b" ;
	}
}

/****************************** browser-specific stuff ************************************/

void new function()	//	wrap everything in an anonymous function to avoid name conflicts
{
//	constants. IE5/6 doesn't support 'const'
	var rootPath = document.getElementsByTagName( "script" )[0].src.replace( /[^\/]*$/ , "" ) ;
	var showSource = "code/showsource.html?" ;
	var oldOnload = window.onload || false ;

	function addStylesheet( href )
	{
		var link = document.createElement( "link" ) ; // appending here sometimes causes errors in IE
		link.href = rootPath + href ;
		link.rel = "stylesheet" ;
		link.disabled = false ;
		document.getElementsByTagName( "head" )[0].appendChild( link ) ;
	}

	function showPrettySource( showSource )
	{
		for ( var i=0 , elements = document.getElementsByTagName( "a" ) ; elements[i] ; i++ )
		{
			if ( /\.(css|js)$/.test( elements[i].href ) && !/\?/.test( elements[i].href ) )
				elements[i].href = rootPath + showSource + elements[i].href ;
		}
	}

	if ( !/MSIE [56]/.test( navigator.appVersion ) || /Opera/.test( navigator.userAgent ) )
	{	// (opera sometimes claims to be MSIE)
		window.onload = function()
		{
			if ( typeof XMLHttpRequest != "undefined" )
				showPrettySource( showSource ) ;

/*			try
			{
				if ( document.defaultView.getComputedStyle(document.body, null).getPropertyValue("MozBorderRadiusTopLeft" ) != "undefined" )
				// Mozilla/Firefox: proprietary style rules for rounded corners
					addStylesheet( "style/moz.css" ) ;
			}
			catch( ignore ) {}*/

			if ( oldOnload ) oldOnload() ;
			window.onload = null ;
		}
	}
	//	Internet Explorer 5/6
	else {
		window.oneEm = function(x) {
				return x * 2 * ( content && content.offsetTop ) || 16 ;
			}
		window.bodyWidth = function( bodyMax , contentMin ) {
			return Math.max( oneEm( contentMin + 1 + 0.5 ) + 2 + ( menu && menu.clientWidth ) || 144 ,
				Math.min( oneEm( bodyMax ) , document.body.clientWidth - oneEm( 1 ) ) );
			}

	//	add IE meta trash. DOM methods don't work for this
		document.write( '<meta http-equiv="imagetoolbar" content="no"><meta name="autosize" content="off">' ) ;
	//	fix IE screwups:
		window.onload = function() {
			for ( var i = 0 , elements = document.images ; elements[i] ; i++ ) {
				if ( ! elements[i].title )
				//	If img has no title, IE shows the image alt text as a tool tip instead of the parent's title
					elements[i].title = getParentAttribute( elements[i].parentNode , "title" ) || "" ;
				if ( elements[i].src.slice( -4 ).toLowerCase() == ".png" ) { // negative slice index loses IE5.01 (good)
				//	IE can't do png alpha channel. But as from 5.5 one of its 'filters' can ...
					elements[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + elements[i].src + "')" ;
					elements[i].width = elements[i].clientWidth ;
					elements[i].src = rootPath + "images/transparentpixel.gif" ; // make the background visible without redex
					}
				}
		//	abbr elements. See http://dean.edwards.name/my/abbr-cadabra.html for explanation
			for ( i = 0 , elements = document.getElementsByTagName( "abbr" ) ; elements[i] ; i++ )
				if ( elements[i].parentNode != latestParent ) {
					var latestParent = elements[i].parentNode;
					latestParent.innerHTML = latestParent.innerHTML.replace(
						/(<\/?)ABBR\b/g , "$1HTML:ABBR" ) ;
					}
			if ( elements )
				document.getElementsByTagName( "HTML" )[0].setAttribute( "xmlns:HTML" , "http://www.w3.org/1999/xhtml" ) ;
		//	IE won't center tables with css
			for ( i = 0 , elements = document.getElementsByTagName( "table" ) ; elements[i] ; i++ ) {
				elements[i].align = "center" ; // 'align' attribute is non-standard, but this is IE
				}
		//	... same for textarea
			for ( i = 0 , elements = document.getElementsByTagName( "textarea" ) ; elements[i] ; i++ ) {
				elements[i].parentNode.style.textAlign = "center" ;
				}
		/*	IE won't show files with a .js suffix: it tries to run them
			IE doesn't always show files with a .css suffix: it opens them in a text editor */
			showPrettySource( showSource ) ;

			if ( oldOnload ) oldOnload() ;
			window.onload = null ; // Drooper! Take out the trash!
		   }
		}
}
