// Taken from "Alternative Style: Working with Alternate Style Sheets"
//    by Paul Sowden @ <http://www.alistapart.com/articles/alternate/>

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 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 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 setupStyle()
{
	if ( ! document.getElementById )
		return;

	// Here we want to read the cookie, if present, and set the preferred
	// active style.

	var cookie = readCookie("navstyle");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}

function setCurrentStyleCookie()
{
	var title = getActiveStyleSheet();
	// Set the cookie only if it's "navtabs".  Otherwise, clear any cookie.
	if ( title == 'navtabs' )
		setCookie('navstyle', title, 365);
	else
		clearCookie('navstyle');
}

addEvent( window, 'load', setupStyle, false );
addEvent( window, 'unload', setCurrentStyleCookie, false );

