function load_children(place){
$('place_id').value = place
document.getElementsByClassName('first_child').each(function(s) {
s.hide();
});
document.getElementsByClassName('second_child').each(function(s) {
s.hide();
});

place_id = "place_" + place;
$(place_id).show();
$(place_id).value = place;
}
function load_second_children(place){
$('place_id').value = place
document.getElementsByClassName('second_child').each(function(s) {
s.hide();
});
if (place.length > 0){
	place_id = "place_" + place;
	$(place_id).show();
	$(place_id).value = place;

}
}
function load_third_children(place){
$('place_id').value = place
}




var map = null;
var geocoder = null;
		
function initialize() {
  if (GBrowserIsCompatible()) {
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/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);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

	if(typeof marker == "undefined"){}else{ map.addOverlay(marker); }
    geocoder = new GClientGeocoder();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	 }
}
function initialize_small() {
  if (GBrowserIsCompatible()) {
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/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);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	map.addControl(new GSmallMapControl());
	plotPoints();
  }
}
		
function plotPoint(lat, lng, name){
addMarker(lat, lng, name);
}

function showAddress(address) { // new local proxy to MultiMap

	new Ajax.Request('/property/geocode_proxy/' + address, {
	  method: 'get',
	  onSuccess: function(transport) {
		geoObject = eval('(' + transport.responseText + ')');

		if (geoObject.location_count == 0){
					$('ajaxResponse').innerHTML = '<h2><font color=red><center>Sorry, location not found</center></font></h2>';
		}
		if (geoObject.location_count == 1){
					$('ajaxResponse').innerHTML = '<div class="info">You location has been marked on the map below.  You can drag the marker to the correct posision</div>';
		}
		if (geoObject.location_count > 1) {
			var multipleLocations = '';
			for (i=0; i<geoObject.location_count; i++){
				multipleLocations += "<h4><a href='#' onclick=\"foundThisPoint('" + geoObject.result_set[i].point.lat;
				multipleLocations += "','" + geoObject.result_set[i].point.lon + "'); return false;\">";
				multipleLocations += geoObject.result_set[i].address.display_name + "</a></h4>";
			}
			$('ajaxResponse').innerHTML = "<div class='info'>Multiple Locations Found.  Please choose one.</div>" + multipleLocations;
		}
		foundThisPoint(geoObject.result_set[0].point.lat, geoObject.result_set[0].point.lon);
	}
	});

}

function foundThisPoint(lat,lng){
	var point = new GLatLng(lat,lng);
	map.setCenter(point,15);
	marker = new GMarker(point, {draggable: true});
		
					GEvent.addListener(marker, "dragstart", function() {
					map.closeInfoWindow();
					});
					
					GEvent.addListener(marker, "dragend", function() {
					marker.openInfoWindowHtml("<div style='width: 200px'><b>Your Location.</b><br><br>Please click Save below.</div>");
					$('form_lat').value = marker.getPoint().lat().toFixed(6);
					$('form_lng').value = marker.getPoint().lng().toFixed(6);
					});
					map.clearOverlays();
					map.addOverlay(marker);
					$('form_lat').value = point.y;
					$('form_lng').value = point.x;

}

function getXmlNodeValue(xmlNode){
	return Try.these(
		function() {return xmlNode.text;},
		function() {return xmlNode.textContent;}
		);
}
    function showAddress_google(address) {
      if (geocoder) {
				  geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 15);
							map.clearOverlays();
              marker = new GMarker(point, {draggable: true});
				
							GEvent.addListener(marker, "dragstart", function() {
							map.closeInfoWindow();
							});
							
							GEvent.addListener(marker, "dragend", function() {
							marker.openInfoWindowHtml("<div style='width: 200px'><b>Your Location.</b><br><br>You can still drag the marker and zoom in to get it just right.</div>");
							$('form_lat').value = marker.getPoint().lat().toFixed(6);
							$('form_lng').value = marker.getPoint().lng().toFixed(6);
							});
							
              map.addOverlay(marker);
							$('form_lat').value = point.y;
							$('form_lng').value = point.x;
              marker.openInfoWindowHtml(address + "<br /><br /><div style='width: 200px'><b>Drag the red marker</b><br /> to the correct position on the map.</div> ");
            }
          }
        );
      }
    }
		
var oldPoly = [];

function eraseCircles() {
	if(oldPoly) {
		for (var i = 0; i < oldPoly.length; i++) {
			map.removeOverlay(oldPoly[i]);
		}
		oldPoly = [];
	}
}

function drawCircle(center, radius) {
	if($('form_distance').value > 34){
	map.setZoom(8);
	} else {
		map.setZoom(9);
	}
	eraseCircles();
	//Function created by Chris Haas
	var circleQuality = 10;			//1 is best but more points, 5 looks pretty good, too
	var M = Math.PI / 180;			//Create Radian conversion constant
	var L = map.getBounds();		//Holds copy of map bounds for use below
	var sw = L.getSouthWest();
	var ne = L.getNorthEast();

	//The map is not completely square so this calculates the lat/lon ratio
	// this works because we create a square map
	var circleSquish = (ne.lng() - sw.lng()) / (ne.lat() - sw.lat());

	var points = [];							//Init Point Array
	//Loop through all degrees from 0 to 360
	for(var i=0; i<360; i+=circleQuality){
		var P = new GLatLng(
			marker.getPoint().lat() + ($('form_distance').value /89.1 * Math.sin(i * M)),
			marker.getPoint().lng() + ($('form_distance').value /89.1 * Math.cos(i * M)) * circleSquish
			);
		points.push(P);
	}
	points.push(points[0]);	// close the circle
	var pw = new GPolyline(points, '#ffffff', '5', '1.0')
	var p = new GPolyline(points, '#ff0000', '1', '1.0')
		map.addOverlay(pw);
		map.addOverlay(p);
	oldPoly.push(p);
	oldPoly.push(pw);
}
		

		
		function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function addMarker(latitude, longitude, description) {
var marker = new GMarker(new GLatLng(latitude, longitude), icon);
GEvent.addListener(marker, 'click',
function() {
marker.openInfoWindowHtml(description);
}
);
map.addOverlay(marker);
}


function enterYourOwnUni() {
	$('uni_verbose').disabled = false;
	$('qualification_uni_id').disabled = true; 
	Element.toggle($('qualification_uni_id'));
	Element.toggle($('uni_verbose'));
	$('uni_verbose').focus();
	$('uni_verbose').select();
	Element.toggle($('chooseYourOwnUni'));
	Element.toggle($('enterYourOwnUni'));

	return false;
}
function chooseYourOwnUni() {
	$('uni_verbose').disabled = true;
	$('qualification_uni_id').disabled = false; 
	Element.toggle($('qualification_uni_id'));
	Element.toggle($('uni_verbose'));
	Element.toggle($('chooseYourOwnUni'));
	Element.toggle($('enterYourOwnUni'));
	return false;
}
function quickMap(lat, lng){
	//map.clearOverlays();
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0)
	{
	    if (window.pageYOffset)
	        ScrollTop = window.pageYOffset;
	    else
	        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	if(ScrollTop > 150){
	scroll(0,150);
	}	
	// highlight map field
	new Effect.Highlight('mini_map', {startcolor:'#4d92d7', endcolor:'#ffffff', duration: 2})
	var point = new GLatLng(lat, lng);
	map.setCenter(point);
}
function alphaMarkers(lat, lng, index){
	var point = new GLatLng(lat, lng);
	var marker = createMarker(point,index)
	map.addOverlay(marker);	
}



// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index) {
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/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);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var letteredIcon = new GIcon(baseIcon);
  letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  return marker;
}

function plotPoints(){
			for (id = 0; id < miniPoints_lat.length; id++) {
				var point = new GLatLng(miniPoints_lat[id], miniPoints_lng[id]);
				var marker = createMarker(point,id)
				map.addOverlay(marker);
			}
}






