var MapController = Class.create();
MapController.prototype = {
	initialize: function() {
		this.mapToolbar = new Toolbar("mapTools", {
			elementTags: [ "SPAN" ],
			elementClass: "mapButtonNormal",
			elementHoverClass: "mapButtonHover",
			elementActiveClass: "mapButtonActive",
			elementSelectedClass: "mapButtonSelected",
			elementDisabledClass: "mapButtonDisabled",
			includeChildChildren: false
		});
		this.mapToolbar.disableAll();

		this.mediaButtons = new Toolbar("mediaButtons", headerToolbarOptions);
		$("mediaButtons").hide();

		this.contactButtons = new Toolbar("contactButtons", formButtonOptions);
		$("contactPane").hide();

		this.resultButtons = new Toolbar("results", headerToolbarOptions);
		$("results").hide();

		this.sectionalTitleButtons = new Toolbar("sectionalTitleButtons", headerToolbarOptions);
		$("sectionalTitleOptionsPane").hide();

		$("mapColoredLegend").hide();

		this.layoutManager = new DockManager();
		this.sideBarLayoutManager = new DockManager({ container: "sideBar" });
		this.separatorLayoutManager = new DockManager({ container: "separator" });
		this.mapPanelLayoutManager = new DockManager({ container: "mapPanel" });
		this.mapBodyLayoutManager = new DockManager({ container: "mapBody" });
		this.sideBarPicLayoutManager = new DockManager({ container: "sideBarPic" });
		this.imagesPanePicLayoutManager = new DockManager({ container: "imagesPane" });
		this.contactPaneLayoutManager = new DockManager({ container: "contactPane" });
		this.sectionalTitleOptionsPaneLayoutManager = new DockManager({ container: "sectionalTitleOptionsPane" });

		var so = new SWFObject("Terrascape3.swf", "flashMap", "100%", "100%", "9", config.mapBackground);
		so.addVariable("url", escape("data.aspx?object=mapper&action=loadBinary&managementsystemid=" + managementSystemId + "&rasterstopmost=false"));
		so.addVariable("onLoad", "mapperLoaded");
		so.addVariable("debug", "true");
		so.addVariable("advancedStrokeRendering", "true");
		so.addVariable("useSlickZoom", config.useSlickZoom);
		so.addVariable("slickZoomDuration", config.slickZoomDuration);
		so.write("map");

		this.help = new PopupPanel("help", { autoHide: false, element: "map", horizontalAnchor: "center", verticalAnchor: "middle" });
		setTimeout(this.help.show.bind(this.help), 1000);

		this.photographIndex = -1;
		this.panoramicIndex = -1;
		setTimeout(function() { this.showPhotograph() }.bind(this), 1000);
	},
	clearMediaEffects: function(t) {
		this.panoramicImg = null;
		this.photographImg = null;
		if(t == "panoramic") {
			this.photographImgOld = null;
		} else {
			this.panoramicImgOld = null;
		}
		try { clearTimeout(this.photographTimeout) } catch(ex) {}
		try { clearTimeout(this.panoramicTimeout) } catch(ex) {}
		try { Effect.Queues.get("photographs").each(function(eff) { eff.cancel() }) } catch(ex) {}
		try { Effect.Queues.get("panoramics").each(function(eff) { eff.cancel() }) } catch(ex) {}
	},
	showPhotograph: function() {
		if(photographs.length == 0) {
			this.showPanoramic();
			return;
		}
		++this.photographIndex;
		if(this.photographIndex >= photographs.length) {
			this.photographIndex = -1;
			this.panoramicIndex = -1;
			this.showPanoramic();
			return;
		}
		if(this.photographImg) {
			this.photographImgOld = this.photographImg;
		}
		this.clearMediaEffects("photograph");
		this.photographImg = document.createElement("img");
		Event.observe(this.photographImg, "load", this.photographLoaded.bind(this));
		this.photographImg.src = photographs[this.photographIndex].FileName.substr(1);
	},
	photographLoaded: function() {
		//if($("panoramicsCheck").checked || !this.photographImg) {
		//	return;
		//}
		if(!this.photographImg) {
			return;
		}
		this.sizePhotograph(this.photographImg);
		if(this.photographImgOld) {
			new Effect.Opacity("photographImg", { duration: 1.5, from: 1.0, to: 0.0, queue: { scope: "photographs" } });
			this.photographTimeout = setTimeout(function() { this.fadeInPhotograph() }.bind(this), 1500);
		} else {
			this.fadeInPhotograph();
		}
	},
	sizePhotograph: function(media) {
		var c = $("imageContainer");
		var dim = c.getDimensions();
		dim.width -= parseInt(c.getStyle("padding-left")) + parseInt(c.getStyle("padding-right"));
		var dw = dim.width / media.width;
		var dh = dim.height / media.height;
		var w1 = media.width * dh;
		var h1 = media.height * dw;
		if(dw > dh) {
			media.width = w1;
			media.height = dim.height;
		} else {
			media.width = dim.width;
			media.height = h1;
		}
	},
	fadeInPhotograph: function() {
		$("imageContainer").update("");
		this.photographImg.id = "photographImg";
		Element.setOpacity(this.photographImg, 0);
		$("imageContainer").appendChild(this.photographImg);
		new Effect.Opacity("photographImg", { duration: 2.5, from: 0.0, to: 1.0, queue: { scope: "photographs" } });
		this.photographTimeout = setTimeout(function() { this.showPhotograph() }.bind(this), 5000);
	},
	showPanoramic: function() {
		if(panoramics.length == 0) {
			this.showPhotograph();
			return;
		}
		++this.panoramicIndex;
		if(this.panoramicIndex >= panoramics.length) {
			this.photographIndex = -1;
			this.panoramicIndex = -1;
			this.showPhotograph();
			return;
		}

		if(this.panoramicImg) {
			this.panoramicImgOld = this.panoramicImg;
		}
		this.clearMediaEffects("panoramic");
		this.panoramicImg = document.createElement("img");
		Event.observe(this.panoramicImg, "load", this.panoramicLoaded.bind(this));
		this.panoramicImg.src = panoramics[this.panoramicIndex].FileName.substr(1);
	},
	panoramicLoaded: function() {
		//if($("photographsCheck").checked || !this.panoramicImg) {
		//	return;
		//}
		if(!this.panoramicImg) {
			return;
		}
		this.sizePanoramic(this.panoramicImg);
		if(this.panoramicImgOld) {
			new Effect.Opacity("panoramicImg", { duration: 1.5, from: 1.0, to: 0.0, queue: { scope: "panoramics" } });
			this.panoramicTimeout = setTimeout(function() { this.fadeInPanoramic() }.bind(this), 1500);
		} else {
			this.fadeInPanoramic();
		}
	},
	sizePanoramic: function(media) {
		var c = $("imageContainer");
		var dim = c.getDimensions();
		if(dim.height > media.height) {
			dim.height = media.height;
		}
		var dh = dim.height / media.height;
		var w1 = media.width * dh;
		media.width = w1;
		media.height = dim.height;
	},
	fadeInPanoramic: function() {
		$("imageContainer").update("");
		this.panoramicImg.id = "panoramicImg";
		this.panoramicImg.style.position = "relative";
		var c = $("imageContainer");
		var dim = c.getDimensions();
		var w = this.panoramicImg.width - dim.width + 15;
		t = w / 100;
		Element.setOpacity(this.panoramicImg, 0);
		$("imageContainer").appendChild(this.panoramicImg);
		new Effect.Opacity("panoramicImg", { duration: 1, from: 0.0, to: 1.0, queue: { scope: "panoramics" } });
		new Effect.MoveBy("panoramicImg", 0, -w, { duration: t, queue: { position: "end", scope: "panoramics" } });
		new Effect.MoveBy("panoramicImg", 0, w, { duration: t, queue: { position: "end", scope: "panoramics" } });
		this.panoramicTimeout = setTimeout(function() { this.showPanoramic() }.bind(this), Math.round(t * 2000) + 1000);
	},

	/* Mapper Code */
	initializeMap: function(bounds) {
		this.bounds = bounds;
		this.mapDisplay = new Terrascape.MapDisplay.AdobeFlash9("flashMap", bounds);
		this.mapDisplay.viewBoxChanged = this.viewBoxChanged.bind(this);
		//this.mapDisplay.enableCursors(false);
		this.mapDisplay.loadCursor("select", "images/cursors/select.gif");
		this.mapDisplay.loadCursor("zoomIn", "images/cursors/zoomIn.png", 6, 8);
		this.mapDisplay.loadCursor("zoomOut", "images/cursors/zoomOut.png", 6, 8);
		this.mapDisplay.loadCursor("center", "images/cursors/center.png", 11, 11);
		this.mapDisplay.loadCursor("pan", "images/cursors/pan.png", 11, 11);
		this.mapDisplay.loadCursor("panClosed", "images/cursors/pan_closed.gif", 11, 11);
		this._defaultHandler = this.mapDisplay.setHandler(new Terrascape.DefaultMapper.DefaultHandler(this));

		this.searchLayer = this.mapDisplay.createLayer("__searchLayer");
		this.selectionLayer = this.mapDisplay.createLayer("__selectionLayer");
		this.animationLayer = this.mapDisplay.createLayer("__animationLayer");
		this.hiliteLayer = this.mapDisplay.createLayer("__hiliteLayer");
		this.currentLevel = levels[0];

		rasterImages.each(function(r) {
			var ri = Terrascape.MapData.rasterImages.find(function(i) {
				return r == i.id;
			}.bind(this));

			if(ri) {
				ri.show(this.mapDisplay);
			}
		}.bind(this));

		additionalLayers.each(function(al) {
			$("layer-" + al.Name).checked = al.IsDefault;
			$("layer-" + al.Name).disabled = false;
		}.bind(this));
		this.showFloor(this.currentLevel.Level);

		this.mapToolbar.enableAll();
		this.selectQuery();
		$("mapLoadingLegend").hide();
		$("mapColoredLegend").show();
		mapper = this;
	},

	viewBoxChanged: function() {
		var viewBox = this.mapDisplay.getViewBox();
		viewBox.x += this.mapDisplay.bounds.x;
		viewBox.y = this.mapDisplay.bounds.y - viewBox.y;
		var zoom = Math.min(viewBox.w, viewBox.h);
		var layers = Terrascape.MapData.getLayersScaledAt(zoom);
		for(var i = 0; i < layers.length; ++i) {
			loadSvgLayer("data.aspx?object=mapper&action=loadlayer&layerid=" + layers[i].id + "&viewboxx=" + viewBox.x + "&viewboxy=" + viewBox.y + "&viewboxw=" + viewBox.w + "&viewboxh=" + viewBox.h + "&boundsx=" + this.mapDisplay.bounds.x + "&boundsy=" + this.mapDisplay.bounds.y + "&boundsw=" + this.mapDisplay.bounds.w + "&boundsh=" + this.mapDisplay.bounds.h);
		}
		layers = Terrascape.MapData.getLabelsScaledAt(zoom);
		for(var i = 0; i < layers.length; ++i) {
			loadSvgLayer("data.aspx?object=mapper&action=loadlabels&id=" + managementSystemId + "&layerid=" + layers[i].id + "&fieldname=" + layers[i].field + "&size=" + layers[i].size + "&viewboxx=" + viewBox.x + "&viewboxy=" + viewBox.y + "&viewboxw=" + viewBox.w + "&viewboxh=" + viewBox.h + "&boundsx=" + this.mapDisplay.bounds.x + "&boundsy=" + this.mapDisplay.bounds.y + "&boundsw=" + this.mapDisplay.bounds.w + "&boundsh=" + this.mapDisplay.bounds.h);
		}
		var layers = Terrascape.MapData.getLayersNotScaledAt(zoom);
		for(var i = 0; i < layers.length; ++i) {
			var layer = this.mapDisplay.getLayer(layers[i].id);
			layer.clear();
		}
		layers = Terrascape.MapData.getLabelsNotScaledAt(zoom);
		for(var i = 0; i < layers.length; ++i) {
			var layer = this.mapDisplay.getLayer("l" + layers[i].id);
			layer.clear();
		}
	},

	layerChecked: function() {
		var m = this.mapDisplay, c = this.currentLevel;
		var legend = null;
		additionalLayers.each(function(al) {
			var checked = $("layer-" + al.Name).checked;
			al.Layers.each(function(all) {
				var inCurrent = false;
				c.Layers.each(function(l) {
					if(l.Id == all.Id) {
						inCurrent = true;
					}
				});
				m.setLayerVisibility(all.Id, checked && inCurrent);
				m.setLayerVisibility("l" + all.Id, checked && inCurrent);
				if(checked && inCurrent && al.SetLegend) {
					legend = all.Id;
				}
			});
		});
		this.generateLegend(legend);
	},
	generateLegend: function(id) {
		var layer = null;
		var category = Terrascape.MapData.categories.find(function(c) {
			var ll = c.layers.find(function(l) {
				return l.id == id;
			}.bind(this));
			if(ll) {
				layer = ll;
				return true;
			} else {
				return false;
			}
		}.bind(this));
		if(layer) {
			var html = "";
			layer.ranges.each(function(range) {
				html += "<span class='legend'><img src='" + range.icon + "&noborder=true&shadow=true' alt='" + range.name + "' align='absmiddle' width='16' height='16' /> - " + range.name + "</span> ";
			}.bind(this));
			$("mapColoredLegend").update(html);
		}
	},
	hideHelp: function() {
		this.help.hide();
		if(levels.length > 1) {
			this.floors = new PopupPanel("floors", { autoHide: false, element: "map", horizontalAnchor: "right", verticalAnchor: "top" });
			setTimeout(this.floors.show.bind(this.floors), 500);
		}
	},
	showFloor: function(floor) {
		if(!this.mapDisplay) return;
		var m = this.mapDisplay;
		var currentLevel = null;
		levels.each(function(l) {
			var checked = l.Level == floor;
			if(checked) {
				currentLevel = l;
			}
			l.Layers.each(function(al) {
				m.setLayerVisibility(al.Id, false);
				m.setLayerVisibility("l" + al.Id, false);
			});
		});
		this.currentLevel = currentLevel;
		this.layerChecked();
	},

	removeSelection: function() {
		if(this.mapDisplay) {
			this.selectionLayer.clear();
		}
	},

	zoomToExtents: function() {
		if(this.mapDisplay) {
			//this.deselectAll();
			//this.mapDisplay.clearHandler();
			this.mapDisplay.zoomToExtents();
		}
	},

	zoomIn: function() {
		if(this.mapDisplay) {
			this.deselectAll();
			if(this.mapDisplay.getHandlerType() == "zoomIn") {
				this.mapDisplay.setHandler(this._defaultHandler);
				this.mapDisplay.setCursor(null);
			} else {
				this.mapDisplay.setHandler(new Terrascape.DefaultMapper.ZoomInHandler(this));
				//$("zoomInButton").firstChild.src = 'templates/mapper/images/zoomIn_over.png';
				this.mapToolbar.select("zoomInButton");
				this.mapDisplay.setCursor("zoomIn");
			}
		}
	},

	zoomOut: function() {
		if(this.mapDisplay) {
			this.deselectAll();
			if(this.mapDisplay.getHandlerType() == "zoomOut") {
				this.mapDisplay.setHandler(this._defaultHandler);
				this.mapDisplay.setCursor(null);
			} else {
				this.mapDisplay.setHandler(new Terrascape.DefaultMapper.ZoomOutHandler(this));
				//$("zoomOutButton").firstChild.src = 'templates/mapper/images/zoomOut_over.png';
				this.mapToolbar.select("zoomOutButton");
				this.mapDisplay.setCursor("zoomOut");
			}
		}
	},

	zoomToSelected: function(layer, padding) {
		if(!padding) {
			padding = config.zoomToSelectionPadding;
		}
		if(this.mapDisplay) {
			//this.deselectAll();
			//this.mapDisplay.setHandler(this._defaultHandler);
			if(!layer) {
				layer = this.selectionLayer;
			}
			if(layer.getShapeCount() == 0) {
				return;
			}
			var shapes = layer.getShapes();
			var p0 = new Point(2147483648, 2147483648);
			var p1 = new Point(-2147483648, -2147483648);
			shapes.each(function(shape) {
				var bBox = this.mapDisplay.getShapeBBox(shape);
				if(bBox.x < p0.x) p0.x = bBox.x;
				if(bBox.y < p0.y) p0.y = bBox.y;
				if(bBox.x + bBox.w > p1.x) p1.x = bBox.x + bBox.w;
				if(bBox.y + bBox.h > p1.y) p1.y = bBox.y + bBox.h;
			}.bind(this));
			this.mapDisplay.zoomToRect(new Rect(p0.x - padding, p0.y - padding, p1.x - p0.x + 2 * padding, p1.y - p0.y + 2 * padding));
		}
	},

	zoomTo: function(x0, y0, x1, y1) {
		if(this.mapDisplay) {
			x0 = x0 - this.bounds.x;
			y0 = this.bounds.y - y0;
			x1 = x1 - this.bounds.x;
			y1 = this.bounds.y - y1;
			this.mapDisplay.zoomToRect(new Rect(x0, y0, x1 - x0, y1 - y0));
		}
	},

	center: function() {
		if(this.mapDisplay) {
			this.deselectAll();
			if(this.mapDisplay.getHandlerType() == "center") {
				this.mapDisplay.setHandler(this._defaultHandler);
				this.mapDisplay.setCursor(null);
			} else {
				this.mapDisplay.setHandler(new Terrascape.DefaultMapper.CenterHandler(this));
				this.mapToolbar.select("centerButton");
				this.mapDisplay.setCursor("center");
			}
		}
	},

	pan: function() {
		if(this.mapDisplay) {
			this.deselectAll();
			if(this.mapDisplay.getHandlerType() == "pan") {
				this.mapDisplay.setHandler(this._defaultHandler);
				this.mapDisplay.setCursor(null);
			} else {
				this.mapDisplay.setHandler(new MapController.PanHandler(this));
				this.mapToolbar.select("panButton");
				this.mapDisplay.setCursor("pan");
			}
		}
	},

	selectQuery: function() {
		if(this.mapDisplay) {
			this.deselectAll();
			if(this.mapDisplay.getHandlerType() == "selectQuery") {
				this.mapDisplay.setHandler(this._defaultHandler);
				this.mapDisplay.setCursor(null);
			} else {
				this.mapDisplay.setHandler(new MapController.SelectQueryHandler(this));
				//$("selectButton").firstChild.src = 'templates/mapper/images/select_over.png';
				this.mapToolbar.select("selectButton");
				this.mapDisplay.setCursor("select");
			}
		}
	},

	query: function() {
		if(this.mapDisplay) {
			var count = this.selectionLayer.getShapeCount();
			if(count == 1) {
				this.clearQuery(true);
				var shapes = this.selectionLayer.getShapes();
				this.startSelectionAnimation(shapes[0]);
				if(this.results) {
					var index = 0;
					var result = this.results.find(function(r, i) {
						if(r.id == shapes[0].id.substr(1)) {
							index = i;
							return true;
						}
					}.bind(this));
					if(!result) {
						this.searchLayer.clear();
						$("results").hide();
					} else {
						this.resultIndex = index;
						$("resultCount").update((this.resultIndex + 1) + " of " + this.results.length);
					}
				}
				url = "default.aspx?r=mapData&c=propertyInfo&layerId=" + getLayerId(shapes[0].id.substr(1)) + "&featureId=" + getFeatureId(shapes[0].id);
				new Ajax.Request( url, { onComplete: this.queryLoaded.bind(this), onError: this.queryError.bind(this) });
			} else {
				this.clearQuery(false);
			}
		}
	},

	clearQuery: function(loading) {
		if(this.propertyInfos) {
			this.propertyInfos.each(function(propertyInfo) {
				$("field-" + propertyInfo.Column).update("<br />");
			});
		}
		if(loading) {
			$("unitNo").value = Language.Loading;
		} else {
			$("unitNo").value = "";
		}
		//$("imageContainer").update("");
		//$("mediaButtons").hide();
	},

	queryLoaded: function(response) {
		var propertyInfos = checkJsonStatus(response);
		if(propertyInfos) {
			this.showPropertyInfo(propertyInfos);
		}
	},

	showPropertyInfo: function(propertyInfos) {
		this.propertyInfos = propertyInfos;
		this.priceInfo = null;
		var isSectionalTitle = false;
		propertyInfos.each(function(propertyInfo) {
			var floatVal = parseFloat(propertyInfo.Value);
			var intVal = parseInt(propertyInfo.Value);
			var val = "";
			if(!isNaN(intVal)) {
				if(floatVal == intVal) {
					if("" + intVal == propertyInfo.Value) {
						val = formatNumeric(intVal);
					} else {
						val = propertyInfo.Value;
					}
				} else {
					val = formatNumeric(floatVal);
				}
			} else {
				if(propertyInfo.Value.trim() == "") {
					val = "<br />";
				} else {
					val = propertyInfo.Value;
				}
			}
			$("field-" + propertyInfo.Column).update(propertyInfo.Prefix + val + propertyInfo.Suffix);
			if(propertyInfo.IsUnitNumber) {
				$("unitNo").value = propertyInfo.Value;
				$("contactUnitNo").value = propertyInfo.Value;
				$("unitNo").select();
			}
			if(propertyInfo.IsPrice) {
				this.priceInfo = propertyInfo;
			}
			if(propertyInfo.IsPhase) {
				this.currentPlotPhase = propertyInfo.Value;
			}
			if(propertyInfo.IsPlotType && propertyInfo.Value == "Sectional Title") {
				//TODO: Generalize value == "sectional title"
				isSectionalTitle = true;
			}
		}.bind(this));

		if(isSectionalTitle) {
			$("field-" + this.priceInfo.Column).update("<a href='#' onclick='return mapController.sectionalTitleOptionsClick()' class='plotInfoLink'>Click to view options</a>");
		}

		//this.mediaIndex = 1;
		//$("mediaButtons").show();
		//this.prevMedia();
	},
	sectionalTitleOptionsClick: function() {
		if(!this.sectionalTitleOptionsVisible) {
			$("imagesPane").hide();
			$("sideBarSearch").hide();
			$("contactPane").hide();
			$("sectionalTitleOptionsPane").show();
			this.layoutManager.performLayout();

			this.sectionalTitleIndex = 0;
			this.showSectionalTitleOption();

			$("field-" + this.priceInfo.Column).update("<a href='#' onclick='return mapController.sectionalTitleOptionsClick()' class='plotInfoLink'>Click to hide options</a>");
			this.sectionalTitleOptionsVisible = true;
		} else {
			$("imagesPane").show();
			$("sideBarSearch").show();
			$("contactPane").hide();
			$("sectionalTitleOptionsPane").hide();
			this.layoutManager.performLayout();

			$("field-" + this.priceInfo.Column).update("<a href='#' onclick='return mapController.sectionalTitleOptionsClick()' class='plotInfoLink'>Click to show options</a>");
			this.sectionalTitleOptionsVisible = false;
		}
	},
	prevSectionalTitle: function() {
		--this.sectionalTitleIndex;
		if(this.sectionalTitleIndex < 0) {
			this.sectionalTitleIndex = sectionalTitlePhases[this.currentPlotPhase].length - 1;
		}
		this.showSectionalTitleOption();
	},
	nextSectionalTitle: function() {
		++this.sectionalTitleIndex;
		if(this.sectionalTitleIndex > sectionalTitlePhases[this.currentPlotPhase].length - 1) {
			this.sectionalTitleIndex = 0;
		}
		this.showSectionalTitleOption();
	},
	showSectionalTitleOption: function() {
		$("sectionalTitleOption").update(sectionalTitlePhases[this.currentPlotPhase][this.sectionalTitleIndex]);
		$("sectionalTitleCount").update((this.sectionalTitleIndex + 1) + " of " + sectionalTitlePhases[this.currentPlotPhase].length);
	},

	queryError: function() {
		alert(Language.QueryError);
	},

	searchByUnitNo: function() {
		var unitNo = $F("unitNo").trim().toUpperCase();
		this.clearQuery(true);
		if(this.mapDisplay) {
			this.searchLayer.clear();
		}
		url = "default.aspx?r=mapData&c=searchByUnitNo&unitNo=" + unitNo;
		new Ajax.Request( url, { onComplete: this.searchByUnitNoLoaded.bind(this), onError: this.searchByUnitNoError.bind(this) });
		return false;
	},

	searchByUnitNoLoaded: function(response) {
		this.clearQuery(false);
		var detail = checkJsonStatus(response);
		if(detail) {
			if(this.mapDisplay) {
				this.selectionLayer.clear();
				this.showFloor(this.findLayerFloor(detail.LayerId));
				var shape = this.mapDisplay.getShape(makeFeatureId(detail.LayerId, detail.FeatureId));
				if(shape) {
					this.showSelection(shape, this.selectionLayer);
					this.showPropertyInfo(detail.PropertyInfoFields);
					if(config.zoomToSearchByUnitNoPadding > 0) {
						this.zoomToSelected(null, config.zoomToSearchByUnitNoPadding);
					} else if(config.zoomToSearchByUnitNoPadding == -1) {
						this.zoomToExtents();
					}
					this.startSelectionAnimation(shape);
				}
			} else {
				this.showPropertyInfo(detail.PropertyInfoFields);
			}
		}
	},
	findLayerFloor: function(layerId) {
		var floor;
		levels.each(function(level) {
			level.Layers.each(function(layer) {
				if(layer.Id == layerId) {
					floor = level.Level;
				}
			});
		});
		return floor;
	},

	searchByUnitNoError: function() {
		alert(Language.QueryError);
	},

	searchByPrice: function() {
		var priceFrom = parseFloat($("priceFrom").value);
		var priceTo = parseFloat($("priceTo").value);
		if(isNaN(priceFrom) || isNaN(priceTo)) {
			alert(Language.InvalidPrice);
			return false;
		}
		this.clearQuery(true);
		if(this.mapDisplay) {
			this.searchLayer.clear();
		}
		$("results").hide();
		url = "default.aspx?r=mapData&c=searchByPrice&priceFrom=" + priceFrom + "&priceTo=" + priceTo;
		new Ajax.Request( url, { onComplete: this.searchByPriceLoaded.bind(this), onError: this.searchByPriceError.bind(this) });
		return false;
	},

	searchByPriceLoaded: function(response) {
		this.clearQuery(false);
		var ids = checkJsonStatus(response);
		if(ids) {
			if(this.mapDisplay) {
				this.searchLayer.clear();
			}
			if(ids.length == 0) {
				alert(Language.NoResults);
				return;
			}
			if(ids.length > config.largeDataSetLimit) {
				alert(Language.TooManyResults.replace(/\$count/g, config.largeDataSetLimit));
			} else {
				alert(Language.SearchResults.replace(/\$count/g, ids.length));
			}
			this.results = [];
			ids.each(function(id, index) {
				if(index > config.largeDataSetLimit - 1) {
					throw $break;
				}
				if(this.mapDisplay) {
					var shape = this.mapDisplay.getShape(id);
					if(shape) {
						this.showSelection(shape, this.searchLayer, searchConfig);
						this.results.add(shape);
					}
				} else {
					this.results.add({ id: id });
				}
			}.bind(this));
			$("results").show();
			this.resultIndex = 1;
			this.prevResult();
		}
	},

	searchByPriceError: function() {
		alert(Language.QueryError);
	},

	showSelection: function(shape, layer, cfg) {
		if(this.mapDisplay) {
			if(!layer) {
				layer = this.selectionLayer
			}
			if(!cfg) {
				cfg = config;
			}
			if(typeof(shape) == "string") {
				var s = this.mapDisplay.getShape(shape);
			} else {
				var s = shape;
			}
			if(s && !layer.containsShape(cfg.selectionPrefix + s.id)) {
				var sel = s.clone();
				sel.id = cfg.selectionPrefix + sel.id;
				sel.strokeColor = cfg.selectionColor;
				sel.fillColor = cfg.selectionColor;
				sel.strokeOpacity = cfg.selectionOpacity;
				sel.fillOpacity = cfg.selectionOpacity;
				this.mapDisplay.addShape(sel, layer);
				return sel;
			} else {
				return null;
			}
		}
	},

	prevResult: function() {
		this.resultIndex--;
		if(this.resultIndex < 0) {
			this.resultIndex = this.results.length - 1;
		}
		this.showResult();
	},

	nextResult: function() {
		this.resultIndex++;
		if(this.resultIndex >=  this.results.length) {
			this.resultIndex = 0;
		}
		this.showResult();
	},

	showResult: function() {
		if(this.mapDisplay) {
			this.selectionLayer.clear();
			this.showSelection(this.results[this.resultIndex], this.selectionLayer, config);
			if(config.zoomToSearchByPricePadding > 0) {
				this.zoomToSelected(null, config.zoomToSearchByPricePadding);
			} else if(config.zoomToSearchByPricePadding == -1) {
				this.zoomToExtents();
			} else if(config.zoomToSearchByPricePadding == -2) {
				this.zoomToSelected(this.searchLayer);
			}
		}
		$("resultCount").update((this.resultIndex + 1) + " of " + this.results.length);
		if(this.mapDisplay) {
			this.query();
		} else {
			url = "default.aspx?r=mapData&c=propertyInfo&layerId=" + getLayerId(this.results[this.resultIndex].id) + "&featureId=" + getFeatureId(this.results[this.resultIndex].id);
			new Ajax.Request( url, { onComplete: this.queryLoaded.bind(this), onError: this.queryError.bind(this) });
		}
	},

	/* Media */
	prevMedia: function() {
		return;
		this.mediaIndex--;
		if(this.mediaIndex < 0) {
			this.mediaIndex = media.length - 1;
		}
		$("imageContainer").update("<img src='media/" + media[this.mediaIndex] + "' />");
		$("mediaCount").update((this.mediaIndex + 1) + " of " + media.length);
	},

	nextMedia: function() {
		return;
		this.mediaIndex++;
		if(this.mediaIndex >= media.length) {
			this.mediaIndex = 0;
		}
		$("imageContainer").update("<img src='media/" + media[this.mediaIndex] + "' />");
		$("mediaCount").update((this.mediaIndex + 1) + " of " + media.length);
	},

	/* Toolbar */
	deselectAll: function() {
		//$("zoomInButton").firstChild.src = 'templates/mapper/images/zoomIn.png';
		//$("zoomOutButton").firstChild.src = 'templates/mapper/images/zoomOut.png';
		//$("panButton").firstChild.src = 'templates/mapper/images/pan.png';
		//$("selectButton").firstChild.src = 'templates/mapper/images/select.png';
		this.mapToolbar.deselectAll();
	},

	/* Animation */
	startSelectionAnimation: function(shape) {
		return;
		if(this.mapDisplay) {
			var viewBox = this.mapDisplay.getViewBox();
			var rect = new Terrascape.Rect({ id: "anim" + shape.id, strokeColor: config.selectionColor, strokeWidth: "0.5%", fillColor: "none", fillOpacity: 0, x: viewBox.x, y: viewBox.y, width: viewBox.w, height: viewBox.h });
			this.mapDisplay.addShape(rect, this.animationLayer);
			setTimeout(function() { this.runSelectionAnimation(shape, rect) }.bind(this), 20);
		}
	},

	runSelectionAnimation: function(shape, rect) {
		if(this.mapDisplay) {
			if(!shape.w) {
				var bbox = this.mapDisplay.getShapeBBox(shape);
				shape.x = bbox.x;
				shape.y = bbox.y;
				shape.w = bbox.w;
				shape.h = bbox.h;
			}
			rect.x += parseInt((shape.x - rect.x) / 8);
			rect.y += parseInt((shape.y - rect.y) / 8);
			rect.width -= parseInt((rect.x + rect.width - shape.x - shape.w) / 8) * 2;
			rect.height -= parseInt((rect.y + rect.height - shape.y - shape.h) / 8) * 2;
			if(rect.width < shape.w + 100 || rect.height < shape.h + 100) {
				this.mapDisplay.removeShape(rect, this.animationLayer);
			} else {
				this.mapDisplay.applyShape(rect);
				setTimeout(function() { this.runSelectionAnimation(shape, rect) }.bind(this), 20);
			}
		}
	},

	/* Contact Agent */
	contactAgent: function() {
		$("sideBarSearch").hide();
		$("imagesPane").hide();
		$("contactPane").show();
		$("sectionalTitleOptionsPane").hide();
		this.layoutManager.performLayout();
		$("contactUnitNo").select();
		$("contactUnitNo").focus();
	},

	contactOk: function() {
		var xml = Xml.Form.createDocument("contactForm");
		if(xml) {
			Form.disable("contactForm");
			new Ajax.Request("default.aspx?r=mapData&c=inquiry", { postBody: xml, onComplete: this.contactOkLoaded.bind(this), onError: this.contactOkError.bind(this) });
		}
		return false;
	},

	contactOkLoaded: function(response) {
		if(checkXmlStatus(response)) {
			$("sideBarSearch").show();
			$("imagesPane").show();
			$("contactPane").hide();
			$("sectionalTitleOptionsPane").hide();
			this.layoutManager.performLayout();
			alert(Language.ContactAgentSent);
		}
		Form.enable("contactForm");
	},

	contactOkError: function() {
		alert(Language.InquiryError);
		Form.enable("contactForm");
	},

	contactCancel: function() {
		$("sideBarSearch").show();
		$("imagesPane").show();
		$("contactPane").hide();
		$("sectionalTitleOptionsPane").hide();
		this.layoutManager.performLayout();
		this.sizePhotograph(this.photographImg);
	},

	/* Destructor */
	destroy: function() {
		this.layoutManager.destroy();
		this.contentLayoutManager.destroy();
		this.infoLayoutManager.destroy();
	}
};
MapController.SelectHandler = Class.extend(Terrascape.DefaultMapper.DefaultHandler, {
	initialize: function(mapper) {
		this.parent(mapper);
		this.type = "select";
	},
	mouseMove: function(e) {
		this.parent(e);
		window.status += " - selecting";
		var hiliteLayer = this.mapper.hiliteLayer;
		hiliteLayer.clear();
		var shape = null;
		this.mapper.currentLevel.Layers.each(function(layer) {
			shape = this.mapDisplay.getShapeFromPoint(this.mapDisplay.getLayer(layer.Id), e.point);
			if(shape) {
				throw $break;
			}
		}.bind(this));

		if(shape) {
			this.mapper.showSelection(shape, hiliteLayer, hiliteConfig);
			config.displayHelpMessage(selectionLayer.getShapeCount() + " feature(s) selected.");
		}
	},
	mouseUp: function(e) {
		var selectionLayer = this.mapper.selectionLayer;
		if(!e.shift) {
			selectionLayer.clear();
		}
		var shape = null;
		this.mapper.currentLevel.Layers.each(function(layer) {
			shape = this.mapDisplay.getShapeFromPoint(this.mapDisplay.getLayer(layer.Id), e.point);
			if(shape) {
				throw $break;
			}
		}.bind(this));

		if(shape) {
			this.mapper.showSelection(shape, selectionLayer);
			config.displayHelpMessage(selectionLayer.getShapeCount() + " feature(s) selected.");
		}
	}
});
MapController.SelectQueryHandler = Class.extend(MapController.SelectHandler, {
	initialize: function(mapper) {
		this.parent(mapper);
		this.type = "selectQuery";
	},
	mouseMove: function(e) {
		this.parent(e);
		window.status += " and querying";
	},
	mouseUp: function(e) {
		//var pt = this.mapDisplay.containerPointToMap(e.point);
		//this.mapDisplay.addShape(new Terrascape.Point({ x: pt.x, y: pt.y, radius: 100 }));
		this.parent(e);
		this.mapper.query();
		if(config.zoomToSelectPadding > 0) {
			this.mapper.zoomToSelected(null, config.zoomToSelectPadding);
		} else if(config.zoomToSelectPadding == -1) {
			this.mapper.zoomToExtents();
		}
	}
});
MapController.PanHandler = Class.extend(Terrascape.DefaultMapper.PanHandler, {
	mouseDown: function(e) {
		this.mapper.mapDisplay.setCursor("panClosed");
		this.parent(e);
	},
	mouseUp: function(e) {
		this.mapper.mapDisplay.setCursor("pan");
		this.parent(e);
	}
});

var mapController, mapper;
Event.observe(window, "load", function() {
	mapController = new MapController();
});
function mapperLoaded(x, y, w, h) {
	if(x && y && w && h) {
		var bounds = new Rect(x, y, w, h);
		mapController.initializeMap(bounds);
	} else {
		alert(Language.LoadError);
	}
}

