/*
 *	PropertyObject.js
 *	=================
 *	
 *	Copyright The Tin Limited, 2007
 *
 *	This javascript file allow search, hide and hisplay for properties and their markers.
 *	
 *	Required script references:-
 *		provide_javascript_collection_object.js
 */
 
 
 
//function PropertyObject(propertyId, isForSale, propertyType, latitude, longitude, price, popupHTML, propertyIcon)
function PropertyObject(propertyId, isForSale, propertyType, price, isUnderOffer, bedrooms, officeName, hasGarden, hasTerrace, latitude, longitude, popupHTML, propertyIcon)
{
	this.PropertyID = propertyId;
	this.IsForSale = isForSale;
	this.PropertyType = propertyType;
	this.Price = price;
	this.PopupHTML = popupHTML;

	this.Latitude = latitude;
	this.Longitude = longitude;
	
	if(propertyIcon)	// NB: This isn't currently working!!!
		this.Icon = propertyIcon;
	else
		this.Icon = new GIcon();
		//this.Icon = icon;
	
	this.IsUnderOffer = isUnderOffer;
	this.Bedrooms = bedrooms;
	this.OfficeName = officeName;
	this.HasGarden = hasGarden;
	this.HasTerrace = hasTerrace;
	
	this.isShowing = false;
	this.showMarker = _showMarker;
	this.hideMarker = _hideMarker;
	
	this.marker = createPropertyMarker();
	
	this.isSearchMatch = _isSearchMatch;
	this.searchShowHide = _searchShowHide;
	this.isSearchMatchRevised = _isSearchMatchRevised;
	this.searchShowHideRevised = _searchShowHideRevised;
	this.searchShowHide_Old = _searchShowHide_Old;
	
	this.getPoint = _getPoint;
	
	function _showMarker(map)
	{
		if(this.isShowing == false)
		{
			//map.addOverlay(createPropertyMarker());
			//map.addOverlay(this.marker);
			//map.addOverlay(createCustomMarker(this.getPoint(), this.PopupHTML, this.Icon));
			this.marker = createCustomMarker(this.getPoint(), this.PopupHTML, boardIcon);
			map.addOverlay(this.marker);
			//map.addOverlay(createCustomMarker(this.getPoint(), this.PopupHTML, iconstar));
			//map.addOverlay(createCustomMarker(this.getPoint(), '13 Chantry Street<br/>London<br/>N1 8NR<br><br>House (Period)<br>2 Bedrooms<br><a href="property.aspx?PropertyID=5">View Property</a>', iconstar));
			//map.addOverlay(new GMarker(this.getPoint()));
			
			showPropertyRow(this.PropertyID);
			
			this.isShowing = true;
			
			//alert("Marker Added");
		}
	}
	
	function _getPoint()
	{
		return new GLatLng(this.Latitude, this.Longitude);
	}
	
	function _hideMarker(map)
	{
		hidePropertyRow(this.PropertyID);
			
		if(this.isShowing == true)
		{
			map.removeOverlay(this.marker);
			
			this.isShowing = false;
		}
	}
	
	function createPropertyMarker()
	{
		return createCustomMarker(new GLatLng(this.Latitude, this.Longitude), this.PopupHTML, this.Icon);
	}
	
	function _isSearchMatch_Old(minPrice, maxPrice, saleProperties, propertyType)
	{
		//alert("Checking for search match (PropertyID = " + this.PropertyID.toString() + ")");
		var msg = "Price = " + this.Price.toString() + "\r\n";
		msg += "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "\r\n";
		msg += "Sales: " + saleProperties.toString() + "\r\n";
		msg += "PropertyType: " + propertyType + "\r\n";
		msg += "PropertyID: " + this.PropertyID.toString();
		//alert(msg);
		if(this.Price < minPrice)
			return false;
		if(this.Price > maxPrice)
			return false;
		if(this.IsForSale != saleProperties)
			return false;
		if(this.PropertyType != propertyType)
			return false;
		//alert("Found a match:\r\n\r\n" + msg);
		return true;
	}

	function _isSearchMatchRevised(minPrice, maxPrice, saleProperties, includeFlats, includeHouses, includeUnderOffer, includeStudios, include1Bedrooms, include2Bedrooms, include3Bedrooms, include4PlusBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		var msg = "About to test for the search\r\n"
			+ map.toString() + "\r\n"
			+ "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "(This property is priced at " + this.Price.toString() + ")\r\n"
			+ "Search Property Type: " + propertyType + " (This property's type is " + this.PropertyType + "\r\n"
			+ "This Office Name: " + this.OfficeName;
		//alert(msg);

		if(this.Price < minPrice)
		{
			//alert("Failed for price being too low:\r\n\r\n" + msg);
			return false;
		}
		// NB: Replaced to allow 2000+ rather than a max of 2000pw ... if (this.Price > maxPrice)
		if (this.Price > maxPrice && !(saleProperties==false && maxPrice==2000)) 
		{
			//alert("Failed for price being too high:\r\n\r\n" + msg);
			return false;
		}
		if(this.IsForSale != saleProperties)
		{
			//alert("Failed for being wrong rental/sale type:\r\n\r\n" + msg);
			return false;
		}
		/* Modified:-
		* // If a rental property, check the flat and house settings and "Under Offer" settings
		* NB: Rental properties can now be "Under Offer", so this check is now handled for Rental properties too
		*/
		if(saleProperties == true) {
			if((((this.PropertyType == 'Flat') && (includeFlats == true)) || ((this.PropertyType == 'House') && (includeHouses == true))) == false)
			{
				return false;
			}
		} // NB: This is new, to replace the one after this next block now that rental properties can be "Under Offer"
		
			if(includeUnderOffer == false)
			{
				if(this.IsUnderOffer == true)
				{
					//alert("Failed for being under offer:\r\n\r\n" + msg);
					return false;
				}
			}
		//}
		
		/*
		if(this.Bedrooms < minBedrooms)
		{
			//alert("Failed for having too few bedrooms:\r\n\r\n" + msg);
			return false;
		}
		if(this.Bedrooms > maxBedrooms)
		{
			//alert("Failed for having too many bedrooms:\r\n\r\n" + msg);
			return false;
		}*/
		if((
			((this.Bedrooms == 0) && (includeStudios == true))
			||
			((this.Bedrooms == 1) && (include1Bedrooms == true))
			||
			((this.Bedrooms == 2) && (include2Bedrooms == true))
			||
			((this.Bedrooms == 3) && (include3Bedrooms == true))
			||
			((this.Bedrooms >= 4) && (include4PlusBedrooms == true))
			) == false)
			return false;
		if(!(
				(includeIslington == true && this.OfficeName == "Islington") 
					|| 
				(includeHighbury == true && this.OfficeName == "Highbury") 
					|| 
				(includeCamden == true && this.OfficeName == "Camden")
			))
		{
			/*
			alert(includeIslington);
			alert(this.OfficeName == "Islington");
			alert(includeHighbury);
			alert(this.OfficeName == "Highbury");
			alert(includeCamden);
			alert(this.OfficeName == "Camden");
			alert("Failed for not being in right office group:\r\n\r\n" + msg); */
			return false;
		}
		if(mustHaveGarden && this.HasGarden != true)
		{
			//alert("Failed for not having a garden:\r\n\r\n" + msg);
			return false;
		}
		if(mustHaveTerrace && this.HasTerrace != true)
		{
			//alert("Failed for not having a terrace:\r\n\r\n" + msg);
			return false;
		}
		//alert("Should be adding a marker here: " + this.Latitude.toString() + ", " + this.Longitude.toString());
		return true;
	}
	
	function _isSearchMatch(minPrice, maxPrice, saleProperties, propertyType, includeUnderOffer, minBedrooms, maxBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		var msg = "About to test for the search\r\n"
			+ map.toString() + "\r\n"
			+ "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "(This property is priced at " + this.Price.toString() + ")\r\n"
			+ "Search Property Type: " + propertyType + " (This property's type is " + this.PropertyType + "\r\n"
			+ "This Office Name: " + this.OfficeName;
		//alert(msg);

		//propertyType, price, isUnderOffer, bedrooms, officeName, hasGarden, hasTerrace
		if(this.Price < minPrice)
		{
			//alert("Failed for price being too low:\r\n\r\n" + msg);
			return false;
		}
		if(this.Price > maxPrice)
		{
			//alert("Failed for price being too high:\r\n\r\n" + msg);
			return false;
		}
		if(this.IsForSale != saleProperties)
		{
			//alert("Failed for being wrong rental/sale type:\r\n\r\n" + msg);
			return false;
		}
		if(this.PropertyType != propertyType)
		{
			//alert("Failed for being wrong property type:\r\n\r\n" + msg);
			return false;
		}
		if(includeUnderOffer == false)
		{
			if(this.IsUnderOffer == true)
			{
				//alert("Failed for being under offer:\r\n\r\n" + msg);
				return false;
			}
		}
		if(this.Bedrooms < minBedrooms)
		{
			//alert("Failed for having too few bedrooms:\r\n\r\n" + msg);
			return false;
		}
		if(this.Bedrooms > maxBedrooms)
		{
			//alert("Failed for having too many bedrooms:\r\n\r\n" + msg);
			return false;
		}
		if(!(
				(includeIslington == true && this.OfficeName == "Islington") 
					|| 
				(includeHighbury == true && this.OfficeName == "Highbury") 
					|| 
				(includeCamden == true && this.OfficeName == "Camden")
			))
		{
			/*
			alert(includeIslington);
			alert(this.OfficeName == "Islington");
			alert(includeHighbury);
			alert(this.OfficeName == "Highbury");
			alert(includeCamden);
			alert(this.OfficeName == "Camden");
			alert("Failed for not being in right office group:\r\n\r\n" + msg); */
			return false;
		}
		if(mustHaveGarden && this.HasGarden != true)
		{
			//alert("Failed for not having a garden:\r\n\r\n" + msg);
			return false;
		}
		if(mustHaveTerrace && this.HasTerrace != true)
		{
			//alert("Failed for not having a terrace:\r\n\r\n" + msg);
			return false;
		}
		//alert("Should be adding a marker here: " + this.Latitude.toString() + ", " + this.Longitude.toString());
		return true;
	}
	
	function _searchShowHideRevised(map, minPrice, maxPrice, saleProperties, includeFlats, includeHouses, includeUnderOffer, includeStudios, include1Bedrooms, include2Bedrooms, include3Bedrooms, include4PlusBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		var msg = "About to test for the search\r\n"
			+ map.toString() + "\r\n"
			+ "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "(This property is priced at " + this.Price.toString() + ")\r\n"
			+ "Search Property Type: " + propertyType + " (This property's type is " + this.PropertyType + "\r\n"
			+ "This Office Name: " + this.OfficeName;
		//alert(msg);
		if(this.isSearchMatchRevised(minPrice, maxPrice, saleProperties, includeFlats, includeHouses, includeUnderOffer, includeStudios, include1Bedrooms, include2Bedrooms, include3Bedrooms, include4PlusBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace) == true)
			this.showMarker(map);
		else
			this.hideMarker(map);
	}

	function _searchShowHide(map, minPrice, maxPrice, saleProperties, propertyType, includeUnderOffer, minBedrooms, maxBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		var msg = "About to test for the search\r\n"
			+ map.toString() + "\r\n"
			+ "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "(This property is priced at " + this.Price.toString() + ")\r\n"
			+ "Search Property Type: " + propertyType + " (This property's type is " + this.PropertyType + "\r\n"
			+ "This Office Name: " + this.OfficeName;
		//alert(msg);
		if(this.isSearchMatch(minPrice, maxPrice, saleProperties, propertyType, includeUnderOffer, minBedrooms, maxBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace) == true)
			this.showMarker(map);
		else
			this.hideMarker(map);
	}

	function _searchShowHide_Old(map, minPrice, maxPrice, saleProperties, propertyType)
	{
		if(this.isSearchMatch_Old(minPrice, maxPrice, saleProperties, propertyType) == true)
			this.showMarker(map);
		else
			this.hideMarker(map);
	}
}

function PropertyCollection(map)
{
	this.Properties = new CCollection();
	this.Map = map;
	this.add = this.Properties.add;
	this.remove = this.Properties.remove;
	this.clear = this.Properties.clear;
	this.size = this.Properties.size;
	
	this.SearchPropertiesRevised = _SearchPropertiesRevised;
	this.SearchProperties = _SearchProperties;
	this.SearchProperties_Old = _SearchProperties_Old;
	
	function _SearchPropertiesRevised(minPrice, maxPrice, saleProperties, /*propertyType,*/ includeFlats, includeHouse, includeUnderOffer, /*minBedrooms, maxBedrooms,*/ includeStudio, include1Bedrooms, include2Bedrooms, include3Bedrooms, include4PlusBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		for(var i = 0; i < this.size(); i++)
		{
			this[i].searchShowHideRevised(this.Map, minPrice, maxPrice, saleProperties, /*propertyType,*/ includeFlats, includeHouse, includeUnderOffer, /*minBedrooms, maxBedrooms,*/ includeStudio, include1Bedrooms, include2Bedrooms, include3Bedrooms, include4PlusBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace);
		}
	}
	
	function _SearchProperties(minPrice, maxPrice, saleProperties, propertyType, includeUnderOffer, minBedrooms, maxBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace)
	{
		var msg = "Searching:\r\n";
		msg += "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "\r\n";
		msg += "Sale Properties?: " + saleProperties.toString() + "\r\n";
		msg += "Property Type: " + propertyType;
		//alert(msg);
		for(var i = 0; i < this.size(); i++)
		{
			this[i].searchShowHide(this.Map, minPrice, maxPrice, saleProperties, propertyType, includeUnderOffer, minBedrooms, maxBedrooms, includeIslington, includeHighbury, includeCamden, mustHaveGarden, mustHaveTerrace);
		}
	}

	function _SearchProperties_Old(minPrice, maxPrice, saleProperties, propertyType)
	{
		var msg = "Searching:\r\n";
		msg += "Price Range: " + minPrice.toString() + " to " + maxPrice.toString() + "\r\n";
		msg += "Sale Properties?: " + saleProperties.toString() + "\r\n";
		msg += "Property Type: " + propertyType;
		//alert(msg);
		for(var i = 0; i < this.size(); i++)
		{
			this[i].searchShowHide_Old(this.Map, minPrice, maxPrice, saleProperties, propertyType);
		}
	}
}
