// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/

var sz = 9; //default stylesheet

function setActiveStyleSheet(argStylesheetsTitle, argVal)
{
	// link items - that is, stylesheets
	var li;
	var title = argStylesheetsTitle ? argStylesheetsTitle : '';
	var val = argVal ? argVal : '';

	//make sure we're under limit
	switch(title)
	{
		case 'typo_size' :

			if(val != '') val = eval(sz + ' +  '  + val);
			
			if (8 < val && val < 16)
			{
				sz = val;
				createCookie("typo_size", sz, 365);	
			}
		
			for(var i = 0; i < document.getElementsByTagName("link").length; i++)
			{
				var li = document.getElementsByTagName("link")[i];
				
				if(li.getAttribute("rel").indexOf("style") != -1 && li.getAttribute("title"))
				{				
					if(li.getAttribute("title").indexOf("typo_size") != -1) li.disabled = true;
				
					if (li.getAttribute("title").indexOf("typo_size" + String(sz)) > -1)li.disabled = false;
				}
			}
		
			break;
			
		case 'typo_contrast' :
			var tempo = readCookie(title, 'false');
			
			val = false;
			if(String(tempo) == "true") val = true;
			
			var disable = true;
			
			if(argVal == 'switch')
			{
				val = !val;
				createCookie(title, String(val), 365);
			}
			
			if(val) disable = false;
		
			for(var i = 0; i < document.getElementsByTagName("link").length; i++)
			{
				var li = document.getElementsByTagName("link")[i];
				
				// get stylesheets
				if(li.getAttribute("rel").indexOf("style") != -1 && li.getAttribute("title"))
				{
					li.disabled = true;
					
					if(li.getAttribute("title").indexOf(title) > -1)
					{
						li.disabled = disable;
					}	
				}
			}	
			setTypoSize();
			
			break;
	}
}


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 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 SetCookie() {
	if(arguments.length < 2) { return; }
	var n = arguments[0];
	var v = arguments[1];
	var d = 0;
	if(arguments.length > 2) { d = parseInt(arguments[2]); }
	var exp = '';
	if(d > 0)
	{
		var now = new Date();
		then = now.getTime() + (d * 24 * 60 * 60 * 1000);
		now.setTime(then);
		exp = '; expires=' + now.toGMTString();
	}
	document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()


function readCookie(argNameCookie, argValDefault)
{
	var cookiecontent = new String();
	
	if(document.cookie.length > 0)
	{
		var cookiename = argNameCookie + '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1)
		{
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			
			if(cookieend < cookiebegin)
			{
				cookieend = document.cookie.length;
			}
			
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
		}
		
		
	}
	
	cookiecontent = unescape(cookiecontent);
	if(argValDefault != undefined && cookiecontent == '') cookiecontent = argValDefault;
	
	return cookiecontent;
} // function ReadCookie()

function readCookie(name, argValDefault)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	var val = null;
	
	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)
		{
			val = c.substring(nameEQ.length,c.length);
		}
	}
	
	if(val == null) val = argValDefault;
	
	return val;
}

function setTypoSize()
{
	var cookie = readCookie("typo_size");
	sz = cookie ? cookie : sz;
	setActiveStyleSheet('typo_size');
}
setTypoSize();

/*var tempo = readCookie("typo_contrast", "false");
//alert(String(tempo));
if(String(tempo) == "true") 
{
	setActiveStyleSheet('typo_contrast', 'switch');
}*/
