OfficeMap =
	function(focus)
	{
		this.map = null;
		this.focus = focus;
		this.directions = null;
		this.to_address = '';
	}

OfficeMap.prototype.init =
	function()
	{
		if(GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById("map"));
			this.map.addControl(new GSmallMapControl());
			this.map.setCenter(new GLatLng(37, -95), 3);

			$.ajax({
				url: "/locations/offices.php",
				dataType: "json",
				success: $.proxy(this, "draw"),
				error: $.proxy(this, "error")
			});
		}
	}

OfficeMap.prototype.draw =
	function(data)
	{
		var fi_icon = new GIcon(G_DEFAULT_ICON);
		fi_icon.image = "/images/logo-icon.png";
//		var marker_options = { icon: fi_icon };
		var marker_options = {};

		for(var id in data)
		{
			var info = "<table><tr><td valign=\"top\"><img src=\"/images/logo-icon2.png\"></td><td>First Investors<br />" + data[id].name + " Office";
			info += "<br />" + data[id].address;
			info += "<br />" + data[id].city + ", " + data[id].state + " " + data[id].zip;
			info += "<br />Phone: " + this.format_phone(data[id].phone);
			info += "<br />Fax: " + this.format_phone(data[id].fax);
			info += "</td></tr></table>";
			marker_options.title = data[id].name;
			var marker = new GMarker(new GLatLng(data[id].latitude, data[id].longitude), marker_options);
			marker.bindInfoWindowHtml(info);
			this.map.addOverlay(marker);
		}

		if(this.focus) {
			this.map.setCenter(new GLatLng(data[this.focus].latitude, data[this.focus].longitude), 10);
			this.to_address = data[this.focus].canonical;
			this.directions = new GDirections(this.map, document.getElementById("dirs"));
			GEvent.addListener(this.directions, "load", 
				function() {
					$("#dirs").show();
				});
			GEvent.addListener(this.directions, "error", 
				$.proxy(this, "dir_error"));
		}
	}

OfficeMap.prototype.format_phone =
	function(phone)
	{
		return "(" + phone.substr(0, 3) + ")" + phone.substr(3, 3) + "-" + phone.substr(6, 4);
	}

OfficeMap.prototype.error =
	function()
	{
		alert("An error occurred.");
	}

OfficeMap.prototype.dir_error =
	function()
	{
		var status = this.directions.getStatus();
		if(status.code == 602)
			alert("Unable to compute directions because the address was unknown.");
		else
			alert("An error occurred while computing directions.");
	}

OfficeMap.prototype.get_dirs =
	function()
	{
		var s = "from: " + $('#t_from').val() + " to: " + this.to_address;
		this.directions.load(s);
	}

