var Map = function (elem) {
        var self = this;
        this.center = true;
        this.levelZoom = 15;
        this.points = new Array();
        this.bounds = new GLatLngBounds();
        this.geocoder = new GClientGeocoder();
        this.icon = new GIcon();
        
        this.icon.image = "/images/icons/fama.png";
        this.icon.iconSize = new GSize(96,53);
        this.icon.iconAnchor = new GPoint(25,53);
        
        this.icon.infoWindowAnchor = new GPoint(20,0);

        this.map = new GMap2(elem.get(0));
        this.map.addControl(new GSmallMapControl());
        this.map.addControl(new GMapTypeControl());

        this.addAddress = function (dir, html) {
                self.geocoder.getLocations(dir, function (response) {
                        var place = response.Placemark[0];
                        self.map.setCenter(new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]), self.levelZoom);
                        var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                        var marker = new GMarker(point,self.icon);
                        marker.title = "fama";
                        self.bounds.extend(marker.getPoint());
                        self.points.push(marker);
                        self.map.addOverlay(marker);
                        if (self.center) centerMap();
                        GEvent.addListener(marker, 'click', function() {
				$('div.finder div.infodir').fadeOut(function() {
					$("#content div.finder div.left div.infodir").html(html);
					$("div.finder div.field input")
						.focus(function () {
							$(this).prev().hide();
						})
						.blur(function () {
							if ($.trim($(this).val()) == "") {
								$(this).prev().show();
							}
						});
					$("div.finder div.field label").click(function () {
						$(this).next().focus();
					});
					$("div.finder a.go.where").prev().keyup(function (e) {
						var source = $(this).val();
						var destination = $(this).next().attr("rel");
						if (e.keyCode == 13 && $.trim(source) != "")
								showmap(source, destination);
					});
					$("div.finder a.go.where").click(function () {
						var source = $(this).prev().val();
						var destination = $(this).attr("rel");
						if ($.trim(source) != "")
							showmap(source,destination);
					});
					$("div.finder div.infodir").fadeIn();
				});
			});
                });
        }

	function showmap(s,d) {
		window.open('http://maps.google.com/maps?daddr='+d+'&saddr='+s);
	}

        function centerMap() {
                self.map.setZoom(self.map.getBoundsZoomLevel(self.bounds) - 1);
                self.map.setCenter(self.bounds.getCenter());
        }
}

