/* Theme Package 2.0: Core JS (last modified: 6.22.07) */

/*** UTILITY FUNCTIONS ***/

//onerror = myErrorHandler;
function myErrorHandler(msg,url,l) {
	var txt="There was an error on this page.\n";
	txt+= "Error: " + msg + "\n";
	txt+= "URL: " + url + "\n";
	txt+= "Line: " + l + "\n\n";
	txt+= "Click OK to continue.\n\n";
	alert(txt); return true;
}

//toggle display
function toggleVis(obj) {
	
	if (obj.hasClassName('hide') || obj.className == '' || obj.className == 'first' || obj.className == 'last' || obj.className == 'on') {
		
			obj.removeClassName('hide');
			obj.addClassName('show');
		} else {
		
		obj.removeClassName('show');
		obj.addClassName('hide');
	}
}

// add attribute
function createAtt(obj,newAtt,newValue) {

	var el = document.createAttribute(newAtt);

	obj.removeAttribute(newAtt);
	el.nodeValue = newValue;
	obj.setAttributeNode(el);
}


/*** NAVIGATION ***/

// Modular Tabs (Click and Hover)
function tabToggle(event) {

	var el = $(Event.element(event));
	
	// selected tab
	var parentDiv = el.up('.tabs');
	var parentUl = parentDiv.down('ul');
	var allLi = parentUl.immediateDescendants();
	var parentLi = el.up(1);
	var parentLiIndex = parentLi.previousSiblings().length;
	
	// tab modules
	var moduleOn = parentDiv.next(parentLiIndex);
	
	allLi.each(function(value,index) {
		
		if (allLi[index].hasClassName('on')) {
			
			allLi[index].removeClassName('on');
			parentDiv.next(index).removeClassName('show');
		}

		parentDiv.next(index).removeClassName('show');
	});
	
	parentLi.addClassName('on');
	moduleOn.addClassName('show');
}

/** Dynamic Menus **/

// Close sub menus
function closeSubMenu(lis) {

	lis.each(function(value,index) {
	
		lis[index].addClassName('hide');	
		lis[index].removeClassName('show');
	});
}


// Find all Primary Menu Items
function findMenuPrimLi(event) {

	var el = $(Event.element(event));
	
	var primDiv = el.up('.click');
	var primUl = primDiv.down('ul');
	var lis = primUl.immediateDescendants();
	
	return lis;
}


// Click Menus
function menuToggle(event) {

	var el = $(Event.element(event));
	
	primLi = el.up('li');
	subUl = primLi.down('ul');
	var contextDiv = primLi.up('div');
	var contextUl = contextDiv.down('ul');
	height = el.getAttribute('menuheight');
	
	theDelay = 1;
	
	var divideHeight = height / 7;
	var parsedHeight = parseInt(divideHeight);
	var roundHeight = Math.round(parsedHeight);
	
	theAmount = roundHeight;
	newHeight = theAmount;
	
	var shownPrimLiArray = contextUl.getElementsByClassName('show');
	
	if (primLi.hasClassName('show')) {return;} // if shown is clicked then end
	
	if (shownPrimLiArray.length > 0) {
		
		shownPrimLi = contextUl.getElementsByClassName('show')[0];
		var shownPrimUlArray = shownPrimLi.getElementsByTagName('ul');
		shownPrimUl = shownPrimUlArray[0];
		
		shownPrimLiHeight = shownPrimLi.down('a').getAttribute('menuheight');
		
		
		var divideShownHeight = shownPrimLiHeight / 7;
		var parsedShownHeight = parseInt(divideShownHeight);
		var roundShownHeight = Math.round(parsedShownHeight);
		
		theShowAmount = roundShownHeight;
		newShowHeight = theShowAmount;
		
		collapseMenu();
	
	} else {expandMenu();}
}

function collapseMenu() {

	if (shownPrimLiHeight >= theShowAmount) {
	
		newShowHeight = (shownPrimLiHeight - theShowAmount); //subtact from height
		shownPrimUl.style.height = newShowHeight + 'px';
		shownPrimLiHeight = newShowHeight;
		
		setTimeout('collapseMenu();',theDelay);
	
	} else {
		
		shownPrimUl.style.height = 'auto';
		shownPrimLi.removeClassName('show');
		shownPrimLi.addClassName('hide');
		
		expandMenu();
	}
}


function expandMenu() {
	
	primLi.removeClassName('hide');
	primLi.addClassName('show');

	newHeight = (newHeight + theAmount); //add to height
	
	if (newHeight < height) {
		
		subUl.style.height = newHeight + 'px';
		setTimeout('expandMenu();',theDelay);
	
	} else {subUl.style.height = 'auto';}
}


// Hover Menus


function menuToggleOver(event) {
	
	var el = $(Event.element(event));
	var primLi = el.up('li');
	var primDiv = el.up('.over');
	var primUl = primDiv.down('ul');
	var lis = primUl.immediateDescendants();
	
	if (primDiv.hasClassName('menu-x')) {
	
		closeSubMenu(lis);
	
		if (event.type == 'mouseover') {
		
			primLi.addClassName('show');
			primLi.removeClassName('hide');
		}
	
	} else {
	
		if (!el.hasClassName('except')) {
	
		closeSubMenu(lis);
	
		primLi.addClassName('show');
		primLi.removeClassName('hide');
		}
	}
}


/* Keyboard Navigation for Click and Hover Menus */

// Prim Menu
function menuKey(event) {

	var el = $(Event.element(event));
	var key = event.keyCode;
	var primUl = el.up(2);
	var primLi = el.up(1);
	var primDiv = el.up(0);
	var subUl = primDiv.next(0);
	var lis = primUl.immediateDescendants();
	
	if (el.up(0).tagName == 'DIV') {	
		
		if (key == Event.KEY_DOWN) {
			
			// close click menu
			if (el.up(3).hasClassName('click')) {
				var primDiv = $(Event.element(event)).up('.click');
				var primUl = primDiv.down(0);
				var lis = primUl.immediateDescendants();
				closeSubMenu(lis);
			}
			
			primLi.removeClassName('hide');
			primLi.addClassName('show');
			subUl.down(1).focus();
		}
	}
}

// Sub Menu
function subMenuKey(event) {

	var el = $(Event.element(event));
	var key = event.keyCode;
	var primLi = el.up(2);
	var subLi = el.up();
	
	if (key == Event.KEY_RETURN) {
	
		return true;
	} // for ie to follow link
	
	
	// Safari alt key
	if (key == Event.KEY_TAB && event.altKey) {
	
		if (event.shiftKey) {
		
		if (primLi.hasClassName('first') || primLi.hasClassName('last')) {primLi.down(1).focus();}
		
			if (!primLi.hasClassName('first') && !primLi.hasClassName('last')) {
			
				primLi.previous(0).down(1).focus();
			}
			
			} else {
			
				if (!primLi.hasClassName('last') && !primLi.hasClassName('last')) {
		
					primLi.next(0).down(1).focus();
				}
			}
		
		Event.stop(event);
	}// end Safari

	
	if (key != Event.KEY_UP && key != Event.KEY_DOWN && key != 16 && key != 18) { // 16 = shift, 18 = alt
		
		primLi.addClassName('hide');
		primLi.removeClassName('show');
	}
	
	if (key == Event.KEY_DOWN) {
	
		if (subLi.hasClassName('last')) {
		
			var firstLi = subLi.siblings()[0]; //go to first li
			var firstAnchor =  firstLi.down(); //go to first A
			
			firstAnchor.focus();
		} else {
		
			var nxtLi = subLi.next();
			var nxtAnchor = subLi.next().down();
			
			nxtAnchor.focus();
		}
	}
	
	if (key == Event.KEY_UP) {
	
		if (subLi.hasClassName('first')) {
		
			var liLength = subLi.siblings().length - 1;
			var lastLi = subLi.siblings()[liLength]; //go to first li
			var lastAnchor =  lastLi.down(); //go to first A
			
			lastAnchor.focus();
		
		} else {
		
			var prevLi = subLi.previous();
			var prevAnchor = subLi.previous().down();
			
			prevAnchor.focus();
		}
	}
	
	if (key == Event.KEY_RIGHT) {
		
		if (primLi.hasClassName('last')) {
		
			var firstPrimLi = primLi.siblings()[0];
			
			firstPrimLi.removeClassName('hide');
			firstPrimLi.addClassName('show');
			firstPrimLi.down(4).focus();
		
		} else {
		
			var nextAnchor = primLi.next(0).down(1);
		
			if (!nextAnchor.hasClassName('except')) {
			
				var nextPrimLi = primLi.next(0);
				var nextSubAnchor = nextPrimLi.down(4);
			
				nextPrimLi.removeClassName('hide');
				nextPrimLi.addClassName('show');
				nextSubAnchor.focus();
			
			} else {nextAnchor.focus()}
		}
	} 
		
		
	if (key == Event.KEY_LEFT) {
	
	
		if (primLi.hasClassName('first')) {
		
			var lastPrimLiLength = primLi.siblings().length - 1;
			var lastPrimLi = primLi.siblings()[lastPrimLiLength]; //go to first li
		
			lastPrimLi.removeClassName('hide');
			lastPrimLi.addClassName('show');
			lastPrimLi.down(4).focus();
		
		} else {
		
			var previousAnchor = primLi.previous(0).down(1);
			
			if (!previousAnchor.hasClassName('except')) {
			
				var previousPrimLi = primLi.previous(0);
				var previousSubAnchor = previousPrimLi.down(4);
				
				previousPrimLi.removeClassName('hide');
				previousPrimLi.addClassName('show');
				previousSubAnchor.focus();
			} else {previousAnchor.focus()}
		}
	}
}


// Dynamic Panels
function panelToggle(event) {

	var el = $(Event.element(event));
	panel = el.up('.panel');
	content = panel.down('.content');
	panelHeight = content.getAttribute('panelheight'); 
	
	var dividePanelHeight = panelHeight / 10;
	var parsedPanelHeight = parseInt(dividePanelHeight);
	var roundPanelHeight = Math.round(parsedPanelHeight);
	
	theDelay = 1;
	thePanelAmount = roundPanelHeight;
	newPanelHeight = thePanelAmount;
	
	if (panel.hasClassName('panel-hide')) { //expand panel
	
		newPanelHeight = thePanelAmount;
		panel.removeClassName('panel-hide');
		content.style.height = newPanelHeight + 'px';
		expandPanel();
	
	} else {collapsePanel();} //collapse panel
}

function collapsePanel() {

	if (panelHeight > thePanelAmount || panelHeight == thePanelAmount) {
	
		newPanelHeight = (panelHeight - thePanelAmount); //subtact from height
		content.style.height = newPanelHeight + 'px';
		
		panelHeight = newPanelHeight;
		setTimeout('collapsePanel();',theDelay);
	
	} else {panel.addClassName('panel-hide');}
}

function expandPanel() {
	
	newPanelHeight = (newPanelHeight + thePanelAmount); //add to height
	
	if (newPanelHeight < panelHeight) {
		
		content.style.height = newPanelHeight + 'px';
		setTimeout('expandPanel();',theDelay);
	
	} else {content.style.height = 'auto';}
}


// Dynamic tables
function tableRowToggle(event) {

	var el = $(Event.element(event));
	var parentTr = el.up(1);
	var nextTr = parentTr.next(0);
	
	if (parentTr.hasClassName('arrow-up')) {
	
		parentTr.removeClassName('arrow-up');
	
	} else {parentTr.addClassName('arrow-up');}
	
	toggleVis(nextTr);
}


// Pop Panel
function popPanel(obj,target) {

	var pop = $(target);
	var link = Position.positionedOffset(obj);
	var xPos = link.first();
	var yPos = link.last();

	pop.style.left = xPos + 15 + 'px';
	pop.style.top = yPos + 'px';
	
	toggleVis(pop);
}


// Flyout Panel
function flyOut(obj,dest,dir) {	
	
	el = $(obj);
	flyCont = $('flyouts-container');
	theDelay = 20;
	destPos = dest;
	desDir = dir;
	
	if (desDir == 'left') {flyCont.style.display = 'block'}
	slide();
}


function slide() {	
	
	var rightPos = el.getStyle('left');
	
	var rightPosP = parseInt(rightPos);
	
	if (desDir == 'left') {
	
		if (rightPosP > destPos) {
		
			el.style.left = (rightPosP - 10) + 'px';
			setTimeout('slide();',theDelay);
		}
	}
	
	if (desDir == 'right') {
	
		if (rightPosP < destPos) {
	
			el.style.left = (rightPosP + 10) + 'px';
			setTimeout('slide();',theDelay);
		}
		
		else {flyCont.style.display = 'none'}
	}
}

// Expand layout
function expandLay() {

	var el = document.getElementsByTagName('body')[0];

	if (el.hasClassName('no-nav')) {el.removeClassName('no-nav');
	} else {el.addClassName('no-nav');}
}


Event.observe(window, 'load', function() {

/*** GET NAV ARRAYS ***/
var menusXClick = getClass('nav-x menu-x click'); var menusYClick = getClass('nav menu click');
var menusYFlyoutClick = getClass('nav menu flyout click');
var allMenusClick = menusXClick.concat(menusYClick,menusYFlyoutClick);

var menusXOver = getClass('nav-x menu-x over'); var menusYOver = getClass('nav menu over');
var menusYFlyoutOver = getClass('nav menu flyout over');
var allMenusOver = menusXOver.concat(menusYOver,menusYFlyoutOver);

var allMenusOverPrimLI = getMenusOver(allMenusOver);
var allMenus = allMenusClick.concat(allMenusOver);

var allYMenus = getClass('nav menu');
var allXMenus = getClass('nav-x menu-x');
var allMenusWithStatic = allYMenus.concat(allXMenus);

var menusClicknOver = menusXClick.concat(menusXOver);
var allTabsClick = getClass('nav-x tabs click');
var allTabsOver = getClass('nav-x tabs over');

function getClass(classes) {

	var containerDiv = $(document.getElementsByTagName('body')[0]);
	var classArray = containerDiv.getElementsByClassName(classes);
	return classArray;
}

// Get primary tier of all hover menus
function getMenusOver(allMenusOver) {

	allMenusOver.each(function(value,index) {
		
		getMenuPrimLI(allMenusOver[index]); 
	});	
}


// Add mouseover event to all menus
function getMenuPrimLI(allMenusOver) {

	var primLIs = allMenusOver.down('ul').immediateDescendants();
	
	primLIs.each(function(value,index) {
	
		primLIs[index].onmouseover = function(event) {
		
		if (!event) {event = window.event;}// for Ie 6.0, must be initialized
		
			var el = $(Event.element(event));
			
			primDivl = el.up('.over');

			if (el.up(0).tagName == 'DIV' && !el.hasClassName('except') && !primDivl.hasClassName('activenav')) {
			
				menuToggleOver(event);
			}
			
			
			Event.observe(primLIs[index], 'click', function(event) {
			
			var shownPrimLi = el.up('li');
			
				if (shownPrimLi.hasClassName('show')) {Event.stop(event);}
			});
		
		};
	});
}


// Close All Menus on load
allMenusWithStatic.each(function(value,index) {

	var everyMenu = allMenusWithStatic[index];

	allMenusWithStaticPrimLi(everyMenu);
});

// Get all Menus
function allMenusWithStaticPrimLi(everyMenu) {

	var allPrimUl = everyMenu.down('ul');
	var allPrimLi = everyMenu.down('ul').immediateDescendants();

	allPrimLi.each(function(value,index) {
	
		var primLi = allPrimLi[index];
		
		var primLiA = primLi.down('div').down('A'); 
		
		if (!primLiA.hasClassName('except')) {
		
			try {
				var subUl = primLi.down('ul');
				var subUlHeight = primLi.down('ul').getStyle('height');
				var parsedHeightClose = parseInt(subUlHeight);
				
				var heightClose = Math.round(parsedHeightClose);
				
				createAtt(primLiA,'menuheight',heightClose);
			
			} catch (e) {var subUl = null;}
		}
		
		
		if (!primLi.hasClassName('show')) {
		
			primLi.addClassName('hide');
		}
	});
}


/*** ADD EVENTS to ELEMENTS ***/

// Add click event to all click menus.
allMenusClick.each(function(value,index) {
	
	Event.observe(allMenusClick[index], 'click', function(event) {
	
		var el = $(Event.element(event));
		
		if (el.up(0).tagName == 'DIV' && !el.hasClassName('except')) {
		
			menuToggle(event);
			Event.stop(event);
		}
	});
});

// Add keyboard events to all menus
allMenus.each(function(value,index) {

	Event.observe(allMenus[index], 'keydown', function(event) {
	
	var el = $(Event.element(event));
	
	if (el.up(0).tagName == 'DIV' && !el.hasClassName('except')) {
	
		menuKey(event);
		} else {
		
			if (!el.hasClassName('except')) {subMenuKey(event);}
		}
	
	if (event.keyCode != Event.KEY_TAB) {Event.stop(event);}
	
	});
});


allMenus.each(function(value,index) {

	Event.observe(allMenus[index], 'keypress', function(event) {
	
		var el = $(Event.element(event));
		
		if ((el.up(4).hasClassName('over') || el.up(4).hasClassName('click')) && !el.hasClassName('except') && event.keyCode != Event.KEY_TAB) {
				
			if (event.keyCode != Event.KEY_RETURN) {Event.stop(event);}
		} else {if (event.keyCode == Event.KEY_DOWN || event.keyCode == Event.KEY_UP) {Event.stop(event);}} //prevent scrolling in FF on bullets
	});
});


// Close sub items on external click for all menus  	
function closeAllSubMenus(event) {

	var el = $(Event.element(event));
	
	menusClicknOver.each(function(value,index) {
	
		var gotMenus = menusClicknOver[index];
		var lis = gotMenus.down(0).immediateDescendants()
		
		closeSubMenu(lis);
	});
}

// Add even to body to close sub items on external click for all menus  	
Event.observe(document.body, 'click', function(event) {closeAllSubMenus(event);});


// Add events to tabs
allTabsClick.each(function(value,index) {

	Event.observe(allTabsClick[index], 'click', function(event) {
	
	tabToggle(event);
	Event.stop(event);
	closeAllSubMenus(event);
	});
});

allTabsOver.each(function(value,index) {

	Event.observe(allTabsOver[index], 'mouseover', function(event) {tabToggle(event);});
});


// PANELS EVENTS
var allPanels = getClass('heading');

allPanels.each(function(value,index) {

	var eventA = allPanels[index].down('A');
	var allContent = allPanels[index].next(0);
	var pxHeight = allContent.getStyle('height');
	var parsedHeight = parseInt(pxHeight);
	var height = Math.round(parsedHeight);

	createAtt(allContent,'panelheight',height);
	
	if (allPanels[index].up(0).hasClassName('panel-hide-load')) {
	
		allPanels[index].up(0).addClassName('panel-hide');
	}

	Event.observe(allPanels[index], 'click', function(event) {
	
		var el = $(Event.element(event));
		
		if (el.tagName == 'A') {
			
			var tag = el.up(1);
		
			if (tag.tagName == 'TR') {
			
				tableRowToggle(event); 
		
			} else {panelToggle(event);}
		
			
			Event.stop(event);
			closeAllSubMenus(event);
		}	
	
	});}
);

}); // end of events



