//<![CDATA[

    var map = null;
    var geocoder = null;

	var aMarkers;
	var markerObj;
	
		
	function scroll()
	{
	      if ( document.documentElement && document.documentElement.scrollTop )
	      {
	            document.documentElement.scrollTop = 0;
	      }
	      else if ( document.body )
	      {
	            document.body.scrollTop = 0;
	      }
	}

	function load()
	{
		aMarkers = new Array();
		
		if (GBrowserIsCompatible())
		{
        	map = new GMap2(document.getElementById("map"));
        	map.enableScrollWheelZoom();

			// let's center the map on colorado springs
	        map.setCenter(new GLatLng(38.9457, -104.7388), 8);
	        var mapControl = new GMapTypeControl();
	        map.addControl(mapControl);
	        map.addControl(new GSmallMapControl());
	        geocoder = new GClientGeocoder();
	
			/*GEvent.addListener(map, "dragend", function()
			{
				Loading();
				LoadScript();
			});
			
			GEvent.addListener(map, "zoomend", function()
			{
				Loading();
				LoadScript();
			});*/
		}
	}

	function loadHomeInfo(listID, latitude, longitude)
	{
		aMarkers = new Array();
		
		if (GBrowserIsCompatible())
		{
        	map = new GMap2(document.getElementById("map"));
        	map.enableScrollWheelZoom();

			// let's center the map on colorado springs
	        map.setCenter(new GLatLng(38.9457, -104.7388), 8);
	        var mapControl = new GMapTypeControl();
	        map.addControl(mapControl);
	        map.addControl(new GSmallMapControl());
	        //map.addControl(new TextualZoomControl());
	        geocoder = new GClientGeocoder();
	
			createPushpin(null, latitude, longitude, null, null, listID, null, null, null, null, null);
	
			/*GEvent.addListener(map, "dragend", function()
			{
				Loading();
				LoadScript();
			});
			
			GEvent.addListener(map, "zoomend", function()
			{
				Loading();
				LoadScript();
			});*/
		}
	}	
	
	function getBounds()
	{
		// get the bounds of the current map view
		bounds = map.getBounds();
		
		// obtain the raw form of the borders (Points)
		var centerRaw = bounds.getCenter();
		var trRaw = bounds.getNorthEast();
		var blRaw = bounds.getSouthWest();
		
		// split the points into individual pieces
		var top = trRaw.lat();
		var right = trRaw.lng();
		var bottom = blRaw.lat();
		var left = blRaw.lng();
		
		// build a new object, and push tl/br into it and return
		// tl/br are predefined in homesJS.php so need to be respected
		var oRet = new Object();
		oRet.tl = top + ", " + left;
		oRet.br = bottom + ", " + right;
		
		return oRet;
	}
	
	function clearPushpins()
	{
		// let's make sure we have a map first before we try to clear teh overlays, otherwise we'll get an exception
		if(map != null)
		{
			map.clearOverlays();
		}
		
		return;
	}
	
	function clearPushpinType(type)
	{
		//alert("removing pushpins");
		
		var aMarkersTemp = new Array();
		
		for(var index = 0; index < aMarkers.length; index++)
		{
			if(aMarkers[index].type != type)
				aMarkersTemp.push(aMarkers[index]);
		}
		
		aMarkers = aMarkersTemp;
		
		map.clearOverlays();
		
		for(var index = 0; index < aMarkers.length; index++)
		{
			map.addOverlay(aMarkers[index].marker);
		}
		
		//alert("removed Pushpins");
		
		return;
	}
	
	function showOnMap(listID)
	{
		for(var i = 0; i < aMarkers.length; i++)
		{
			if(aMarkers[i].listID == listID)
			{
				// we found the marker, let's center on it and open it's window
				GEvent.trigger(aMarkers[i].marker, "click");
				map.setZoom(11);
				scroll();
				break;
			}
		}
	}
	
	function showPushpinType(type)
	{
		if(type == 'hospital')
		{
			
		}
		else if(type == 'school')
		{
			
		}
	}
	
	function centerMap()
	{
		var latTot = 0;
		var lngTot = 0;
		var latAvg = 0;
		var lngAvg = 0;
		
		
		for(var index = 0; index < aMarkers.length; index++)
		{
			latTot += aMarkers[index].marker.getPoint().lat();
			lngTot += aMarkers[index].marker.getPoint().lng();
		}
		
		latAvg = latTot / aMarkers.length;
		lngAvg = lngTot / aMarkers.length;
		
		map.setCenter(new GLatLng(latAvg, lngAvg), 8);
	}

	function createPushpin(label, lat, lng, image, houseImage, listID, price, bed, bath, size, district)
	{
		// this is "backwards" as longitude = x and latitude = y
		var point = new GPoint(lng, lat);
		var type;
		
		// create our icon
		// leave mapIcon.shadow blank to keep all shadows off
		
		var mapIcon = new GIcon();

		if((image == null) || (image == ''))
		{
			mapIcon.image = "http://www.springshomes.com/houseIcon.png";
			type = 'house';
		}
		else if(image == 'school')
		{
			mapIcon.image = "http://www.springshomes.com/schoolIcon.png";
			type = 'school';
		}
		else if(image == 'hospital')
		{
			mapIcon.image = "http://www.springshomes.com/hospitalIcon.png";
			type = 'hospital';
		}
		else
		{
			mapIcon.image = image;
		}

        mapIcon.iconSize = new GSize(24, 24);
    	mapIcon.iconAnchor = new GPoint(12, 12);
        mapIcon.infoWindowAnchor = new GPoint(19, 4);
		
		// cast mapIcon as an icon, and store it as markerOptions
		markerOptions = { icon:mapIcon };
		
		// create our marker with the point, and the mapIcon
		var marker = new GMarker(point, markerOptions);
		
		markerObj = new Object();
		
		markerObj.listID = listID;
		markerObj.marker = marker;
		markerObj.type = type;
		
		aMarkers.push(markerObj);

		// add the marker to the map
		map.addOverlay(marker);
		
		// if houseImage is null, we can assume bed/bed/size/etc are null too, and therefore we don't need a listener for clicking on the house
		
		if(houseImage != null)
		{
			// this is where we what happens when we click on the icon
			// currently we are opening an InfoWindow with HTML support
			GEvent.addListener(marker, "click", function()
			{
				marker.openInfoWindowHtml('<div style="width: 310px; height: 145px;"><div align="center" style="float: left; height: 145px;"><br/><img border=1 src="' + houseImage + '" width=150/><br/>(search result: ' + label + ')</div><div align="left" style="padding-left: 10px;float: right;valign: center; height: 145px;"><br/><font face="Tahoma">' +	bed + ' Bed<br/>' + bath + ' Bath<br/>' + size + ' Sq. Feet<br/>' + 'School District #' + district + '<br/>' + price + '<br/><br/><a href="home.php?listingid=' + listID + '">More Info</a><br/><a href="account.php?addlistingid=' + listID + '">Add to Favorites</a></font></div></div>');
			});
		}
		else if(image == null)
		{
			map.setCenter(new GLatLng(lat, lng), 13);
		}
		else
		{
			
		}
	}
	
	function showAddress(address, desc, features, type)
	{
		if(geocoder)
		{
	    	geocoder.getLatLng(address, 
	        	function(point)
	        	{
	            	if(!point)
			        {
	              		alert(address + " not found");
	            	}
	            	else
	            	{
						var marker = new GMarker(point);
						map.addOverlay(marker);
						//marker.openInfoWindowHtml('<img src="http://www.thememyiphone.net/lasplanas/images/marina.png"/><br/><font face="Tahoma">' + address + '<br/><br/>' + desc + '<br/>' + features + '</font>');

						GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml('<img src="http://www.thememyiphone.net/lasplanas/images/marina.png"/><br/><font face="Tahoma">' + address + '<br/><br/>' + desc + '<br/>' + features + '</font>');
          });

						if(type == 'home')
						{
							map.setCenter(point, 10);
							marker.setImage('http://maps.google.com/mapfiles/dd-start.png');
						}
					}
				}
			);
		}
	}

    //]]>