function ReferencePointManager(map){
	this.refArray = [];
  this.refIcon = new GIcon();
  this.refIcon.image = "http://labs.google.com/ridefinder/images/mm_20_purple.png";
  this.refIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  this.refIcon.iconSize = new GSize(12, 20);
  this.refIcon.shadowSize = new GSize(22, 20);
  this.refIcon.iconAnchor = new GPoint(6, 20);
  this.refIcon.infoWindowAnchor = new GPoint(5, 1);

	this.map = map;
	this.geocoder = new GClientGeocoder();
}

// TODO: make into a utility function
var precision = function(num, p){
	var mult = Math.pow(10 , p) * 1.0;	
	return (Math.round(num * mult)) / mult;
}

ReferencePointManager.prototype.clearPoints = function(){
	// "Function to draw a reference point such as a geocoded address or place name."
	this.clearMarkers(this.refArray);
}

ReferencePointManager.prototype.plotPoint = function(lat, lng, name){
	// rounded to 4 decimal places for display
	var pname; 
	if (name) pname = name;
	else pname = "Lat: " + precision(lat, 4) + ", Lng: " + precision(lng, 4); 
	var point = new GLatLng(lat, lng);
	if(!point){
	   alert("Location not found");
	}
	else{
		var marker = this.createReferenceMarker(point, pname);
	  this.refArray.push(marker);
	  this.map.addOverlay(marker);
		this.map.setCenter(point, this.map.getZoom() + 3);
	}
}

ReferencePointManager.prototype.clearMarkers = function(markerArray){
	// "Removes all vertices (GMarker objects) from the map."
	for(i=0; i< markerArray.length; i++){
	   this.map.removeOverlay(markerArray[i]);
	}
	markerArray.clear(); // prototype.js Array extension method
}


ReferencePointManager.prototype.createReferenceMarker = function(point, name){
	var self = this;
	var marker = new GMarker(point, {icon: this.refIcon, title: name});
	marker.mType = "reference";
	GEvent.addListener(marker, "click", function(){
		var pt = marker.getPoint();
		self.map.panTo(pt);
	});
	return marker;
}

ReferencePointManager.prototype.geocode = function(address){
	var adder = AddAddressClosure.init(this.map, this.refIcon);
	
	if(this.geocoder){
		this.geocoder.getLocations(address, adder.addAddressToMap);
	}
	else alert("Geocoder is not initialized.");
}	



AddAddressClosure = function(){
	var map;
	var refIcon;
	
	function addAddressToMap(response){
		if (!response || response.Status.code != 200) {
			alert("\"" + address + "\" not found");
		} 
		else {
		  var place = response.Placemark[0];
		  var point = new GLatLng(place.Point.coordinates[1],
		                      		place.Point.coordinates[0]);

		  var marker = new GMarker(point, {icon: refIcon, title: place.address });
			var iw_html = h4( place.address) + p("Lat: " + precision(place.Point.coordinates[1], 4) + ", Lng: " + precision(place.Point.coordinates[0], 4));


			marker.bindInfoWindowHtml(iw_html);

			GEvent.addListener(marker, "click", function(){
				var iw = $("iw_overlay"); if(iw) iw.remove();
			});

			map.addOverlay(marker);
			map.setCenter(point, 12);
		}
	}
	
	function init(mapObj, icon){
		map = mapObj;
		refIcon = icon;
		return {addAddressToMap: addAddressToMap};
	}
	
	return {init: init};
}();






// TOF.ConsRegistry.ReferencePointManager = function(){
//     var refArray = [];
//     var refIcon = new GIcon()
//         refIcon.image = "http://labs.google.com/ridefinder/images/mm_20_purple.png";
//         refIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
//         refIcon.iconSize = new GSize(12, 20);
//         refIcon.shadowSize = new GSize(22, 20);
//         refIcon.iconAnchor = new GPoint(6, 20);
//         refIcon.infoWindowAnchor = new GPoint(5, 1);;
//     var map;
//     var geocoder;
// 
//     function precision(num, p){
// 			var mult = Math.pow(10 , p) * 1.0;
// 			return (Math.round(num * mult)) / mult
//     }
// 
//     function clearPoints(){
// 			// "Function to draw a reference point such as a geocoded address or place name."
// 			clearMarkers(refArray);
//     }
// 
//     function plotPoint(lat, lng, name){
// 			// rounded to 4 decimal places for display
// 			var pname; 
// 			if (name) pname = name;
// 			else pname = "Lat: " + precision(lat, 4) + ", Lng: " + precision(lng, 4); 
// 			var point = new GLatLng(lat, lng);
// 			if(!point){
// 			    alert("Location not found");
// 			}
// 			else{
// 			    var marker = createReferenceMarker(point, pname);
// 			    refArray.push(marker);
// 			    map.addOverlay(marker);
// 				// 	    map.panTo(point);
// 				// map.zoomIn();
// 				// map.zoomIn();
// 				map.setCenter(point, map.getZoom() + 3);
// 			}
//     }
// 
//     function clearMarkers(markerArray){
// 			// "Removes all vertices (GMarker objects) from the map."
// 			for(i=0; i< markerArray.length; i++){
// 			    map.removeOverlay(markerArray[i]);
// 			}
// 			markerArray.clear(); // prototype.js Array extension method
//     }
// 
// 
//     function createReferenceMarker(point, name){
// 			var marker = new GMarker(point, {icon: refIcon, title: name});
// 			marker.mType = "reference";
// 			GEvent.addListener(marker, "click", function(){
// 				var pt = marker.getPoint();
// 				map.panTo(pt);
// 			    });
// 			return marker;
//     }
// 
// 	function geocode(address){
// 		if(geocoder){
// 			geocoder.getLocations(address, addAddressToMap);
// 		}
// 		else alert("Geocoder is not initialized.");
// 	}	
// 	
// 	
// 	function addAddressToMap(response) {
// 		
// 		if (!response || response.Status.code != 200) {
// 		  alert("\"" + address + "\" not found");
// 		} 
// 	  	else {
// 		    var place = response.Placemark[0];
// 		    var point = new GLatLng(place.Point.coordinates[1],
// 		                        place.Point.coordinates[0]);
// 		
// 		    var marker = new GMarker(point, {icon: refIcon, title: place.address });
// 			    // console.log(response.Placemark);
// 				marker.bindInfoWindowHtml(h4( place.address) + p("Lat: " + precision(place.Point.coordinates[1], 4) + ", Lng: " + precision(place.Point.coordinates[0], 4)));
// 				GEvent.addListener(marker, "click", function(){var iw = $("iw_overlay"); if(iw) iw.remove();});
// 				map.addOverlay(marker);
// 				map.setCenter(point, 12);
// 	  	}
// 	}
// 		
// 
// 	function init(mapObj){
// 		map = mapObj;
// 		geocoder = new GClientGeocoder();
// 		return {plotPoint:plotPoint, clearPoints:clearPoints, geocode:geocode};
// 	}
// 	return {init:init};
// }();
