var boxoffset = new Object();
boxoffset.headerboxmargin = 4;
boxoffset.logoboxwidth = 100;
boxoffset.titleboxheight = 80;
boxoffset.titleboxmargin = 4;
boxoffset.adboxwidth = 468;
boxoffset.textcolumnpaddingleft = 18;
boxoffset.textcolumnpaddingright = 12;
boxoffset.adcolumnwidth = 160;
boxoffset.adcolumnpadding = 6;
boxoffset.texttopoffset = 184;
boxoffset.adcolumntopoffset = 108;
boxoffset.adboxtopoffset = 116;

var taselement = new Object();
taselement.headerbox = null;
taselement.headertitlebox = null;
taselement.headertitleinnerbox = null;
taselement.headermenubox = null;
taselement.topadbox = null;
taselement.topadboxcontent = null;
taselement.sideadbox = null;
taselement.textbox = null;
taselement.navigationbox = null;
taselement.navigationnewpages = null;
taselement.navigationtoclinks = null;
taselement.navigationotherlinks = null;
taselement.navigationfooter = null;
taselement.calendarbox = null;
taselement.calendarboxlogstyle = null;
taselement.calendarfooterbox = null;
taselement.searchbox = null;

taselement.calendarboxindex = 100;
taselement.textboxindex = 200;

function setupPage( filename, displayyear )
{
	taselement.headerbox = document.getElementById( "HeaderBox" );
	taselement.headertitlebox = document.getElementById( "HeaderTitleBox" );
	taselement.headertitleinnerbox = document.getElementById( "HeaderTitleInnerBox" );
	taselement.headermenubox = document.getElementById( "HeaderMenuBox" );
	taselement.topadbox = document.getElementById( "TopAdBox" );
	taselement.topadboxcontent = document.getElementById( "TopAdBoxContent" );
	taselement.sideadbox = document.getElementById( "SideAdColumn" );
	taselement.textbox = document.getElementById( "TextColumn" );
	taselement.navigationbox = document.getElementById( "NavigationBox" );
	taselement.navigationnewpages = document.getElementById( "NavigationNewPages" );
	taselement.navigationtoclinks = document.getElementById( "NavigationTableOfContentsLinks" );
	taselement.navigationotherlinks = document.getElementById( "NavigationOtherLinks" );
	taselement.navigationfooter = document.getElementById( "NavigationFooter" );
	taselement.calendarbox = document.getElementById( "CalendarBox" );
	taselement.calendarboxlogstyle = document.getElementById( "CalendarBoxLogStyle" );
	taselement.calendarfooterbox = document.getElementById( "CalendarFooterBox" );
	taselement.searchbox = document.getElementById( "SearchBox" );

	resizePage();

	readSessionCookie();
	writeTASCookie();
	createFooter();

	if ( filename != null ) {
		if ( taselement.navigationnewpages != null ) {
			if ( writeNewPageList != null ) {
				readXMLFile( filename, writeNewPageList );
				return;
			}
		}
		else if ( taselement.calendarbox != null ) {
			if ( setCalendarBox != null ) {
				setCalendarBox( filename, taselement.calendarbox,
									taselement.calendarfooterbox,
									taselement.calendarboxindex,
									false,
									displayyear );
				return;
			}
		}
		else if ( taselement.calendarboxlogstyle != null ) {
			if ( setCalendarBox != null ) {
				setCalendarBox( filename, taselement.calendarboxlogstyle,
									taselement.calendarfooterbox,
									taselement.calendarboxindex,
									true,
									displayyear );
				return;
			}
		}
		else if ( taselement.navigationotherlinks != null ) {
			readXMLFile( filename, writeNavigationOtherLinks );
			return;
		}
	}
	finish();
	return;
}

function resizePage()
{
	if ( window.navigator.appName == "Microsoft Internet Explorer"
			&& window.navigator.appVersion.match( /\bMSIE\s[1-6]./i ) != null
			&& document.body != null && document.body.parentElement != null
			&& document.body.parentElement.clientWidth != null ) {
		var headerbox = taselement.headerbox;
		var headermenubox = taselement.headermenubox;
		var adbox = taselement.topadbox;
		var headertitlebox = taselement.headertitlebox;
		var headertitleinnerbox = taselement.headertitleinnerbox;
		var textbox = taselement.textbox;

		var windowwidth = document.body.parentElement.clientWidth;

		var headerboxwidth = windowwidth - 2*boxoffset.headerboxmargin;
		if ( headerbox != null )
			headerbox.style.width = headerboxwidth.toString() + "px";

		var titleboxwidth = headerboxwidth - boxoffset.logoboxwidth
				- boxoffset.titleboxmargin;
		if ( headertitlebox != null ) {
			headertitlebox.style.width = titleboxwidth.toString() + "px";
			headertitlebox.style.height = boxoffset.titleboxheight.toString() + "px";
		}

		if ( headertitleinnerbox != null ) {
			headertitleinnerbox.style.width = titleboxwidth.toString() + "px";
			headertitleinnerbox.style.height = boxoffset.titleboxheight.toString() + "px";
		}

		if ( headermenubox != null ) {
			headermenubox.style.width = titleboxwidth.toString() + "px";
			headermenubox.style.bottom = "-"
					+ boxoffset.titleboxmargin.toString() + "px";
		}

		if ( adbox != null ) {
			var adoffset = ( windowwidth - boxoffset.adcolumnwidth
					- 2*boxoffset.adcolumnpadding - boxoffset.adboxwidth )/2;
			adbox.style.left = adoffset.toString() + "px";
			adbox.style.top = boxoffset.adboxtopoffset.toString() + "px";
		}
		if ( taselement.topadboxcontent == null ) {
			boxoffset.texttopoffset = boxoffset.adboxtopoffset;
		}

		if ( textbox != null ) {
			var textboxwidth = windowwidth - boxoffset.adcolumnwidth
					- 2*boxoffset.adcolumnpadding
					- boxoffset.textcolumnpaddingleft - boxoffset.textcolumnpaddingright;
			textbox.style.width = textboxwidth.toString() + "px";
			textbox.style.top = boxoffset.texttopoffset.toString() + "px";

		}

		if ( taselement.sideadbox != null ) {
			taselement.sideadbox.style.top = boxoffset.adcolumntopoffset.toString() + "px";
		}
	}
	else {
		if ( taselement.topadboxcontent != null ) {
			taselement.textbox.style.top = boxoffset.texttopoffset.toString() + "px";
		}
	}

	return;
}

function createFooter()
{
	if ( taselement.navigationfooter == null
			|| taselement.navigationtoclinks == null )
		return;
	var linklist = taselement.navigationtoclinks.getElementsByTagName( "a" );

	if ( linklist == null || linklist.length < 2 )
		return;

	for ( ii = 0; ii < linklist.length; ii++ ) {
		if ( linklist[ii].href == window.location.href ) {
			var table = document.createElement( "table" );
			var row = table.insertRow( 0 );
			var cell;
			var link;
			if ( ii == 0 ) {
				cell = row.insertCell( 0 );
				cell.appendChild( document.createTextNode( "Beginning" ) );
				cell = row.insertCell( 1 );
				cell.appendChild( document.createTextNode( "Previous" ) );
			}
			else {
				link = document.createElement( "a" );
				link.appendChild( document.createTextNode( "Beginning" ) );
				link.href = linklist[0].href;
				link.title = linklist[0].childNodes[0].data;
				cell = row.insertCell( 0 );
				cell.appendChild( link );

				link = document.createElement( "a" );
				link.appendChild( document.createTextNode( "Previous" ) );
				link.href = linklist[ii-1].href;
				link.title = linklist[ii-1].childNodes[0].data;
				cell = row.insertCell( 1 );
				cell.appendChild( link );
			}

			if ( ii >= linklist.length - 1 ) {
				cell = row.insertCell( 2 );
				cell.appendChild( document.createTextNode( "Next" ) );
			}
			else {
				link = document.createElement( "a" );
				link.appendChild( document.createTextNode( "Next" ) );
				link.href = linklist[ii+1].href;
				link.title = linklist[ii+1].childNodes[0].data;
				cell = row.insertCell( 2 );
				cell.appendChild( link );
			}
			taselement.navigationfooter.appendChild( table );
			return;
		}
	}
	return;
}

function removeSelfLinks( element, isnavigation )
{
	if ( element == null )
		return;
	var linklist = element.getElementsByTagName( "a" );
	var newnode;
	var parentnode;
	if ( linklist != null && linklist.length > 0 ) {
		var ii;
		for ( ii = 0; ii < linklist.length; ii++ ) {
			if ( linklist[ii].href == window.location.href ) {
				if ( isnavigation ) {
					newnode = document.createElement( "strong" );
					newnode.appendChild( document.createTextNode( linklist[ii].childNodes[0].data ) );
				}
				else
					newnode = linklist[ii].childNodes[0];
				parentnode = linklist[ii].parentNode;
				parentnode.replaceChild( newnode, linklist[ii] );
			}
		}
	}
	return;
}

var tabindex;

function setTabOrder()
{
	tabindex = 1;
	setTabIndexes( "a", taselement.headerbox );
	setTabIndexes( "input", taselement.searchbox );
	tabindex = taselement.textboxindex;
	setTabIndexes( "a", taselement.textbox );
	return tabindex;
}

function setTabIndexes( tagname, element )
{
	if ( element == null )
		return;
	anchorlist = element.getElementsByTagName( tagname );
	if ( anchorlist != null ) {
		for ( ii = 0; ii < anchorlist.length; ii++ ) {
			anchorlist[ii].tabIndex = tabindex;
			tabindex++;
		}
	}
}

function Page( href, title, description, topic )
{
	this.href = href;
	this.title = title;
	this.desc = description;
	this.topic = topic;
}

new Page( "", "", "" );

function writeNavigationOtherLinks( xmldoc )
{
	var item = xmldoc.getElementsByTagName( "pageList" );
	if ( item == null || item.length == 0 )
		return;

	var pagelist = item[0].getElementsByTagName( "page" );
	var page = new Object();
	var href;
	var title;
	var desc;
	var topic;
	var pageid = null;
	
	var ii;
	for ( ii = 0; ii < pagelist.length; ii++ ) {
		id = pagelist[ii].getAttribute( "id" );
		href = pagelist[ii].getAttribute( "href" );
		if ( href == window.location.pathname )
			pageid = id;

		item = pagelist[ii].getElementsByTagName( "description" );
		if ( item.length > 0 )
			desc = item[0].firstChild.data;
		else
			desc = "Null Desc";

		item = pagelist[ii].getElementsByTagName( "title" );
		if ( item.length > 0 )
			title = item[0].firstChild.data;
		else
			title = "No Name";

		item = pagelist[ii].getElementsByTagName( "topic" );
		if ( item.length > 0 )
			topic = item[0].firstChild.data;
		else
			topic = "No Topic";

		page[id] = new Page( href, title, desc, topic );
	}

	if ( pageid == null ) {
		finish();
		return;
	}

	var pair = xmldoc.getElementsByTagName( "pairList" );
	pair = pair[0].getElementsByTagName( "pair" );
	var linklisthigh = new Array();
	var linklistmedium = new Array();
	var linklistlow = new Array();
	var firstid;
	var secondid;
	var anchor;
	var priority;
	var link;

	var topiclist = new Array();
	var pagelist = new Object();
	nlinks = 0;
	for ( ii = 0; ii < pair.length; ii++ ) {
		item = pair[ii].getElementsByTagName( "page" );
		if ( item.length == 2 ) {
			id = null;
			firstid = item[0].getAttribute( "id" );
			secondid = item[1].getAttribute( "id" );
			if ( firstid == pageid )
				id = secondid;
			else if ( secondid == pageid )
				id = firstid;

			if ( id != null ) {
				link = page[id];
				anchor = document.createElement( "a" );
				anchor.href = link.href;
				if ( link.desc != null )
					anchor.title = link.desc;
				if ( link.title != null )
					anchor.appendChild( document.createTextNode( link.title ) );
				nlinks++;
				if ( pagelist[link.topic] == null ) {
					pagelist[link.topic] = new Array();
					topiclist.push( link.topic );
				}
				pagelist[link.topic].push( anchor );
			}
		}
	}

	if ( nlinks == 0 ) {
		finish();
		return;
	}

	topiclist.sort();

	var sectionitem = xmldoc.getElementsByTagName( "dataName" );
	item = document.createElement( "h2" );
	if ( sectionitem.length > 0 )
		item.appendChild( document.createTextNode(
				sectionitem[0].firstChild.data ) );
	taselement.navigationotherlinks.appendChild( item );

	var jj;
	var sublist;
	for ( ii = 0; ii < topiclist.length; ii++ ) {
		item = document.createElement( "h3" );
		item.appendChild( document.createTextNode( topiclist[ii] ) );
		taselement.navigationotherlinks.appendChild( item );
		sublist = pagelist[topiclist[ii]];
		sublist.sort();
		for ( jj = 0; jj < sublist.length; jj++ )
			appendToNavigationOtherLinks( sublist[jj] );
	}
	finish();
	return;
}

function finish()
{
	removeSelfLinks( taselement.headerbox, false );
	removeSelfLinks( taselement.navigationbox, true );
	return setTabOrder();
}

function appendToNavigationOtherLinks( anchor )
{
	var item = document.createElement( "p" );
	item.className = "related";
	item.appendChild( anchor );
	taselement.navigationotherlinks.appendChild( item );
	return;
}
