function web_standards() {
if (typeof document.body.style.maxHeight != "undefined") {
	return true;
	}
else {
	if (typeof document.getElementById != "undefined") {
		return true;
		}
	else {
		return false;
		}
	}
}

jQuery(document).ready(function() {
	init_style_switcher();
	});
	


function init_style_switcher() {
if (web_standards()) {
	var cookie = readCookie("cm_text_style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	document.getElementById('text_small').onclick = function() {
		setActiveStyleSheet('small_text');
		return false;
		}
	document.getElementById('text_normal').onclick = function() {
		setActiveStyleSheet('default');
		return false;
		}
	document.getElementById('text_large').onclick = function() {
		setActiveStyleSheet('large_text');
		return false;
		}
	}
}

window.onunload = function() {
style_switcher_unload();
}
	
function style_switcher_unload() {
if (web_standards()) {
	var title = getActiveStyleSheet();
	createCookie("cm_text_style", title, 365);
	}
}

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;
    }
  }
  if ((jQuery("a#text_small")) && (jQuery("a#text_normal")) && (jQuery("a#text_large"))) {
  	updateAccessibility();
  	}
}

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 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;
}

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

function updateAccessibility() {
var activeStyle = getActiveStyleSheet();
if (web_standards()) {
	if (activeStyle == "small_text") {
		jQuery("a#text_small").addClass("text_option");
		jQuery("a#text_normal").removeClass("text_option");
		jQuery("a#text_large").removeClass("text_option");
		}
		
	if ((activeStyle == "default") || (activeStyle == null)) {
		jQuery("a#text_small").removeClass("text_option");
		jQuery("a#text_normal").addClass("text_option");
		jQuery("a#text_large").removeClass("text_option");
		}
		
	if (activeStyle == "large_text") {
		jQuery("a#text_small").removeClass("text_option");
		jQuery("a#text_normal").removeClass("text_option");
		jQuery("a#text_large").addClass("text_option");
		}
	}
}

