function MenuGroup(id, label, parent_path) {
	this.type = "group";
	this.vis = true;
	
	this.id = id;
	this.label = label;
	this.name_path = (id == "Root")? parent_path: parent_path+".groups."+this.id;
	
	this.items = new Array();
	this.groups = new Array();
}

MenuGroup.getGroupID = function() {
	MenuGroup.group_num = ++MenuGroup.group_num  ||  1;
	return "g"+MenuGroup.group_num;
}

MenuGroup.resetGroupIDNum = function() {
	MenuGroup.group_num = 1;
}

MenuGroup.prototype.addGroup = function(label) {
	var id = MenuGroup.getGroupID();
	this.groups[id] = new MenuGroup(id, label, this.name_path);
	return this.groups[id];
}

MenuGroup.prototype.addItem = function(cruise) {
	this.items[cruise.id] = new MenuItem(cruise, this.name_path);
}				

MenuGroup.prototype.createMenuItems = function(menu_obj) {
	if (this.id != "Root") {
		menu_obj.html += "<div id='group-"+this.id+"' class='menuGroupHead'>";
			menu_obj.html += "<table class='menuGroupHead'>";
				menu_obj.html += "<tr>";
					menu_obj.html += "<td class='checkBox'><input type='checkbox' name='cb-"+this.id+"' id='cb-"+this.id+"' onClick=\""+this.name_path+".toggleMapOverlayVis();\" checked></td>";
					menu_obj.html += "<td class='title'><p class='assetGroupTitle'><label for='cb-"+this.id+"'>"+this.label+"</label></p></td>";
					menu_obj.html += "<td class='visButton'><a href=\"javascript: "+this.name_path+".toggleGroupOpenClose();\"><img id='btn_vis-"+this.id+"' src='/nvs/images/misc_buttons/btn_collapse_group.gif' width='13' height='13' border='0'></a></td>";
				menu_obj.html += "</tr>";
			menu_obj.html += "</table>";
		menu_obj.html += "</div>";
		menu_obj.html += "<div id='items-"+this.id+"' class='menuGroupItems'>";
	}
	for (var i in this.items) {
		//var item_name_path = this.name_path+"items."+this.items[i].id;
		menu_obj.html += "<table class='menuItem'>";
			menu_obj.html += "<tr>";
				menu_obj.html += "<td class='aux'><input type='checkbox' name='cb-"+this.items[i].id+"' id='cb-"+this.items[i].id+"' onClick=\""+this.items[i].name_path+".toggleMapOverlayVis();\" checked></td>";
				menu_obj.html += "<td class='icon'><img src='"+this.items[i].cruise.icon_img+"' width='18' height='18' border='0'></td>";
				menu_obj.html += "<td class='title'>"+this.items[i].label+"</td>";
				if (this.items[i].cruise.cruise_route) {
					menu_obj.html += "<td class='aux'>";
						if (this.items[i].cruise.cruise_route) {
							menu_obj.html += "<a id='crv_norm-"+this.items[i].cruise.id+"' style='margin-right: 3px;' href='javascript: "+this.items[i].name_path+".toggleCruiseRouteLineVis();' title='Toggle Route Line'><img src='/nvs/images/aux_buttons/btn_route_vis-norm.gif' width='18' height='18' border='0'></a>";
							menu_obj.html += "<a id='crv_active-"+this.items[i].cruise.id+"' style='margin-right: 3px; display: none;' href='javascript: "+this.items[i].name_path+".toggleCruiseRouteLineVis();' title='Toggle Route Line'><img src='/nvs/images/aux_buttons/btn_route_vis-active.gif' width='18' height='18' border='0'></a>";
						}
					menu_obj.html += "</td>";
				}
				menu_obj.html += "<td class='fill'></td>";
			menu_obj.html += "</tr>";
		menu_obj.html += "</table>";
	}
	for (var i in this.groups) {
		this.groups[i].createMenuItems(menu_obj);
	}
	if (this.id != "Root") {
		menu_obj.html += "</div>";
	}
}

MenuGroup.prototype.toggleGroupOpenClose = function() {
	var is_open = (document.getElementById("items-"+this.id).style.display == "none")? false: true;
	if (is_open) {
		this.closeGroup();
	} else {
		this.openGroup();
	}
}

MenuGroup.prototype.expandAllGroups = function() {
	if (this.id != "Root") this.openGroup();
	for (var i in this.groups) {
		this.groups[i].expandAllGroups();
	}
}

MenuGroup.prototype.collapseAllGroups = function() {
	if (this.id != "Root") this.closeGroup();
	for (var i in this.groups) {
		this.groups[i].collapseAllGroups();
	}
}

MenuGroup.prototype.openGroup = function() {
	var container = document.getElementById("items-"+this.id);
	var button = document.getElementById("btn_vis-"+this.id);
	container.style.display = "block";
	button.src = "/nvs/images/misc_buttons/btn_collapse_group.gif";
}

MenuGroup.prototype.closeGroup = function() {
	var container = document.getElementById("items-"+this.id);
	var button = document.getElementById("btn_vis-"+this.id);
	container.style.display = "none";
	button.src = "/nvs/images/misc_buttons/btn_expand_group.gif";
}

/*
MenuGroup.prototype.toggleMapOverlayVis = function() {
	this.vis = (this.vis == true)? false: true;
	this.setAllVis(this.vis); //recursively set visibility of all child items
}

MenuGroup.prototype.setAllVis = function(vis) {
	for (var i in this.items) {
		if (vis == true && this.vis == true) {
			//this.items[i].cruise.showOverlay();
		} else {
			//this.items[i].cruise.hideOverlay();
		}
	}
	for (var i in this.groups) {
		this.groups[i].setAllVis(vis);
	}
}
*/

MenuGroup.prototype.setAllVis = function(vis) {
	if (this.id != "Root") {
		if (vis) {
			this.vis = vis;
			document.getElementById("cb-"+this.id).checked = vis;
		}
	}
	for (var i in this.items) {
		this.items[i].vis = vis;
		document.getElementById("cb-"+this.items[i].id).checked = vis;
	}
	vis = (vis == true && this.vis == true)? true: false;
	for (var i in this.groups) {
		this.groups[i].setAllVis(vis);
	}
}

MenuGroup.prototype.checkAllVis = function(vis) {
	for (var i in this.items) {
		if (vis == true && this.vis == true) {
			for (var i in this.items) {
				if (this.items[i].vis == true) {
					for (var j in this.items[i].cruise.stations) {
						var station = this.items[i].cruise.stations[j];
						if (!inArray(station, visible_stations)) visible_stations.push(station);
					}
					visible_cruises.push(this.items[i].cruise.cruise_name_abbrev); //add cruise to visibility list
				}
			}
		} else {
			//nothing
		}
	}
	for (var i in this.groups) {
		this.groups[i].checkAllVis(vis);
	}
}

MenuGroup.prototype.updateMarkerVisibility = function() { //should only be called for menu root
	if (map.getInfoWindow()) map.getInfoWindow().hide(); //hide info window so that menu items can be updated when reopened
	
	visible_stations = new Array();
	visible_cruises = new Array();
	
	this.checkAllVis(true);
	
	for (var i in cruise_station_list) {
		cruise_station_list[i].hideOverlay();
	}
	
	for (var i in visible_stations) {
		cruise_station_list[visible_stations[i]].showOverlay();
	}
	//SHOW MARKERS IN visible_stations
}

MenuGroup.prototype.toggleMapOverlayVis = function() {
	this.vis = (this.vis == true)? false: true;
	//this.setAllVis(this.vis); //recursively set visibility of all child items
	menus.updateMarkerVisibility(true); //set to true since it always starts at the menus root
}



function MenuItem(cruise, parent_path) {
	this.vis = true;
	this.type = "item";
	this.id = cruise.id;
	this.cruise = cruise;
	this.label = cruise.cruise_month;
	this.name_path = parent_path+".items."+this.id;
}

MenuItem.prototype.toggleMapOverlayVis = function() {
//window.alert(this.name_path);
	this.vis = (this.vis == true)? false: true;
	menus.updateMarkerVisibility(true); //set to true since it always starts at the menus root
}

/*
MenuItem.prototype.openMarkerHTML = function() {
	this.cruise.openInfoWindow();
}
*/

MenuItem.prototype.toggleCruiseRouteLineVis = function() {
	var button_norm = document.getElementById("crv_norm-"+this.cruise.id);
	var button_active = document.getElementById("crv_active-"+this.cruise.id);
	if (button_norm.style.display == "none") {
		button_norm.style.display = "inline";
		button_active.style.display = "none";
		this.cruise.hideCruiseRouteLine();
	} else {
		button_norm.style.display = "none";
		button_active.style.display = "inline";
		this.cruise.showCruiseRouteLine();
	}
}


