/** Client-side font resizing code
 * Run addResizeButtons() onload, and if the browser supports 
 * enough JS and DOM, it will add buttons to enable increasing 
 * and decreasing the font size.
 */

// Detection functions courtesy of http://www.quirksmode.org/js/detect.html
var detect = navigator.userAgent.toLowerCase();
function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
 
function addResizeButtons () {
	if (window.navigator.cookieEnabled && document.getElementById) { 
			/* *** load previous setting from cookie *** */
			loadSize();
	
			/*  *** Add controls *** */
			//var container = document.getElementById("container");
			var banner = document.getElementById("banner");
			
			// Div to hold resize links
			var resizeDiv = document.createElement("div");
			resizeDiv.setAttribute("id", "resizeButtons")
			//container.insertBefore(resizeDiv, banner);
			banner.insertBefore(resizeDiv, banner.firstChild);
			
			// smaller link
			var smallerLink = imgLink("#", "fontSize('smaller'); return false;", "/inc/img/fs-s.png", "Decrease text size");
			resizeDiv.appendChild(smallerLink);
			
			// bigger link
			var biggerLink = imgLink("#", "fontSize('bigger'); return false;", "/inc/img/fs-b.png", "Increase text size");
			resizeDiv.appendChild(biggerLink);
	}
}

function imgLink (href, onclick, src, title) {
  // In IE .createElement() is broken

  if (checkIt('msie')
  ) {
	  var newLink = document.createElement("<a href=\""+href+"\" onclick=\""+onclick+"\"/>");
	} else {
		var newLink = document.createElement("a");
		newLink.setAttribute("href", href);
		newLink.setAttribute("onclick", onclick);
	}

	if (src) {
		var newImg = document.createElement("img");
		newImg.setAttribute("src", src);
		newImg.setAttribute("title", title);
		newImg.setAttribute("alt", title);
		newLink.appendChild(newImg);
	} else {
    newLink.textContent = title;
	}
	
	return newLink;
}

function fontSize (change) {
	var bodyStyle = document.getElementsByTagName("body").item(0).style;
	if (bodyStyle.fontSize == "") {
	  // This has no value in the DOM initially.
		bodyStyle.fontSize = "80%";
	}
	
	var fs = bodyStyle.fontSize;
	var oldSize = Number(fs.substr(0, fs.length-1));
	
	if (change == "bigger") {
		newSize = oldSize + 10;
	} else if (oldSize >= 80) {
		newSize = oldSize - 10;
	}
	
	bodyStyle.fontSize = newSize + "%";
	
	// Save to cookie
	createCookie("fs",newSize + "%","999")
}

// Load size from a cookie (if present), and use it.
function loadSize () {
	// For each name:value in the cookie
  size = readCookie("fs");
	
	if (size) {
		bodyStyle = document.getElementsByTagName("body").item(0).style;
		bodyStyle.fontSize = size;
	}
}

// Cookie functions courtesy of http://www.quirksmode.org/js/cookies.html
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 var 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;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
