/*
// 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;
  }
  // depricated func for giving netscape a helping hand with dynamic stylesheet changes
  //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;
  }
  // depricated func for giving netscape a helping hand with dynamic stylesheet changes
  //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);
	}
}
/* new and improved text sclarer; simplified by working form a singl class applied to the body rather than multiple stylesheets. By Ned Schwartz and Yannick Pelletier July, 2007*/
function getDefaultTextSize(){
	return "size0";
}
function bodysizeup() { 							//switch class on <body>
  var active = getTextSize();
  switch (active) {
    case 'size-1' : 
      setTextSize('size0');
      break;
    case 'size0' : 
      setTextSize('size1');
      break;
    case 'size1' : 
      setTextSize('size2');
      break;
    case 'size2' : 
      setTextSize('size3');
      break;
    case 'size3' :
      break;
    default :
      setTextSize(getDefaultTextSize());
      break;
  }
}
function bodysizedown() { 							//switch active stylesheet down
  var active = getTextSize();
  switch (active) {
    case 'size3' : 
      setTextSize('size2');
      break;
    case 'size2' : 
      setTextSize('size1');
      break;
    case 'size1' : 
      setTextSize('size0');
      break;
    case 'size0' : 
	  setTextSize('size-1');
      break;
    case 'size-1' : 
      break;
    default :
      setTextSize(getDefaultTextSize());
      break;
  }
}
function getTextSize()
{
	var bodyClassses = document.body.className.split(" ");
	for(i = 0; i<bodyClassses.length; i++)
	{
		if(bodyClassses[i].indexOf("size") != -1) return bodyClassses[i];
	}
	return getDefaultTextSize();
}
function setTextSize(size)
{
	document.body.className = addTextSizeClass(document.body, size);
}
function addTextSizeClass(e, class1)
{
	var classes = e.className.split(" ");
	
	if(classes.length < 1)
	{
		e.className = class1; 
		return;
	}
	else
	{
		for(i = 0; i < classes.length; i++)
		{
			if(classes[i].indexOf("size") == 0) {classes[i] = class1; break;}
		}
		
		var returnClassName = "";
		
		for(i = 0; i < classes.length; i++)
		{
			returnClassName += classes[i];
			
			if(i < classes.length -1) 
				returnClassName +=" ";
		}
		return returnClassName;
	}
}
/*///////////////////////////////////////////
// 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 content = document.getElementById("content");
	if(content != null){
		var p = content.getElementsByTagName("p");			//retrieve all <p> elements
		for(var i = 0; i < p.length; i++){
	//		if(line == ""){line = getPreferredLineHeight();}
			p[i].style.lineHeight=line;						// set the line-height css style to heigh 'line'.
		}
		//alert("setLineHeight "+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"); 			//retrieve all <p> elements
	if (p.length == 0){
		//alert("no p");
		var lineCookie = readCookie("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%";
}

/*///////////////////////////////////////////
// **************DEPRICATED - firefox is way better! *****************
// 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("style"); 				//retrieve font size css from cookie
  var lineCookie = readCookie("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("style"); 				//retrieve font size css from cookie
  var lineCookie = readCookie("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
	if(document.getElementById("content")){
		//alert("onunload");
		var title = getActiveStyleSheet();				// get current text size
		var line = getLineHeight();						//get current line height
		createCookie("style", title, 365);				//store in "style" cookie for 365 days
		createCookie("line", line, 365);					//store in "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("style"); 					//retrieve font size css from cookie
var lineCookie = readCookie("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 args=setActiveMenu.arguments;
	var navContainer;
	
	if(args.length == 0) navContainer = "navcontainer";
	else navContainer = args[0];
	
	var navc = document.getElementById(navContainer);
	if(navc != null)
	{
		var a = navc.getElementsByTagName("a");			
		var currentUrl = location.href;
		//alert(currentUrl);
	
		if(currentUrl.indexOf(".aspx") !=-1){
			currentUrl = currentUrl.substring(0,currentUrl.lastIndexOf(".aspx")+5);
			var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf(".aspx"));
		}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("/#"));
		}
		if(currentUrl.indexOf("self_assessment2") !=-1){
			currentUrl = currentUrl.replace(/self_assessment2/,"self_assessment");
		}
		
		var page = currentUrl.substring(currentUrl.lastIndexOf("/")+1,currentUrl.lastIndexOf(".aspx"));
		//alert(page);
	
		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.
					//alert(a[i].text);
				}
			}		
		}
	}
}
///////////////////////////////////////////////
////       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();
		
		
	}else{
		//printPage.alert("false");
		setTimeout('setPrint()',100);
	}
}


// *** This function is used for all new courses (new courses template from recreation_leisure and beyond)
function printPopUp()
{	
	// opens the window and after a timeout to give it time to load, sets the alt stylesheet to 'print'	
	thisPage = location.href;
	cnStart = thisPage.toLowerCase().indexOf("/courses/") + 9;
	cnEnd = thisPage.substr(cnStart,thisPage.length).indexOf("/");
	course_name = thisPage.substr(cnStart,cnEnd);
	if(thisPage.indexOf("_sm") != -1)
	{
		var startPos;
		var endPos;
		var lessonNum;
		startPos = thisPage.indexOf("lesson");
		endPos = thisPage.indexOf("_sm");
		lessonNum = thisPage.substr(startPos+6, endPos-startPos-6);
		
 		printPage = window.open("../_lib/print.aspx"+"?lesson="+lessonNum+"&courseName="+course_name,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
	}
	else
	{
		printPage = window.open(thisPage,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
		//setTimeout("setPrint()",500);
		setTimeout('setPrint()',100);
	}
}


// *** this function is not used for the new courses (recreation_leisure and beyond): use PrintPopUp() instead.
// *** older courses still use this function.
function printPop(){	// opens the window and after a timeout to give it time to load, sets the alt stylesheet to 'print'
	
	thisPage = location.href;
	cnStart = thisPage.toLowerCase().indexOf("/courses/") + 9;
	cnEnd = thisPage.substr(cnStart,thisPage.length).indexOf("/");
	course_name = thisPage.substr(cnStart,cnEnd);
	if(thisPage.indexOf("_sm") != -1)
	{
		var startPos;
		var endPos;
		var lessonNum;
		startPos = thisPage.indexOf("lesson");
		endPos = thisPage.indexOf("_sm");
		lessonNum = thisPage.substr(startPos+6, endPos-startPos-6);
		
 		printPage = window.open("../scripts/print.aspx"+"?lesson="+lessonNum+"&courseName="+course_name,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
	}
	// this is a real nasty hack for the flash study notes in middle_east (by ned)
	else if(thisPage.indexOf("study_notes.aspx") != -1 && thisPage.toLowerCase().indexOf("middle_east") != -1)
	{
		var startPos;
		var endPos;
		var lessonNum;
		startPos = thisPage.indexOf("lesson");
		endPos = thisPage.indexOf("/study_notes");
		lessonNum = thisPage.substr(startPos+6, endPos-startPos-6);
 		printPage = window.open("../studyMaterials/print.cfm?courseName=middle_east&LessonOrder="+lessonNum,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
	}
	// this is same as above; to print all questions of the self-assessment at the same time
	else if(thisPage.indexOf("self-assessment.aspx") != -1 && thisPage.toLowerCase().indexOf("real_estate") != -1)
	{
		printPage = window.open(thisPage+'?print=yes','printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
		//setTimeout("setPrint()",500);
		setTimeout('setPrint()',100);
	}
	else
	{
		printPage = window.open(thisPage,'printerPage','menubar=yes,scrollbars=yes,resizable=yes,width=790,height=600');
		//setTimeout("setPrint()",500);
		setTimeout('setPrint()',100);
	}
}