// Declare variables
var intervalID;
var intervalID2;
var inMotion = false;
var inMotion2 = false;
var tags = new Array('div','td','tr','p','b','span','table','strong','emphasis','a','h1','h2','h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd');

// determine browser type
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
var IE = (navigator.appName == 'Microsoft Internet Explorer')? true : false; //dumb browser check

window.onresize = reDo		//Sets the actions that the page does when the window is resized: in this case, reset the content size and reset to Section 1 page 0

function reDo(){ 
	window.location.reload() 
	
	initMenu('start')
	setContent()
}

function setContent() {						// Resize the content so that it fits on the page
	H = Math.round(document.documentElement.clientHeight)
	W = Math.round(document.documentElement.clientWidth)

	resizeThis('bg',8*W,8*H)
	resizeThis('logoimg',(W-40)*827/1280,89*(W-40)/1280 )
	resizeThis('logo',W-40,89*(W-40)/1280)
	resizeThis('nav', 152, H-89*(W-40)/1280-15)
	resizeThis('contentnav',W-180,H-89*(W-40)/1280-15 )
	resizeThis('ifnav',W-180,H-89*(W-40)/1280-15)
	moveThisTo('nav',10,89*(W-40)/1280+10)
	moveThisTo('contentnav',170,89*(W-40)/1280+10)
	
	if(IE){var basefont = document.documentElement.currentStyle.fontSize
	}else{var basefont = window.getComputedStyle(document.documentElement, null).fontSize}
	fontSizer(basefont,W/1270)
}

if (NS4 || IE4) {			// If they are using NS4 or IE4, tell them to upgrade.
	alert('You are using an outdated web browser, this site will not display correctly. Please upgrade to (at least) MSIE 6.0, NS6, or FireFox.')
}

function switchbg(obj,newstate) {		// switches the class of obj to newstate
	if (obj.className.indexOf('dn')==-1) {
		if (obj.className.indexOf('sub')==-1) {obj.className="nav"+newstate
		}else{obj.className="navsub"+newstate}
	}
}



function initMenu(id){				//initMenu initializes the menu when a button is pressed...this gets it ready to display the new configuration.){
	//alert('initMenu calling ClearAll for '+id)
	clearAll()
	inMotion = true; 				//inMotion flag is used by setMenu (which is recursive) to determine if the menu is currently moving
	
	if(id=='start') {}
	else if(id.indexOf('nav')!=-1 && id.indexOf('sub')==-1){document.getElementById('sub'+id).style.visibility="visible"}
	else {return}

	obj=document.getElementById('nav')
	var subnavlocs=new Array()
	var navheight=new Array();
	var navtargets=new Array();
	var navlocs=new Array();
	var subnavtargets=new Array();
	var distance=new Array()
	var counter=0;
	var counter2=0;

	//compute where the buttons and subnavs should be if EVERYTHING IS CLOSED
	for (i = 0 ; i < obj.childNodes.length ; i++ ) {
		if(obj.childNodes[i].id){	
			if(obj.childNodes[i].id.indexOf('nav')!=-1 && obj.childNodes[i].id.indexOf('sub')==-1){
				//Do this if it is a nav button
				navheight[counter]=0
				navheight[counter]+=obj.childNodes[i].offsetHeight
				navlocs[counter]=obj.childNodes[i].offsetTop 				//current location of nav i
				counter+=1
			}
			if(obj.childNodes[i].id.indexOf('subnav')!=-1){
				//Do this if it is a subnav
				subnavlocs[counter2]=obj.childNodes[i].offsetTop 			//current location of subnav i
				counter2 +=1
			}
		}
	}
	
	
	
	navtargets[0]=0							// get the targets of the subnavs
	for (i = 0; i<counter; i++){
		if (i>0){
			navtargets[i] = navtargets[i-1]+navheight[i]+2
		}
		subnavtargets[i]=navheight[0]+navtargets[i]
		distance[i] = subnavtargets[i]-subnavlocs[i]
	}

	for (i = 0; i<counter-1; i++){				// move the subnavs
		temp = 'nav'
		howfar = navtargets[i]-navlocs[i]
		moveThis(temp.concat(i+1+''),0,howfar)

		temp = 'subnav'
		moveThis(temp.concat(i+1+''),0,distance[i])
	}
	moveThis('nav999',0,navtargets[counter-1]-navlocs[counter-1])
	moveThis('subnav999',0,distance[counter-1])
	setMenu(id)
}

// setMenu is the meat of the menu sliding code. It determines the current positions of all the nav buttons, then determines
// how far the button needs to get to get where it should be, then moves the buttons. 
function setMenu(id){
	rate=0.75; 					//0<rate<=1 -- determines the speed of the motion of the nav buttons.

	obj=document.getElementById('nav')
	var idindex=-1;
	var subnavheight=new Array();
	var navheight=new Array();
	var navlocs=new Array()
	var navtargets=new Array();
	var distance=new Array()
	var navdirs = new Array()
	var counter=0;
	var counter2=0;

	//compute where the buttons should be if the subnav corresponding to nav button "id" is open
	for (i = 0 ; i < obj.childNodes.length ; i++ ) {
		if(obj.childNodes[i].id){	
			if(obj.childNodes[i].id.indexOf('nav')!=-1 && obj.childNodes[i].id.indexOf('sub')==-1){
				//Do this if it is a nav button
				navheight[counter]=0
				navheight[counter]+=obj.childNodes[i].offsetHeight			//height of nav i
				navlocs[counter]=obj.childNodes[i].offsetTop 				//current location of nav i
				if(obj.childNodes[i].id.indexOf(id) !=-1){				// note the index corresponding to the clicked nav button
					idindex=counter
				}
				counter+=1
			}
			if(obj.childNodes[i].id.indexOf('subnav')!=-1){
				//Do this if it is a subnav
				subnavheight[counter2]=0;
				if(obj.childNodes[i].childNodes){
					for (j = 0 ; j < obj.childNodes[i].childNodes.length ; j++ ) {
						if(obj.childNodes[i].childNodes[j].id && obj.childNodes[i].childNodes[j].id.indexOf('sub')!=-1){
							//Do this for each subnav button
							subnavheight[counter2]+=obj.childNodes[i].childNodes[j].offsetHeight+2;
						}
					}
				}
				counter2 +=1
			}
		}
	}

	// get the targets of the navs
	navtargets[0]=0
	for (i = 1; i<counter; i++){navtargets[i] = navtargets[i-1]+navheight[i]+2}			//find the target positions of the nav buttons
	if (idindex !=-1){for (i=idindex+1; i<counter; i++){navtargets[i] = navtargets[i]+subnavheight[idindex]}}	//adjust for the open submenu

	for (i = 0; i<counter; i++){						//compute the distance each nav needs to move
		if (navtargets[i]>=navlocs[i]){navdirs[i]=1}
		else {navdirs[i]=-1}
		distance[i]=navdirs[i]*Math.floor(Math.pow(Math.abs(navtargets[i]-navlocs[i]),0.9*rate));
	}

	for (i = 0; i<counter-1; i++){		// move the navs
		temp = 'nav'
		moveThis(temp.concat(i+1+''),0,distance[i])
	}
	moveThis('nav999',0,distance[counter-1])

	allOK=true
	for (i = 1; i<counter; i++){if (Math.abs(navlocs[i]-navtargets[i])>=1.5){allOK=false;}} //check that you've arrived at the correct positions

	if (allOK) {				//you arrived at the correct location
		clearTimeout(intervalID2)
		inMotion=false;
		if (id != 'start'){
			subnavs(id+'sub0')
			switchbg(document.getElementById(id),'dn');
		}else{subnavs('nav1sub0')}
	} else {					//you're not there yet, so keep going.
		timeoutStr2 = 'setMenu("'+id+'")';
		intervalID2 = setTimeout(timeoutStr2, 1);
		inMotion=true;
	} 	
}


// subnavs sets the content appropriate to a specific subnav button. setMenu calls subnavs when it has finally reached its destination.
function subnavs(id) {
	temp='sub'
	temp = temp.concat(id.substr(0,id.indexOf('sub')))
	obj = document.getElementById(temp)
	if(obj.childNodes){											// close the other "active" buttons in the subnav menu
		for (j = 0 ; j < obj.childNodes.length ; j++ ) {
			if(obj.childNodes[j].id && obj.childNodes[j].id.indexOf('sub')!=-1){
				//Do this for each subnav button
				obj.childNodes[j].className='navsuboff';
			}
		}
	}
	populate(id);
}



function clearAll(){		//close everything...
	obj=document.getElementById('nav')
	for (i = 0 ; i < obj.childNodes.length ; i++ ) {
		if(obj.childNodes[i].id){
			if(obj.childNodes[i].id.indexOf('nav')!=-1 && obj.childNodes[i].id.indexOf('sub')==-1){
				//Do this if it is a nav button
				obj.childNodes[i].className='navoff';
			}
			if(obj.childNodes[i].id.indexOf('subnav')!=-1){
				//Do this if it is a subnav
				document.getElementById(obj.childNodes[i].id).style.visibility="hidden"
				if(obj.childNodes[i].childNodes){
					for (j = 0 ; j < obj.childNodes[i].childNodes.length ; j++ ) {
						if(obj.childNodes[i].childNodes[j].id && obj.childNodes[i].childNodes[j].id.indexOf('sub')!=-1){
							//Do this for each subnav button
							obj.childNodes[i].childNodes[j].className='navsuboff';
						}
					}
				}
			}
		}
	}
}


function populate(id) {			// fill in the content the user clicked on
	var i=0;
	var j=0;
	
	if (id.indexOf('sub0')==-1){switchbg(document.getElementById(id),'dn')};		// set the appropriate button to the down position

	i = id.substr(id.indexOf('nav')+3,id.indexOf('sub')-3)-1
	j = id.substr(id.indexOf('sub')+3)

	if(i==998){i=0;j=0}
	
	temp = 'Section'
	document.getElementById('ifnav').src=temp.concat(i+1,'/page',j,'.htm');
}


function moveThis(id,dx,dy){			// move id by increment(dx,dy)
	//alert('MoveThis: '+id+' by (x,y)='+dx+','+dy+')')
	var obj=document.getElementById(id)
	var noPx = document.childNodes ? 'px' : 0;
	if( !obj ) { window.alert('Nothing works in this browser'); return; }
	if(obj.style.left=='' ) { //first round
		obj.style.left=obj.offsetLeft+'px';
		obj.style.top=obj.offsetTop+'px';
		obj.style.left=parseInt(obj.style.left)+dx+'px';
		obj.style.top=parseInt(obj.style.top)+dy+'px';
	} else {
		obj.style.left=parseInt(obj.style.left)+dx+'px';
		obj.style.top=parseInt(obj.style.top)+dy+'px';
	}
}


function moveThisTo(id,x,y){			// move id to the location (x,y)
	//alert('MoveThisTo: '+id+' to (x,y)='+x+','+y+')')
	var obj=document.getElementById(id)
	obj.style.left=Math.round(x)+'px';
	obj.style.top=Math.round(y)+'px';
}

function setThis(id, state) {
	document.getElementById(id).style.visibility=state;
}

function fontSizer(basefont,newpercent) {
	basefont = parseInt(basefont.replace('px',''))
	newbasefont = basefont*newpercent
	getBody = document.getElementsByTagName('body')[0];
	for (i = 0 ; i < tags.length ; i++ ) {
		getallTags = getBody.getElementsByTagName(tags[i]);
		for (k = 0 ; k < getallTags.length ; k++){

			if(IE){var currfont = getallTags[k].currentStyle.fontSize
			}else{var currfont = window.getComputedStyle(getallTags[k], null).fontSize}
		
			if (currfont.indexOf('px')!=-1){newsize = Math.round(newbasefont*parseInt(currfont.replace('px',''))/basefont)
			}else if (currfont.indexOf('%')!=-1){newsize = Math.round(newbasefont*parseInt(currfont.replace('%',''))/100)}
			getallTags[k].style.fontSize = newsize+'px'
		}
	}
} 

function setup(id) {		// setup the content in the content page after it loads
	W = Math.round(document.documentElement.clientWidth)
	H = Math.round(document.documentElement.clientHeight)

	if(IE){var basefont = document.documentElement.currentStyle.fontSize
	}else{var basefont = window.getComputedStyle(document.documentElement, null).fontSize}

	percent = W/1074
	fontSizer(basefont,percent)

	if(IE){var temp = document.getElementById(id).previousSibling.clientHeight
	}else{
		var temp = window.getComputedStyle(document.getElementById(id).previousSibling.previousSibling, '').getPropertyValue('height');
		temp=parseInt(temp.replace('px',''))
	}

	resizeThis(id,W,H-temp)
	moveThisTo(id,0,temp)
}

function resizeThis(id,xsize,ysize){
	var obj=document.getElementById(id)
	obj.style.width = xsize+'px'
	obj.style.height = ysize+'px'
}

function interrogate(what) {		// this is a useful thing - it lists all the attributes of an element node as an alert box
	var output = '';
	for (var i in what)
	if (i!='outerHTML' & i!='innerHTML'){if (i!='outerText' & i!='innerText'){output += i+ ' <'+what[i]+'>		';}}
	alert(output);
}

