/******************************************************************
Original script by glish.com - Eric Costello
This script has been modified from the original code by
Kindler Chase - www.roubaixinteractive.com
	Some of the modifications were minor and not documented - sorry :(
	Others were moderate and are documented.
	
*******************************************************************/

//added the cssElements variable; used in two places. 
var cssElements = 'body, td, p, div, ul, ol, li'
var agt = navigator.userAgent.toLowerCase();

if (document.layers) {
	rule='<style type="text/css">' +cssElements+ ' {font-size: 12px; font-family: "Verdana, Arial, Helvetica, sans-serif;}<\/style>';
	document.write(rule)
	}


/********************************************************************
	What shall we call our cookies? Something friendly, so as not to scare the
	internet geeks.
*/
var fontSizeCookieName = 'fontSize3';
var fontFamilyCookieName = 'fontFamily3';

/********************************************************************
	An array of font-size keywords, which we'll use to change the page's font-size.
	More info on these keywords can be found at the following URLs:
		http://www.alistapart.com/stories/sizematters/
		http://www.w3.org/TR/REC-CSS1#font-size
*/
var sizeA = new Array('xx-small','x-small','small','medium','large','x-large','xx-large');

/********************************************************************
	if you don't like the insanely large keywords to be available, you can truncate the
	array like so:
*/
sizeA.length=5; // this makes only the first 5 keywords available

/********************************************************************
	currentSizeAIndex holds the default value for the pages font-size, which is set in
	your stylesheets with a rule for the body element, like so :
		body { font-size: xx-small; }
	If your rule sets the size to xx-small, currentSizeAIndex would be 0; x-small
	would be 1, small would be 2, etc. etc.
	
	It would have been nice to write a little script that figures this out automatically,
	but NS6 has some real problems, and the code would be too ugly. See here:
		http://developer.apple.com/internet/javascript/styles.html
*/
var currentSizeAIndex = 1; // this corresponds to the rule "body { font: x-small; }"

/********************************************************************
	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">body {font-size: '+sizeA[currentSizeAIndex]+';}<\/style>';
	document.write(rule);
	}

if (readCookie(fontFamilyCookieName)!='') {
	currentFontFamily = readCookie(fontFamilyCookieName)

	if (document.layers) {
		rule='<style type="text\/css">' +cssElements+ ' {font-family: '+currentFontFamily+';}<\/style>';
	} else {
		rule='<style type="text\/css">body {font-family: '+currentFontFamily+';}<\/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.
	
	I Added the text to display in the activeButton. Ideally, you should keep
	the text in the activeButton function the same as in the actual
	buttons in theForm - see the writeControls function for theForm
*/
function changeFontSize(i) {
	var newSizeAIndex = currentSizeAIndex-(-i);
	if (newSizeAIndex<0 || newSizeAIndex==sizeA.length) return false;
	activateButton('sizeUp',' k + ');
	activateButton('sizeDown',' k - ');
	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()
	//added theSize to be called with each font change
	//thus, also had to add the appropriate element in theForm
	theSize('preferencesSize');
	}

//added this function to show the user the current size.
//is called in the font size change function as well as
//onLoad with the writeControls function
//******should probably add similar to the font family change
function theSize(id){
  if (document.getElementById) {
    document.getElementById(id).innerText = "font size: "+(sizeA[currentSizeAIndex]);
  }
  else if (document.all) {
    document.all[id].innerText = "font size: "+(sizeA[currentSizeAIndex]);
  }
}

/********************************************************************
	changeFontFamily() is the function that changes the font-family. It takes one
	parameter, a string, such as "verdana, arial, helvetica, sans-serif", or any legal
	value for the CSS font-family attribute.
*/
function changeFontFamily(ff) {
	document.body.style.fontFamily=ff;
	// let's stuff it in a cookie so we can remember it
	setCookie(fontFamilyCookieName,ff);
	// 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()
	}

/********************************************************************
	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">';
		theForm+='<fieldset id="fs">';
		theForm+='<div id="preferencesSize"></div><br \/>'; //added this element to show the current font size
		theForm+='<input type="button" id="sizeDown" class="button-preferences" onclick="changeFontSize(-1)" value=" k - " \/>&#160;';
		theForm+='<input type="button" id="sizeUp" class="button-preferences" onclick="changeFontSize(1)" value=" k + " \/>';
		theForm+='<br \/><br \/><select class="select-type1" onchange="if(this.selectedIndex>0) {changeFontFamily(this.options[this.selectedIndex].value)}">';
		theForm+='<option value="">change font:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/option>';
		theForm+='<option value="Arial, Helvetica, sans-serif">Arial<\/option>';
		theForm+='<option value="Georgia, Times Roman, serif">Georgia<\/option>';
		theForm+='<option value="Lucida console, Monaco, monospace">Lucida<\/option>';
		theForm+='<option value="\'Trebuchet MS\', sans-serif">Trebuchet<\/option>';
		theForm+='<option value="Verdana, Arial, Helvetica, sans-serif">Verdana<\/option>';
		theForm+='<option value="Wingdings">Wingdings<\/option>';
		theForm+='<option value="X-Files">X-Files<\/option>';
		theForm+='<\/select>';
		theForm+='</fieldset>';
		theForm+='</form>';
		//removed the preferencesheader - if you want it, uncomment the next line
		//and then comment out the line following.
		//formDiv.innerHTML = "<h3 id='preferencesHeader'>text preferences<\/h3>"+theForm;
		formDiv.innerHTML = theForm;
		
		//here's my added function to call the current font size
		theSize('preferencesSize');
		// For IE5.0 Mac (it's fixed in later versions), which puts the contents of elements
		// outside of their containers, unless we do this:
		if (agt.indexOf('msie 5.0; mac') != -1) document.getElementById('fs').innerHTML=document.getElementById('fs').innerHTML;
		
		if (currentSizeAIndex==0) {
			fadeButton('sizeDown');
		} else if (currentSizeAIndex==sizeA.length-1) {
			fadeButton('sizeUp');
			}
		}
	}

/********************************************************************
	the following line calls writeControls() when the document has fully loaded. If
	you would rather have the function called as the page is being rendered, you can
	comment out the line below and insert the following line in your HTML markup
	immediately following the preferences div (<div id="preferences"></div>):

			<script type="text/javascript" id="controlsScript">writeControls()</script>

	Be warned, however, that script elements outside of the head element may not
	validate for some HTML document types.
*/			


//commented out the button styles for both Button functions
function fadeButton(bId) {
	var button = document.getElementById(bId);
	button.blur();
	button.disabled='disabled';
	button.value="no mas";
	//button.style.borderColor='#999';
	//button.style.color='#999';
	}

//modified the active button so the text can be called as well
function activateButton(bId,text) {
	var button = document.getElementById(bId);
	button.disabled=null;
	button.value=text;
	//button.style.borderColor='#000';
	//button.style.color='#000';
	}


/********************************************************************
	cookie functions modified from code found at Alexei Kourbatov's
	javascripter.net/faq/
	
	And I changed it back to the original script (sort of) 
	so the cookie will last for 30 days.
	And then I changed it back because it wasn't working all that well.
	The cookies were not being saved on each changed event and seemed
	to be only reading from the cookie if you were on the same page
	the change was made.
	
	And finally changed it back again.... .js cookies just are not
	as reliable as asp cookies.
*/
//if (agt.indexOf('netscape/7') != -1 ) {alert(agt)}  //just a little test line a place here and there.

function setCookie(cookieName,cookieValue) {
//this function is killing NS7 - I don't know why... so we'll just hide it until we
// find the culprit. (being hidden with asp on the include head script files.)
//if (readCookie(fontFamilyCookieName)!='') {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*30); //change the last value here for number of days to expire
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();

	//original cookie
	//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));
	}
	
