
function fontSizeUp() {
	var active = getActiveStyleSheet();
	enableLink('increaseFont');
	enableLink('decreaseFont');
	switch (active) {
		case 'A--' :
			setActiveStyleSheet('A-');
			break;
		case 'A-' :
			setActiveStyleSheet('A');
			break;
		case 'A' :
			setActiveStyleSheet('A+');
			break;
		case 'A+' :
			setActiveStyleSheet('A++');
			break;
		case 'A++' :
			setActiveStyleSheet('A+++');
			disableLink('increaseFont');
			break;
		case 'A+++' :
			disableLink('increaseFont');
			break;
		default :
			setActiveStyleSheet('A');
			break;
	}
}

function fontSizeDown() {
	var active = getActiveStyleSheet();
	enableLink('increaseFont');
	enableLink('decreaseFont');
	switch (active) {
		case 'A+++' :
			setActiveStyleSheet('A++');
			break;
		case 'A++' :
			setActiveStyleSheet('A+');
			break;
		case 'A+' :
			setActiveStyleSheet('A');
			break;
		case 'A' :
			setActiveStyleSheet('A-');
			break;
		case 'A-' :
			setActiveStyleSheet('A--');
			disableLink('decreaseFont');
			break;
		case 'A--' :
			disableLink('decreaseFont');
			break;
		default :
			setActiveStyleSheet('A');
			break;
	}
}

function defaultFontSize() {
	var defaultFont = getPreferredStyleSheet();
	enableLink('increaseFont');
	enableLink('decreaseFont');
	setActiveStyleSheet(defaultFont);
}

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 macUnixTest() {
	var agent = navigator.userAgent.toLowerCase();
	var isMacOrUnix  = ((agent.indexOf('mac')!=-1) || 
						(agent.indexOf('x11')!=-1) || (agent.indexOf('sunos')!=-1) || 
						(agent.indexOf('irix')!=-1) || (agent.indexOf('hp-ux')!=-1) || 
						(agent.indexOf('sco')!=-1) || (agent.indexOf('unix_system_v')!=-1) || 
						(agent.indexOf('ncr')!=-1) || (agent.indexOf('reliantunix')!=-1) || 
						(agent.indexOf('dec')!=-1) || (agent.indexOf('sinix')!=-1) || 
						(agent.indexOf('aix')!=-1) || (agent.indexOf('inux')!=-1) || 
						(agent.indexOf('bsd')!=-1) || (agent.indexOf('freebsd')!=-1));
	return (isMacOrUnix);
}

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() {
	if (macUnixTest()) {
		return ('A+');
	} else {
		return ('A');
	}
}

/*----------------------------------------------------
   Create cookie
-----------------------------------------------------*/
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=/';
}

/*----------------------------------------------------
   Read cookie details with given name
-----------------------------------------------------*/
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;
}

/*----------------------------------------------------
   Set given link class to 'enabled'
-----------------------------------------------------*/
function enableLink(linkId) {
	var link = document.getElementById(linkId);
	link.className = 'fontSize enabled';
}

/*----------------------------------------------------
   Set given link class to 'disabled'
-----------------------------------------------------*/
function disableLink(linkId) {
	var link = document.getElementById(linkId);
	link.className = 'fontSize disabled';
}

/*----------------------------------------------------
   Create containing box and links for font size
   widget
-----------------------------------------------------*/
function fontChanger(divId) {
	var div, linkDecrease, linkReset, linkIncrease;
	div = createHtmlElement('div', {'id' : 'fontBox', 'text' : 'Font size:'});
	document.getElementById(divId).appendChild(div);
	
	linkDecrease  = createHtmlElement('a', {'class' : 'fontSize enabled', 'title' : 'Decrease font size', 'href' : '', 'text' : '-', 'id' : 'decreaseFont'});
	div.appendChild(linkDecrease);
	linkDecrease.onclick = function() { fontSizeDown(); return false; }
	
	linkReset  = createHtmlElement('a', {'class' : 'fontSize enabled', 'title' : 'Reset to default font size', 'href' : '', 'text' : 'A', 'id' : 'defaultFont'});
	div.appendChild(linkReset);
	linkReset.onclick = function() { defaultFontSize(); return false; }
	
	linkIncrease  = createHtmlElement('a', {'class' : 'fontSize enabled', 'title' : 'Increase font size', 'href' : '', 'text' : '+', 'id' : 'increaseFont'});
	div.appendChild(linkIncrease);
	linkIncrease.onclick = function() { fontSizeUp(); return false; }
	return true;
}

// Output HTML element
function createHtmlElement(name, attributes) {
	var element = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml',name) : document.createElement(name);
	if (typeof attributes != 'undefined') {
		for (var i in attributes) {
			if (i == 'text') {
				element.appendChild(document.createTextNode(attributes[i]));
			} else if (i == 'class') {
				element.className = attributes[i];
			} else {
				element.setAttribute(i, attributes[i]);
			}
		}
	}
	return(element);
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100) ? 99.999 : opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity: "+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

var fadeTimeoutId;

function fadeIn(objId, opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 20;
			fadeTimeoutId = window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 10);
		}
	}
}

// init function
function init() {
	var cookie = readCookie('edcFontSize');
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	// Check 4 W3C DOM support
	var W3CDOM = (document.createElement && document.getElementsByTagName);
	if (!W3CDOM) return;
	var fontChange = new fontChanger('mainMenu');
	
	var mainContent = document.getElementById('content');
	var sidebar = document.getElementById('sidebar');
	// Check for links to filetypes and show relevant icons
	styleFileLinks(mainContent);
	if (sidebar != null) styleFileLinks(sidebar);
	// Check for figures and set size
	sizeFigures(mainContent);
	// Check for external link and set appropriate class
	styleExtLinks(mainContent);

	// Check for project lists - wtf? 
//	var pList = getElementsByClassName(mainContent, 'projectList');
//	if (pList.length > 0) {
//		styleProjectList(pList);
//	}

	// make a list of footnotes containing external links when printing
	// footnoteLinks(mainContent);
}

// exit function
function goodbye() { 
	var title = getActiveStyleSheet();
	createCookie('edcFontSize', title, 365);
}

// *** Main code ***

// gecko, safari, konqueror and generic
if (typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', init, false);
	window.addEventListener('unload', goodbye, false);
}
// opera 7
else if (typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', init, false);
	document.addEventListener('unload', goodbye, false);
}
// win/ie
else if (typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload', init);
	window.attachEvent('onunload', goodbye);
}

var cookie = readCookie("edcFontSize");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);

