var geocoder;
var map;
var errtxt = "";
var baseIcon;
var markers = new Array();

function filterResults()
{
	var maxprice_filter = Math.floor(getValue('PriceMax'));
	var with_den_filter = getElem('With_den').checked;
	var bedrooms_filter = 0;
	var characteristics_filter = new Array();
	
	var bedradios = document.searchform.Bedrooms;
	for (i=0;i<bedradios.length;i++)
	{
		if (bedradios[i].checked) bedrooms_filter = bedradios[i].value;
	}
	
	for(k=0;k<myCharacteristics.length;k++)
	{
		if (getElem('Char_'+myCharacteristics[k]).checked) characteristics_filter.push(myCharacteristics[k]);
	}
	
	for (j=0;j<myZones.length;j++) myZones[j].nrlocations = 0;
	newpoints = new Array();
	
	for (i=0;i<myPoints.length;i++)
	{
		point = myPoints[i];
		hasallcharacts = true;
		for(k=0;k<characteristics_filter.length;k++)
		{
			if (!my_in_array(characteristics_filter[k], point.characteristics)) {hasallcharacts = false;break;}
		}
		if (
			(maxprice_filter=='9999' || point.price<=maxprice_filter) //filter by price
			&&(bedrooms_filter==0 || point.bedrooms&bedrooms_filter)//filter by bedrooms
			&& hasallcharacts //filter by characts
			&& (!with_den_filter || point.with_den)
			)
		{
			showElem('zoneloc_'+point.id);
			newpoints.push(point);
			for (j=0;j<myZones.length;j++)
			{
				if (myZones[j].id==point.zone_id) myZones[j].nrlocations++;
			}
		}
		else
		{
			hideElem('zoneloc_'+point.id);
		}
	}
	for (j=0;j<myZones.length;j++)
	{
		getElem('zonecount_'+myZones[j].id).innerHTML = '('+myZones[j].nrlocations+')';
	}
	map.clearOverlays();
	loadGMap(newpoints);
	
}
function filterSuiteResults()
{
//	var with_den_filter = getElem('With_den').checked;
	var bedrooms_filter = Math.floor(getValue('Bedrooms'));
	
	newpoints = new Array();
	
	for (i=0;i<myPoints.length;i++)
	{
		point = myPoints[i];
		if (
			(bedrooms_filter==0 || point.bedrooms&bedrooms_filter)//filter by bedrooms
//			&& (!with_den_filter || point.with_den)
			)
		{
			newpoints.push(point);			
		}
	}
	map.clearOverlays();
	loadGMap(newpoints);
	
}
function loadGMap(points)
{
	if (GBrowserIsCompatible()) 
	{
		if (map==null)
		{
			map = new GMap2(document.getElementById("div_gmap"));
			map.addControl(new GSmallMapControl());
        	map.addControl(new GMapTypeControl());
		}

        // Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		
		for (i=0;i<points.length;i++)
		{
			point = points[i];

			
			options = null;
			if (point.icon!=null)
			{
				var ico = new GIcon();
				ico.image = point.icon.src;
				ico.iconSize = new GSize(point.icon.width, point.icon.height);
				
				if (point.shade!=null)
				{
					ico.shadow = point.shade.src;
					ico.shadowSize = new GSize(point.shade.width, point.shade.height);
				}
				
				ico.iconAnchor = new GPoint(point.icon.anchorX, point.icon.anchorY);
				ico.infoWindowAnchor = new GPoint(point.icon.width-5, 1);
	
				if (draggable!=null && draggable) drag=true;
				else drag=false;
				options = { icon:ico,draggable: drag };
			}
			if (apsumode!=null && category!='suite')
			{
				baseIcon = new GIcon(G_DEFAULT_ICON);
				baseIcon.shadow = ""+MAIN_URL+"img/design/shadow50.png";
				baseIcon.iconSize = new GSize(20, 34);
				baseIcon.shadowSize = new GSize(37, 34);
				baseIcon.iconAnchor = new GPoint(9, 34);
				baseIcon.infoWindowAnchor = new GPoint(9, 2);
				
				var letteredIcon = new GIcon(baseIcon);
				letteredIcon.image = ''+MAIN_URL+"marker.php?index="+(i+1);
			
				// Set up our GMarkerOptions object
				options = { icon:letteredIcon };
			}
			if (filter_location=='all')
			{
				map.setCenter(new GLatLng(city_lat, city_lng), city_zoom);
			}
			else
			{
				if (point.zoom==null)
					map.setCenter(new GLatLng(point.latitude, point.longitude), city_zoom);
				else
					map.setCenter(new GLatLng(point.latitude, point.longitude), point.zoom);
			}	
			if (point.type!='loc')
				marker = createSimpleInfoMarker(new GLatLng(point.latitude, point.longitude), point.details, options);
			else
				marker = createSimpleInfoMarker(new GLatLng(point.latitude, point.longitude), point.details, options, point.url);
			
			markers[i] = marker;
			// Add the marker to map
			map.addOverlay(markers[i]);
		}
		if (apsumode=='location') showPoint(id_curloc);
	}
	else
	{
		alert ('Your browser version does not support Google Maps. If the problem persists, please report this to Site Administrators. !');
	}
}
function createSimpleInfoMarker(point, content, markerOptions, location_url) 
{
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click",	function() 
	{
		if (apsumode==null && location_url!=null)
		{
			self.location = location_url;			
		}
	});
	
	GEvent.addListener(marker, "mouseover",	function() 
	{
		if (apsumode!=null)
		{
			marker.openInfoWindowHtml(content);
		}
		
	});
	
	GEvent.addListener(marker, "mouseout",	function() 
	{
		if (apsumode!=null)
		{
//			marker.closeInfoWindow();
		}
		
	});
	
	return marker;
}

function searchAndDisplay(address, company, islast, zoom, index)
{
	if (zoom==null) zoomLevel = 10;
	else zoomLevel = zoom;
	geocoder = new GClientGeocoder();
	
	geocoder.getLocations(address, function (response) 
	{
		if (!response)
		{
			if (errtxt!='') errtxt+='\n\n';
			errtxt += 'ADDRESS "'+address+'" not found!';
			errtxt += '\nREASON: Internal error. Try again. If the problem persists, please report this to Site Administrators.';
		}
		else if (response.Status.code!=200)
		{
			if (errtxt!='') errtxt+='\n\n';
			errtxt += 'ADDRESS "'+address+'" not found!';
			switch (response.Status.code)
			{
//				case 400: errtxt = '\nREASON: A directions request could not be successfully parsed. For example, the request may have been rejected if it contained more than the maximum number of waypoints allowed. ';break;
//				case 500: errtxt = '\nREASON: A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known. ';break;
//				case 601: errtxt = '\nREASON: The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input ';break;
				case 400: 
				case 500:
				case 601: 
				case 602: errtxt += '\nREASON: The address is relatively new, or it may be incorrect.';break;
				case 603: errtxt += '\nREASON: The address details cannot be returned due to legal or contractual reasons.';break;
				case 604: errtxt += '\nREASON: Google error . We do not have enough data regarding that region.';break;
				case 610: errtxt += '\nREASON: The given key is either invalid or does not match the domain for which it was given.';break;
				case 620: errtxt += '\nREASON: Too many queries in too short a period. Try again. If the problem persists, please report this to Site Administrators.';break;
			}
		}
		else
		try {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			map.setCenter(point, zoomLevel);
			
			var infoHtml = '<div align="left" style="width:180px;"> <b>'+company+'</b> <br>'+ address + '</div>';

			markers[index] = createInfoMarker(point, infoHtml, index+1);
			// Add the marker to map
			map.addOverlay(markers[index]);
			
		}
		catch (err)
		{
			if (errtxt!='') errtxt+='\n\n';
			errtxt += 'ADDRESS "'+address+'" not found!';
			errtxt += '\nREASON: Internal error. Try again. If the problem persists, please report this to Site Administrators.';
		}
		
		if (errtxt!="" && islast) 
		{
			errtxt+="\n\nClick OK to continue.";
			alert(errtxt);
		}
	});
	
	
}


function createInfoMarker(point, content, index) 
{
	
	var letteredIcon = new GIcon(baseIcon);
	letteredIcon.image = "http://89.137.97.177/minto/marker.php?index="+index;

	// Set up our GMarkerOptions object
	markerOptions = { icon:letteredIcon };
 
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click",	function() {
		marker.openInfoWindowHtml(content);
	});
	
	return marker;
}

