
 
var map = null;
var geocoder = null;



function loadrepoloc(Lat, Long, Zoomlev){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(parseFloat(Lat), parseFloat(Long)), parseInt(Zoomlev));
		map.setUIToDefault();
		painthouse(Lat, Long, parseInt(Zoomlev));

		
	}
}

function loadhybridrepoloc(Lat, Long, Zoomlev){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(parseFloat(Lat), parseFloat(Long)), parseInt(Zoomlev));
		map.setUIToDefault();
		map.setMapType(G_HYBRID_MAP);
		painthouse(Lat, Long, parseInt(Zoomlev));

		
	}
}


function initialiseEditRepo(Lat, Long, Zoomlev){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(parseFloat(Lat), parseFloat(Long)), parseInt(Zoomlev));
		map.setUIToDefault();
		GEvent.addListener(map, "click", getAddress);
		geocoder = new GClientGeocoder();
		painthouse(Lat, Long, parseInt(Zoomlev));

		
	}
}

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(-28.8, 24.4), 5);
		map.setUIToDefault();
		GEvent.addListener(map, "click", getAddress);
		geocoder = new GClientGeocoder();
	}
}

function setIOLPropertyRepoPage(){

	if (GBrowserIsCompatible()) {
	
	
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(-28.8, 24.4), 5);

		
		
		// Custom Zoom Functionality
		var otherOpts = { 
		          buttonStartingStyle: {background: '#FFF', paddingTop: '0px', paddingLeft: '0px', border:'1px solid black'},
		          buttonHTML: '<img title="Drag Zoom In" src="/images/zoomcustom2.gif">',
		          buttonStyle: {width:'70px', height:'70px', border:'1px solid #999999'},
		          buttonZoomingHTML: '<p style="color:#6688cc;line-height:20px;font: 13px  Arial;">Drag a region on the map <BR>(click here to reset)',
		          buttonZoomingStyle: {background:'white',width:'200px', height:'100%', border:'1px solid #999999'},
		          backButtonHTML: '<img title="Zoom Back Out" src="images/zoomout.gif">',  
		          backButtonStyle: {display:'none',marginTop:'5px',width:'25px', height:'23px'},
		          backButtonEnabled: false, 
		          overlayRemoveTime: 1500} 
		 
		map.addControl(new DragZoomControl({}, otherOpts, {}), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5,280)));
		
		var customUI = map.getDefaultUI();
		//customUI.maptypes.hybrid = false;
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		
      		geocoder = new GClientGeocoder();
      		
        
	}

}



function setRepoFilter(status, bank, price, beds, price_min, province){

	GO('searchstatdiv').innerHTML = "<table align='center' border='0px'><tr><td><img border=0px src='/images/ajax-loader.gif'></td></tr></table>";
	
	GO('mapdiv').style.display = 'block';
	if (map == null){ setIOLPropertyRepoPage();}

	map.clearOverlays();

	
	var repocordsrc = "/repo_locs.jsp?";
	
	if (status != null){ repocordsrc = repocordsrc + "&repostat=" + status;}
	if (bank != null){ repocordsrc = repocordsrc + "&bank=" + bank;}
	if (price != null){ repocordsrc = repocordsrc + "&price=" + price;}
	if (beds != null){ repocordsrc = repocordsrc + "&beds=" + beds;}
	if (price_min != null){ repocordsrc = repocordsrc + "&price_min=" + price_min;}
	if (province != null){ repocordsrc = repocordsrc + "&province=" + province;}	
	
	repocordsrc = repocordsrc + "&tmstmp" + new Date().getTime();
	
	
	GDownloadUrl(repocordsrc, function(data) {
	  var xml = GXml.parse(data);
	  if (xml != null){
		  
		  var markers = xml.documentElement.getElementsByTagName("marker");  
		  if ((markers != null) && (markers.length > 0)){
		  
		  

		  
				
			GO('searchstatdiv').innerHTML = "<h2>" + markers.length + " properties were found for your search criteria.</h2>";
			
			
			// first re-center the map
			var bounds = new GLatLngBounds();
			for (var i = 0; i < markers.length; i++) {
				var newpoint = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));	
				bounds.extend(newpoint);
			}
			
			var Zoomlevel = map.getBoundsZoomLevel(bounds);
			if (Zoomlevel > 15){Zoomlevel = 15;} // prevent it zooming nonsenically
			map.setZoom(Zoomlevel);
   			map.setCenter(bounds.getCenter());
			
			// Now paint the properties
			for (var i = 0; i < markers.length; i++) {
				paintrepo(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")), parseInt(markers[i].getAttribute("id")));	
			}

		  }else{
		  	GO('searchstatdiv').innerHTML = "<h2 style='padding:20px;'>No properties were found for your search criteria.</h2>";
		  }
	  }else{
	  	GO('searchstatdiv').innerHTML = "<h2 style='padding:20px;'>No properties were found for your search criteria.</h2>";
	  }
	  
	});


}


function paintrepo(lat, long, id){

	var housepoint = new GPoint(long, lat);		
	var housemarkerOptions = { icon:gethouseIcon() };
	var marker = new GMarker(housepoint, housemarkerOptions);
	
	

	
	          GEvent.addListener(marker, 'click', function(){ 
	            marker.openExtInfoWindow(
	              map,
	              "custom_info_window_red",
	              "<div style='padding:5px;width:100%;float:left;'><table border=0px align=center><tr><td><img src='/images/loading.gif' style='padding:10px'></td></tr></table></div>",
	              {
	                ajaxUrl: "/repo_summary.jsp?t=" + new Date().getTime() + "&id=" + id, 
	                beakOffset: 3
	              }
	            ); 
	          });


	
	map.addOverlay(marker);

}

function painthouse(lat, long, zoomlev){

	var housepoint = new GPoint(long, lat);	
	map.setCenter(new GLatLng(lat, long), zoomlev);	
	var housemarkerOptions = { icon:gethouseIcon() };
	var marker = new GMarker(housepoint, housemarkerOptions);
	map.addOverlay(marker);


}

function gethouseIcon(){
	var houseicon = new GIcon(G_DEFAULT_ICON); 
	houseicon.image = "/images/bighouse.png";
	houseicon.shadow = "/images/bighouse-shadow.png";
	houseicon.iconSize = new GSize(32, 32);
	houseicon.shadowSize = new GSize(59, 32);
	return houseicon;

}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(  address,
			  function(point) {
			    	if (!point) {
			      		alert(address + " not found, please try again.");
			    	} else {
			      		map.setCenter(point, 13);
					markerOptions = { icon:gethouseIcon() };
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
					var InitialMessage = "<U>" + address + "</U><p style='padding-top:10px;'>To set the property's location, navigate to the location on the map and click.</p><p>If the map isn't displaying the correct area, simply enter a new search in the box underneath.</p>";
					marker.openInfoWindowHtml(InitialMessage, {pixelOffset:new GSize(5,5), maxWidth:200} );

			    	}
			  }
		);
	}
}

function showPremAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(  address,
			  function(point) {
			    	if (point){
			      		map.setCenter(point, 15);
					markerOptions = { icon:gethouseIcon() };
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
					var InitialMessage = "<U>" + address + "</U><p style='padding-top:10px;'>To set the property's location, navigate to the location on the map and click.</p><p>If the map isn't displaying the correct area, simply enter a new search in the box underneath.</p>";
					marker.openInfoWindowHtml(InitialMessage, {pixelOffset:new GSize(5,5), maxWidth:200} );

			    	}
			  }
		);
	}
}



function showAddressandSet(address) {
	map.clearOverlays();
	if (geocoder) {
		geocoder.getLatLng(  address,
			  function(point) {
			    	if (!point) {
			      		alert(address + " not found, please try again.");
			    	} else {
			      		map.setCenter(point, 13);
					markerOptions = { icon:gethouseIcon() };
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
					var cordstr = marker.getLatLng().lat() + "," + marker.getLatLng().lng();
					var InitialMessage = "<U>" + address + "</U><p style='padding-top:10px;'>If the icon is at the correct location, then simply click the 'Set' button to set the location of the property.</p>" + 
					"<p><input type='button' name='setlst' value='Set' onclick='GO(\"cords\").value = \"" + cordstr + "\";map.closeInfoWindow();' style='width:50px'></p>" +
						"<div style='float:left;width:100%;padding-top:10px;'>If not, then either navigate to the location or perform a new location search by typing in the street/area below." + 
						"</div><div style='float:left;width:100%;padding-top:10px;'><B>NOTE: Once the location is set it will only be recorded when you click the update button below.</b></div>";
					marker.openInfoWindowHtml(InitialMessage, {pixelOffset:new GSize(5,5), maxWidth:300, maxHeight:220} );
					

			    	}
			  }
		);
	}
}



function showRepoAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(  address,
			  function(point) {
			    	if (!point) {
			      		alert(address + " not found, please try again.");
			    	} else {
			      		map.setCenter(point, 13);
			    	}
			  }
		);
	}
}


function getAddress(overlay, latlng) {
	if (latlng != null) {
		address = latlng;
		geocoder.getLocations(latlng, showNewAddress);
	}
}
 
function showNewAddress(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("Status Code:" + response.Status.code);
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
				    place.Point.coordinates[0]);
				    
		markerOptions = { icon:gethouseIcon() };		    
		marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		GO("address").value = place.address;
		GO("cords").value = place.Point.coordinates[1] + "," + place.Point.coordinates[0];
		
		/**
		marker.openInfoWindowHtml(
		'<b>orig latlng:</b>' + response.name + '<br/>' + 
		'<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
		'<b>Status Code:</b>' + response.Status.code + '<br>' +
		'<b>Status Request:</b>' + response.Status.request + '<br>' +
		'<b>Address:</b>' + place.address + '<br>' +
		'<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
		'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
		**/
		
	}
}
 
