// Controls the drop down menus at the top of the site
function menuswap(foo) {
	submenus = document.getElementsByTagName('*');
	for (i=0 ; i<submenus.length ; i++) {
		if (submenus[i].className === 'submenu') submenus[i].style.visibility = 'hidden';
	}
	if (foo!=='') document.getElementById('submenu'+foo).style.visibility = 'visible';
}

// Controls how the four catalog tags on the home page operate
var count = 1;
function catSwap() {
	catHeaders = document.getElementsByTagName('div');
	for (i=0 ; i<catHeaders.length ; i++) if (catHeaders[i].className === 'catalogHeader') catHeaders[i].style.display = 'none';
	if (count < 4) count++;
	else count = 1;
	document.getElementById('catalogHeader'+count).style.display = 'block';
	
	for (j=1 ; j<5 ; j++) { document.getElementById('catalogTab'+j).className = 'catalogTab'; }
	document.getElementById('catalogTab'+count).className = 'catalogTabActive';
	
	setTimeout("catSwap()",7000);
}

// Controls the sidebar "A-Z" menu
function menuToggle(foo) {
	clearToggle();
	document.getElementById('sideMenu'+foo).style.display = 'block';
}

function clearToggle() {
	lists = document.getElementsByTagName('ul');
	for (i=0 ; i<lists.length ; i++) if (lists[i].className === 'sideMenuSwitch') lists[i].style.display = 'none';
}

// Activates email forms
function activate(brillig) {
	document.getElementById('config').name = "config";
	document.getElementById('config').value = brillig;
}

// Controls all functions required to run when the page loads
function setup() {
	// OUTBOUND LINKS OPEN IN A NEW WINDOW UNLESS EXPLICITLY TOLD OTHERWISE
	// FORCE SAME WINDOW: rel="self" in anchor
	// FORCE EXTERNAL WINDOW: rel="external" in anchor
	var domain = window.location+'';
	domain = domain.replace('//','');
	domain = domain.substring(domain.indexOf(':')+1,domain.indexOf('/'));
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var URL = anchors[i].href;
		if ((URL.search(domain)===-1 && URL.search('http')!==-1 && anchors[i].rel !== 'self') || anchors[i].rel === 'external') anchors[i].target = "_blank";
	}
	
	if(document.getElementById('catalogHeader1')) {
		// Preloads the home page rotating catalog images
		var preload = '';
		var pics = 0;
		allDivs = document.getElementsByTagName('div');
		for (i=0 ; i<allDivs.length ; i++) if (allDivs[i].className === 'catalogHeader') pics++;
		for (j=0 ; j<pics ; j++) preload += '<img src="/images/catalogheader' + (j+1) + '-bg.jpg" height="1" width="1" alt="">';
		document.getElementById('preload').innerHTML = preload;
		
		// Looks for the home page header and starts the switching countdown
		setTimeout(catSwap,7000);
	}
}

window.onload = setup;