(function() {
	var divs = {},
		div_timeouts = {},
		selected_class = "selected";

	function showdiv(id)
	{
		el = MM_findObj(id);
		if (el)
		{
			if (div_timeouts[id])
			{
				window.clearTimeout(div_timeouts[id]);
				div_timeouts[id] = 0;
			}
			el.style.visibility = 'visible';
		}
	}

	function hidediv(id)
	{
		el = MM_findObj(id);
		if (el) {
			el.style.visibility = 'hidden';
		}
	}

	set_reset_timer = function(element_ids, delay)
	{
		var i, id;

		if (!delay) {
			delay = 500;
		}
		if (typeof(element_ids) == 'string') { // allow for a single string argument
			element_ids = [ element_ids ];
		}

		for (i=0; i<element_ids.length; i++)
		{
			id = element_ids[i];
			if (!div_timeouts[id]) {
				div_timeouts[id] = window.setTimeout((function() { var a=id; return function () { hidediv(a); }; })(), delay);
			}
		}
	};

	clear_reset_timer = function(element_ids)
	{
		var i, id;
		if (typeof(element_ids) == 'string') { // allow for a single string argument
			element_ids = [ element_ids ];
		}

		for (i=0; i<element_ids.length; i++)
		{
			id = element_ids[i];
			if (div_timeouts[id])
			{
				window.clearTimeout(div_timeouts[id]);
				div_timeouts[id] = 0;
			}
		}
	};

	function add_class(el, class_name)
	{
		var temp, i;

		temp = el.className.split(" ");
		for (i=0; i<temp.length; i++)
		{
			if (temp[i] == class_name) // already present
				return;
		}

		temp.push(class_name);
		el.className = temp.join(" ");
	}

	function remove_class(el, class_name)
	{
		var temp, i;

		temp = el.className.split(" ");
		for (i=0; i<temp.length; i++)
		{
			if (temp[i] == class_name) // present
				delete(temp[i]);
		}

		el.className = temp.join(" ");
	}

	MenuItem = function(link, text, child, onmousedown)
	{
		this.link = link;
		this.text = text;
		this.child = child;
		this.onmousedown = onmousedown;
		this.li = null;
	};

	MenuItem.prototype.makeElement = function(parent_div, ul)
	{
		var li, over, down, item, link;

		this.parent_div = parent_div;
		this.li = li = document.createElement("li");
		li.innerHTML = this.text;

		if (this.child) {
			li.className = "child";
		}

		item = this;
		over = function(e) { parent_div.setActive(item); };

		if (this.link)
		{
			link = this.link;
			if (typeof(this.link) == 'function') {
				down = this.link;
			}
			else {
				down = function () { location.href = link };
			}
		}

		if (li.addEventListener)
		{
			li.addEventListener('mouseover', over, false);
			if (down) {
				li.addEventListener('mousedown', down, false);
			}
		}
		else if (li.attachEvent)
		{
			li.attachEvent('onmouseover', over);
			if (down) {
				li.attachEvent('onmousedown', down);
			}
		}

		ul.appendChild(li);
	};

	// Set position for child div
	//   After this point, this.child is a MenuDiv, not a string
	MenuItem.prototype.complete = function()
	{
		if (this.child)
		{
			var top, left, li, div, child;

			li = this.li;
			div = this.parent_div.div;

			top = li.offsetTop + div.offsetTop;
			left = li.offsetLeft + div.offsetLeft + li.offsetWidth + 3;

			this.child = divs[this.child];
			div = this.child.div;

			div.style.top = top + "px";
			div.style.left = left + "px";
		}
	}

	function real_mouse_out(container, e)
	{
		var to;

		if (!e) {
			e = window.event;
		}

		// if the element we're moving to is a child of this element, ignore this event
		to = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while (to != container && to.nodeName != "BODY") {
			to = to.parentNode;
		}

		return to != container;
	}

	MenuDiv = function(id, top, left, entries, parent_div)
	{
		var div, ul, str, sep, i, over, out, layers, obj;

		this.id = id;
		this.parent_div = parent_div;
		this.entries = entries;
		this.selected_item = null;

		this.layers = [id];
		i = parent_div;
		while (i) {
			this.layers.push(i.id);
			i = i.parent_div;
		}

		this.div = div = document.createElement("div");

		div.id = id;
		div.className = "menu"; // most properties inherited from here
		div.style.top = top + "px";
		div.style.left = left + "px";

		obj = this;
		over = function () { obj.show(); };
		out = function (e) { if (real_mouse_out(div, e)) { obj.hide(); } };

		if (div.addEventListener)
		{
			div.addEventListener('mouseover', over, false);
			div.addEventListener('mouseout', out, false);
		}
		else if (div.attachEvent)
		{
			div.attachEvent('onmouseover', over);
			div.attachEvent('onmouseout', out);
		}

		ul = document.createElement("ul");

		for(i=0; i<entries.length; i++)
		{
			if (typeof(entries[i]) == 'object') { // IE6 fix for possible arrays with trailing commas (passes undefined elements)
				entries[i].makeElement(this, ul);
			}
			else {
				this.entries.splice(i, 1);
			}
		}

		div.appendChild(ul);
		document.body.appendChild(div);

		divs[id] = this;
	};

	MenuDiv.prototype.complete = function()
	{
		for (i=0; i<this.entries.length; i++)
		{
			this.entries[i].complete();
		}
	};

	MenuDiv.prototype.hideSelected = function(immediate_hide)
	{
		var item;

		if (this.selected_item)
		{
			item = this.selected_item;
			remove_class(item.li, selected_class);

			if (item.child) {
				remove_class(item.li, "child_selected");
				add_class(item.li, "child");
				item.child.hide(immediate_hide);
			}
		}
	}

	MenuDiv.prototype.showSelected = function()
	{
		var item;

		if (this.selected_item)
		{
			item = this.selected_item;
			add_class(item.li, selected_class);
			if (item.child) {
				remove_class(item.li, "child");
				add_class(item.li, "child_selected");
			}
		}
	};

	MenuDiv.prototype.show = function()
	{
		var i, show_selected;

		show_selected = false; // only show path in parent divs
		i = this;
		while (i)
		{
			if (show_selected) {
				i.showSelected();
			}
			showdiv(i.id);
			i = i.parent_div;
			show_selected = true;
		}
	}

	// Clear selected item and possibly child div
	//   By default, set timer for the child div
	MenuDiv.prototype.hide = function(immediate_hide)
	{
		var old_item;

		if (immediate_hide) {
			hidediv(this.id);
		}
		else {
			set_reset_timer(this.layers);
		}

		if (this.selected_item)
		{
			old_item = this.selected_item;
			this.hideSelected(immediate_hide);
		}
	}

	MenuDiv.prototype.setActive = function(item)
	{
		this.hideSelected(true);
		this.selected_item = item;
		this.showSelected();

		if (item.child) {
			item.child.show();
		}
	};

	function log(msg)
	{
		MM_findObj('log').innerHTML += msg + "<br/>";
	}

	leftmenu = function()
	{
		// Left-side menu -- common to all *INTERNATIONAL* regions
		var entries = [
			new MenuItem('../_asia', 'Asia Pacific'),
			new MenuItem('../_australia', 'Australia'),
			new MenuItem('../_canada', 'Canada'),
			new MenuItem('../_europe', 'Europe'),
			new MenuItem('../_japan', 'Japan'),
			new MenuItem('../_latin', 'Latin America')
		];

		new MenuDiv('international', 88, 208, entries);
	};

	// establish positions and proper child links
	complete_divs = function()
	{
		for (i in divs)
		{
			divs[i].complete();
		}
	};
})();


