Include(
	"/gmaps/scripts/component/clusterer.js",
	"/gmaps/scripts/component/directions.js",
	"/gmaps/scripts/component/json-descriptors-getter.js",
	"/gmaps/scripts/component/agent-searcher.js"
);

var MapDialog = function(){
	var dialog, showBtn, houseMap, houses;
	
    return {
        show : function(houseId, lat, lng){
		this.currentId = houseId;

		if (!houseMap) {
			houseMap =
				new GMap2(document.getElementById("houseMap"));
			houseMap.addControl(new GLargeMapControl());
			houseMap.addControl(new GMapTypeControl());
			houseMap.setCenter( new GLatLng(0, 0), 6 );
			
			GEvent.addListener(
				houseMap, "dragend",
				function () { houses.showVisible(); }
				);
			GEvent.addListener(
				houseMap, "zoomend",
				function () { houses.showVisible(); }
				);
		}

		if (!houses) {
			houses = new AgentSearcher(
				houseMap,
				new JSONDescriptorsGetter("/NeighborListJS.asmx/GetList"),
				{
					markerContentURL: "/house-balloon.sdf/",
					directions: new Directions(),
					clusterItemTitle:
						function (item) { return item.data.description; }
				}
				);

			var neighborIcon  = new GIcon();
			neighborIcon.image = "/gmaps/images/neighbor.png";
			neighborIcon.iconSize = new GSize(22, 27);
			neighborIcon.shadow = "/gmaps/images/house_shadow.png";
			neighborIcon.shadowSize = new GSize(40, 28);
			neighborIcon.iconAnchor = new GPoint(11, 27);
			neighborIcon.infoWindowAnchor = new GPoint(11, 3);

			var houseIcon  = new GIcon();
			houseIcon.image = "/gmaps/images/house.png";
			houseIcon.iconSize = new GSize(22, 27);
			houseIcon.shadow = "/gmaps/images/house_shadow.png";
			houseIcon.shadowSize = new GSize(40, 28);
			houseIcon.iconAnchor = new GPoint(11, 27);
			houseIcon.infoWindowAnchor = new GPoint(11, 3);

			var clusterIcon = new GIcon();
			clusterIcon.image = "/images/cluster.png";
			clusterIcon.shadow = "/images/cluster_shadow.png";
			clusterIcon.iconSize = new GSize(30, 32);
			clusterIcon.shadowSize = new GSize(47, 32);
			clusterIcon.iconAnchor = new GPoint(18, 32);
			clusterIcon.infoWindowAnchor = new GPoint(15, 1);

			houses.markerIcon = neighborIcon;
			houses.clusterIcon = clusterIcon;
			houses.singleIcon = houseIcon;
		}

		if(!dialog){ 
			dialog = new Ext.BasicDialog("map-dlg", { 
				shadow:true,
				minWidth:325,
				minHeight:325,
				collapsible: false,
				constraintoviewport: false,
				fixedcenter:true,
				proxyDrag: true
			});
			dialog.addKeyListener(27, dialog.hide, dialog);
			dialog.setContentSize(400, 300);
			houseMap.checkResize();
			dialog.on( "resize", function () {				 
					houseMap.checkResize();
					houses.showVisible();
				});
		}

		houses.showSingle(houseId);
		houseMap.setCenter( new GLatLng(lat, lng), 6 );
		dialog.show();
        }
        
    };
}();
