// Global geo_maps array and array size
var geo_maps = new Array(); 
var num_maps = 0;
var geo_map; 

// Creates a map 
function geopress_makemap(map_id, name, lat, lon, map_format) {
  num_maps = geo_maps.push(new Mapstraction("geo_map" + map_id, map_format)) - 1;
  var myPoint = new LatLonPoint(lat, lon);
  geo_maps[num_maps].addSmallControls();
  geo_maps[num_maps].setCenterAndZoom(myPoint, 11);
  geo_maps[num_maps].setMapType(Mapstraction.HYBRID);
  var marker = new Marker(myPoint);
  marker.setInfoBubble(name);
  geo_maps[num_maps].addMarker(marker);
}
function geopress_setmap() {
	geo_map.removeAllMarkers();
	var myPoint = new LatLonPoint(30,-90);
	geo_map.setCenterAndZoom(myPoint, 8);
	var marker = new Marker(myPoint);
	marker.setInfoBubble("@ Pointed");
	geo_map.addMarker(marker);
}

// @todo - make this use a Mapstraction geocoder or geocoder independent
 var geocoder = new GClientGeocoder();

// addPointToMap() adds a marker at a specific point from either 
// a geocoder response or the user clicking on the map. 
// @todo handle drawing polylines
function addPointToMap(point) {
	geo_map.removeAllMarkers();
	marker = new Marker(point);
	geo_map.setCenterAndZoom(point,10);
	marker.setInfoBubble(point.toString());
	geo_map.addMarker(marker);
}
 // addAddressToMap() is called when the geocoder returns an
 // answer.  It adds a marker to the map with an open info window
 // showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
//   map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new LatLonPoint(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    addPointToMap(point);
  }
}

 // showLocation() is called when you click on the Search button
 // in the form.  It geocodes the address entered into the form
 // and adds a marker to the map at that location.
function showLocation() {
	var address = document.forms[0].addr.value;
	if(address) {
		// If the 'address' is just points, map them
   		if(matches = address.match(/\[(.+),[ ]?(.+)\]/)) {
			setMapPoint(new LatLonPoint(matches[1], matches[2]));
		} else {
			geocoder.getLocations(address, addAddressToMap);
		}
	}
}

function geocode() {
	document.forms[0].locname.value = "";
	showLocation();   
}
// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	document.forms[0].addr.value = address;
	showLocation();
}
var gPoint;
// setMapPoint() handles a user clicking on a map
function setMapPoint(point) {
	document.forms[0].addr.value = "[" + point + "]";
	geo_map.removeAllMarkers();
	addPointToMap(point);
}

function resetMap() {
	geo_map.setCenterAndZoom(new LatLonPoint(0,0),1);
}

// used to register onload events to the body 
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

// Handles loading a saved address
function geopress_loadsaved(oSel) { 

   var addr = oSel.options[oSel.selectedIndex].value;
   var name = oSel.options[oSel.selectedIndex].text;
   
   addrobj = document.getElementById("addr");
   locnameobj = document.getElementById("locname");
   addrobj.value = addr;
   locnameobj.value = name;

   oSel.selectedIndex = 0;
}
function checkEnter(e,elem){ //e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4s which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IEs keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		geocode();				
		return false
	}
	else{
		return true
	}
}