
//##################################################################
//## Object References
//##################################################################

var oMap; // object reference to mapping
var oPropertyTrackerManager = ( typeof EstateWeb_Objects_TrackerManager != "undefined" ? new EstateWeb_Objects_TrackerManager() : null );
var aProperties = new Array(); // array to hold properties in search result
var _map_center_latitude = 0;
var _map_center_longitude = 0;
var _map_minheight = 400;
var __searchResultMapping_isInitialised = false;
var _map_callback_function = null;
var __searchResultMapping_Map_DrawMarkers_Position = 0;
var _initialviewtype = 0; // map

var _map_icons_letting_available = "images/controls/map/propertyboardToLet.png";
var _map_icons_sales_available = "images/controls/map/availableHouse.png";
var _map_icons_letting_status = "images/controls/map/propertyboardLetAgreed.png";
var _map_icons_sales_status = "images/controls/map/underOfferHouse.png";

//##################################################################
//## Object extension
//##################################################################

EstateWeb_Objects_Property.prototype.DrawMarker = SearchResultMapping_Property_DrawMarker;
EstateWeb_Objects_Property.prototype.Marker = null;
EstateWeb_Objects_Property.prototype.MarkerBubble = null;



function SearchResultMapping_Initialise(){
	//this must be called first, this will initialise the map using the correct view
	//if they have a cached view then use that
	//oMap = new GMap(HttpManager.Document.GetObject("pnlGoogleMap"));
	//oMap = new GMap(HttpManager.Document.GetObject("pnlGoogleMap"));
	if ( HttpManager.Request.Cookies.Get("EstateWeb_Mapping_SearchResults_Viewtype") ){
		_initialviewtype = parseInt( HttpManager.Request.Cookies.Get("EstateWeb_Mapping_SearchResults_Viewtype") );
	}else{
		//if the page number is greater than 1 or listview param passed then default to list view
		if ( HttpManager.Request.QueryString("page") ){
			if ( parseInt( HttpManager.Request.QueryString("page") ) > 0 ){ _initialviewtype = 1 }
		}
		if ( HttpManager.Request.QueryString("lv") ){
			_initialviewtype = 1;
		}
	} 
	SearchResultMapping_OnLoad(1);
}

function SearchResultMapping_Reset(){
	aProperties = [];
	__searchResultMapping_isInitialised = false;
	__searchResultMapping_Map_DrawMarkers_Position = 0;
	GUnload();
}	


function SearchResultMapping_OnLoad(forceviewtype){
	if ( typeof forceviewtype != "undefined" ){
		_initialviewtype = forceviewtype;
	}
	SearchResultMapping_ToggleView( _initialviewtype );
	if ( (_initialviewtype == 0 || _initialviewtype == 2 || _initialviewtype == 3) & !__searchResultMapping_isInitialised ){
		if ( location.href.toLowerCase().indexOf("search.aspx") > 0 ){
			
			var ilistingtype = HttpManager.Request.QueryString("ListingType");
			var iminimumprice = HttpManager.Request.QueryString("minprice");
			var imaximumprice = HttpManager.Request.QueryString("maxprice");
			var sarea = HttpManager.Request.QueryString("area")
			var ibedrooms = HttpManager.Request.QueryString("bedrooms");
			var squicksearch = HttpManager.Request.QueryString("quicksearch");
			var irentperiod = HttpManager.Request.QueryString("rentperiod");
			var bonlyavailableproperties = ( HttpManager.Request.QueryString("oa").length == 0 ? false : true );
			Simple.GetSearchData(ilistingtype,iminimumprice,imaximumprice,sarea,ibedrooms,squicksearch,irentperiod, bonlyavailableproperties, SearchResultMapping_OnInit);	
		}else{
				PropertyCartManager.PropertyInformation(SearchResultMapping_OnInit);
		}	
	}
	
	//if ( _initialviewtype == 1 ){
	//		SearchResultMapping_ToggleView(1);
	//}else if (!  ) {
	//	SearchResultMapping_ToggleView(0);
		
	//}else{}
}


function SearchResultMapping_CallbackFunction(){
	if ( _map_callback_function ){
		eval(_map_callback_function);
		_map_callback_function = null;
	}
}

function SearchResultMapping_OnInit(oproperties){
	if ( GBrowserIsCompatible() ){
		if ( !__searchResultMapping_isInitialised ){
			if ( oproperties.value.length > 0 ){
				oMap = new GMap(HttpManager.Document.GetObject("pnlGoogleMap"));
				//events
				GEvent.addListener( oMap, 'dragend', function(){ SearchResultMapping_Map_Pointer_OnDragEnd() } );
				GEvent.addListener( oMap, 'zoomend', function(){ SearchResultMapping_Map_Pointer_OnZoomEnd() } );
				oMap.addControl(new GSmallMapControl());
				oMap.centerAndZoom(new GPoint(-2.0015, 53.4967),11);
				oMap.checkResize();
				//get properties
					for ( var i = 0; i < oproperties.value.length; i ++ ){
						var oitem = oproperties.value[i];
						if ( oitem[1] && oitem[2] ){
							var oProperty = new EstateWeb_Objects_Property();
							oProperty.PropertyListingType = oitem[3];
							oProperty.PropertyReference = oitem[0];
							oProperty.PropertyLatitude =  parseFloat(oitem[1])
							oProperty.PropertyLongitude = parseFloat(oitem[2])
							oProperty.PropertyStatusDisplay = (oitem[4] ? oitem[4] : "");
							oProperty.PropertyFormattedPrice = oitem[5];
							oProperty.PropertyAddress = oitem[6];
							aProperties[aProperties.length] = oProperty;
						}
					}

				if ( aProperties.length > 0 ) {
					//now work out the max bounds of the map
					var nlat;
					//get max lat
					aProperties.sort(sortLatitudeDESC);
					nlat = aProperties[0].PropertyLatitude;
					var slat;
					//get min lat
					aProperties.sort(sortLatitudeASC);
					slat = aProperties[0].PropertyLatitude;
					var elon;
					//get max lon
					aProperties.sort(sortLongitudeDESC);
					elon = aProperties[0].PropertyLongitude;
					var wlon;
					//get min lon
					aProperties.sort(sortLongitudeASC);
					wlon = aProperties[0].PropertyLongitude;
					//
					_map_center_latitude = ((nlat+slat)/2);
					_map_center_longitude = ((elon+wlon)/2);
					oMap.setCenter(new GLatLng(_map_center_latitude, _map_center_longitude),5);
					var obound1 = new GLatLng(nlat,wlon);
					var obound2 = new GLatLng(slat,elon);
					
					oMap.setZoom(oMap.getBoundsZoomLevel(new GLatLngBounds(obound1,obound2))); 
					
					var obounds = oMap.getBounds();
					oMap.savePosition();
					SearchResultMapping_Map_DrawMarkers();

					try {
					switch(_initialviewtype)	{
						case 0:
						oMap.setMapType( G_NORMAL_MAP );
						break;
					case 2:
						oMap.setMapType( G_SATELLITE_MAP );
						break;
					case 3:
						oMap.setMapType( G_HYBRID_MAP );
						break;
					}
					}catch(e){ }
					
				}else{
					//notihng to load
					SearchResultMapping_Map_Loaded();
				}
			}else{
				SearchResultMapping_Map_NoProperties();
			}
			__searchResultMapping_isInitialised = true;
		}
	}
}


function SearchResultMapping_Map_Loaded(){
	setTimeout("HttpManager.Document.GetObject(\"pnlLoading\").style.display = \"none\";", 800);
	SearchResultMapping_CallbackFunction();
	//show last looked at property if we have that
	if ( HttpManager.Request.Cookies.Get("EstateWeb_SearchResults_Mapping_LastProperty") ){
		SearchResultMapping_Property_ShowMarker( HttpManager.Request.Cookies.Get("EstateWeb_SearchResults_Mapping_LastProperty") );	
	}
}


function SearchResultMapping_Map_NoProperties(){
	HttpManager.Document.GetObject("pnlLoading").style.display = "none";
	if ( HttpManager.Document.GetObject("tdMapView") ){
		HttpManager.Document.GetObject("tdMapView").style.display = "none";
	}
	SearchResultMapping_ToggleView(1);
}

function SearchResultMapping_Map_DrawMarkers(){
	var imax = parseInt(HttpManager.Document.GetObject("pnlBarContainer").style.width.replace("px",""));
	
	//draws all markers on the map
	if ( aProperties[__searchResultMapping_Map_DrawMarkers_Position].Marker == null ){
		aProperties[__searchResultMapping_Map_DrawMarkers_Position].DrawMarker();
		

		var iinterval = imax/(aProperties.length+1);
		HttpManager.Document.GetObject("pnlBar").style.width = ( (__searchResultMapping_Map_DrawMarkers_Position+1) * iinterval)+"px";

		__searchResultMapping_Map_DrawMarkers_Position ++;
		if ( __searchResultMapping_Map_DrawMarkers_Position <= (aProperties.length-1)){
			setTimeout("SearchResultMapping_Map_DrawMarkers();", 5);
		}else{
			HttpManager.Document.GetObject("pnlBar").style.width = imax+"px";
			SearchResultMapping_Map_Loaded();
		}
	}
}

function SearchResultMapping_Map_Pointer_OnClick(oMarker){
	//SearchResultMapping_Initialise();
	//store last looked at property
	HttpManager.Request.Cookies.Add("EstateWeb_SearchResults_Mapping_LastProperty", oMarker.Property.PropertyReference);
	//load brief property details
	if ( oMarker.Property.MarkerBubble == null ){
		var response = SearchResults.GetPointerDetails(oMarker.Property.PropertyReference, ( location.href.toLowerCase().indexOf("search.aspx") >= 0 ?"1.xsl" : "2.xsl" ));
		oMarker.Property.MarkerBubble = response.value;
	}
	oMarker.openInfoWindowHtml( oMarker.Property.MarkerBubble );
}

function SearchResultMapping_Map_Pointer_ViewProperty_OnClick(PropertyReference){
	//SearchResultMapping_Map_ViewProperty_OnView(PropertyReference);
	location.href = "Viewproperty.aspx?adid="+PropertyReference;
	if ( oPropertyTrackerManager ){
		oPropertyTrackerManager.Add(PropertyReference);
		//instruct the recentproperties tabbing control to refresh itself
		RecentProperties_Refresh(PropertyReference);	
	}
}

function SearchResultMapping_Map_ViewProperty_OnView(PropertyReference){
	//create a div with an IFRAME to display the property details
	SearchResultMapping_ToggleView(1);
	if ( HttpManager.Document.GetObject("pnlPropertyFullDetails").childNodes.length > 0 ){
		for ( var i = 0; i < HttpManager.Document.GetObject("pnlPropertyFullDetails").childNodes.length; i ++ ){
			HttpManager.Document.GetObject("pnlPropertyFullDetails").removeChild( HttpManager.Document.GetObject("pnlPropertyFullDetails").childNodes[i] );
		}
	}
	var ocontainer = document.createElement("div");
	var oframe = document.createElement("iframe");
	oframe.name = "frmFullDetails";
	oframe.id = "frmFullDetails";
	oframe.width = "507";
	oframe.height = "350";
	oframe.scrolling = "no";
	oframe.frameBorder = "0";
	oframe.src = "viewpropertyadvanced.aspx?adid=" + PropertyReference;
	ocontainer.appendChild( oframe );
	HttpManager.Document.GetObject("pnlPropertyFullDetails").appendChild( ocontainer );
}

function SearchResultMapping_ToggleView(val){
	//store the view type
	HttpManager.Request.Cookies.Add("EstateWeb_Mapping_SearchResults_Viewtype", val);
switch( val ){
		case 0:
			if ( HttpManager.Document.GetObject("pnlResults") ) {
				HttpManager.Document.GetObject("pnlResults").style.display = "none";
			}
			if ( HttpManager.Document.GetObject("pnlMap") ) {
				HttpManager.Document.GetObject("pnlMap").style.display = "block";
			}
			if ( HttpManager.Document.GetObject("imgMapView") ){
				HttpManager.Document.GetObject("imgMapView").src = "images/structure/mapViewSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgListView") ){
				HttpManager.Document.GetObject("imgListView").src = "images/structure/listViewUnSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgMapSatelliteView") ){
				HttpManager.Document.GetObject("imgMapSatelliteView").src = "images/structure/SatelliteUnSelected.gif";
			}
			
			if ( HttpManager.Document.GetObject("imgMapHybridView") ){
				HttpManager.Document.GetObject("imgMapHybridView").src = "images/structure/HybridUnSelected.gif";
			}
			
			HttpManager.Document.GetObject("pnlSearchPagingHeader").style.display = "none";
			HttpManager.Document.GetObject("pnlSearchPagingFooter").style.display = "none";
			break;
		case 1:
			if ( HttpManager.Document.GetObject("pnlResults") ){
				HttpManager.Document.GetObject("pnlResults").style.display = "block";
			}
			if ( HttpManager.Document.GetObject("pnlMap") ){
				setTimeout("HttpManager.Document.GetObject('pnlMap').style.display = 'none'", 50);
			}
			if ( HttpManager.Document.GetObject("imgMapView") ){
				HttpManager.Document.GetObject("imgMapView").src = "images/structure/mapViewunSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgListView") ){
				HttpManager.Document.GetObject("imgListView").src = "images/structure/listViewSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgMapSatelliteView") ){
				HttpManager.Document.GetObject("imgMapSatelliteView").src = "images/structure/SatelliteUnSelected.gif";
			}
			
			if ( HttpManager.Document.GetObject("imgMapHybridView") ){
				HttpManager.Document.GetObject("imgMapHybridView").src = "images/structure/HybridUnSelected.gif";
			}
			if (HttpManager.Document.GetObject("pnlSearchPagingHeader")){
				HttpManager.Document.GetObject("pnlSearchPagingHeader").style.display = "block";
			}
			if (HttpManager.Document.GetObject("pnlSearchPagingFooter")){
				HttpManager.Document.GetObject("pnlSearchPagingFooter").style.display = "block";
			}
			break;
			
		case 2:
			if ( HttpManager.Document.GetObject("pnlResults") ){
				HttpManager.Document.GetObject("pnlResults").style.display = "none";
			}
			if ( HttpManager.Document.GetObject("pnlMap") ){
				setTimeout("HttpManager.Document.GetObject('pnlMap').style.display = 'block'", 50);
			}
			if ( HttpManager.Document.GetObject("imgMapView") ){
				HttpManager.Document.GetObject("imgMapView").src = "images/structure/mapViewunSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgListView") ){
				HttpManager.Document.GetObject("imgListView").src = "images/structure/listViewUnSelected.gif";
			}
			if ( HttpManager.Document.GetObject("imgMapSatelliteView") ){
				HttpManager.Document.GetObject("imgMapSatelliteView").src = "images/structure/SatelliteSelected.gif";
			}
			
			if ( HttpManager.Document.GetObject("imgMapHybridView") ){
				HttpManager.Document.GetObject("imgMapHybridView").src = "images/structure/HybridUnSelected.gif";
			}
			
			HttpManager.Document.GetObject("pnlSearchPagingHeader").style.display = "none";
			HttpManager.Document.GetObject("pnlSearchPagingFooter").style.display = "none";
			
			break;
			
			case 3:
				if ( HttpManager.Document.GetObject("pnlResults") ){
					HttpManager.Document.GetObject("pnlResults").style.display = "none";
				}
				if ( HttpManager.Document.GetObject("pnlMap") ){
					setTimeout("HttpManager.Document.GetObject('pnlMap').style.display = 'block'", 50);
				}
				if ( HttpManager.Document.GetObject("imgMapView") ){
					HttpManager.Document.GetObject("imgMapView").src = "images/structure/mapViewunSelected.gif";
				}
				if ( HttpManager.Document.GetObject("imgListView") ){
					HttpManager.Document.GetObject("imgListView").src = "images/structure/listViewUnSelected.gif";
				}
				if ( HttpManager.Document.GetObject("imgMapSatelliteView") ){
					HttpManager.Document.GetObject("imgMapSatelliteView").src = "images/structure/SatelliteUnSelected.gif";
				}
				
				if ( HttpManager.Document.GetObject("imgMapHybridView") ){
					HttpManager.Document.GetObject("imgMapHybridView").src = "images/structure/HybridSelected.gif";
				}
				
				HttpManager.Document.GetObject("pnlSearchPagingHeader").style.display = "none";
				HttpManager.Document.GetObject("pnlSearchPagingFooter").style.display = "none";

			break;
	}
	//when we toggle if there are no properties avaialble then hide the map view
	if ( HttpManager.Document.GetObject("_ctl1_CenterRegion__ctl0_txtResultsCount").innerHTML.length == 0 ){
		if ( HttpManager.Document.GetObject("tdMapView") ){
			HttpManager.Document.GetObject("tdMapView").style.display = "none";
		}
	}
}

function SearchResultMapping_Property_DrawMarker(){
	if ( this.PropertyLatitude  && this.PropertyLongitude ){
		var oPropertyIcon = new GIcon();
		oPropertyIcon.image = ( this.PropertyStatusDisplay.length ? ( this.PropertyListingType == "5" ? _map_icons_sales_status :  _map_icons_letting_status ) : ( this.PropertyListingType == "5" ? _map_icons_sales_available : _map_icons_letting_available ) );
		oPropertyIcon.iconSize = new GSize(18,18);
		oPropertyIcon.iconAnchor = new GPoint(18,18);
		oPropertyIcon.infoWindowAnchor = new GPoint(0,0);
		//append the icon to the map
		var oPropertyPoint = new GPoint(this.PropertyLongitude, this.PropertyLatitude);
		
		var oMarker = new PdMarker(oPropertyPoint, oPropertyIcon);
		//var oMarker = new GxMarker(oPropertyPoint, oPropertyIcon);
		oMarker.setTooltip("<div id=\"pnlTooltip\"><b>"+this.PropertyAddress+" "+this.PropertyFormattedPrice+"</b></div>");
		oMarker.setOpacity(90);
		oMarker.Property = this;
		GEvent.addListener( oMarker, 'click', function() { SearchResultMapping_Map_Pointer_OnClick(this); } );
		//GEvent.addListener( oMarker, 'mouseover', function(){ HttpManager.Document.GetObject("transDiv"+oMarker.Property.PropertyReference).className="TransDivOver"; } );
		//GEvent.addListener( oMarker, 'mouseout', function(){ HttpManager.Document.GetObject("transDiv"+oMarker.Property.PropertyReference).className="TransDiv"; } );
		GEvent.addListener( oMarker, 'infowindowclose', function(){ SearchResultMapping_Map_Pointer_OnInfoWindowClose(this); } );
		this.Marker = oMarker;
		oMap.addOverlay( oMarker );
	}
}

function SearchResultMapping_Property_ShowMarker(PropertyReference){
	if ( !__searchResultMapping_isInitialised ){
		_map_callback_function = "SearchResultMapping_Property_ShowMarker('"+PropertyReference+"')"
		SearchResultMapping_OnLoad(0);
	}else{
		for ( var i = 0; i < aProperties.length; i ++ ){
			if ( aProperties[i].PropertyReference.toLowerCase() == PropertyReference.toLowerCase() ){
				SearchResultMapping_ToggleView(0);
				SearchResultMapping_Map_Pointer_OnClick(aProperties[i].Marker);
				break;
			}
		}	
	}
}

function SearchResultMapping_Map_Pointer_OnInfoWindowClose(){
	oMap.returnToSavedPosition();
}

function SearchResultMapping_Map_Pointer_OnDragEnd(){
	oMap.savePosition();
}

function SearchResultMapping_Map_Pointer_OnZoomEnd(){
	oMap.savePosition();
}

function sortLatitudeDESC(a,b){
	return (b.PropertyLatitude - a.PropertyLatitude);
}	

function sortLatitudeASC(a,b){
	return (a.PropertyLatitude - b.PropertyLatitude);
}	

function sortLongitudeDESC(a,b){
	return (b.PropertyLongitude - a.PropertyLongitude);
}	

function sortLongitudeASC(a,b){
	return (a.PropertyLongitude - b.PropertyLongitude);
}	
