// Note the default parameters
function cDestination(inMap, inName, inAddress, inCity, inLongitude, inLatitude, inNotes, inMapIconID) {
	var inDatabaseID = defaultParameter(arguments, 9, 0);

	this.ctor = function(inMap, inName, inAddress, inCity, inLongitude, inLatitude, inNotes, inDatabaseID, inMapIconID) {
		this.mMap = inMap;
		this.mName = inName;
		this.mAddress = inAddress;
		this.mCity = inCity;
		this.mLongitude = inLongitude;
		this.mLatitude = inLatitude;
		this.mNotes = inNotes;
		this.mLatLng = new GLatLng(inLatitude, inLongitude);
		this.mMarker = null;
		this.mShowing = false;
		this.mDatabaseID = inDatabaseID;
		this.mIcon = null;
		var ctIcon = getIconFromIconsArray(inMapIconID);
		this.mBaseIcon = new GIcon();
		this.mBaseIcon.iconSize = new GSize(32, 32);
		this.mBaseIcon.image = 'http://' + serverName + '/images/mapicons/' + ctIcon.mFilename;
		this.mBaseIcon.iconAnchor = (ctIcon.mAnchorAtCenter ? new GPoint(15, 15) : new GPoint(15, 31));
		this.mBaseIcon.infoWindowAnchor = new GPoint(15, 0);

		this.mMarkerListener = null;
		this.mOpenListener = null;
		this.mCloseListener = null;
		this.mClickListener = null;
	
		this.mHTML =
			'<p>' + inName + '</p>' +
			'<p>' + inAddress + '</p>' +
			'<p>' + inCity + '</p>' +
			'<p>' + inNotes + '</p>';
	
		this.resetMarkerIcon();
	}
	
	this.listen = function(callbackFunction) {
		var listen = defaultParameter(arguments, 2, true);
		
		if(listen) {
			this.mMarkerListener = GEvent.addListener(this.mMarker, "dblclick", callbackFunction);
		} else {
			GEvent.removeListener(this.mMarkerListener);
			this.mListener = null;
		}
	}
	
	this.resetMarkerIcon = function() {
		if(this.mMarker != null) {
			if(this.mMap != null)
				this.mMap.removeOverlay(this.mMarker);
				
			this.mMarker = null;
		}
		
		this.mMarker = new GMarker(this.mLatLng);
		this.mMarker.bindInfoWindowHtml(this.mHTML);
		this.showMarker(this.mShowing);
	}
	
	// The destinations array can be created before the map exists. Allow other
	// parts of the script to tell us about the map.
	this.setMap = function(inMap) {
		this.mMap = inMap;
	}
	
	this.showInfo = function() {
		this.mMarker.openInfoWindowHtml(this.mHTML);
	}
	
	this.showMarker = function() {
		var show = defaultParameter(arguments, 1, true);
		var iconInfo = defaultParameter(arguments, 2, this.mBaseIcon);
		var infoWindowOpenHandler = defaultParameter(arguments, 3, null);
		var infoWindowCloseHandler = defaultParameter(arguments, 4, null);
		var clickHandler = defaultParameter(arguments, 5, null);
		
		if(iconInfo != null) {
			this.mIcon = iconInfo;
			
			if(this.mMarker != null) {
				if(this.mMap != null)
					this.mMap.removeOverlay(this.mMarker);
					
				this.mMarker = null;
			}

			var markerOptions = {icon : iconInfo};
			this.mMarker = new GMarker(this.mLatLng, markerOptions);
			this.mMarker.bindInfoWindowHtml(this.mHTML);
		}

		if(show) {
			if(this.mMap != null) {
				if(this.mMarker == null) {
					this.mMarker = new GMarker(this.mLatLng);
					this.mMarker.bindInfoWindowHtml(this.mHTML);
				}
					
				this.mMap.addOverlay(this.mMarker);
			
				// Don't really remember why I did this. I think it's for the end-user page, where we
				// change the default icon to a start or finish flag. I think that I wanted to make sure
				// that the user gets to see the destination info. Using iconInfo != null is a terrible
				// way to decide. Should be if(we're here due to user click) or something like that.
				if(iconInfo != this.mBaseIcon) {
					this.mMarker.openInfoWindowHtml(this.mHTML);
				}

				if(infoWindowCloseHandler != null)
					this.mOpenListener = GEvent.addListener(this.mMarker, "infowindowopen", infoWindowOpenHandler);

				if(infoWindowCloseHandler != null)
					this.mCloseListener = GEvent.addListener(this.mMarker, "infowindowclose", infoWindowCloseHandler);

				if(clickHandler != null) {
					this.mClickListener = GEvent.addListener(this.mMarker, "dblclick", clickHandler);
				}
			}
		} else {
			if(this.mMarker != null) {
				if(this.mMap != null) {
					this.mMap.removeOverlay(this.mMarker);
				}

				if(this.mOpenListener != null) {
					GEvent.removeListener(this.mOpenListener);
				}
				
				if(this.mCloseListener != null) {
					GEvent.removeListener(this.mCloseListener);
				}

				if(this.mClickListener != null) {
					GEvent.removeListener(this.mClickListener);
				}

				this.mMarker = null;
			}
		}
			
		this.mShowing = show;
	}
	
	this.ctor(inMap, inName, inAddress, inCity, inLongitude, inLatitude, inNotes, inDatabaseID, inMapIconID);
}

