
function AddressToLatLng(address) {
// take the address string, get the geolocation and set the hidden form
// variables
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } 
      else {
	lat.setAttribute('value', point.lat());
	lng.setAttribute('value', point.lng());
	// jcg 6/23/2010 i added marketing_program_id code to make filtered version work, but I think
	// it broke .new. version, so check if null and paste if necessary
	// jcg 11/2/2009 seeing if i can call page again with lat,lng values
	marketing_prog = document.getElementById('marketing_program_id');
	if (marketing_prog == null){
	window.location = '?latitude=' + lat.value + '&longitude=' + lng.value + 
			'&address=' + address + '&radius=' + document.getElementById('radius').value;
	}
	else {
	window.location = '?latitude=' + lat.value + '&longitude=' + lng.value + 
			'&address=' + address + '&radius=' + document.getElementById('radius').value +
			'&marketing_program_id=' + document.getElementById('marketing_program_id').value;
	}
      }
    }
  );
}
function DisplayMap(lat, lng){
//alert('In DisplayMap() with lat: ' + lat + ' Lng: ' + lng);
   if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
        var homepoint = new GLatLng(lat, lng);

        var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
	markerOptions = { icon:blueIcon };

	map.setCenter(homepoint, 13);
	map.addOverlay(new GMarker(homepoint, markerOptions));
        // Create our "tiny" marker icon

	


	map.addControl(new GSmallMapControl());
	// get agency list
	//var agency_info = getElementsByClassName(document.getElementById('agency_container'), 'div', 'agency_info');
	var agency_info = getElementsByClassName('agency_info', 'div', document.getElementById('agency_container'));
	var len = agency_info.length;
	for (i = 0; i < len; i++){
		var agency_name = getElementsByClassName('agency_name', 'p', agency_info[i])[0].innerHTML;
		var agency_addr = getElementsByClassName('agency_addr1', 'p', agency_info[i])[0].innerHTML;
		agency_addr = agency_addr + '\n' +  getElementsByClassName('agency_addr2', 'p', agency_info[i])[0].innerHTML;
		var lat = getElementsByClassName('lat', 'p', agency_info[i])[0];
		var lng = getElementsByClassName('lng', 'p', agency_info[i])[0];
		var point = new GLatLng(lat.innerHTML, lng.innerHTML);
		var bubbleText = "<pre>" + agency_name + "\n" + agency_addr + "\n<a href = ''>website</a></pre>";
		map.addOverlay(CreateMarker(point, bubbleText));
	}
//alert("trying to resize");
//alert ('Bounds: ' + SBound + ', ' + WBound + ' - ' + NBound + ', ' + EBound + ' Zoom level: ' + map.getZoom());
	// resize map to fit data
	var SBound = document.getElementById('SBound').innerHTML;
	var WBound = document.getElementById('WBound').innerHTML;
	var NBound = document.getElementById('NBound').innerHTML;
	var EBound = document.getElementById('EBound').innerHTML;
//alert ('Bounds: ' + SBound + ', ' + WBound + ' - ' + NBound + ', ' + EBound + ' Zoom level: ' + map.getZoom());
//alert ('Bounds: ' + map.getBounds().getSouthWest().lat());
	while ((map.getBounds().getSouthWest().lat() > SBound) ||
		(map.getBounds().getSouthWest().lng() > WBound) ||
		(map.getBounds().getNorthEast().lat() < NBound) ||
		(map.getBounds().getNorthEast().lat() < EBound) 
		){
		map.zoomOut();
/*
alert ('Bounds: ' + SBound + ', ' + WBound + ' - ' + NBound + ', ' + EBound + ' Zoom level: ' + map.getZoom()
+ '\n ++ ' + map.getBounds().getSouthWest().lat() + ', ' + map.getBounds().getSouthWest().lng() + ' - ' + 
map.getBounds().getNorthEast().lat() + ', ' + map.getBounds().getNorthEast().lng() );
*/
	}
}
function CreateMarker(point, message){
	var marker = new GMarker(point)
	GEvent.addListener(marker, "click", function(){
		map.openInfoWindowHtml(point, message);
	});
	return marker;
}

function AddPoints(map, lat, lng) {
   if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
 
        // Add 10 markers to the map at random locations
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
          var point = new GLatLng(lat, lng);
          map.addOverlay(new GMarker(point));
        }
    }
}


function ShowAddress(address) {

    var map = new GMap2(document.getElementById("map_canvas"));
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, 15);
	map.addControl(new GSmallMapControl());
	var marker = new GMarker(point);
 	//markerOptions = TinyIcon();
        map.addOverlay(marker);

// jcg .. adding more to put in bubble
	// get phone for bubble

//	phone = document.getElementById('phone_for' + iata);
	bubbleText = "<p>Agency Name</p><p>" + address + "</p><p><a href = ''>website</a></p>";
        marker.openInfoWindowHtml(bubbleText);
        //marker.openInfoWindowHtml(address);
      }
    }
  );
}
// Create our "tiny" marker icon
function TinyIcon(){
var tinyIcon = new GIcon();
	tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	tinyIcon.iconSize = new GSize(12, 20);
	tinyIcon.shadowSize = new GSize(22, 20);
	tinyIcon.iconAnchor = new GPoint(6, 20);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);

	// Set up our GMarkerOptions object literal
	markerOptions = { icon:tinyIcon };
	return markerOptions;
}


function MyShowAddress(addr){
	//alert ('MyShowAddress shows: ' + what);
	ShowAddress(addr);
}
function MySortBy(){
//	alert('MySortBy ' + this + ' ' + this.value);
	document.getElementById('agency_list_by_name').style.display = 'none';
	document.getElementById('agency_list_by_city').style.display = 'none';
	document.getElementById('agency_list_by_state').style.display = 'none';
	if (this.value == 'name'){
		document.getElementById('agency_list_by_name').style.display = 'inline';
	}
	if (this.value == 'city'){
		document.getElementById('agency_list_by_city').style.display = 'inline';
	}
	if (this.value == 'province'){
		document.getElementById('agency_list_by_state').style.display = 'inline';
	}
}
function OnAgencySelect(){
    MyShowAddress(this.value);
}
function OnAddressSubmit(){
    MyShowAddress(reader_addr.value);
    return false;
}
function OnAddressSubmit2(){
    AddressToLatLng(reader_addr.value);
    return false;
}

window.onload = function () {
    lng = document.getElementById('longitude');
    lat = document.getElementById('latitude');
    // reader_addr is global
    reader_addr = document.getElementById('address');
    var reader_addr_submit2 = document.getElementById('address_submit2');
    reader_addr_submit2.onclick = OnAddressSubmit2;


/* jcg 11/2/2009 not for this form
    var sort_by_name = document.getElementById('sort_by_name');
    var sort_by_city = document.getElementById('sort_by_city');
    var sort_by_state = document.getElementById('sort_by_state');
    sort_by_name.onclick = MySortBy;
    sort_by_city.onclick = MySortBy;
    sort_by_state.onclick = MySortBy;

    // set to name on start
    sort_by_state.onclick();


   var agency_list_by_name = document.getElementById('agency_list_by_name');
   var agency_list_by_city = document.getElementById('agency_list_by_city');
   var agency_list_by_state = document.getElementById('agency_list_by_state');

   agency_list_by_name.onchange = OnAgencySelect;
   agency_list_by_city.onchange = OnAgencySelect;
   agency_list_by_state.onchange = OnAgencySelect;
*/
   if(document.getElementById('agency_container')){
//	var lat = document.getElementById('latitude').value;
//	var lng = document.getElementById('longitude').value;
//alert('lat: ' + lat + ' lng: ' + lng);
	DisplayMap(lat.value, lng.value);
   }
   else{
	//alert("can't find agency_container");
   }
}
