var urlBase = document.URL;
urlBase = urlBase.replace(/^.*:\/\/[^\/]*/, '');
urlBase = urlBase.replace(/(\?|#).*$/, '');
var styleSheetLocation = document.getElementsByTagName('link').item(0).getAttribute('href');
while (styleSheetLocation.match(/^\.\.\//)) {
	urlBase = urlBase.replace(/[^\/]+\/$/, '');
	styleSheetLocation = styleSheetLocation.replace(/^\.\.\//, '');
}

window.onload = function () {
	googleAnalytics();
	alternativeStyleSheet('index-alt.css', 'White');
//	addMenuItem(urlBase + 'teams/', 'Teams');
//	addMenuItem(urlBase + 'join/', 'Join');
	addMenuItem(urlBase + 'regional/', 'Regional');
}

function googleAnalytics() {
	loadJavascript('http://www.google-analytics.com/urchin.js');
	_uacct = 'UA-2710560-1';
	setTimeout('urchinTracker()', 1000);
}

function loadJavascript(url) {
	var headTag = document.getElementsByTagName('head').item(0);
	var scriptTag = document.createElement('script');
	scriptTag.setAttribute('type', 'text/javascript');
	scriptTag.setAttribute('src', url);
	headTag.appendChild(scriptTag);
}

function alternativeStyleSheet(url, title) {
	var defaultStyleSheet = document.getElementsByTagName('link').item(0);
	defaultStyleSheet.setAttribute('title', 'Default');
	if (!(url.match(/\//))) {
		url = defaultStyleSheet.getAttribute('href').replace(/[^\/]+$/, url);
	}
	var altStyleSheet = document.createElement('link');
	altStyleSheet.setAttribute('rel', 'alternate stylesheet');
	altStyleSheet.setAttribute('type', 'text/css');
	altStyleSheet.setAttribute('title', title);
	altStyleSheet.setAttribute('href', url);
	document.getElementsByTagName('head').item(0).appendChild(altStyleSheet);
}

function addMenuItem(url, title) {
	var uls = document.getElementsByTagName('ul');
	var menu;
	for ( i = 0; i < uls.length; i++ ) {
		if ( uls[i].className == 'menu') {
			menu = uls[i];
		}
	}
	var liTag = document.createElement('li');
	var aTag = document.createElement('a');
	aTag.setAttribute('href', url);
	aTag.appendChild(document.createTextNode(title));
	liTag.appendChild(aTag);
	if (document.URL.match(url)) {
		liTag.className = 'selected';
	}
	menu.appendChild(liTag);
}
