var orig_width 	= null;
var orig_height = null;
var margin_displacement = (document.layers) ? 625 : 615;
var menus = ["basics", "equipment", "programming", "business", "resources", "contact", "about", "login"];
var local_state = null;
var active_state = null;
var reset_timer = null;
var sImgPath = "images/";
var fLoaded = false;
var aPreloads = [
	"nav_home",
	"nav_basics",
	"nav_equipment",
	"nav_programming",
	"nav_business",
	"nav_resources",
	"nav_contact",
	"nav_about",
	"nav_login",
	"top_calendar",
	"top_faq",
	"top_home",
	"top_logout",
	"top_members",
	"top_profile",
	"top_search",
	"top_register",
];

for(var i=0; i<aPreloads.length; i++){
	eval(aPreloads[i] + "=new Image()");
	eval(aPreloads[i] + ".src='" + sImgPath + aPreloads[i] + ".gif'");
	eval(aPreloads[i] + "_on=new Image()");
	eval(aPreloads[i] + "_on.src='" + sImgPath + aPreloads[i] + "_on.gif'");
}

function getCBObjRef(id) {
	if(document.layers && document.layers[id]) return document.layers[id];
	else if(document.getElementById && document.getElementById(id)) return document.getElementById(id).style;
	else if(document.all && document.all[id]) return document.all[id].style;
	else return false;
}

function main() {
	if(window.innerWidth) { 
		w = window.innerWidth;
		orig_height = window.innerHeight
	} else if(document.body.clientWidth) { 
		w = document.body.clientWidth;
		orig_height = document.body.clientHeight;
	}
	fLoaded = true;
	//positionMenus(w);
	setLocalState();
}

function positionMenus(screen_width) {
	if(!fLoaded) return false;
	left = (screen_width > margin_displacement) ? screen_width - margin_displacement : margin_displacement;
	for(var i=0; i<menus.length; i++) {
		try {
			getCBObjRef(menus[i]).left = left;
		} catch(e) {
			return false;
		}
	}
}

function swapMenu(obj, state) {
	if(! fLoaded) return false;
	clearTimer();
	// if active_state is not null (not first mouseover of page session) then hide the last element moused over
	if(active_state == null) active_state = local_state;
	swapImage("nav_" + active_state, '');
	toggleMenuVisibility(active_state, "hidden");

	active_state = obj;
	// set target image active
	swapImage( "nav_" + obj, state);
	// set target menu active
	toggleMenuVisibility(obj, "visible");
}

function toggleMenuVisibility(obj, state) {
	if(getCBObjRef(obj)) getCBObjRef(obj).visibility = state;
}

function clearTimer() {
	// clear any pending reset action
	clearTimeout(reset_timer);
}

function resetMenu() {
	reset_timer = setTimeout("resetLocalState()", 1000);
}

function resetLocalState(obj, state) {
	// hide currently active
	if(active_state != local_state) {
		toggleMenuVisibility(active_state, "hidden");
		toggleMenuVisibility(local_state, "visible");
		swapImage("nav_" + local_state, '_on');
		swapImage("nav_" + active_state, '');
	}
	active_state = local_state;
	clearTimeout(reset_timer);
}

function swapImage(name, state){
	try {
		document.images[name].src = eval( name + state + ".src" );
	} catch (e) {
		// alert(e);
	}
}

function setLocalState() {
	for(var i=0; i<document.images.length; i++) {
		if(document.images[i].src.indexOf("_on")!=-1) {
			img = document.images[i].src;
			local_state = img.substring(img.indexOf("nav_") +4, img.lastIndexOf("_on"));
			break;
		}
	}
	toggleMenuVisibility(local_state, "visible");
}

window.onresize = main;