
function Producer (habitat) {
	this.habitat = habitat;
	this.main_menus = {
		"active": ""
	};
	this.sub_menus = {
		"active": ""
	};
	this.center_menu();
}

Producer.prototype.center_menu = function() {
	var css = this.habitat.css;
	var dim = this.habitat.dimensions;
	var menu = css.get_element( "menu_divs" );
	if (menu) {
		var x = 0;
		for( var element = menu; element; element = element.offsetParent ) {
			x += element.offsetLeft;
		}
		menu.style.left = x + dim.padding + "px";
		var pages = menu.childNodes;
		x = 20;
		y = 2;
		var max_width = 0;
		var height = 0;
		for( var inc = 0; inc < pages.length; inc++) {
			var page = pages[inc];
			if (page.nodeType == 1) {
				var page_info = page.id.split("_");
				if (page_info[0] != "page") {
					continue;
				}

				var width = css.get_width(page);
				var page_height = css.get_height(page);
				if ((x + width + 20) > dim.habitat_width) {
					y = y + page_height + 2;
					x = 20;
				}
				if ((x + width) > max_width) {
					max_width = x + width;
				}
				if ((y + page_height) > height) {
					height = y + page_height;
				}
				page.style.top = y + "px";
				page.style.left = x + "px";
				x += width + 20;
				var sub_menus = page.childNodes;
				if (sub_menus.length > 0) {
					var menu_test = false;
					for( var inc2 = 0; inc2 < sub_menus.length; inc2++) {
						var sub_menu = sub_menus[inc2];
						if (sub_menu.nodeType == 1) {
							var sub_info = sub_menu.id.split("_");
							sub_type = sub_info[0];
							if (sub_type == "menu") {
								this.place_sub_menu(page, sub_menu);
								menu_test = true;
							}
						}
					}
					if (menu_test) {
						page.onmouseover = this.open_main_menu;
					}
				}
			}
		}
		menu.style.width = (max_width + 20) + "px";
		menu.style.height = (height + 4) + "px";
	}
};

Producer.prototype.hide = function(menu) {
	var pages = menu.childNodes;
	if (pages.length > 0) {
		for( var inc = 0; inc < pages.length; inc++) {
			var page = pages[inc];
			if (page.nodeType == 1) {
				var sub_menus = page.childNodes;
				if (sub_menus.length > 0) {
					var menu_test = false;
					var submenu;
					for( var inc2 = 0; inc2 < sub_menus.length; inc2++) {
						var sub_menu = sub_menus[inc2];
						if (sub_menu.nodeType == 1) {
							var sub_info = sub_menu.id.split("_");
							sub_type = sub_info[0];
							if (sub_type == "menu") {
								menu_test = true;
								submenu = sub_menu;
							}
						}
					}
					if (menu_test) {
						this.hide(submenu);
						submenu.style.visibility = "hidden";
					}
				}
			}
		}
	}
};

Producer.prototype.hide_all_menus = function() {
	if( document.getElementById ) {
		var menu = document.getElementById( "menu_divs" );
		if (menu) {
			this.hide(menu);
		}
	}
};

Producer.prototype.menu_close = function() {
	//window.alert("Close " + this);
};

Producer.prototype.open_main_menu = function() {
	var pro = window.habitat.producer;
	var main_menus = pro.main_menus;
	if (main_menus.active != this.id) {
		pro.hide_all_menus();
		main_menus.active = this.id;
		var container = this.childNodes;
		if (container.length > 0) {
			var sub_menu_test = false;
			var sub_menu;
			for( var inc = 0; inc < container.length; inc++) {
				var item = container[inc];
				if (item.nodeType == 1) {
					var item_info = item.id.split("_");
					var item_type = item_info[0];
					if (item_type == "menu") {
						sub_menu = item;
						sub_menu_test = true;
					}
				}
			}
			if (sub_menu_test) {
				sub_menu.style.visibility = "visible";
			}
		}
	}
	return 1;
};

Producer.prototype.open_sub_menu = function() {
	var pro = window.habitat.producer;
	var sub_menus = pro.sub_menus;
	var parent = this.parentNode;
	if (sub_menus.active != this.id) {
		sub_menus.active = this.id;
		var container = this.childNodes;
		if (container.length > 0) {
			var sub_menu_test = false;
			var sub_menu;
			for( var inc = 0; inc < container.length; inc++) {
				var item = container[inc];
				if (item.nodeType == 1) {
					var item_info = item.id.split("_");
					var item_type = item_info[0];
					if (item_type == "menu") {
						sub_menu = item;
						sub_menu_test = true;
					}
				}
			}
			if (sub_menu_test) {
				pro.hide(parent);
				sub_menu.style.visibility = "visible";
			}
		}
	}
	return 1;
};

Producer.prototype.place_sub_menu = function( page, sub_menu) {
	var parent_id = page.getAttribute("id");
	var parent_info = parent_id.split("_");
	var type = parent_info[0];
	var page_width = page.offsetWidth + page.offsetLeft;

	var sub_pages = sub_menu.childNodes;
	var max_width = 0;
	for( var inc = 0; inc < sub_pages.length; inc++) {
		var sub_page = sub_pages[inc];
		if (sub_page.nodeType == 1) {
			var width = sub_page.offsetWidth;
			if (max_width < width) {
				max_width = width;
			}
		}
	}
	sub_menu.style.width = (max_width + 10) + "px";

	if (type == "page") {
		sub_menu.style.top = (page.offsetTop + page.offsetHeight) + "px";
		sub_menu.style.left = "0px";
	} else {
		//window.alert("Page Parent ID");
		var page_parent = page.parentNode;
		sub_menu.style.left = (page_parent.offsetWidth - 7) + "px";
		sub_menu.style.top = "-5px";
	}
	var y = 5;
	for( var inc = 0; inc < sub_pages.length; inc++) {
		var sub_page = sub_pages[inc];
		if (sub_page.nodeType == 1) {
			var sub_page_info = sub_page.id.split("_");
			if (sub_page_info[0] != "subpage") {
				continue;
			}

			sub_page.style.left = "5px";
			sub_page.style.width = max_width + "px";
			sub_page.style.top = y + "px";

			y += (sub_page.offsetHeight + 5);
			var sub_menus = sub_page.childNodes;
			if (sub_menus.length > 0) {
				var menu_test = false;
				for( var inc2 = 0; inc2 < sub_menus.length; inc2++) {
					var new_sub_menu = sub_menus[inc2];
					if (new_sub_menu.nodeType == 1) {
						var sub_info = new_sub_menu.id.split("_");
						var sub_type = sub_info[0];
						if (sub_type == "menu") {
							this.place_sub_menu(sub_page, new_sub_menu);
							menu_test = true;
						}
					}
				}
				if (menu_test) {
					sub_page.onmouseover = this.open_sub_menu;
				}
			}
		}
	}
	sub_menu.style.height = (y + 5) + "px";
	sub_menu.onmouseout = this.menu_close;
};
