/*
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;
    }
  }
}
*/

/*-------------------------------------------------------------------
Font Resize Functions
---------------------------------------------------------------------*/

//fontSize config
var atb_fontsize_debug 				= false;			//Display debugging information on setting font size
var atb_fontsize_applyto 			= "Container";		//ID of page element holding content
var atb_fontsize_cookiename 		= "MKCFontSize";		//Cookie name under which to store font size
var atb_fontsize_cookielifetime 	= 365;				//Lifetime (days) of above cookie
var atb_fontsize_default 			= 1.0;				//Default font size in em
var atb_fontsize_max 				= 1.6;				//Maximum size allowed in em
var atb_fontsize_min 				= 0.9;				//Minimim size allowed in em


function fontResizeBy(amount) {
	var cookie = readCookie(atb_fontsize_cookiename);
	var cursize, newsize;

	if(cookie) { cursize = parseFloat(cookie); }
	else { cursize = atb_fontsize_default; }
		
	cursize = Math.round(cursize * 10) / 10; //round to 1dp
	
	newsize = cursize+amount;
	newsize = fontCheckSize(newsize);		//check size is allowed	
	newsize = Math.round(newsize * 10) / 10; //round to 1dp
	
	createCookie(atb_fontsize_cookiename, newsize, atb_fontsize_cookielifetime);		//Save newsize to cookie
	document.getElementById(atb_fontsize_applyto).style.fontSize = newsize + "em";		//Apply newsize to document
		
	if (atb_fontsize_debug) {
		debug("Debug: [cookie=" +cookie+ "]; [cursize=" +cursize+ "]; [newsize=" +newsize+ "]<br>", false);
	}
	
}

function cookiefontResizeTo(size) {
	document.getElementById(atb_fontsize_applyto).style.fontSize = size + "em";		//Apply newsize to document
}

function fontResizeTo() {
	var args = fontResizeTo.arguments;
	var newsize = atb_fontsize_default;
	if (args.length > 0) { newsize = args[0]; }
	
	newsize = fontCheckSize(newsize);		//check size is allowed
	newsize = Math.round(newsize * 10) / 10; //round to 1dp
	
	createCookie(atb_fontsize_cookiename, newsize, atb_fontsize_cookielifetime);		//Save newsize to cookie
	document.getElementById(atb_fontsize_applyto).style.fontSize = newsize + "em";		//Apply newsize to document
	
	if (atb_fontsize_debug) {
		debug("Debug: [newsize=" +newsize+ "]<br>", false);
	}
	
}

function fontCheckSize(size) {
	if (size > atb_fontsize_max) { 
		return atb_fontsize_max;
	}
	else if (size < atb_fontsize_min) { 
		return atb_fontsize_min;
	}
	else {
		return size	
	}
}

/////////////////////////////////////////////////////////////////

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 (document.getElementById('lnkPageStyle_' + a.getAttribute("title"))) {
				document.getElementById('lnkPageStyle_' + a.getAttribute("title")).className = null;
			}
			
			if(a.getAttribute("title") == title) {
				a.disabled = false;
				
				if (document.getElementById('lnkPageStyle_' + a.getAttribute("title"))) {
					document.getElementById('lnkPageStyle_' + a.getAttribute("title")).className = 'hlite';
				}
				//createCookie(atb_pagestyle_cookiename, title, atb_pagestyle_cookielifetime);
			}
		}
	}
}


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;
}

/*-------------------------------------------------------------------
Debugging
---------------------------------------------------------------------*/
function debug(msg, append) {
	if (!document.getElementById(atb_debug_element_id)) {
		var div = document.getElementById(atb_content);
		var newnode = document.createElement("p");
		newnode.id = atb_debug_element_id;
		newnode.innerHTML = msg;
		div.appendChild(newnode);
	}
	else {
		if (append) {
			document.getElementById(atb_debug_element_id).innerHTML += msg;
		}
		else {
			document.getElementById(atb_debug_element_id).innerHTML = msg;
		}
	}
}


window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  
  var fontcookie = readCookie("MKCFontSize");
  if (fontcookie != null) {
  	cookiefontResizeTo(fontcookie)
  }
  
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

