var sectormenu = [ {
	'Sector (ob,y)' : {
		className : 'sectorcoords',
		disabled : true
	}
}, $.contextMenu.separator, {
	'Ship Actions' : {
		className : 'shipactions',
		onclick : function(menuItem, menu) {
		}
	}
}, {
	'Toggle Transponders' : {
		className : 'toggletransponders',
		onclick : function(menuItem, menu) {
	       showTransponderDialog($(this).attr('id'));
		}
	}
}, {
	'Create Designs' : {
		className : 'createdesigns',
		onclick : function(menuItem, menu) {
			showDesignDialog($(this).attr('id'));
		}
	}
}, {
	'Build Ships' : {
		className : 'buildships',
		onclick : function(menuItem, menu) {
			showBuildShipsDialog($(this).attr('id'));
		}
	}
}, {
	'Repair Ships' : {
		className : 'repairships',
		onclick : function(menuItem, menu) {
			showRepairDialog($(this).attr('id'));
		}
	}
}, {
	'Manage RUs' : {
		className : 'manageRUs',
		onclick : function(menuItem, menu) {
		}
	}
}, {
	'Transmit Portal Data' : {
		className : 'transmitportals',
		onclick : function(menuItem, menu) {
	        showTransmitDialog($(this).attr('id'));
		}
	}
}, {
	'Share Scan Data' : {
		className : 'sharescan',
		onclick : function(menuItem, menu) {
		}
	}
}, ];

function getCoords(id) {
	var sector = sectors[id];
	return "(" + sector.oblique + "," + sector.y + ")";
}

function plural(num) {
	return num == 1 ? "" : "s";
}

function getShips(id) {
	var sector = sectors[id];
	var rv = {};
	if (sector != null) {
		var shipIds = sector.ships;
		if (shipIds != null) {
			for ( var i = 0; i < shipIds.length; i++) {
				var shipId = shipIds[i];
				var ship = ships[shipId];
				rv[shipId] = ship;
			}
		}
	}
	return rv;
}

function getTitle(id) {
	var title = getCoords(id);
	var sector = sectors[id];
	var worldId = sector.world;
	if (worldId != null) {
		var world = worlds[worldId];
		title += " " + world.name;
	}
	var portalIds = sector.portals;
	if (portalIds != null) {
		title += " ";
		for ( var i = 0; i < portalIds.length; i++) {
			var portalId = portalIds[i];
			var portal = portals[portalId];
			if (i > 0) {
				title += "/";
			}
			title += portal.name;
			var collapsed = portal.collapsed;
			if (collapsed) {
				title += "*";
			}
		}
	}
	return title;
}

function isAllied(empireId) {
	var allies = empire.allies;
	var rv = false;
	for ( var index in allies) {
		var allyId = allies[index];
		if (empireId == allyId) {
			rv = true;
			break;
		}
	}
	return rv;
}

function isMissileHull(hull) {
	var rv = false;
	if (hull != undefined) {
		var name = hull.name;
		if (name == "missile") {
			rv = true;
		}
	}
	return rv;
}

function isDeviceHull(hull) {
	var rv = false;
	if (hull != undefined) {
		var name = hull.name;
		if (name == "device") {
			rv = true;
		}
	}
	return rv;
}

function isOrbitalHull(hull) {
	var rv = false;
	if (hull != undefined) {
		var name = hull.name;
		if (name == "orbital") {
			rv = true;
		}
	}
	return rv;
}

function getShipclass(shipId) {
	var ship = ships[shipId];
	var shipclass = undefined;
	if (ship != undefined) {
		var shipclassId = ship.shipclass;
		shipclass = shipclasses[shipclassId];
	}
	return shipclass;
}

function getHull(shipId) {
	var hull = undefined;
	var shipclass = getShipclass(shipId);
	if (shipclass != undefined) {
		var hullId = shipclass.hull;
		hull = hulls[hullId];
	}
	return hull;
}

function isDevice(shipId) {
	var hull = getHull(shipId);
	var rv = isDeviceHull(hull);
	return rv;
}

function isMissile(shipId) {
	var hull = getHull(shipId);
	var rv = isMissileHull(hull);
	return rv;
}

function isOrbital(shipId) {
	var hull = getHull(shipId);
	rv = isOrbitalHull(hull);
	return rv;
}

function getWorld(sectorId) {
	var sector = sectors[sectorId] || 0;
	var worldId = sector.world || 0;
	var world = undefined;
	if (worldId > 0) {
		world = worlds[worldId];
	}
	return world;
}

function getPortal(sectorId) {
	var sector = sectors[sectorId] || 0;
	var portalIds = sector.portals;
	var portal = null;
	if (portalIds) {
		var portalId = portalIds[0];
		portal = portals[portalId];
	}
	return portal;
}

function getStockpile(sectorId) {
	var rv = 0;
	var world = getWorld(sectorId);
	if (world != undefined) {
		var stockpiles = world.stockpiles;
		if (stockpiles) {
			var empireId = empire.id;
			rv = stockpiles[empireId + ''] || 0;
		}
	}
	return rv;
}

function addSelectOptions(selectList, jsonOpts) {
	for ( var key in jsonOpts) {
		var val = jsonOpts[key];
		$(selectList).append(
				$("<option></option>").attr("value", key).text(val));
	}
}

function isEmpty(object) {
	var rv = true;
	for ( var key in object) {
		rv = false;
		break;
	}
	return rv;
}

function addCheckboxCell(className, attrs, text) {
	var s = "<td><input type='checkbox' class='" + className + "' " + attrs + ">" + text + "</td>";
	return s;
}

function addSelectCell(className, value, text) {
	var s = "<td><select class='" + className + "'><option value='" + value
			+ "'>" + text + "</option></select></td>";
	return s;
}

function addCell(className, text) {
	var s = "<td class='centertext " + className + "'>" + text + "</td>";
	return s;
}

function addTextCell(className, size, maxlength, text) {
	var s = "<td><input class='" + className + "' type='text' size='" + size
			+ "' maxlength='" + maxlength + "' value='" + text + "'></td>";
	return s;
}

function addImageCell(className, imagesrc) {
	var s = "<td><input type='image' class='" + className + "' src='"
			+ imagesrc + "'></td>";
	return s;
}
