 
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.getAttribute("title") != 'Default') { // never disable the default
       a.disabled = true;
      // alert (a.title + ' ' + title.value);
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function quickSwitch(size)
{
	var fontSizePref = size;
	document.body.style.fontSize = size;
}

function applyPreview(elem)
{
	//font-size 0
	// font-family 1
	// text-transform 2

	//var elemParts=elem.name.split('.');
	//var elementName=elemParts[0];
	var elementAttr=elem.name;
//	var theForm=document.getElementById("customStyler");
	
	switch (elementAttr)
	{
		case 'font-size':
		case 0:
			var fontSizePref=elem.value;
			document.body.style.fontSize=elem.value;
			//if (theForm)//x.options[x.selectedIndex].text
			//	theForm.fontSize.selectedIndex=2;
				
			
		break;
		case 'font-family':
		case 1:
			var fontFamilyPref=elem.value;
			document.body.style.fontFamily=elem.value;
			//	document.p.style.fontFamily=elem.value;
			//alert(elem.value);
			var ptags= document.getElementsByTagName("p");
				for (i=0; i<ptags.length; i++)
				{
					ptags[i].style.fontFamily=(elem.value ? elem.value : 'Verdana, Arial, Helvetica, Sans-serif');
				}
			var h1tags=document.getElementsByTagName("h1");
				for (i=0; i<h1tags.length; i++)
				{
					h1tags[i].style.fontFamily=(elem.value ? elem.value : 'Times New Roman, serif');
				}
			var h2tags=document.getElementsByTagName("h2");
				for (i=0; i<h2tags.length; i++)
				{
					h2tags[i].style.fontFamily=(elem.value ? elem.value : 'Verdana, Arial, Helvetica, Sans-serif');
				}
			var menu=document.getElementById("menu");
				menu.style.fontFamily=(elem.value ? elem.value : 'Times New Roman, serif');
		break;
		case 'text-transform':
		case 2:
			var textTransformPref=elem.value;
			document.body.style.textTransform=elem.value;
		break;
	}

}


function applyStylePrefs()
{
	var cookie=readCookie("stylePrefs");
	var prefs = cookie ? cookie : '';
	if (prefs.indexOf(":") != -1)
	{
		var prefarray = prefs.split(':');
		for (x=0; x < prefarray.length; x++)
		{
			if (prefarray[x].length > 0)
			{
				elem=new Object;
				elem.value=prefarray[x];
				elem.name=x;
				//alert(elem.name + ' ' + elem.value);
				applyPreview(elem);
			}
		}
	}
}

function readStylePrefs()
{
//	alert(document.body.style.fontSize);/
//	alert(document.body.style.textTransform);
//	alert(document.getElementsByTagName("p")[0].style.fontFamily)
//	alert(document.getElementById("menu").style.fontFamily)
	return document.body.style.fontSize + ':' + document.body.style.fontFamily +  ':'+ document.body.style.textTransform;
//	return document.body.style.fontSize + ':' + 
//		(document.getElementById("menu").style.fontFamily == document.body.style.fontFamily ? document.body.style.fontFamily : '')
//		+ ':'+ document.body.style.textTransform;
	
}

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 && a.getAttribute("title") != 'Default') 
    	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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  applyStylePrefs();
}



window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
  var stylePrefs=readStylePrefs();
  createCookie("stylePrefs", stylePrefs, 365);
}

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

