var standardMenuWidth = 170;
var arrowImageSrc = "/bay2/gfx/tridown.gif";
var arrowAbsolutePositioning = false;
var arrowTopOffset = 3;
var arrowLeftOffset = 5;

var menuItems = {
	menu: new Array(
		{
			id: "menu_home",
			link_old: "/index.html"
		},
		{
			id: "menu_about",
			link_old: "/about.html",
			submenu: {
				width: standardMenuWidth,
				menu: new Array(
					{ text: "The Value of Independence", link: "/about.html" },
					{ text: "Why Choose Bay Financial?", link: "/why-choose-bay-financial.html" },
					{ text: "Our Team", link: "/team.html" },
					{ text: "Advisor Directory", link: "/bios/" },
					{ text: "Career Opportunities", link: "/careers.html" }
				)
			}
		},
		{
			id: "menu_services",
			link_old: "/services.html",
			submenu: {
				width: standardMenuWidth,
				menu: new Array(
					{ text: "The Services We Offer", link: "/services.html" },
					{ text: "Employee Benefits", link: "/benefits/index.html" }
				)
			}
		},
		{
			id: "menu_resources",
			link_old: "/resources.html",
			submenu: {
				width: standardMenuWidth,
				menu: new Array(
					{ text: "Bulletins & Whitepapers", link: "/info-archive.html" },
					{ text: "Investment Tools & Research", link: "/investment-tools-research.html" }
				)
			}
		},
		{
			id: "menu_clients",
			link_old: "/client-account.html",
			submenu: {
				width: standardMenuWidth,
				menu: new Array(
					{ text: "Your LPL Account", link: "/client-account.html" }
				)
			}
		},
		{
			id: "menu_contact",
			link_old: "/contact.html"
		}
	)
};



/*******************************************************************************************************\
*********************************************************************************************************
**** DO NOT MODIFY AFTER THIS POINT! DO NOT MODIFY AFTER THIS POINT! DO NOT MODIFY AFTER THIS POINT! ****
*********************************************************************************************************
\*******************************************************************************************************/



function getElem(item) {
	if (document.getElementById) { // this is the way the standards work
		return document.getElementById(item);
	}
	else if(document.all) { // this is the way old msie versions work
		return document.all[item];
	}
	else if(document.layers) { // this is the way nn4 works
		return document.layers[item];
	}
}
function get_size(obj) {
	if (obj == document.body) return page_size();
	var ret = new Object();
	ret.width = ret.height = 0;
	if (obj.offsetHeight) {
		ret.width = obj.offsetWidth;
		ret.height = obj.offsetHeight;
	}
	else if (obj.style.pixelHeight) {
		ret.width = obj.style.pixelWidth;
		ret.height = obj.style.pixelHeight;
	}
	else if (obj.style.height) {
		ret.width = obj.style.width;
		ret.height = obj.style.height;
	}
	else if (obj.clip) {
		ret.width = obj.clip.width;
		ret.height = obj.clip.height;
	}
	return ret;
}

function get_pos(obj) {
	var ret = new Object();
	ret.x = ret.y = 0;
	if (obj && obj.offsetParent) {
		do {
			ret.x += obj.offsetLeft;
			ret.y += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return ret;
}

function get_scroll_pos() {
	var ret = new Object();
	ret.x = ret.y = 0;
	if (window.pageXOffset) {
		ret.x = Math.max(ret.x, window.pageXOffset);
		ret.y = Math.max(ret.y, window.pageYOffset);
	}
	if (document.documentElement) {
		ret.x = Math.max(ret.x, document.documentElement.scrollLeft);
		ret.y = Math.max(ret.y, document.documentElement.scrollTop);
	}
	if (document.body) {
		ret.x = Math.max(ret.x, document.body.scrollLeft);
		ret.y = Math.max(ret.y, document.body.scrollTop);
	}
	return ret;
}

function window_size() {
	var ret = new Object();
	ret.width = ret.height = 0;
	if (typeof(window.innerWidth) == Number) {
		if (navigator.userAgent.indexOf("Opera") != -1) {
			ret.width = window.innerWidth;
			ret.height = window.innerHeight;
		}
		else {
			div = document.createElement("div");
			div.style.width = "100%";
			div.style.height = "100%";
			div.style.visibility = "hidden";
			div.style.position = "absolute";
			div.style.top = "0px";
			div.style.left = "0px";
			document.body.appendChild(div);
			size = get_size(div);
			div.parentNode.removeChild(div);
			
			ret.width = size.width;
			ret.height = size.height;
		}
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		ret.width = document.documentElement.clientWidth;
		ret.height = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		ret.width = document.body.clientWidth;
		ret.height = document.body.clientHeight;
	}
	return ret;
}

function page_size() {
	var ret = new Object();
	ret.width = ret.height = 0;
	if (window.innerHeight && window.scrollMaxY) {	
		ret.width = window.innerWidth + window.scrollMaxX;
		ret.height = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight) {
		ret.width = document.body.scrollWidth;
		ret.height = document.body.scrollHeight;
	}
	else {
		ret.width = document.body.offsetWidth;
		ret.height = document.body.offsetHeight;
	}
	var win_size = window_size();
	ret.width = Math.max(ret.width, win_size.width);
	ret.height = Math.max(ret.height, win_size.height);
	return ret;
}

function redraw_win() {
	if (window.pageXOffset) {
		window.pageXOffset++; window.pageXOffset--;
	}
	if (document.documentElement) {
		document.documentElement.scrollLeft++; document.documentElement.scrollLeft--;
	}
	if (document.body) {
		document.body.scrollLeft++; document.body.scrollLeft--;
	}
}

function show_flashes(obj) {
	if (!obj || !obj._hidden || !obj._hidden.length) return;
	for (var i = 0;i < obj._hidden.length;i++) {
		h = obj._hidden[i];
		if (h.length && h.length == 2)
			h[0].style.visibility = h[1];
	}
	redraw_win();
}

function hide_flashes(node, obj, coords, obj_size) {
	if (!node || !obj) return;
	if (node == obj) return;
	if (!coords) coords = get_pos(obj);
	if (!obj_size) obj_size = get_size(obj);
	var succ = false;
	if (node.tagName && (node.tagName == "EMBED" || node.tagName == "OBJECT")) {
		var n_pos = get_pos(node);
		var n_size = get_size(node);
		if (((n_pos.x >= coords.x && n_pos.x <= coords.x + obj_size.width) ||
			 (n_pos.x + n_size.width >= coords.x && n_pos.x + n_size.width <= coords.x + obj_size.width)) &&
			((n_pos.y >= coords.y && n_pos.y <= coords.y + obj_size.height) ||
			 (n_pos.y + n_size.height >= coords.y && n_pos.y + n_size.height <= coords.y + obj_size.height))) {
			if (!obj._hidden || !obj._hidden.length) obj._hidden = new Array();
			vis = node.style.visibility ? node.style.visibility : "visible";
			obj._hidden.push([ node, vis ]);
			node.style.visibility = "hidden";
			succ = true;
		}
	}
	if (!succ) {
		for (var elem = node.firstChild;elem;elem = elem.nextSibling) 
			hide_flashes(elem, obj, coords, obj_size);
	}
}

function reposition(obj, position, anch, offset) {
	// Return if no target object or we need an anchor and don't have one
	if (!obj || (!anch && position != 4)) return;
	
	// Target needs to be display: block in order to return correct size
	var sv = obj.style.display;
	obj.style.display = "block";
	var coords = get_pos(anch);
	var obj_size = get_size(obj);
	var win_size = window_size();
	var scroll = get_scroll_pos();
	obj.style.display = sv;
	
	switch (position) {
		default:
		case 0: // below right
			break;
		case 1: // below left
			coords.x -= obj_size.width;
			break;
		case 2: // above right
			coords.y -= obj_size.height;
			break;
		case 3: // above left
			coords.x -= obj_size.width;
			coords.y -= obj_size.height;
			break;
		case 4: // centered in window
			coords.x = (win_size.width - obj_size.width) / 2 + scroll.x;
			coords.y = (win_size.height - obj_size.height) / 2 + scroll.y;
			break;
	}
	
	if (coords.x < scroll.x) coords.x = scroll.x;
	if (coords.y < scroll.y) coords.y = scroll.y;
	if ((coords.x + obj_size.width) > (win_size.width + scroll.x))
		coords.x = win_size.width - obj_size.width + scroll.x;
	if ((coords.y + obj_size.height) > (win_size.height + scroll.y))
		coords.y = win_size.height - obj_size.height + scroll.y;

	if (offset) {
		if (typeof offset.x == "number")
			coords.x += offset.x;
		if (typeof offset.y == "number")
			coords.y += offset.y;
	}

	obj.style.top = coords.y.toString() + "px";
	obj.style.left = coords.x.toString() + "px";
	
	show_flashes(obj);
	hide_flashes(document, obj, coords, obj_size);
}

function create_pop(id, content, position, cover) {
	// Return if a div with this ID already exists
	if (getElem(id)) return;
	// Return if we need and anchor and don't have one
	var anch = getElem(id + "_a");
	if (!anch && position != 4) return;
	
	var obj = document.createElement("div");
	obj.id = id;
	obj.style.position = "absolute";
	obj.style.zIndex = "100";
	obj.style.visibility = "hidden";
	obj.style.display = "block";
	if (position != 4) obj.style.paddingTop = "1px";
	obj.innerHTML = content;
	document.body.appendChild(obj);
	obj.style.display = "none";
	
	reposition(obj, position, anch);
	
	if (cover) {
		obj.cover_id = cover.id;
		obj.animate_cover = cover.animate;
	}
	obj.close_pop = function() {
		if (this.child_popups) {
			for (var i = 0;i < this.child_popups.length;i++) {
				var e = getElem(this.child_popups[i]);
				if (e) e.close_pop();
			}
		}
		this.parentNode.removeChild(this);
		// unsupported
		// if (this.cover_id && this.animate_cover) new Effect.Fade(this.cover_id, { duration: 1.0 });
		show_flashes(this);
	}
	obj.add_child = function(id) {
		if (!this.child_popups) this.child_popups = new Array();
		this.child_popups.push(id);
	}
	
	obj.style.visibility = "visible";
	obj.style.display = "block";
}

function exec_pop(str) {
	var m = str.match(/<script (?:.|\n)*?>(?:.|\n)*?(?=<\/script>)/gi);
	for (var i = 0;i < m.length;i++) m[i] = m[i].replace(/<script (?:.|\n)*?>/gi, "").replace(/^\s*</m, "// <");
	eval(m.join(";\n"));
}

var falseVal =		0;
var trueVal = 		1;
var lastMenuID =		0;
var hideMenuDelay =	100;

function createMenuID() { return "menu__" + (lastMenuID++).toString(); }

function toggle_menu(check_id, self) {
	var obj = getElem(check_id);
	if (obj && obj.getAttribute("over_flag") == falseVal) {
		if (self) {
			var this_menu = getElem(self);
			if (this_menu.getAttribute("over_flag") == trueVal) return;
			for (var item = this_menu.firstChild;item;item = item.nextSibling)
				if (item.getAttribute("over_flag") == trueVal) return;
			this_menu.style.display = "none";
		}
		else {
			if (getElem(obj.getAttribute("parent_id")).getAttribute("over_flag") == trueVal) return;
			obj.style.display = "none";
		}
	}
}

// TODO: finish nested menus
function createMenu(menu_def, parent) {
	if (!menu_def || typeof menu_def == "undefined") menu_def = menuItems;

	if (parent) {
		// new menu container
		var cont = document.createElement("div");
		cont.id = createMenuID();
		cont.className = "menu_popup";
		cont.setAttribute("over_flag", falseVal);
		cont.setAttribute("parent_id", parent.id);
		cont.style.display = "none";
		cont.style.position = "absolute";
		if (menu_def.width)
			cont.style.width = (typeof menu_def.width == "number") ? menu_def.width.toString() + "px" : menu_def.width;

		cont.onmouseover = function() { this.setAttribute("over_flag", trueVal); };
		cont.onmouseout = function() {
			this.setAttribute("over_flag", falseVal);
			if (this.getAttribute("parent_id")) {
				setTimeout("toggle_menu(\"" + this.getAttribute("parent_id") + "\", \"" + this.id + "\")", hideMenuDelay);
			}
		};
	}

	for (var i = 0;i < menu_def.menu.length;i++) {
		var item = menu_def.menu[i];
		var obj;
		if (item.id && (obj = getElem(item.id))) {
			if (item.text) obj.innerHTML = item.text;
		}
		else {
			// new menu object hurr
			obj = document.createElement("div");
			//obj.className = "menu_item";
			if (item.id) obj.id = item.id;
			else obj.id = createMenuID();
			if (item.text) obj.innerHTML = item.text;
			if (cont) cont.appendChild(obj);
		}

		obj.setAttribute("over_flag", falseVal);
		obj.className = obj.className.toString() + " menu_item";

		if (item.submenu) {
			anch = document.createElement("img");
			anch.width = 0;
			anch.height = 0;
			anch.id = createMenuID();
			anch.className = "menu_anchor";
			obj.insertBefore(anch, obj.firstChild);
			obj.setAttribute("menu_anchor_id", anch.id);
			obj.setAttribute("submenu_id", createMenu(item.submenu, obj).id);

			arrow_cont = document.createElement("div");
			if (arrowAbsolutePositioning) {
				arrow_cont.style.position = "absolute";
				obj.insertBefore(arrow_cont, obj.firstChild);
			}
			else {
				arrow_cont.style.display = "inline";
				obj.appendChild(arrow_cont);
			}

			var sz = get_size(obj);
			var pos_cont = get_pos(obj);
			var pos_arrw = get_pos(arrow_cont);

			arrow = document.createElement("img");
			arrow.src = arrowImageSrc;
			arrow.width = 10;
			arrow.height = 5;
			arrow.style.position = "relative";
			if (arrowAbsolutePositioning) {
				arrow.style.top = (sz.height - arrow.height - (pos_arrw.y - pos_cont.y) - 2 + arrowTopOffset).toString() + "px";
				arrow.style.left = (sz.width - arrow.width - (pos_arrw.x - pos_cont.x) + arrowLeftOffset).toString() + "px";
			}
			else {
				arrow.style.top = arrowTopOffset.toString() + "px";
				arrow.style.left = arrowLeftOffset.toString() + "px";
			}
			arrow_cont.appendChild(arrow);
		}

		obj.onmouseover = function() {
			this.setAttribute("over_flag", trueVal);
			this.className = this.className.toString() + " menu_item_over";
			if (this.getAttribute("submenu_id")) {

				for (var item = this.parentNode.firstChild;item;item = item.nextSibling) {
					if (item.getAttribute && item.getAttribute("submenu_id") != null && item.getAttribute("submenu_id") != this.getAttribute("submenu_id"))
						toggle_menu(item.getAttribute("submenu_id"), false);
				}

				var submenu = getElem(this.getAttribute("submenu_id"));
				reposition(submenu, 0, getElem(this.getAttribute("menu_anchor_id")), { x: -3, y: 8 });
				submenu.style.display = "block";
			}
		};
		obj.onmouseout = function() {
			this.setAttribute("over_flag", falseVal);
			this.className = this.className.toString().replace(" menu_item_over", "");
			if (this.getAttribute("submenu_id")) {
				setTimeout("toggle_menu(\"" + this.getAttribute("submenu_id") + "\", false)", hideMenuDelay);
			}
		};

		if (item.link) {
			obj.link = item.link;
			if (obj.captureEvents) obj.captureEvents(Event.MOUSEUP);
			if (item.target && item.target == "_blank")
				obj.onmouseup = function() { window.open(this.link); };
			else
				obj.onmouseup = function() { window.location = this.link; };
		}
	}

	if (cont) {
		document.body.appendChild(cont);

		//cont.style.display = "block";
		//var cont_size = get_size(cont);
		//cont.style.display = "none";
		//for (var item = cont.firstChild;item;item = item.nextSibling)
		//	item.style.width = cont_size.width.toString() + "px";

		return cont;
	}
}

var existing_onload = window.onload;
window.onload = function() {
	if (typeof existing_onload == "function") existing_onload();
	createMenu(menuItems);
};
