/*
// FUNCTIONS FOR ACCESSIBILITY BUTTONS.
// Ned Schwartz, 2004. with significant help from http://www.alistapart.com article "Power to the People"
// http://www.alistapart.com/articles/relafont/
//
// increase and decrease text size of content in <p> elements inside table with id="content"
// using alternative stylesheets linked to document as follows:
// <link rel="alternate stylesheet" type="text/css" href="style/large.css" title="A++" />
// <link rel="alternate stylesheet" type="text/css" href="style/medium.css" title="A+" />
// <link rel="alternate stylesheet" type="text/css" href="style/small.css" title="A" />
// <link rel="alternate stylesheet" type="text/css" href="style/x-small.css" title="A-" />
// <link rel="alternate stylesheet" type="text/css" href="style/xx-small.css" title="A--" />
// and buttons with onclick="fontsizeup()" and onclick="fontsizedown()" events.
// 
// increase and decrease line height of <p> elements inside table with id="content
// using buttons with onclick="lineup()" and onclick="linedown()" events
//
## NOTE: 
## these text modifications are stored in a cookie for persistance accross pages and accross sessions.
## the cookie is stored with a call from the window.onunload() event. 
## the cookie is retrieved with a call from window.onload() event. 
## IF A onload() EVENT IS REGISTERED IN THE <body> OF THE DOCUMENT (OR ANYWHERE ELSE) - FOR EXAMPLE
## AS PART OF MM_preloadImages(), THEN THE FUNCTION onLoadCookie() MUST ALSO BE CALLED FROM THE SAME
## onload() EVENT IN ORDER TO ENSURE THAT THE TEXT MODIFICATIONS ARE ACCESSED FROM THE COOKIE.
*/

/*///////////////////////////////////////////
// FUNCTIONS FOR CHANGING TEXT SIZE FROM 
// ALISTAPART.COM
//////////////////////////////////////////*/
function fontsizeup() { 							//switch active stylsheet up
  var active = getActiveStyleSheet();
  switch (active) {
    case 'A--' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      setActiveStyleSheet('A++');
      break;
    case 'A++' :
      break;
    default :
      setActiveStyleSheet('A');
      break;
  }NS_reloadPage();
}

function fontsizedown() { 							//switch active stylesheet down
  var active = getActiveStyleSheet();
  switch (active) {
    case 'A++' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
      setActiveStyleSheet('A--');
      break;
    case 'A--' : 
       break;
    default :
      setActiveStyleSheet('A--');
      break;
  }NS_reloadPage();
}
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 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 getPreferredStyleSheet() {
  return ('A-');
}

function toggleStyleSheet(title){						// does what it says with a style sheet; first
	if(getActiveStyleSheet() == title){					// click turns the sheet on - next click turns
		setActiveStyleSheet(getPreferredStyleSheet);	// the sheet off. by Ned Schwartz
	}else{
		setActiveStyleSheet(title);
	}
}

/*///////////////////////////////////////////
// FUNCTIONS FOR CHANGING LINE HEIGHT BY 
// NED SCHWARTZ
//////////////////////////////////////////*/
function lineup(){										// increase line height to max 280% by Ned Schwartz
	var height = getLineHeight();
	if(parseInt(height) < 280){
		var height = (parseInt(height)+40)+"%";
		setLineHeight(height);
	}
}
function linedown(){ 									//decrease line height to min 120% by Ned Schwartz
	var height = getLineHeight();
	if(parseInt(height) > 120){
		var height = (parseInt(height)-40)+"%";
		setLineHeight(height);
	}
}

function setLineHeight(line){ 							//set line height to percentage 'line' by Ned Schwartz
	var p = document.getElementsByTagName("p");			//retrieve all <p> elements
	for(var i = 0; i < p.length; i++){
		p[i].style.lineHeight=line;						// set the line-height css style to heigh 'line'.
	}
}


function getLineHeight(){  								//get the current line height by reading the <p> elemants of the doc.
	//var content = document.getElementById("content");	// by Ned Schwartz
	//var p = content.getElementsByTagName("p");
	var p = document.getElementsByTagName("p"); 			//retrieve all <p> elements
	if (p.length == 0){
		//alert("no p");
		var lineCookie = readCookie("ec_line"); 				//retrieve line height from cookie
		var line = lineCookie ? lineCookie : getPreferredLineHeight();	//if no cookie, use default
		return line;									// return the lineheight fromt he cookie and exit.
	}
	for(var i = 0; i < p.length; i++){
		var height = p[i].style.lineHeight;				// retrieve line height by checking docuement <p> tags
	}
	if(height == ""){height = getPreferredLineHeight();}//if no height set, use default
	//alert("getLineheight "+height);
	return height;
}
function getPreferredLineHeight(){						//default line height if no line height set.
	//alert("getPreferredLineHeight");
	return "120%";
}

/*///////////////////////////////////////////
// FUNCTIONS FOR DEALING WITH NETSCAPE'S
// PROBLEMS WITH DYNAMICLY CHANGED PAGE CONTENT
// BY NED SCHWARTZ
//////////////////////////////////////////*/
function refresh() {
  window.location.href = self.location.href;
}
function NS_reloadPage(){								// reload page if netscape
	if ((navigator.appName=="Netscape")){				// called from fontsizeup() and fontsizedown()
		setTimeout('refresh()',500)						// to deal with netscape crapola
	}
}

/*///////////////////////////////////////////
// FUNCTIONS FOR SETTING AND RETRIEVING
// COOKIE FOR TEXT AND LINE STYLE PERSISTANCE
// FROM ALISTAPART.COM
//////////////////////////////////////////*/
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=/";
}

function readCookie(name) {
  //alert("readCookie");
  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 onLoadCookie(e){ 							//workaround for MM_preLoadImages call form <body onLoad>
  //alert("onLoadCookie ");							// this call needs to be put in the same <body> onLoad event
  var cookie = readCookie("ec_style"); 				//retrieve font size css from cookie
  var lineCookie = readCookie("ec_line"); 				//retrieve line height from cookie
  var title = cookie ? cookie : getPreferredStyleSheet();			//if no cookie, use default
  var line = lineCookie ? lineCookie : getPreferredLineHeight();	//if no cookie, use default
  if(title == "print"){title = getPreferredStyleSheet();}			//do not maintian persitance of print css
  setActiveStyleSheet(title);						//set text style
  setLineHeight(line);								//set line height
}
window.onload = function(e) { 						//read cookie for font size and line height
  //alert("onLoad ");
  var cookie = readCookie("ec_style"); 				//retrieve font size css from cookie
  var lineCookie = readCookie("ec_line"); 				//retrieve line height from cookie
  var title = cookie ? cookie : getPreferredStyleSheet();			//if no cookie, use default
  var line = lineCookie ? lineCookie : getPreferredLineHeight();	//if no cookie, use default
  if(title == "print"){title = getPreferredStyleSheet();}			//do not maintian persitance of print css
  setActiveStyleSheet(title);						//set text style
  setLineHeight(line);								//set line height
}

window.onunload = function(e) { 					//store font size and line height in cookie for persistance
  //alert("onunload");
  var title = getActiveStyleSheet();				// get current text size
  var line = getLineHeight();						//get current line height
  createCookie("ec_style", title, 365);				//store in "ec_style" cookie for 365 days
  createCookie("ec_line", line, 365);					//store in "ec_line" cookie for 365 days 
}

/*
#########################################################
## this code was included in alistapart's implementation,
## but it seems to cause problems and not be necessary.
#########################################################
var cookie = readCookie("ec_style"); 					//retrieve font size css from cookie
var lineCookie = readCookie("ec_line"); 				//retrieve line height from cookie
var title = cookie ? cookie : getPreferredStyleSheet();			//if no cookie, use default
if (title == 'null') {title = getPreferredStyleSheet();}
var line = lineCookie ? lineCookie : getPreferredLineHeight();	//if no cookie, use default
if(line == 'null'){getPreferredLineHeight();}
setActiveStyleSheet(title);
setLineHeight(line);
*/

/*///////////////////////////////////////////
// FUNCTIONS FOR DEBUGGING PURPOSES
// BY NED SCHWARTZ
//////////////////////////////////////////*/
function domtest(){									// just trying to find the p elements of the content div
	var content = document.getElementById("content");
	var p = content.getElementsByTagName("p");
	debug("P.LENGTH: "+p.length);
	debug("CONTENT: "+content);
	debug(content.childNodes.length);
}
function debug(msg){ 								// create a div with the id"debug" in
	var p = document.createElement("p");			// the doument you are working on to
	var debug = document.getElementById("debug");	// print debug messeges to.
	p.appendChild(document.createTextNode(msg));
	debug.appendChild(p);
	
}
function howBig(){ 										// used to trace line height for designing -
  var content = document.getElementById("content");		// by Ned Schwartz
  var p = content.getElementsByTagName("p");			//  can be removed for live site
  for(var i = 0; i < p.length; i++){
	var height = p[i] .style.lineHeight;
	if(height == ""){height = "120%";}
	}alert(height);
}




///////////////////////////////////////////////
////       MENU
////////////////////////////////////////////////

function setActiveMenu(){ 							
	
	var navc = document.getElementById("navcontainer");
	var a = navc.getElementsByTagName("a");			
	var currentUrl = location.href;
	//var currentUrl = document.location.toString();
	//alert(currentUrl.lastIndexOf(".cfm"));

	if(currentUrl.indexOf(".cfm") !=-1){
		currentUrl = currentUrl.substring(0,currentUrl.lastIndexOf(".cfm")+4);
		var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf(".cfm"));
	}else if (currentUrl.indexOf("?") !=-1){
		currentUrl = currentUrl.substring(0,currentUrl.lastIndexOf("/?")+1);
		var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf("/?"));
	}else if (currentUrl.indexOf("#") !=-1){
		currentUrl = currentUrl.substring(0,currentUrl.lastIndexOf("/#")+1);
		var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf("/#"));
	}
	var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf(".cfm"));

	if(currentUrl.indexOf('_sm/')!=-1){								//if page is in a _sm or study notes folder
		for(var i = 0; i < a.length; i++){							// if it is a nice clean link to a file
			if (a[i].href.indexOf('_sm/') != -1) {					// find the link taht corresponds to the url
				a[i].parentNode.id="active";						// and highlight it.
			}
		}	
	}else if (currentUrl.lastIndexOf("/") == currentUrl.length-1) {		// if there is only a link to a directory - not to a specific file
		var page = currentUrl.substring(currentUrl.lastIndexOf("/",currentUrl.lastIndexOf("/")-1)+1,currentUrl.lastIndexOf("/"));	//get the sub string which is the deepest directory of the url
		for(var i = 0; i < a.length; i++){
			var myLink = a[i].href.substring(a[i].href.lastIndexOf("/",a[i].href.lastIndexOf("/")-1)+1,a[i].href.lastIndexOf("/"));	//get the substring which is the deepest directory of the link
			if(a[i].href.lastIndexOf("/") == a[i].href.length-1){	// if there is no direct ref to a file in the link
				if(myLink == page){									// check to see if the link directory is the same as the url directory
					a[i].parentNode.id="active";					// if so, this is the active link
				}
			}else if (a[i].href.indexOf("index") != -1) {			// check to see if there is an index
				a[i].parentNode.id="active";						// if so, this is the link
			}
		}
		
	} else {
		for(var i = 0; i < a.length; i++){							// if it is a nice clean link to a file
			if (a[i].href.indexOf(page) != -1) {					// find the link taht corresponds to the url
				a[i].parentNode.id="active";						// and highlight it.
			}
		}		
	}
}
///////////////////////////////////////////////
////       PRINT PREVIEW POP UP
////////////////////////////////////////////////
var printPage;			// global variable to hold the print page object
//var isLoaded = false;

if(window.opener){
	//isLoaded = true;
	//window.onunload = function(){isLoaded = false;}
}
function setPrint(){	// set the print preview window's stylesheet to 'print'
	if (printPage.isLoaded == true){
		//printPage.alert("true");
		printPage.setActiveStyleSheet('print');
		printPage.focus();
		//alert("loaded");
		
	}else{
		//printPage.alert("false");
		setTimeout('setPrint()',100);
		//alert("not loaded");
	}
}
function printPop(){	// opens the window and after a timeout to give it time to load, sets the alt stylesheet to 'print'
	thisPage = location.href;
	printPage = window.open(thisPage,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=770,height=600');
	//setTimeout("setPrint()",500);
	setTimeout('setPrint()',100);
}



