/* ******************************************************************************************************** *
 * 				 ASA [http://www.americanskin.org/] - Version2 2008.09.24 [asa2.js] 						*
 * ******************************************************************************************************** */
function addLoadEvent(func) { // add functions to window.onload w/out overwriting existing function(s) --> addLoadEvent(functionName);
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
/* ******************************************************************************************************** *
 * 								 START MAIN NAVIGATION MENU v2 												*
 * 								*******************************												*
 * Place menu inside list w/ id="nav" 																		*
 * Top level links in list items <li> & dropdowns in nested lists in same <li> or 2nd, following <li> 		*
 * ******************************************************************************************************** */
function prepNav() { // add event handlers to nav links
	if(!document.getElementById) return false;
	
	var nav = document.getElementById('nav');	
	var items = nav.getElementsByTagName('li');

	for(var i=0; i<items.length; i++) {	
		var li = items[i];				
		li.bgcolor = '#fff7a3'; // **** backgroundColor on menu links when dropdown ***********************
		
		if(li.getElementsByTagName('a')[0]) li.link = li.getElementsByTagName('a')[0];	

		if(li.getElementsByTagName('ul')[0] && !li.skip) li.dropdown = li.getElementsByTagName('ul')[0]; // if ul subnavs are in same li as link			
		
		if(!li.getElementsByTagName('ul')[0] && li.nextSibling) { // if ul subnavs are in li following link li
			if(li.dropdown == undefined && li.nextSibling.getElementsByTagName && li.nextSibling.getElementsByTagName('ul')) { // IE
					li.dropdown = li.nextSibling.getElementsByTagName('ul')[0];
					li.nextSibling.skip = true;
			}
			if(li.dropdown == undefined && li.nextSibling.nextSibling && li.nextSibling.nextSibling.getElementsByTagName && li.nextSibling.nextSibling.getElementsByTagName('ul')) { // Mozilla
					li.dropdown = li.nextSibling.nextSibling.getElementsByTagName('ul')[0];
					li.nextSibling.nextSibling.skip = true;
			}
		}	
		
		if(li.dropdown) {
			li.onmouseover = function() {				
				this.show = true;
				if(this.getElementsByTagName('a')[0].id == 'school_health_nav') { // show img button over state
					this.getElementsByTagName('a')[0].style.backgroundPosition = 'right top';
				}
				dropdownCallTimer(this);
			}
			li.onmouseout = function() {
				this.show = false;
				dropdownCallTimer(this);			
			}		
		}
		
	}
}


function dropdownCallTimer(li) { // set timer for show/hide
	if(li.timer) clearTimeout(li.timer);
	
	if(li.show == true) li.setTimer = function() { dropdownShow(li); }; // set timeout function to show dropdown
	else li.setTimer = function() { dropdownHide(li); }; // set timeout function to hide dropdown

	li.timer = setTimeout(li.setTimer, 150); 
}


function dropdownShow(li) {	// show dropdown & set event handlers for dropdown
	li.dropdown.onmouseover = function() {
		if(li.timer) clearTimeout(li.timer);			
		li.show = true;
		if(li.getElementsByTagName('a')[0].id == 'school_health_nav') { // show img button over state
			li.getElementsByTagName('a')[0].style.backgroundPosition = 'right top';
		}
	}
	li.dropdown.onmouseout = function() {
		if(li.timer && li.show == true) clearTimeout(li.timer);						
		li.show = false;
		dropdownCallTimer(li);			
	}	
	var position = findPos(li.getElementsByTagName('a')[0]); // find position of button (li) & use it for dropdown	
	li.dropdown.style.left = (position[0]-1) + 'px';	
	li.dropdown.style.top = (position[1]+48) + 'px';	
	if(!li.dwidth) {
		li.dwidth = dropdownWidth(li);
		li.dropdown.style.width = li.dwidth;
	}	
	li.link.style.backgroundColor = li.bgcolor;				
	li.dropdown.style.display = 'block'; 		
}
	
function dropdownHide(li) {	// hide dropdown		
	li.dropdown.style.display = 'none';
	li.link.style.backgroundColor = 'transparent';			
	if(li.timer) clearTimeout(li.timer);
	if(li.getElementsByTagName('a')[0].id == 'school_health_nav') {// revert from img button over state
		li.getElementsByTagName('a')[0].style.backgroundPosition = 'left top';
	}
}


function findPos(obj) { // get position of obj clicked
	var posx = posy = 0;	
	if(obj.offsetParent) {
	
		/*do {
			posx += obj.offsetLeft;
			posy += obj.offsetTop;
		} while (obj = obj.offsetParent);*/
		
		posx = obj.parentNode.offsetLeft;
		posy = obj.parentNode.offsetTop;	
		return [posx, posy];
	}
}

function dropdownWidth(li) { // get width for dropdown from links length
	var text = li.dropdown.getElementsByTagName('a');
	var width = 0;
	
	for(i=0; i<text.length; i++) {
		var curr_width = (text[i].firstChild.nodeType == 3) ? (text[i].firstChild.nodeValue.length *7.5) : (text[i].firstChild.firstChild.nodeValue.length *7.5);
		if(curr_width > width) width = curr_width;
	}
	if(li.offsetWidth > width) width = li.offsetWidth+1;
	return width+ 'px';
}
/* ****************************** END MAIN NAVIGATION MENU ********************************************** *
 * ****************************************************************************************************** */

function heights() { // adjusts height of mainContent div if box(es) expand(s) beyond it on wonders layout
	if(!document.getElementById) return false;
	if((!document.getElementById('box1')) && (!document.getElementById('box2'))) return false;
	if(!document.getElementById('mainContent')) return false;
	
	var box1 = (document.getElementById('box1')) ? document.getElementById('box1').offsetHeight : 0;
	var box2 = (document.getElementById('box2')) ? document.getElementById('box2').offsetHeight : 0;	
	
	var height = ( box1 >= box2) ? box1 : box2;
	
	if(height > document.getElementById('mainContent').offsetHeight) document.getElementById('mainContent').style.height = (height+15)+ 'px';
}


function setHeights() { // compare heights of 2 items w/ class setHeights(same # on both) and set height for both to tallest
	if(!document.getElementsByTagName) return false;
	var elems = document.getElementsByTagName('div');
	var items = new Array();
	
	for(i=0; i<elems.length; i++) {
		if(elems[i].className && elems[i].className.match('setHeights')) {
			items.push(elems[i]);
		}
	}
	
	if(items[0]) {
		var regex = /setHeights\d+/; // get setHeights+digit from class name(s)	
		items.sort(sortArray); // sort array alphabetically based on className
					
		while(items[0]) {
			if(items[1]) {
				var i0class = items[0].className.match(regex);
				var i1class = items[1].className.match(regex);	
				
				if (i0class.toString() == i1class.toString()) {
					var height = (items[0].offsetHeight >= items[1].offsetHeight) ? items[0].offsetHeight : items[1].offsetHeight;
					items[0].style.height = height+ 'px';
					items[1].style.height = height+ 'px';
					items.splice(0,2);
				} else { // if 
					items.splice(0,1);
				} // else 
			} else {
				items.splice(0,1);
			} // if/else
		} // while

	} // if 
	function sortArray(a,b) {
		var a = a.className.match(regex);
		var b = b.className.match(regex);
		return ((a < b) ? -1 : ((a > b) ? 1 : 0))
	}
} // setHeights


function subnavs() { // highlight current page in navContent sidebar 
	if(!document.getElementById('navContent')) return false;
	var h1child = document.getElementById('mainContent').getElementsByTagName('h1')[0].firstChild;
	var h1 = (h1child.nodeType == 3) ? h1child.nodeValue : h1child.firstChild.nodeValue;
	var navContent = document.getElementById('navContent');
	var links = navContent.getElementsByTagName('a');
	for(i=0; i<links.length; i++) {
		if(links[i].firstChild) var text = (links[i].firstChild.nodeType == 3) ? links[i].firstChild.nodeValue : links[i].firstChild.firstChild.nodeValue;
		if((text) && text == h1) { 
			links[i].className = 'subnavs';
		}
	}
}

function tableStripes() { // table stripes
	if(!document.getElementById('mainContent').getElementsByTagName('table')[0]) return false;
	var tables = document.getElementById('mainContent').getElementsByTagName('table');
	var odd = true;
	for(var i=0; i<tables.length; i++) {
		var rows = tables[i].getElementsByTagName('tr');
		
			for(var j=0; j<rows.length; j++) {
				if(rows[j].getElementsByTagName('th')[0]) rows[j].getElementsByTagName('th')[0].style.textAlign = 'left';			
				if(rows[j].getElementsByTagName('td')[0]) rows[j].getElementsByTagName('td')[0].style.textAlign = 'left';
				
				if(odd == false) {
					rows[j].className = 'tableStripes';				
					odd = true;				
				} else {
					odd = false;
				}
			}
			
	}
}
addLoadEvent(prepNav);
addLoadEvent(setHeights);
addLoadEvent(heights);
addLoadEvent(subnavs);
// addLoadEvent(tableStripes);


/* ****************************************************************************************************** *
 * ****************************** PRINTER FRIENDLY TOGGLE  ********************************************** *
 * ****************************************************************************************************** */
function printerFriendly() // add on click event to printer icon
{
	if(!document.getElementById('printerFriendly')) return false;
	var friendly = document.getElementById('printerFriendly');
	
	friendly.onclick = function()
							{
								if(location.href.match('printer=1'))
								{
									location.href = location.href.replace('?printer=1','');
									return false;
								}								
							}
}
addLoadEvent(printerFriendly);


function printerCheckURL() // apply print style if printer=1 found in URL
{
	if(location.href.match('printer=1'))
	{				
		var CSSlink = document.createElement('link');
		CSSlink.setAttribute('type','text/css');
		CSSlink.setAttribute('href','/css/print.css');
		CSSlink.setAttribute('rel','stylesheet');
		document.getElementsByTagName('head')[0].appendChild(CSSlink);
	} 
}
addLoadEvent(printerCheckURL);

