var img_folder = "/images/";
//var img_folder = "";
//var img_folder = "/clec/images/";
var activeMenu = null;
var currentMenu = null;
var timeOutVal = null;
var timeOutVal2 = null;
var imagesLoaded = false;
var currHigh = '';
var delayTime = 1000;
var onMain = false;

if (is_nav4 && isDHTML) {
	onResize = reloadPage(true);
	document.captureEvents(Event.MOUSEOVER, Event.MOUSEOUT);
}

function initMouse() {
//alert('init_mouse');
	for (thisLayer in myLayers) {
		if (thisLayer.indexOf("Menu") != -1) {
			myLayers[thisLayer].currLayer.onmouseout = checkActive;
			myLayers[thisLayer].currLayer.onmouseover = setActive;
		}
	}
	menusLoaded = true;
}

function loadImage(fileName) {
	newImage = new Image();
	newImage.src = fileName;
	return newImage;
}

function indicatorObj(on, off, width, height) {
	this.on = on;
	this.off = off;
	this.width = width;
	this.height = height;
}


function preloadImages() {
	if(document.images) {
		ind_on  = loadImage(img_folder + indicator.on);
		ind_off = loadImage(img_folder + indicator.off);

		residential_service_off = loadImage(img_folder + "button_residential_service_off.gif");
		residential_service_on  = loadImage(img_folder + "button_residential_service_on.gif");

		//about_madriver_off = loadImage(img_folder + "button_about_madison_river_off.gif");
		//about_madriver_on  = loadImage(img_folder + "button_about_madison_river_on.gif");

		about_madriver_off = loadImage(img_folder + "button_about_blank.gif");
		about_madriver_on  = loadImage(img_folder + "button_about_blank.gif");

		business_service_off = loadImage(img_folder + "button_business_service_off.gif");
		business_service_on  = loadImage(img_folder + "button_business_service_on.gif");

		customer_care_off = loadImage(img_folder + "button_customer_care_off.gif");
		customer_care_on  = loadImage(img_folder + "button_customer_care_on.gif");

		my_account_off = loadImage(img_folder + "button_my_account_off.gif");
		my_account_on  = loadImage(img_folder + "button_my_account_on.gif");

		agents_program_off = loadImage(img_folder + "button_agents_program_off.gif");
		agents_program_on  = loadImage(img_folder + "button_agents_program_on.gif");

		blank_off = loadImage(img_folder + "button_blank_off.gif");
		blank_on  = loadImage(img_folder + "button_blank_on.gif");

		//madriver_off = loadImage(img_folder + "madriver_off.gif");
		//madriver_on = loadImage(img_folder + "madriver_on.gif");

		//customerservice_off = loadImage(img_folder + "customerservice_off.gif");
		//customerservice_on = loadImage(img_folder + "customerservice_on.gif");

		//about_off = loadImage(img_folder + "buttons/about_off.gif");
		//about_on = loadImage(img_folder + "buttons/about_on.gif");
		//
		//custcare_off = loadImage(img_folder + "buttons/custcare_off.gif");
		//custcare_on = loadImage(img_folder + "buttons/custcare_on.gif");

		imagesLoaded = true;
	}
}

function changeImage(imageIn, imageSrc) {
//alert('start changeImage.  imageIn: ' + imageIn + ' - imageSrc: ' + imageSrc + ' (menuAPI.js)');
//alert('source is ' + imageIn.src);

	if (!imagesLoaded || !document.images) {
		return;
	}

	currImage = eval("document." + imageIn);
	if (!currImage) {
		if (is_nav4) {
			var foundImage = false;
			for (i = 0; i < document.layers.length; i++) {
				currImage = eval("document.layers[" + i + "].document." + imageIn);
				if (currImage) {
					foundImage = true;
					break
				}
			}
			if (!foundImage) {
				return;
			}
		} else {
			return;
		}
	}
	if (is_nav4) {
		currImage.src = eval(imageSrc + ".src");
	} else {
		currImage.setAttribute("src", eval(imageSrc + ".src"));
	}
//alert('done with changeImage - ' + imageIn + ' - ' + imageSrc + ' (menuAPI.js');
}

function initMenu(id) {
	this.id = id;
	this.prev = null;
	this.next = null;
}	

function CTO(timeIn) {
	clearTimeout(timeIn);
	return null;
}

function showMenu(whichLayer) {
//alert('now in showMenu (menuAPI.js)');
	if (activeMenu != null) {
		if (isInActiveList(whichLayer)) {
			timeOutVal2 = CTO(timeOutVal2);
			clearCurrent();
			clearSubs(whichLayer);
			currentMenu = activeMenu;
			activeMenu = activeMenu.prev;
			currentMenu.prev = null;
			if (activeMenu != null)
				activeMenu.next = null;
		} else {
			timeOutVal2 = CTO(timeOutVal2);
			clearCurrent();
			if (menusList[whichLayer].menuLevel == 0)
				clearSubs();

			createCurrent(whichLayer);
		}
	} else {
		if (currentMenu != null) {
			if (currentMenu.id.name == whichLayer) {
				timeOutVal = CTO(timeOutVal);
			} else {
				clearCurrent();
			}
		}
		createCurrent(whichLayer);
	}
//alert('done with showMenu (menuAPI.js)');
}

function createCurrent(whichLayer) {
	currentMenu = new initMenu(myLayers[whichLayer]);

	if (is_nav4) {
		if (menusList[whichLayer].left != currentMenu.id.objGetLeft() || menusList[whichLayer].top != currentMenu.id.objGetTop()) {
			currentMenu.id.objMoveAbsolute(menusList[whichLayer].left, menusList[whichLayer].top);
		}
	}
	currentMenu.id.objShow();
}

function initCheckMenu() {
	timeOutVal = setTimeout("clearCurrent()", delayTime);
}

function clearCurrent() {
	timeOutVal = CTO(timeOutVal);
	if (currentMenu != null) {
		currentMenu.id.objHide();
		currentMenu = null;
	}
	if (activeMenu == null && !onMain) {
		clearHighlight();
	}
}

function clearCurrent2(whichLayer) {
	if (currentMenu == null) {
		return;
	}

	if (currentMenu.id.name == whichLayer) {
		timeOutVal = CTO(timeOutVal);
	} else {
		clearCurrent();
	}
}

function checkActive() {
	if (!onMain) {
		timeOutVal2 = setTimeout("hideMenu()", 20);
	}
}

function setActive() {
	if (timeOutVal2 != null) {
		timeOutVal2 = CTO(timeOutVal2);
	}

	if (activeMenu != null && this.id == activeMenu.id.name) {
		return;
	}

	if (isInActiveList(this.id)) {
		if (currentMenu != null) {
			clearCurrent();
		}
		clearSubs(this.id);
	} else {
		if (currentMenu != null) {
			if (currentMenu.id.name == this.id) {
				timeOutVal = CTO(timeOutVal);
				if (activeMenu != null) {
					activeMenu.next = currentMenu;
					currentMenu.prev = activeMenu;
					activeMenu = currentMenu;
					currentMenu = null;
				} else {
					activeMenu = currentMenu;
					currentMenu = null;
				}
			}
		}
	}
}

function isInActiveList(checkThis) {
	var isInList = false;

	tempList = activeMenu;
	if (tempList == null) {
		return isInList;
	}

	while(tempList.next != null) {
		tempList = tempList.next;
	}

	while(tempList != null) {
		if (tempList.id.name == checkThis) {
			isInList = true;
			break;
		}
		tempList = tempList.prev;
	}
	return isInList;
}

function hideMenu(keepShowing) {
	timeOutVal2 = CTO(timeOutVal2);

	if (currentMenu != null) {
		clearCurrent();
	}

	if (activeMenu != null) {
		clearSubs(keepShowing);
	}
	
	if (activeMenu == null) {
		clearHighlight();
	}
}

function clearHighlight(keepHigh) {
	if (currHigh != "" && currHigh != null && currHigh != keepHigh) {
		changeImage(currHigh, currHigh + "_off");
	}
	currHigh = keepHigh;
}

function clearSubs(clearTo) {
	if (activeMenu != null) {
		while (activeMenu.next != null) {
			activeMenu = activeMenu.next;
		}
	
		while (activeMenu != null && activeMenu.id.name != clearTo) {
			activeMenu.id.objHide();
			activeMenu = activeMenu.prev;
			if (activeMenu != null) {
				activeMenu.next = null;
			}
		}	
	}
}

function reloadPage(init) {
	//reloads the window if Nav4 resized
	if (init == true) {
		document.pageW = innerWidth;
		document.pageH = innerHeight;
		onresize = reloadPage;
	} else if (innerWidth != document.pageW || innerHeight != document.pageH) {
		location.reload();
	}
}
