// JavaScript Document

// ================================== PRELOAD IMAGES ================================ //
// preload the mouseover images for the topnav
function preload(root,preloadStr) {
	imgArr = preloadStr.split(',');
	preArr = new Array();
	// create image object
	for(i=0; i<imgArr.length; i++) {
		preArr[i] = new Image();
		preArr[i].src = imgArr[i];
	}
}
function changeImg(id,img) {
	imgObj = document.getElementById(id);
	imgObj.src = img;
}


// =================================== CHANGE FONT SIZE ==================================== //
//
// initialize universal variables for cookie scripts
cookie_name = "fontSize";
var index; 
var num;
// create array of font sizes .. these percentages are applied to the <body> font-size
// all other font sizes are declared in 'em' and are inherited from the body
var sizes = new Array('70%','85%','100%');

// function to change the size of the text
// called from increase/decrease font size buttons
// takes one argument 'inc' which is either 1 or -1 and advances the pointer in the sizes array
function textSize(size) {
	// redefine 'num' as 'arr'
	// ensures that the function can work again, since it won't refresh and trigger getCookie() with each click
	num = size;
	// retrieve the appropriate number from the array and set the <body> font-size to that percentage
	document.getElementById('body').style.fontSize = sizes[size];
	// run the setCookie() script to make sure that the current font-size is stored for other pages and future visits
	setCookie(size);
}




// set cookie function .. called from within textSize()
function setCookie(arrNum) {
	// this checks to see if the new number matches the old one
	// if so, the function ends .. if not, it sets 'index = -1' which means that 'document.cookie.indexOf(cookie_name)' has a value of -1 .. ie. it doesn't exist.
	if(document.cookie != document.cookie) {
		index = document.cookie.indexOf(cookie_name);
	}
	else {
		index = -1;
	}
	// if the cookie does not match the new number called from textSize(), overwrite the cookie with the new number
	if(index == -1) {
		document.cookie = cookie_name + "=" + arrNum + "; path=/"
	}
}


// retrieve cookie function .. called in <body onLoad>
function getCookie() {
	var splits = document.cookie.split(';');
	// run through the various cookie entries to find the font size cookie
	for(i=0; i<splits.length; i++) {
		index = document.cookie.indexOf(cookie_name);
		// check to see if cookie exists .. as long as 'index' does not equal -1, the cookie exists.
		if(index != -1) {
			// retrieve only the number
			num = splits[i].substring(splits[i].indexOf('=')+1, splits[i].length);
			// pull the corresponding value from the sizes array and set the <body> font-size to that percentage
			document.getElementById('body').style.fontSize = sizes[num];
		} else {
			// if there is no cookie present, set '1' (medium) as a default font size
			num = '1';
			document.getElementById('body').style.fontSize = sizes[num];
		}
	}
}





// =============================== CHANGE TABS ON THE LESSON PLAN PAGE ========================== //
//
// function to change tabs on the lesson plan page
function changeTabs(num) {
	tabs = Array('tab1','tab2','tab3','tab4');
	containers = Array('cont1','cont2','cont3','cont4');
	for(i=0; i<tabs.length; i++) {
		tabId = tabs[i];
		contId = containers[i];
		if(i == num) {
			document.getElementById(tabId).className = 'tab_on';
			document.getElementById(contId).style.display = 'block';
		} else {
			document.getElementById(tabId).className = 'tab';
			document.getElementById(contId).style.display = 'none';
		}
	}
}



// ============================ DEMUNG THE MAILTO LINKS ===================================== //
//
//
function mung(address) {
	splits = address.split(' ');
	mail_str = '';
	for(i=0; i<splits.length; i++) {
		strip_left = splits[i].replace(/\[/, '');
		strip_right = strip_left.replace(/\]/, '');
		switch(strip_right) {
			case "at": str = '@'; break;
			case "dot": str = '.'; break;
			default: str = strip_right;
		}
		mail_str += str;
	}
	link_str = '<a href="mailto:' + mail_str + '">' + mail_str + '</a>';
	document.write(link_str);
}


// --------------------------------------------- DYNAMIC DROP DOWN LISTS -------------------------------------------- //
// functions for dynamic <select> boxes

// this function is called from within the html (make sure the original dropdown is loaded before calling)
// sets the global variables and clones the master <select> list(s) for later use
function initDynBox(sBoxId, dBoxId) {
	// make sure the appropriate JS are available
	if(document.getElementById && document.getElementsByTagName) {
		// set global variables for the static <select> box and the dynamic <select> box
		sBox = document.getElementById(sBoxId);
		dBox = document.getElementById(dBoxId);
		// clone the dynamic <select> box
		clone = dBox.cloneNode(true);
		// create an array of all the cloned <option> tags
		optClones = clone.getElementsByTagName('option');
		populate();
	}
}

// called onchange() of the static <select> box
function populate(selAll) {
	// recall the global variables .. global variables are assigned as properties of 'window'
	sBox = window.sBox;
	dBox = window.dBox;
	optClones = window.optClones;
	// remove all <options> from the dynamic <select> box
	while(dBox.length > 0) {
		dBox.remove(0);
	}
	// set variable for the value of the chosen static item
	selUnit = sBox.value;
	// set a variable for the matching "class" attribute .. (ie. subunits belonging to unit 1 should have class="js_unit1")
	subClass = 'js_parent' + eval(selUnit);
	// loop through all <option> tags in the clone array
	for(i=0; i<optClones.length; i++) {
		// check to see if the class matches the one defined above or if it labeled as class='default'
		if(optClones[i].className == subClass || optClones[i].className == 'default') {
			// if it matches, add it to the dynamic <select> box
			dBox.appendChild(optClones[i].cloneNode(true));
		}
	}
}

