//from ppk: 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=/";
}

//from ppk: http://www.quirksmode.org/js/cookies.html
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 emZoom(magnification) {
	createCookie('emZoom', magnification, 360);
}
//and a hack for the inability to pass vairalbes to generated onclick events
function emZoom0() {
	emZoom(.8);
}
function emZoom1() {
	emZoom(1);
}
function emZoom2() {
	emZoom(1.4);
}
function emZoom3() {
	emZoom(2);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if(oldonload) {
				oldonload();
			}
		func();
	    }
	}
}

//show the Em Zoom user interface
addLoadEvent(
	function() {
		var
			parentID="siteTools",
			childTag="li";

		createCookie('emZoomTest', 'emZoomTest', 1);
		var 
			x = readCookie('emZoomTest'),
			y,
			magnification = 1,
			magLevels = new Array(.8, 1, 1.4, 2),
			magSize = new Array("S", "M", "L", "XL"),
			magPercent,
			magLevel,
			newElement;
		if(x) {
			if(readCookie('emZoom')) {
				magnification = readCookie('emZoom');
			}
			if(document.getElementById(parentID)) {
				var childElement = document.createElement(childTag);
				var txt = document.createTextNode("Page Size: ");
				childElement.appendChild(txt);
				childElement.setAttribute("id", "emZoom");
				
				for (y in magLevels) {
					magLevel = magLevels[y];
					magPercent = magLevel * 100;
					if(magLevel == magnification) {
						newElement = document.createElement("span");
						newElement.setAttribute("id", "currentZoom"); //a class would be more appropriate here, but IE chokes
						txt = document.createTextNode(magSize[y]);
						newElement.appendChild(txt);
						childElement.appendChild(newElement);
						txt = document.createTextNode(" ");
						childElement.appendChild(txt);
					} else {
						newElement = document.createElement("a");
						newElement.setAttribute("href", "");
						var idVal="mag"+y;
						newElement.setAttribute("id", idVal); //need to set ID so we can use DOM[0] to set onclick events later... can't use setAttribute for onclick, as IE chokes						
//						newElement.setAttribute("onclick", "emZoom("+magLevel+"); return false;");
						txt = document.createTextNode(magSize[y]);
						newElement.appendChild(txt);
						childElement.appendChild(newElement);
						txt = document.createTextNode(" ");
						childElement.appendChild(txt);
					}
				}
				var parentElement = document.getElementById(parentID);
				parentElement.appendChild(childElement);
				
				//now we add the onclick events
				var theElement=new Array();
				var z=0;
				for (z in magLevels) {
					var idVal="mag"+z;
					if(document.getElementById(idVal)) {
						theElement[z] = document.getElementById(idVal);
						if(0==z){
							theElement[z].onclick = emZoom0;
						}else if(1==z){
							theElement[z].onclick = emZoom1;
						}else if(2==z){
							theElement[z].onclick = emZoom2;
						}else if(3==z){
							theElement[z].onclick = emZoom3;
						}
					}
				
				}

			}
		}
	}
);