Event.observe(window, 'load', function() {
	var valueElement = "rel";
	var cookieName = 'mstStyles';
	var browserName = navigator.appName;

	// define where the active style sheet needs to be changed
	var styleObj = $$('head link[type=text/css][rel=stylesheet]')[0];

	// create array of the links with class loadStyle
	$$('a.loadStyle').each(function(a) {

		// base current style based on previous visit from cookie
		if(readCookie(cookieName))
		{
			var pastStyle = readCookie(cookieName);

			if(browserName == 'Microsoft Internet Explorer')
			{
				document.styleSheets[0].href = pastStyle;
			}
			else
			{
				styleObj.setAttribute('href', pastStyle);
			}
		}

		// create observers for each link
		Event.observe(a, 'click', function(event) {
			// when a click happens and the rel element has a value
			if(a.hasAttribute(valueElement))
			{
				// create a JSON string out of the rel value for use
				var valueObj = new String('{'+ a.getAttribute(valueElement) +'}').replace(/(\[|\])/g, '"').evalJSON();

				// catch possible problem
				if(typeof valueObj.sheet == 'undefined') return alert('Sorry, no stylesheet defined'); false;

				// if no problem, change the style sheet
				if(typeof styleObj !== "undefined")
				{
					if(browserName == 'Microsoft Internet Explorer')
					{
						document.styleSheets[0].href = valueObj.sheet;
					}
					else
					{
						styleObj.setAttribute('href', valueObj.sheet);
					}

					// set a 30 day cookie to remember the latest selection
					createCookie(cookieName, valueObj.sheet, 30);
				}
				else return alert('switchStyle: stylesheet definition not found on this page'); false;
			}
			Event.stop(event);
		}, false);
	});
});

// cookie functions by http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}