
var map;
var gdir;
var geocoder = null;
var addressMarker;
var noel_address = "2631 N. Halsted, Chicago, IL 60614";

function map_initialize() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("map_canvas"));
		gdir = new GDirections(map, document.getElementById("directions"));
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
		//GEvent.addListener(gdir, "error", handleErrors);
	
		//setDirections(noel_address, noel_address, "en_US");
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(noel_address, function(point)
			{      
				if (!point) {        
					alert(address + " not found");      
				} else {        
					map.setCenter(point, 15);        
					var marker = new GMarker(point);        
					map.addOverlay(marker);
					var bubble_size = new GSize(60,60);
					var bubble_content = document.getElementById('bubble');
					
					marker.openInfoWindow(bubble_content);
					bubble_content.style.display="block";
				}    
			});
		map.setUIToDefault();
	}
	insert_state_select();
}

function setDirections() {
	
	var fade_area = document.getElementById('current_content');
	
	var start_height = fade_area.scrollHeight;
	
	var street = document.getElementById('street').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;
	
	document.getElementById('current_map_height').value = document.getElementById('current_content').scrollHeight;
	
	var fromAddress = "";
	if(street != ""){
		street += ", ";
	}
	if(city != ""){
		city += ", ";
	}
	fromAddress += street + city + state + " " + zip;
	
	map.checkResize();
	
	gdir.load("from:" + fromAddress + " to: 2631 N. Halsted, Chicago, IL 60614");
	
	
	window.setTimeout(checkHeight, 1000);

}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}

function onGDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.
	//get starting height
	var start_height = Number(document.getElementById('current_map_height').value);
	var end_height = document.getElementById('current_content').scrollHeight;
	
	document.getElementById('footer').innerHTML = "onloaded " + start_height + ", " + end_height;
	
	var start_rect = new Spry.Effect.Utils.Rectangle();
	start_rect.width = 615;
	start_rect.height = start_height;
	var end_rect = new Spry.Effect.Utils.Rectangle();
	end_rect.width = 615;
	end_rect.height = end_height;					
	var current_content = document.getElementById('current_content');
	
	Spry.Effect.makeClipping(current_content);
	var change_height = new Spry.Effect.Size(current_content, start_rect, end_rect, {duration: 300});
	change_height.start();
  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

	//make state select input
function insert_state_select(){
	var html_select = "<select name='state' id='state' size='1'>";
	html_select += "<option value=''>Select a state</option>";
	html_select += "<option value='AL'>Alabama</option>";
	html_select += "<option value='AK'>Alaska</option>";
	html_select += "<option value='AZ'>Arizona</option>";
	html_select += "<option value='AR'>Arkansas</option>";
	html_select += "<option value='CA'>California</option>";
	html_select += "<option value='CO'>Colorado</option>";
	html_select += "<option value='CT'>Connecticut</option>";
	html_select += "<option value='DE'>Delaware</option>";
	html_select += "<option value='DC'>District of Columbia</option>";
	html_select += "<option value='FL'>Florida</option>";
	html_select += "<option value='GA'>Georgia</option>";
	html_select += "<option value='HI'>Hawaii</option>";
	html_select += "<option value='ID'>Idaho</option>";
	html_select += "<option selected value='IL'>Illinois</option>";
	html_select += "<option value='IN'>Indiana</option>";
	html_select += "<option value='IA'>Iowa</option>";
	html_select += "<option value='KS'>Kansas</option>";
	html_select += "<option value='KY'>Kentucky</option>";
	html_select += "<option value='LA'>Louisiana</option>";
	html_select += "<option value='ME'>Maine</option>";
	html_select += "<option value='MD'>Maryland</option>";
	html_select += "<option value='MA'>Massachusetts</option>";
	html_select += "<option value='MI'>Michigan</option>";
	html_select += "<option value='MN'>Minnesota</option>";
	html_select += "<option value='MS'>Mississippi</option>";
	html_select += "<option value='MO'>Missouri</option>";
	html_select += "<option value='MT'>Montana</option>";
	html_select += "<option value='NE'>Nebraska</option>";
	html_select += "<option value='NV'>Nevada</option>";
	html_select += "<option value='NH'>New Hampshire</option>";
	html_select += "<option value='NJ'>New Jersey</option>";
	html_select += "<option value='NM'>New Mexico</option>";
	html_select += "<option value='NY'>New York</option>";
	html_select += "<option value='NC'>North Carolina</option>";
	html_select += "<option value='ND'>North Dakota</option>";
	html_select += "<option value='OH'>Ohio</option>";
	html_select += "<option value='OK'>Oklahoma</option>";
	html_select += "<option value='OR'>Oregon</option>";
	html_select += "<option value='PA'>Pennsylvania</option>";
	html_select += "<option value='RI'>RhodeHODE Island</option>";
	html_select += "<option value='SC'>South Carolina</option>";
	html_select += "<option value='SD'>South Dakota</option>";
	html_select += "<option value='TN'>Tennessee</option>";
	html_select += "<option value='TX'>Texas</option>";
	html_select += "<option value='UT'>Utah</option>";
	html_select += "<option value='VT'>Vermont</option>";
	html_select += "<option value='VA'>Virginia</option>";
	html_select += "<option value='WA'>Washington</option>";
	html_select += "<option value='WV'>West Virginia</option>";
	html_select += "<option value='WI'>Wisconsin</option>";
	html_select += "<option value='WY'>Wyoming</option>";
	html_select += "</select>";
	
	document.getElementById('state_select').innerHTML=html_select;	
}
