// Title: Stylesheet Switcher
// Version: 1.0
// Date: 11/22/2006
// Author: Adam Wilson
// Copyright © 2006 International Digital Design Group
// Unauthorized duplication or use of this script is prohibited by copyright law.

// Switches the active stylesheet
// title = title of stylesheet to enable
function setActiveStyleSheet(title) {
	var i, a, main;
	// loop through all stylesheets
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		// check for "rel" attribute containing "style" and having a "title" attribute
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			// disable stylesheet
			a.disabled = true;
			// if stylesheet's title matches title passed in to function, then enable it
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

// Gets the title of the active stylesheet
function getActiveStyleSheet() {
	var i, a;
	// loop through all stylesheets
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		// if "rel" attribute contains "style" and it has a "title" attribute and it's not disabled, then return the "title" attribute
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

// Gets the title of the first non-alternate stylesheet
function getPreferredStyleSheet() {
	var i, a;
	// loop through all stylesheets
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		// if "rel" attribute contains "style" and "rel" attribute doesn't contain "alt" and it has a "title" attribute, then return the "title" attribute
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
	}
	// otherwise, return null
	return null;
}

// Creates a cookie in the browser
// name = cookie name
// value = cookie value
// days = days before expires
function createCookie(name,value,days) {
	// check if days parameter was passed in
	if (days) {
		// it was, so set expiration date
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	} else {
		// otherwise, so to expire when browser closes
		expires = '';
	}
	// set cookie for entire site
	document.cookie = name+'='+value+expires+'; path=/';
}

// Reads a cookie from the browser
// name = cookie name
function readCookie(name) {
	name += '=';
	// make array of cookies
	var ca = document.cookie.split(';');
	// loop through all cookies
	for(var i=0;i < ca.length;i++) {
		// get individual cookie
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
	}
	return null;
}

// gets the default style from either cookie setting or from scanning active stylesheets
function getDefaultStyle() {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	return title;
}

// set the active stylesheet to the stylesheet matching "title" and stores the setting in a "style" cookie
function switchStyle(title) {
	setActiveStyleSheet(title);
	createCookie("style", title, 30);
	if (window.activeStyleLink) window.activeStyleLink.className = '';
	window.activeStyleLink = document.getElementById('stl'+title);
	if (window.activeStyleLink) window.activeStyleLink.className = 'hilite';
}
