/*	Loosely based on http://www.alistapart.com/stories/alternate/styleswitcher.js
	with a whole lot of updating
*/

function setActiveStyleSheet(title) {
	linksFound = document.getElementsByTagName("link")
	for (i=0; i<linksFound.length; i++) {
		thisLink = linksFound[i]
    	if (thisLink.getAttribute("rel").indexOf("style") > -1 && thisLink.getAttribute("title")) {
			thisLink.disabled = true
			if (thisLink.getAttribute("title") == title) {
				thisLink.disabled = false
			}
		}
	}
}

function getActiveStyleSheet() {
	linksFound = document.getElementsByTagName("link")
	for (i=0; i<linksFound.length; i++) {
		thisLink = linksFound[i]
    	if (thisLink.getAttribute("rel").indexOf("style") > -1 && thisLink.getAttribute("title") && !thisLink.disabled) {
			return thisLink.getAttribute("title")
		}
	}
}

function getPreferredStyleSheet() {
	linksFound = document.getElementsByTagName("link")
	for (i=0; i<linksFound.length; i++) {
		thisLink = linksFound[i]
		if (thisLink.getAttribute("rel").indexOf("style") > -1 && thisLink.getAttribute("rel").indexOf("alt") == -1 && thisLink.getAttribute("title")) {
		 	return thisLink.getAttribute("title")
		}
	}
}

// MT cookies
function cookieVal(cookieName) {
	thisCookie = document.cookie.split("; ")
	for (i=0; i<thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1]
		}
	}
	return ""
}

window.onload = function() {
	thisCookie = cookieVal("style")
	if (thisCookie) {
		title = thisCookie
	}
	else {
		title = getPreferredStyleSheet()
	}
	setActiveStyleSheet(title)
}

window.onunload = function() {
	expireDate = new Date
	expireDate.setYear(expireDate.getYear()+1)
	document.cookie = "style="+getActiveStyleSheet()+"; expires="+expireDate.toGMTString()+"; path=/"
}

/* Show Hide menus - see Designing with Web Standards, by Jeffrey Zeldman, Chapter 15 */
function toggleMenu(currMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currMenu).style
		if (thisMenu.display == "block") {
			thisMenu.display = "none"
		}
		else {
			thisMenu.display = "block"
		}
		return false
	}
	else {
		return true
	}
}

// AutoBlink
// Puts Google's Autolink on the Blink :)
// (c) 2005 Chris Ridings   http://www.searchguild.com
// Redistribute at will but leave this message intact

var linkcount;
function checklinks() {
	if (!(linkcount==document.links.length)) {
		// Something changed the links!
		// Iterate for an id of _goog
		for (i=0; i < document.links.length; i++) {
			if (document.links[i].id.substring(0,5)=="_goog") {
				// If we find an id of _goog then remove the link!
				var tr = document.links[i].parentTextEdit.createTextRange();
				tr.moveToElementText(document.links[i]);
				tr.execCommand("Unlink",false);
				tr.execCommand("Unselect",false);
			}
		}
	}
		setTimeout("checklinks()",500);
}
if (document.getElementById && document.createElement) {
	linkcount=document.links.length;
	setTimeout("checklinks()",500);
}
