function EstateWeb_Objects_MappingManager(oid){
	//properties (public)
	this.id = oid;
	this.searchManager = new EstateWeb_Objects_PropertySearch();
	this.createUniqueID = EstateWeb_Objects_MappingManager_CreateUniqueID;
	//properties (private)
	//objects (public)
	this.style = new EstateWeb_Objects_MappingManager_Style();
	//objects (private)
	this.mapoptions = new EstateWeb_Objects_MappingManager_MapOptions(this);
	this.markers = new EstateWeb_Objects_MappingManager_Markers(this);
	//methods
	this.load = EstateWeb_Objects_MappingManager_Load;
	this.setCenter = EstateWeb_Objects_MappingManager_SetCenter;
	this.preloadvalidation = EstateWeb_Objects_MappingManager_PreloadValidation;
	this.backToMap = EstateWeb_Objects_MappingManager_BackToMap;
	this.showBackToMap = EstateWeb_Objects_MappingManager_ShowBackToMap;
	this.hideBackToMap = EstateWeb_Objects_MappingManager_HideBackToMap;
	//events
	this.events = new HttpManager.Browser.eventManager();
	this.onPropertySearchComplete = EstateWeb_Objects_MappingManager_OnPropertySearchComplete;
	this.onPropertySearchBegin = EstateWeb_Objects_MappingManager_OnPropertySearchBegin;
	this.onPageDispose = EstateWeb_Objects_MappingManager_onPageDispose;
	this.onPropertyMarkerClick = EstateWeb_Objects_MappingManager_OnPropertyMarkerClick;
	this.onPropertyMarkerGroupClick = EstateWeb_Objects_MappingManager_OnPropertyMarkerGroupClick;
	this.onMapMoveEnd = EstateWeb_Objects_MappingManager_OnMapMoveEnd;
	this.onMapInfoWindowClose = EstateWeb_Objects_MappingManager_OnMapInfoWindowClose;
	this.onPropertyFullDetailsClick = EstateWeb_Objects_MappingManager_OnPropertyFullDetailsClick;
	this.onPropertyFullDetailsLoaded = EstateWeb_Objects_MappingManager_OnPropertyFullDetailsLoaded;
	this.onPropertyBackToMapRequest = EstateWeb_Objects_MappingManager_OnPropertyBackToMapRequest;
	this.onBrowserWindowResize = EstateWeb_Objects_MappingManager_OnBrowserWindowResize;
	this.onPropertyGroupOptionClick = EstateWeb_Objects_MappingManager_OnPropertyGroupOptionClick;
	this.onPropertyGroupOptionMouseOver = EstateWeb_Objects_MappingManager_OnPropertyGroupOptionMouseOver;
	this.onPropertyGroupOptionMouseOut = EstateWeb_Objects_MappingManager_OnPropertyGroupOptionMouseOut;
	this.onPropertyMarkerMouseOver = EstateWeb_Objects_MappingManager_OnPropertyMarkerMouseOver;
	this.onPropertyMarkerMouseOut = EstateWeb_Objects_MappingManager_OnPropertyMarkerMouseOut;
}

//#### Root Methods ######

function EstateWeb_Objects_MappingManager_CreateUniqueID(id){
	return HttpManager.Strings.Format("{0}_{1}", this.id, id);
}

//Prepares the map
function EstateWeb_Objects_MappingManager_Load(){
	if ( this.preloadvalidation() ){
		if ( GBrowserIsCompatible() ){
			this.mapoptions.checkSize();
			this.mapoptions.mapreference = new GMap2( HttpManager.Document.GetObject(this.mapoptions.renderToContainer) );
			this.mapoptions.apply();
			this.events.raiseEvent("mapload", this, null);
		}
	}
}

function EstateWeb_Objects_MappingManager_Markers(o){
	this.parent = o;
	this.items = new Array();
	this.add = EstateWeb_Objects_MappingManager_Markers_Add;
	this.show = EstateWeb_Objects_MappingManager_Markers_Show;
	this.removeAll = EstateWeb_Objects_MappingManager_Markers_RemoveAll;
	this.showByKey = EstateWeb_Objects_MappingManager_ShowByKey;
}

function EstateWeb_Objects_MappingManager_ShowBackToMap(){
	if ( HttpManager.Document.GetObject( this.id + "_backToMapContainer" ) ){
		HttpManager.Document.GetObject( this.id + "_backToMapContainer" ).style.visibility = "visible";
	}	
}

function EstateWeb_Objects_MappingManager_HideBackToMap(){	
	if ( HttpManager.Document.GetObject( this.id + "_backToMapContainer" ) ){
		HttpManager.Document.GetObject( this.id + "_backToMapContainer" ).style.visibility = "hidden";
	}	
}

function EstateWeb_Objects_MappingManager_BackToMap(){
	this.mapoptions.hidePropertyDetailFrame();
	this.mapoptions.show();
	this.hideBackToMap();
}

function EstateWeb_Objects_MappingManager_ShowByKey(propertyReference){
	for ( var i = 0; i < this.items.length; i ++ ){
		var oitem = this.items[i];
		if ( oitem.propertyReference == propertyReference ){
			this.parent.mapoptions.hidePropertyDetailFrame();
			this.parent.mapoptions.show();
			if ( this.parent.mapoptions.groupManager.reference.FindClusterFromMarker( oitem.marker ) ){
				Clusterer.PopUp ( this.parent.mapoptions.groupManager.reference.FindClusterFromMarker( oitem.marker ), oitem.marker );
			}else{
				this.parent.events.raiseEvent("propertymarkerclick", this, { "marker" : oitem.marker });
			}
			
			break;
		}
	}
}

function EstateWeb_Objects_MappingManager_Markers_Add(oproperty,omarker){
	this.items.push({
									"marker" : omarker,
									"propertyReference" : oproperty.propertyReference
								}
							  )
}

function EstateWeb_Objects_MappingManager_Markers_RemoveAll(){
	if ( this.parent.mapoptions.groupManager.reference ){
		for ( var i = 0; i < this.items.length; i ++ ){
			this.parent.mapoptions.groupManager.reference.RemoveMarker( this.items[i].marker );
		}
	}
}

function EstateWeb_Objects_MappingManager_Markers_Show(){
	omapoptions = this.parent.mapoptions;
	var iposition = 0;
	//draw markers on the map
	for ( var i = 0; i < this.parent.searchManager.properties.items.length; i ++ ){
		var oproperty = this.parent.searchManager.properties.items[i];
		var opropertyIcon = omapoptions.icons.createGIcon(omapoptions.icons.getIconForProperty(oproperty), oproperty.listingType);
		var opropertyMarker = new GMarker( new GLatLng( oproperty.mapping.latitude, oproperty.mapping.longitude ), opropertyIcon);
		opropertyMarker.key = oproperty.propertyReference;
		iposition = i;
		omapoptions.groupManager.reference.AddMarker( opropertyMarker, oproperty.propertyReference );
		this.add( oproperty, opropertyMarker );
		setTimeout("omapoptions.parent.events.raiseEvent(\"propertymarkersplotprogresschange\", omapoptions.parent, { \"index\" : "+i+" , \"total\" : "+this.parent.searchManager.properties.items.length+" } );",20);
		
		GEvent.addListener( opropertyMarker, "click", function(){
			omapoptions.parent.events.raiseEvent("propertymarkerclick", omapoptions.parent, { "marker" : this } );
		});
		GEvent.addListener( opropertyMarker, "mouseover", function(){ 
			omapoptions.parent.events.raiseEvent("propertymarkermouseover", omapoptions.parent, { "marker" : this } );
		} );
		GEvent.addListener( opropertyMarker, "mouseout", function(){ 
			omapoptions.parent.events.raiseEvent("propertymarkermouseout", omapoptions.parent, { "marker" : this } );
		} );
	}
}

function EstateWeb_Objects_MappingManager_SetCenter(bounds, withCorrectZoomLevel){
	var viewWidth = HttpManager.Document.GetObject(this.mapoptions.renderToContainer).offsetWidth * 0.9;
	var viewHeight = HttpManager.Document.GetObject(this.mapoptions.renderToContainer).offsetHeight * 0.9;
	var oGLatLng = new GLatLng( ((bounds[0]+bounds[1])/2), ((bounds[2]+bounds[3])/2) );
	var oGLatLngBounds = new GLatLngBounds( new GLatLng(bounds[0],bounds[3]), new GLatLng(bounds[1],bounds[2]) );
	var ocorrectzoomlevel = this.mapoptions.mapreference.getBoundsZoomLevel( oGLatLngBounds );
	this.mapoptions.mapreference.setCenter( oGLatLng , ocorrectzoomlevel ); 
}


function EstateWeb_Objects_MappingManager_MapOptions(o){
	this.parent = o;
	this.styleDirectory = "images/controls/maps/style1";
	this.mapreference;
	this.renderToContainer = "";
	this.alignToContainer = "";
	this.mapLayoutContainer = "";
	this.resultsLayoutContainer = "";
	this.minimumHeight = 300;
	this.heightPadding = 0;
	this.showSmallNavigationControl = false;
	this.showLargeNavigationControl = false;
	this.enableDoubleClickZooming = true;
	this.showMapTypeControl = false;
	this.viewPropertyDetailTypes = new EstateWeb_Objects_MappingManager_MapOptions_ViewPropertyDetailTypes();
	this.viewPropertyDetailType = this.viewPropertyDetailTypes.normal;
	this.icons = new EstateWeb_Objects_MappingManager_MapOptions_Icons(this);
	this.groupManager = new EstateWeb_Objects_MappingManager_MapOptions_GroupManager(this);
	this.apply = EstateWeb_Objects_MappingManager_MapOptions_Apply;
	this.checkSize = EstateWeb_Objects_MappingManager_MapOptions_CheckSize;
	this.maximumTabs = 3;
	this.lastPosition;
	this.tooltips = new EstateWeb_Objects_MappingManager_MapOptions_Tooltips(this);
	this.show = EstateWeb_Objects_MappingManager_ShowMap;
	this.showPropertyDetailFrame = EstateWeb_Objects_MappingManager_ShowPropertyDetailFrame;
	this.hidePropertyDetailFrame = EstateWeb_Objects_MappingManager_HidePropertyDetailFrame;
	this.hide = EstateWeb_Objects_MappingManager_HideMap;
	this.showMapLayoutContainer = EstateWeb_Objects_MappingManager_MapOptions_ShowMapLayoutContainer;
	this.hideMapLayoutContainer = EstateWeb_Objects_MappingManager_MapOptions_HideMapLayoutContainer;
	this.zoomToPoint = EstateWeb_Objects_MappingManager_MapOptions_ZoomToProperties;
	this.showListView = EstateWeb_Objects_MappingManager_MapOptions_ShowListView;
}

function EstateWeb_Objects_MappingManager_MapOptions_ZoomToProperties(aproperties){
	var creator = this.parent;
	creator.mapoptions.mapreference.closeInfoWindow();
	var oresults = new EstateWeb_Objects_PropertySearch();
	for ( var i = 0; i < aproperties.length; i ++ ){
		oresults.properties.items.push( creator.searchManager.properties.getProperty( aproperties[i] ) );
	}
	creator.setCenter( oresults.mapping.calculateCenter() );
}

function EstateWeb_Objects_MappingManager_MapOptions_ShowListView(aproperties){
	var creator = this.parent;
	var oresponse = MapView.GetPropertiesListView( aproperties, creator.id );
	if ( oresponse.value ){
		creator.mapoptions.mapreference.closeInfoWindow();
		var odialog_listview = new EstateWeb_Objects_DialogManager("mapsearch_listview");
		with ( odialog_listview ){
			parentID = "cntrlPropertySearch_map_pnlMap";
			backgroundOptions.opacity = 0.5;
			dialogOptions.title.text = "Property Details";
			dialogOptions.title.className = "";
			dialogOptions.type = dialogTypes.panel;
			dialogOptions.height = this.mapreference.getSize().height;
			dialogOptions.width = this.mapreference.getSize().width;
			dialogOptions.panelOptions.contentData = oresponse.value;
			show();
		}
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_HideMapLayoutContainer(){
	if ( HttpManager.Document.GetObject( this.mapLayoutContainer ) && HttpManager.Document.GetObject( this.resultsLayoutContainer ) ){
		HttpManager.Document.GetObject( this.resultsLayoutContainer ).style.display = "block";
		HttpManager.Document.GetObject( this.mapLayoutContainer ).style.display = "none";
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_ShowMapLayoutContainer(){
	if ( HttpManager.Document.GetObject( this.mapLayoutContainer ) && HttpManager.Document.GetObject( this.resultsLayoutContainer ) ){
		HttpManager.Document.GetObject( this.resultsLayoutContainer ).style.display = "none";
		HttpManager.Document.GetObject( this.mapLayoutContainer ).style.display = "block";
	}
}

function EstateWeb_Objects_MappingManager_ShowPropertyDetailFrame(){
	if ( HttpManager.Document.GetObject( this.parent.id + "_PropertyDetails") ){
		with ( document.getElementById( this.parent.id + "_PropertyDetails" ) ){
			style.visibility = "visible";
		}
	}
}

function EstateWeb_Objects_MappingManager_HidePropertyDetailFrame(){
	if ( HttpManager.Document.GetObject( this.parent.id + "_PropertyDetails") ){
		document.getElementById( this.parent.id + "_PropertyDetails" ).style.visibility = "hidden";
		document.getElementById( this.parent.id + "_PropertyDetails" ).style.height = "1px";
	}
}

function EstateWeb_Objects_MappingManager_ShowMap(){
	with ( HttpManager.Document.GetObject( this.renderToContainer) ) {
		style.display = "block";
		parentNode.style.height = this.mapreference.getSize().height + "px";
	}
	
}

function EstateWeb_Objects_MappingManager_HideMap(){
	HttpManager.Document.GetObject( this.renderToContainer ).style.display = "none";
}

function EstateWeb_Objects_MappingManager_MapOptions_ViewPropertyDetailTypes(){
	this.inline = 0;
	this.normal = 1;
}

function EstateWeb_Objects_MappingManager_MapOptions_CheckSize(){
	if ( HttpManager.Document.GetObject( this.alignToContainer ) ){
		var isize = Math.max( this.minimumHeight, HttpManager.Document.GetObject( this.alignToContainer ).offsetHeight );
		HttpManager.Document.GetObject( this.renderToContainer ).style.height = ( isize + this.heightPadding ) + "px";	
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_Tooltips(o){
	this.parent = o;
	this.enabled = true;
	this.style = new EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Style(this);
	this.show = EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Show;
	this.hide = EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Hide;
	this.create = EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Create;
}

function EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Create(){
	//make sure not already rendered
	if (! HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")) ){
		var otooltip = document.createElement("div");
		otooltip.id = this.parent.parent.createUniqueID("markertooltip");
		with ( otooltip.style ){
			position = "absolute";
			backgroundColor = this.style.backgroundColour;
			fontColor = this.style.fontColor;
			border = HttpManager.Strings.Format("{0} {1} {2}", this.style.borderStyle, this.style.borderWidth + "px", this.style.borderColour);
			padding = this.style.padding + "px";
		}
		HttpManager.Document.GetObject(this.parent.renderToContainer).appendChild( otooltip );
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Hide(){
	//hide the tooltip div
	if ( HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")) ){
		HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")).style.display = "none";
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Show(omarker,oicon,text){
	if ( omarker ){
		//hide if its already visible
		this.hide();
		//convert the position of the marker to relative x,y
		var opoint = this.parent.mapreference.getCurrentMapType().getProjection().fromLatLngToPixel( this.parent.mapreference.getBounds().getSouthWest(), this.parent.mapreference.getZoom() );
		var ooffset = this.parent.mapreference.getCurrentMapType().getProjection().fromLatLngToPixel( omarker.getPoint(), this.parent.mapreference.getZoom() );
		var opos = new GControlPosition( G_ANCHOR_BOTTOM_LEFT, new GSize( ( ooffset.x - opoint.x )+oicon.tooltipAnchorX, (- ooffset.y + opoint.y) + oicon.tooltipAnchorY) );
		
		this.create();
		opos.apply(  HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")) );
		with ( HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")) ){
			innerHTML = text;
		}
		HttpManager.Document.GetObject(this.parent.parent.createUniqueID("markertooltip")).style.display = "block";
	}
}

function EstateWeb_Objects_MappingManager_MapOptions_Tooltips_Style(o){
	this.parent = o;
	this.backgroundColour = "#000000";
	this.transparencyPerecentage = 100;
	this.borderColour = "#000000";
	this.borderWidth = 1;
	this.borderStyle = "solid";
	this.padding = 3;
	this.anchorX = 0;
	this.anchorY = 0;
	this.fontColor = "#FFFFFF";
}

function EstateWeb_Objects_MappingManager_MapOptions_GroupManager(o){
	this.parent = o;
	this.reference;
	this.maximumVisibleMarkers = 10;
	this.minimumMarkersPerCluster = 2;
}

function EstateWeb_Objects_MappingManager_MapOptions_Apply(){
	var o = this;
	if ( this.showSmallNavigationControl ){
		this.mapreference.addControl( new GSmallMapControl() );
	}else if ( this.showLargeNavigationControl ){
		this.mapreference.addControl( new GLargeMapControl() );
	}
	if ( this.showMapTypeControl ){
		this.mapreference.addControl( new GMapTypeControl() );
	}
	if ( this.enableDoubleClickZooming ){
		this.mapreference.enableDoubleClickZoom();
		this.mapreference.enableContinuousZoom();
	}
	if (!this.groupManager.reference ){
		this.groupManager.reference = new Clusterer( this.parent );
		this.groupManager.reference.SetMaxVisibleMarkers( this.groupManager.maximumVisibleMarkers );
		this.groupManager.reference.SetMinMarkersPerCluster( this.groupManager.minimumMarkersPerCluster );
		this.groupManager.reference.SetIcon( this.icons.createGIcon(this.icons.getIconsByType(this.icons.iconTypes.group)[0],-1) );
		this.groupManager.reference.events.addHandler("markergroupclicked", this.parent, this.parent.onPropertyMarkerGroupClick);
		this.groupManager.reference.events.addHandler("markergroupmouseover", this.parent, this.parent.onPropertyGroupOptionMouseOver);
		this.groupManager.reference.events.addHandler("markergroupmouseout", this.parent, this.parent.onPropertyGroupOptionMouseOut);
		o.parent.events.addHandler("onpropertygroupoptionclick", this.parent, this.parent.onPropertyGroupOptionClick );
	}
	GEvent.addListener(this.mapreference, "moveend", function(){
		o.parent.events.raiseEvent("onmapmoveend", o.parent, null);
	});
	GEvent.addListener(this.mapreference, "infowindowclose", function(){
		o.parent.events.raiseEvent("onmapinfowindowclose", o.parent, null );
	});
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons(o){
	this.parent = o;
	this.items = new Array();
	this.add = EstateWeb_Objects_MappingManager_MapOptions_Icons_Add;
	this.icon = EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon;
	this.iconTypes = new EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_IconTypes(this);
	this.getIconsByType = EstateWeb_Objects_MappingManager_MapOptions_Icons_GetIconsByType;
	this.getIconForProperty = EstateWeb_Objects_MappingManager_MapOptions_Icons_GetIconForProperty;
	this.createGIcon = EstateWeb_Objects_MappingManager_MapOptions_Icons_CreateGIcon;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_GetIconForProperty(oproperty){
	//gets the most appropriate icon from the collection for a property
	var oIconForProperty;
	var oIconsSingle = this.getIconsByType( this.iconTypes.single );
	for ( var i = 0; i < oIconsSingle.length; i ++ ){
		var oIcon = oIconsSingle[i];
		if ( oIcon.listingTypes.contains( oproperty.propertyListingType ) && oIcon.statuses.contains( oproperty.propertyStatusID ) ){
			oIconForProperty = oIcon;
			break;
		}
	}
	if (!oIconForProperty){
		oIconForProperty = this.getIconsByType( this.iconTypes.generic )[0];
	}
	return oIconForProperty;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_CreateGIcon(oIcon, listingType){
	var oGIcon = new GIcon();
	if ( oIcon != null ){
		oGIcon.image = this.parent.parent.style.formatURL(oIcon.imageURL, listingType);
		oGIcon.iconSize = new GSize( oIcon.imageWidth, oIcon.imageHeight );
		oGIcon.iconAnchor = new GPoint( oIcon.anchorX, oIcon.anchorY );
		if ( oIcon.shadowURL.length > 0 ){
			oGIcon.shadow = oIcon.shadowURL;
			oGIcon.shadowSize = new GSize( oIcon.shadowWidth, oIcon.shadowHeight );
		}
	}else{
		alert("Unable to find icon");
	}
	return oGIcon;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_GetIconsByType(type){
	var result = new Array();
	for ( var i = 0 ; i < this.items.length; i ++ ){
		if ( this.items[i].iconType == type ){
			result.push( this.items[i] );
		}	
	}
	return result;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon(){
	this.propertyListingTypes = new EstateWeb_Objects_Property_PropertyListingTypes();
	this.listingTypes = new EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_ListingTypes();
	this.propertyStatuses = new EstateWeb_Objects_Property_PropertyStatuses();
	this.statuses = new EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses();
	this.imageURL = "";
	this.imageWidth = 0;
	this.imageHeight = 0;
	this.anchorX = 0;
	this.anchorY = 0;
	this.shadowURL = "";
	this.shadowWidth = 0;
	this.shadowHeight = 0;
	this.shadowAnchorX = 0;
	this.shadowAnchorY = 0;
	this.tooltipAnchorY = 6;
	this.tooltipAnchorX = 6;
	this.iconTypes = new EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_IconTypes(this);
	this.iconType = this.iconTypes.generic;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_ListingTypes(){
	this.items = [];
	this.add = EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Add;
	this.contains = EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Contains;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Contains( propertyListingType ){
	for ( var i = 0; i < this.items.length; i ++ ){
		if ( this.items[i] == propertyListingType ){
			return true;
		}
	}
	return false;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Add( propertyListingType ){
	this.items.push( propertyListingType );
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses(){
	this.items = [];
	this.add = EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Add;
	this.contains = EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Contains;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Contains( propertyStatus ){
	for ( var i = 0; i < this.items.length; i ++ ){
		if ( this.items[i] == propertyStatus ){
			return true;
		}
	}
	return false;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_Statuses_Add( propertyStatus ){
	this.items.push( propertyStatus );
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Icon_IconTypes(o){
	this.generic = 0;
	this.single = 1;
	this.group = 2;
}

function EstateWeb_Objects_MappingManager_MapOptions_Icons_Add(o){
	this.items.push( o );
}

//########## Event Handling #############################

function EstateWeb_Objects_MappingManager_OnPropertySearchComplete(creator,sender,eventArgs){
	creator.markers.removeAll();
	creator.mapoptions.mapreference.clearOverlays();
	if ( sender.properties.items.length > 0 ){
		creator.setCenter( sender.mapping.calculateCenter() );
		creator.markers.show();	
	}
}

function EstateWeb_Objects_MappingManager_OnPropertySearchBegin(creator,sender,eventArgs){ }

function EstateWeb_Objects_MappingManager_onPageDispose(creator,sender,eventArgs){
	//perform any tear down - cleanup methods
	try{
		GUnload();
	}catch(e){}
	delete creator.mapoptions.groupManager.reference;
}

function EstateWeb_Objects_MappingManager_OnPropertyMarkerClick(creator,sender,eventArgs){ 
	creator.mapoptions.lastPoint = creator.mapoptions.mapreference.getCenter();
	var oproperty = creator.searchManager.properties.getProperty( eventArgs.marker.key );
	if ( oproperty ){
		var shtml = oproperty.propertyDetails.getPropertyDetails( oproperty.propertyDetails.propertyDetailTypes.googleBriefDetails, false, creator.id );
		creator.mapoptions.mapreference.openInfoWindow( eventArgs.marker.getPoint(), shtml );
	}
}

function EstateWeb_Objects_MappingManager_OnPropertyMarkerGroupClick(creator,sender,eventArgs){ 
	var apropertyreferences = [];
	if ( creator.mapoptions.mapreference.getZoom() < 17 ){
		var aInfoTabs = [];
		creator.mapoptions.lastPoint = creator.mapoptions.mapreference.getCenter();
		var iSelectedIndex = 0;
		var amorepropertyreferences = [];
		for ( var i = 0; i < eventArgs.cluster.markers.length; i ++ ){
			var omarker = eventArgs.cluster.markers[i];
			apropertyreferences.push( omarker.key );
			if ( i < (creator.mapoptions.maximumTabs-1) || creator.mapoptions.maximumTabs == eventArgs.cluster.markers.length ){
				var oproperty = creator.searchManager.properties.getProperty( omarker.key );
				var shtml = oproperty.propertyDetails.getPropertyDetails( oproperty.propertyDetails.propertyDetailTypes.googleBriefDetails, false, creator.id );
				var otab = new GInfoWindowTab(i+1, shtml);
				if ( eventArgs.marker ){
					if ( omarker.key == eventArgs.marker.key ){
						iSelectedIndex = aInfoTabs.length;
					}
				}
				aInfoTabs.push( otab );
			}else{
				amorepropertyreferences.push( omarker.key );
			}
		}
		if ( amorepropertyreferences.length > 0 ){
			var oresponse = MapView.GetMoreTabDetails(apropertyreferences, amorepropertyreferences, creator.id );
			if ( oresponse.value ){
				aInfoTabs.push( new GInfoWindowTab( "more...", oresponse.value ) );
			}
		}
		creator.mapoptions.mapreference.openInfoWindowTabsHtml( eventArgs.cluster.marker.getPoint(),  aInfoTabs );
		setTimeout( function(){
			creator.mapoptions.mapreference.getInfoWindow().selectTab(iSelectedIndex);
			}, 100);	
	}else{
		for ( var i = 0; i < eventArgs.cluster.markers.length; i ++ ){
			apropertyreferences.push( eventArgs.cluster.markers[i].key );
		}
		creator.mapoptions.showListView( apropertyreferences );
	}

 }
 
 function EstateWeb_Objects_MappingManager_OnMapMoveEnd(creator,sender,eventArgs){ }
 
 function EstateWeb_Objects_MappingManager_OnMapInfoWindowClose(creator,sender,eventArgs){
	if ( creator.mapoptions.lastPoint ){
		creator.mapoptions.mapreference.panTo( creator.mapoptions.lastPoint );
	}
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyFullDetailsClick(creator,sender,eventArgs){ 
	creator.mapoptions.hidePropertyDetailFrame();
	creator.mapoptions.show();
	switch ( creator.mapoptions.viewPropertyDetailType ){
		case creator.mapoptions.viewPropertyDetailTypes.normal:
			location.href = creator.searchManager.properties.getProperty( eventArgs.propertyReference ).detailsURL;
			break;
		case creator.mapoptions.viewPropertyDetailTypes.inline:
			creator.events.raiseEvent("propertyfulldetailsloading", creator, null );
			with ( document.getElementById( creator.id + "_PropertyDetails") ){
				style.height = creator.mapoptions.mapreference.getSize().height + "px";
			}
			with ( window[ creator.id + "_PropertyDetails" ] ){
				location.href = creator.searchManager.properties.getProperty( eventArgs.propertyReference ).detailsURL;
				HttpManager.Browser.events.addHandler("onpropertyfulldetailsloaded", creator, creator.onPropertyFullDetailsLoaded);
			}
			break;
	}
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyFullDetailsLoaded(creator,sender,eventArgs){
	with ( document.getElementById( creator.id + "_PropertyDetails") ){
		style.height = Math.max( creator.mapoptions.mapreference.getSize().height , eventArgs.height ) + "px";
		parentNode.style.height = style.height;
	}
	creator.events.raiseEvent("propertyfulldetailsloaded", creator, null );
	creator.showBackToMap();
 }
 
 function EstateWeb_Objects_MappingManager_OnBrowserWindowResize(creator,sender,eventArgs){
	if ( creator.mapoptions.mapreference ){
		creator.mapoptions.mapreference.checkResize();
		creator.mapoptions.mapreference.panTo( creator.mapoptions.mapreference.getCenter() );
	}
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyGroupOptionClick(creator,sender,eventArgs){
	switch ( eventArgs.option ){
		case "zoomTo":
			creator.mapoptions.mapreference.closeInfoWindow();
			creator.mapoptions.mapreference.setCenter( eventArgs.cluster.marker.getPoint() );
			creator.setCenter( eventArgs.cluster.getProperties().mapping.calculateCenter() );
			break;
	}
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyMarkerMouseOver(creator,sender,eventArgs){
	if ( creator.mapoptions.tooltips.enabled ){
		var oproperty = creator.searchManager.properties.getProperty( eventArgs.marker.key );
		var oicon = creator.mapoptions.icons.getIconForProperty( oproperty );
		var stext = HttpManager.Strings.Format("<b>{0}</b><br />{1}", oproperty.propertyAddress.formattedAddress , oproperty.propertyPrice.formattedPrice);
		creator.mapoptions.tooltips.show(eventArgs.marker, oicon, stext);
	}
 }
 
function EstateWeb_Objects_MappingManager_OnPropertyMarkerMouseOut(creator,sender,eventArgs){
	creator.mapoptions.tooltips.hide();
}
 
 function EstateWeb_Objects_MappingManager_OnPropertyGroupOptionMouseOver(creator,sender,eventArgs){
	if ( creator.mapoptions.tooltips.enabled ){
		//get the correct icon for this group
		var aicons = creator.mapoptions.icons.getIconsByType( creator.mapoptions.icons.iconTypes.group );
		var oicon = aicons[0];
		var stext = HttpManager.Strings.Format("<b>{0} propert{1} in {2}</b><br/>Click for details", eventArgs.cluster.markers.length, ( eventArgs.cluster.markers.length > 1 ? "ies" :  "y" ), ( eventArgs.cluster.isSinglePointCluster ? "this area" : "this area" ) );
		creator.mapoptions.tooltips.show(eventArgs.cluster.marker, oicon, stext);
	}
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyGroupOptionMouseOut(creator,sender,eventArgs){
	creator.mapoptions.tooltips.hide();
 }
 
 function EstateWeb_Objects_MappingManager_OnPropertyBackToMapRequest(creator,sender,eventArgs){

 }

//####### Error Handling ###############

function EstateWeb_Objects_MappingManager_PreloadValidation(){
	var msg = "";
	
	if ( !this.id ){
		HttpManager.Document.Errors.add("Mapping Manager", "A unqiue ID must be set for this object");
	}else if ( typeof GBrowserIsCompatible != "function" ){ 
		HttpManager.Document.Errors.add("Mapping Manager", "Unable to find google API");
	}else if ( this.mapoptions.renderToContainer.length == 0 ){
		HttpManager.Document.Errors.add("Mapping Manager", "renderToContainer not set");
	}else if (! HttpManager.Document.GetObject( this.mapoptions.renderToContainer ) ){
		HttpManager.Document.Errors.add("Mapping Manager", "Unable to find the object in the DOM for " + this.mapoptions.renderToContainer);
	}
	//set default icons
	if ( this.mapoptions.icons.getIconsByType(this.mapoptions.icons.iconTypes.group).length == 0 ){
		var oGroupIcon = new this.mapoptions.icons.icon();
		with ( oGroupIcon ){
			imageURL = "icon_group.png";
			imageWidth = 23;
			imageHeight = 22;
			anchorX = 7;
			anchorY = 7;
			iconType = oGroupIcon.iconTypes.group;
		}
		this.mapoptions.icons.add( oGroupIcon );
	}
	if ( this.mapoptions.icons.getIconsByType(this.mapoptions.icons.iconTypes.generic).length == 0 ){
		var oAvailableIcon = new this.mapoptions.icons.icon();
		with ( oAvailableIcon ){
			imageURL = "icon_single_generic.png";
			imageWidth = 23;
			imageHeight = 22;
			anchorX = 7;
			anchorY = 7;
			iconType = oAvailableIcon.iconTypes.generic;
		}
		this.mapoptions.icons.add( oAvailableIcon );
	}
	var oSSTCIcon = new this.mapoptions.icons.icon();
	with ( oSSTCIcon ){
		imageURL = "icon_single_status.png";
		imageWidth = 23;
		imageHeight = 22;
		anchorX = 8;
		anchorY = 8;
		iconType = oSSTCIcon.iconTypes.single;
		statuses.add( oSSTCIcon.propertyStatuses.soldSubjectToContract );
		statuses.add( oSSTCIcon.propertyStatuses.underOffer );
		statuses.add( oSSTCIcon.propertyStatuses.letAgreed );
		listingTypes.add( oSSTCIcon.propertyListingTypes.sale );
		listingTypes.add( oSSTCIcon.propertyListingTypes.rent );
	}
	this.mapoptions.icons.add( oSSTCIcon );
	//report any errors
	if ( HttpManager.Document.Errors.errors.length > 0 ){
		HttpManager.Document.Errors.show();
		return false;
	}
	
	//inital event settings
	if (this.searchManager){
		this.searchManager.events.addHandler( "searchcompleted", this, this.onPropertySearchComplete );
		this.searchManager.events.addHandler( "searchbegin", this, this.onPropertySearchBegin );
		HttpManager.Browser.events.addHandler("onunload", this, this.onPageDispose );
	}
	this.events.addHandler("propertymarkerclick", this, this.onPropertyMarkerClick );
	this.events.addHandler("propertymarkermouseover", this, this.onPropertyMarkerMouseOver );
	this.events.addHandler("propertymarkermouseout", this, this.onPropertyMarkerMouseOut );
	this.events.addHandler("onmapmoveend", this, this.onMapMoveEnd );
	this.events.addHandler("onmapinfowindowclose", this, this.onMapInfoWindowClose );	
	HttpManager.Browser.events.addHandler("onresize", this, this.onBrowserWindowResize );
	
	return true;
}

function EstateWeb_Objects_MappingManager_ShowError(msg){
	alert("EstateWeb Mapping Manager:\n\nAn Error has occured in this application.\n\n"+msg);
}

//################# Map Objects #####################//

function EstateWeb_Objects_MappingManager_Style(){
	this.baseDirectory = "/images/controls/maps";
	this.sales = new EstateWeb_Objects_MappingManager_Style_Settings();
	this.lettings = new EstateWeb_Objects_MappingManager_Style_Settings();
	this.unspecified = new EstateWeb_Objects_MappingManager_Style_Settings();
	this.sales.styleDirectory = 1;
	this.lettings.styleDirectory = 2;
	this.unspecified.styleDirectory = 1;
	this.formatURL = EstateWeb_Objects_MappingManager_Style_FormatURL;
}

function EstateWeb_Objects_MappingManager_Style_FormatURL(url, listingType){
	switch ( listingType ){
		case "5":
			return this.baseDirectory + "/style"+this.sales.styleDirectory+"/"+url;
			break;
		case "6":
			return this.baseDirectory + "/style"+this.lettings.styleDirectory+"/"+url;
			break;
		default:
			return this.baseDirectory + "/style"+this.unspecified.styleDirectory+"/"+url;
			break;
	}
}

function EstateWeb_Objects_MappingManager_Style_Settings(){
	this.styleDirectory = 1;
}

//########### KEY Control ######################

function EstateWeb_Objects_MappingManager_KeyControl(omap, o){
	this.mapReference = omap;
	this.contentElement = o;
}
EstateWeb_Objects_MappingManager_KeyControl.prototype = new GControl();
EstateWeb_Objects_MappingManager_KeyControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var ocontent = this.contentElement.cloneNode(true);
	ocontent.style.display = "block";
	container.appendChild( ocontent );
	map.getContainer().appendChild( container );
	return container;
}
EstateWeb_Objects_MappingManager_KeyControl.prototype.getDefaultPosition = function(){
	return new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize(7,30) );
}

//########### Property List #####################

function EstateWeb_Objects_MappingManager_PropertyListControl(omap, scontent){
	this.mapReference = omap;
	this.content = scontent;
}

EstateWeb_Objects_MappingManager_PropertyListControl.prototype = new GControl();
EstateWeb_Objects_MappingManager_PropertyListControl.prototype.initialize = function(map) {
	var ocontainer = document.createElement("div");
	ocontainer.innerHTML = this.content;
	map.getContainer().appendChild( ocontainer );
	return ocontainer;
}

EstateWeb_Objects_MappingManager_PropertyListControl.prototype.getDefaultPosition = function(){
	return new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize(7,30) );
}

