// JavaScript Document

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")
		}
	}
}

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.getFullYear()+1)
	document.cookie = "style="+getActiveStylesheet()+"; expires="+expireDate.toGMTString()+"; path=/"
}
		
	