var agt = navigator.userAgent.toLowerCase();

var fontSizeCookieName = 'fontSizeCES';
var sizeA = new Array('x-small','small','medium','large','x-large');

if(agt.indexOf('msie') != -1){
			sizeA.length=4;
		}

var currentSizeAIndex = 3; 

/********************************************************************
	Let's see if there are cookies, and if there use the fontSize cookie value for
	currentSizeAIndex and add some rules to set the page's font-family and font-size
	to the correct keyword.
*/
if (readCookie(fontSizeCookieName)!='') {
	currentSizeAIndex = readCookie(fontSizeCookieName)
	rule='<style type="text\/css">@media screen{body{font-size: '+sizeA[currentSizeAIndex]+';}}<\/style>';
	document.write(rule)
	}

/********************************************************************
	changeFontSize() is the function that changes the font-size. It takes one
	parameter, an integer. Ideally that integer is 1 or -1, which either increases or
	decreases the font-size by one keyword.
*/
function changeFontSize(i) {
	var newSizeAIndex = currentSizeAIndex-(-i);

	if (newSizeAIndex<0 || newSizeAIndex==sizeA.length) return false;
	activateButton('sizeUp');
	activateButton('sizeDown');
	if (newSizeAIndex==0) fadeButton('sizeDown');
	if (newSizeAIndex==sizeA.length-1) fadeButton('sizeUp');
			
	document.body.style.fontSize=sizeA[newSizeAIndex];
	currentSizeAIndex = newSizeAIndex;
	// let's stuff it in a cookie so we can remember it
	setCookie(fontSizeCookieName,currentSizeAIndex);
	// NS6 won't properly apply the changes in abs positioned divs unless we do
	// this trickery:
 	//if (!document.all) { 
 	//	e = document.getElementsByTagName('body')[0];
 	//	e.parentNode.replaceChild(e,e);
 	//	}
	//menu.moveEm()
	//if(agt.indexOf('msie') != -1){
		
	 //}else{
 		//window.location.href = unescape(window.location.pathname);
	 //}
	}
	

/********************************************************************
	writeControls() inserts the form in your document for browsers that these scripts
	work with, which right now are IE5+ Mac and PC, and Mozilla and Mozilla-based
	browsers (NS6).
	
	I would have liked to have done this without innerHTML, but trying it with pure
	W3C DOM methods like createElement and insertBefore created too many
	problems related to different browser implementations. It came down to the fact
	that when using abs positioned divs, NS6 did not apply changes made with the
	form controls until i added the following bit to changeFontSize and
	changeFontFamily:
			e = document.getElementsByTagName('body')[0];
			e.parentNode.replaceChild(e,e);
	
	Problem is, that this causes NS6 to hang when nodes have been inserted into
	the document via insertbefore() or appendChild(). So I'm sticking with innerHTML
	for now.
*/

function writeControls() {	
	if (document.getElementsByTagName) { // kosher
		var formDiv = document.getElementById('preferences');
		if (!formDiv) {
			alert('<div id="preferences"></div> must appear in your markup where you want the form to appear\n(and before <script type="text/javascript" id="controlsScript">writeControls()</script>)');
			return;
			}
		
		// Because NS6 will try to call writeControls() every time the
		// e.parentNode.replaceChild(e,e) trick is used when writeControls()
		// is called from within a script element in the body of the HTML doc:
		if (formDiv.innerHTML != '') return;
		
		var theForm='<form id="preferencesForm" onsubmit="return false">';

		theForm+='<input type="image" src="/images/font-decrease.gif" id="sizeDown" class="preferencesEl" onclick="changeFontSize(-1)"value=" A - " >'
		theForm+='&nbsp;<input type="image" src="/images/font-increase.gif" id="sizeUp" class="preferencesEl" onclick="changeFontSize(1)" value=" A+ ">'		
		theForm+='</form>';

		formDiv.innerHTML = theForm;
		
		if (currentSizeAIndex==0) {
			fadeButton('sizeDown');
		} else if (currentSizeAIndex==sizeA.length-1) {
			fadeButton('sizeUp');
			}
		}
	}

function fadeButton(bId) {
	var button = document.getElementById(bId);
	button.blur();
	button.disabled='disabled';
	button.style.borderColor='#999';
	button.style.color='#999';
	}
	
function activateButton(bId) {
	var button = document.getElementById(bId);
	button.disabled=null;
	button.style.borderColor='#000';
	button.style.color='#000';
	}

/********************************************************************
	cookie functions modified from code found at Alexei Kourbatov's
	javascripter.net/faq/
*/
function setCookie(cookieName,cookieValue) {
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="
	}
	
function readCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	}


Event.observe(window,"load", function () {
			if ($('preferences') != undefined){
				writeControls();
			}
		});