﻿var MainSwitch = 0;
DDisOn = new Array();
DDList = new Array();

function turnOn() {
	MainSwitch = 1;
	menu_center('y');
}

function pop_file(f, w, h) {
	var temp_url = f;
	var new_win = window.open(temp_url, 'pop' + h + w, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + w + ',height=' + h + ',alwaysRaised=yes');
	new_win.focus();
}

function check_search_form(opt) {
	form = document.search_form;
	if (form.SearchTerm.value == '') {
		alert('Please enter a term to search on and then click "SEARCH".');
		if (opt == 0) return false;
	} else {
		if (opt == 0)
			return true;
		else if (opt == 1)
			form.submit();
	}
}

// menu code
function menu_center(opt) {
	var current_width = determine_page_width();
	if (current_width >	987)
		new_left_position = Math.round((current_width-986)/2, 0);
	else
		new_left_position = 0;
	if (theobjs['TM']) theobjs['TM'].setLP(new_left_position);

	var DIVs = document.getElementsByTagName('div');
	for (var d = 0; d < DIVs.length; d++) {
		if (DIVs[d].id.substr(0, 2) == 'DD' && theobjs[DIVs[d].id]) {
			theobjs[DIVs[d].id].setLP(new_left_position);
		}
	}
	if (opt == 'y' && MainSwitch == 1 && theobjs['TM']) theobjs['TM'].objShow();
}

function determine_page_width() {
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = document.width;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
	}
	return myWidth;
} 

function create_nodes() {
	var TDs = document.getElementsByTagName('td');
	for (var t = 0; t < TDs.length; t++) {
		if (TDs[t].className == 'TopNavItem') {
			var node = TDs[t];
			addEvent(node, 'mouseover', getMoverFor(node), false);
			addEvent(node, 'mouseout', getMoutFor(node), false);
			node.TMin = false;
			node.DDin = false;
		}
	}
	var DIVs = document.getElementsByTagName('div');
	for (var d = 0; d < DIVs.length; d++) {
		if (DIVs[d].id.substr(0, 2) == 'DD') {
			if (theobjs[DIVs[d].id]) DDisOn[DIVs[d].id.toString] = DIVs[d].id;
			var node = DIVs[d];
			addEvent(node, 'mouseover', getMoverFor(node), false);
			addEvent(node, 'mouseout', getMoutFor(node), false);
			node.TMin = false;
			node.DDin = false;
			DDList[DIVs[d].id] = node;
			
			// change the class for the last "li" in list to "DDBottom"
			node.getElementsByTagName('li')[node.getElementsByTagName('li').length-1].className += 'DDBottom';
		}
	}
	turnOn();
}

function getMoverFor(node) {
	return function(e) { mover(e, node); };
}

function getMoutFor(node) {
	return function(e) { mout(e, node); };
}

function mover(e, targetElement) {
	var el = window.event ? targetElement : e ? e.currentTarget : null;
	if (!el) return;
	clearTimeout(el.outTimeout);
	if (DDList['DD' + el.id.substr(2)]) {
		if (!DDList['DD' + el.id.substr(2)].TMIn && !DDList['DD' + el.id.substr(2)].DDIn) {
			clearInterval(DDList['DD' + el.id.substr(2)].intervalID);
			DDList['DD' + el.id.substr(2)].style.display = 'block';
			DDList['DD' + el.id.substr(2)].savedOW = DDList['DD' + el.id.substr(2)].offsetWidth;
			DDList['DD' + el.id.substr(2)].savedOH = DDList['DD' + el.id.substr(2)].offsetHeight;
			DDList['DD' + el.id.substr(2)].clippingRectangle = [0, DDList['DD' + el.id.substr(2)].offsetWidth, 0, 0];
			DDList['DD' + el.id.substr(2)].style.display = 'none';
			DDList['DD' + el.id.substr(2)].intervalID = setInterval(function() {showMenu(DDList['DD' + el.id.substr(2)]);}, 10);
			//if (theobjs['DD' + el.id.substr(2)]) show_menu(el.id.substr(2));
		}
		if (el.id.substr(0, 2) == 'DD')
			DDList['DD' + el.id.substr(2)].DDIn = true;
		else if (el.id.substr(0, 2) == 'TM')
			DDList['DD' + el.id.substr(2)].TMIn = true;
	}
	el.className += '';   // Force IE to recompute styles
}

function mout(e, targetElement) {
	var el = window.event ? targetElement : e ? e.currentTarget : null;
	if (!el) return;
	if (DDList['DD' + el.id.substr(2)]) el.outTimeout = setTimeout(function() { mout2(el); }, 300);
}

function mout2(el) {
	if (el.id.substr(0, 2) == 'DD')
		DDList['DD' + el.id.substr(2)].DDIn = false;
	else if (el.id.substr(0, 2) == 'TM')
		DDList['DD' + el.id.substr(2)].TMIn = false;
	if (!DDList['DD' + el.id.substr(2)].TMIn && !DDList['DD' + el.id.substr(2)].DDIn) {
		clearInterval(DDList['DD' + el.id.substr(2)].intervalID);
		DDList['DD' + el.id.substr(2)].intervalID = setInterval(function() {hideMenu(DDList['DD' + el.id.substr(2)]);}, 10);
	}
	//if (!DDList['DD' + el.id.substr(2)].TMIn && !DDList['DD' + el.id.substr(2)].DDIn && theobjs['DD' + el.id.substr(2)]) theobjs['DD' + el.id.substr(2)].objHide();
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		EventCache.add(elm, evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

function showMenu(el) {
	el.clippingRectangle[2] += 10;
	if (el.clippingRectangle[2] >= el.savedOH) {
		el.clippingRectangle[2] = el.savedOH;
		clearInterval(el.intervalID);
		// reset the clip: browser-specific
		if (document.all && !window.opera) {
			el.style.clip = 'rect(auto, auto, auto, auto)';
		} else {
			el.style.clip = '';
		}
		return;
	}
	el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
	el.style.display = 'block';
  }

function hideMenu(el) {
	el.clippingRectangle[2] -= 10;
	if (el.clippingRectangle[2] <= 0) {
		el.clippingRectangle[2] = 0;
		clearInterval(el.intervalID);
		// reset the clip: browser-specific
		if (document.all && !window.opera) {
			el.style.clip = 'rect(auto, auto, auto, auto)';
		} else {
			el.style.clip = '';
		}
		el.style.display = 'none';
		return;
	}
	el.style.clip = 'rect(' + el.clippingRectangle.join('px ') + 'px)';
}

function show_menu(id) {
	if (MainSwitch == 1) {
		theobjs['DD'+id].objShow();
	}
}

window.onresize = menu_center;