//* Paul Snowden's JavaScript style switcher from http://www.alistapart.com/articles/alternate/  */


//* Data-driven javascript - the following array holds details of the stylesheet options:
//*	the CSS id of the selector button and title attribute of the corresponding CSS file as declared in the HTML <head>
//* This approach allows a simple method of changing or adding options without intervention in the javascript code
//* NOTE:  row(0) of the array is effectively just headings and is ignored in the code that follows

var param_styleOptions =
[
	['**button CSS ID**',		'**CSS stylesheet title**'],
	['sidebar-show',			'sidebar-show'],
	['sidebar-hide',			'sidebar-hide']
	
];

//* function to set the active style sheet */
function setActiveStyleSheet(title)
{
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

//* function to determine the currently active style sheet */
function getActiveStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled
			) return a.getAttribute("title");
	}
	return null;
}

//* function to get the required style sheet */
function getPreferredStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("rel").indexOf("alt") == -1
			&& a.getAttribute("title")
			) return a.getAttribute("title");
	}
	return null;
}

//* function to create a cookie to store the active stylesheet so that it can be used in all pages during the current session */
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 expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//* function to read the cookie if present and determine the required style sheet */
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;
}

//* detect when a page is loaded and determine the preferred style sheet from any existing cookie */
window.onload = function(e)
{
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}

//* detect when the current page is unloaded and cause the current stylesheet to be stored in a cookie, which expires in 365 days */
window.onunload = function(e)
{
	var title = getActiveStyleSheet();
	createCookie("style", title, 365);
}

//*  */
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

//* function to set up the required clickhandlers */
function styleButtonClickHandler(styleId, cssFile)
{
	var styleChoice =
	{
		init: function()
		{
			var link = document.getElementById(styleId);
//			alert("clickHandler active for CSS id: "+styleId+", "+cssFile);
			link.onclick = styleChoice.clickHandler;
		},
		clickHandler: function()
		{
			setActiveStyleSheet(cssFile);
			return false;
		}
	};
	Core.start(styleChoice);		
}

//* cycle throught the available options and call the clickhandler setting function for each */
for (var i = 1; i < param_styleOptions.length; i++)
{
	var nextStyle = param_styleOptions[i];
	var styleId = nextStyle[0];
	var cssFile = nextStyle[1];
	styleButtonClickHandler(styleId, cssFile);
}

