
// -----------------------------------------------------------------------
// C L A S S   DKGoogleMap
// -----------------------------------------------------------------------

function DKGoogleMap() {
	this.debugging = false;
	this.map = null;
	this.geocoder = null; //Geocoding Obj
	this.gdir = null; //Routing Obj
	this.gdir_language = null;
	this.apiKeyMap = new Object();
	this.markerDragEndCallback = null;
}
DKGoogleMap.prototype.debugMsg = function(message) {
	if (this.debugging == true) {
		alert(message);
	}
}
DKGoogleMap.prototype.registerApiKey = function(domain, apiKey) {
	this.apiKeyMap[domain] = apiKey;
}
DKGoogleMap.prototype.init = function() {
	var loc = document.location.host;
	if (this.apiKeyMap[loc]) {
		document.write('<script type=text/javascript src="http://maps.google.com/maps?file=api&amp;v=2.84&amp;key=' + this.apiKeyMap[loc] + '"><\/script>\n');
	} else {
		alert('error: no api key found for domain ' + loc);
	}
}
DKGoogleMap.prototype.setLocationMarkerDragEndCallback = function(cb) {
	this.markerDragEndCallback = cb;
}
DKGoogleMap.prototype.loadMap = function(mapContainerId, centerOnLat, centerOnLng, centerOnZoomLevel) {
	if (GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById(mapContainerId));
		this.geocoder = new GClientGeocoder();
		centerOnZoomLevel = parseInt(centerOnZoomLevel);
		this.map.setCenter(new GLatLng(parseFloat(centerOnLat), parseFloat(centerOnLng)), centerOnZoomLevel);
		window.onunload = function() { GUnload(); }
	}
}
DKGoogleMap.prototype.initDirections = function(language, directionPanelId) {
	if (this.map == null) {
		alert('Map must be initialized before using "DKGoogleMap.initDirections(...)"!\nUse: DKGoogleMap.loadMap(...);');
		return;
	}
	if (this.gdir == null) {
		this.gdir = new GDirections(this.map, (directionPanelId!=undefined ? document.getElementById(directionPanelId) : null));
		GEvent.bind(this.gdir,"load",this,this.handleGDirOnLoad);
		if (language == undefined) {
			language = 'en';
		}
		this.gdir_language = language;
		if (this.gdir_language.toLowerCase() == 'de') {
			GEvent.bind(this.gdir,"error",this,this.handleGDirErrorsDE);			
		} else {
			GEvent.bind(this.gdir,"error",this,this.handleGDirErrorsEN);
		}
	}
}
DKGoogleMap.prototype.loadDirectionStrings = function(fromAddress, toAddress) {
	if (this.gdir != null) {
		this.map.clearOverlays();
		this.gdir.load("from: "+fromAddress+" to: "+toAddress,{"locale":this.gdir_language});
	}
}
DKGoogleMap.prototype.loadDirectionFromWaypoints = function(startPoint, endPoint, betweenPointsArr) {
	// Graz: Lat: 47.07412615053965, Lng: 15.439953804016113     -> startPoint = 'Graz@47.07412615053965,15.439953804016113';
	// Wien: Lat: 48.209206, Lng: 16.372778
	// Saalfelden: Lat: 47.424690305619905, Lng: 12.856879234313965
	if (this.gdir != null) {
		if (betweenPointsArr == undefined) {
			betweenPointsArr = [];
		}
		betweenPointsArr.unshift(startPoint);
		betweenPointsArr.push(endPoint);
		if (betweenPointsArr.length >= 2) { //at least 2 points are necessary
			this.map.clearOverlays();
			this.gdir.loadFromWaypoints(betweenPointsArr,{"locale":this.gdir_language});
			//DEMO call: this.gdir.loadFromWaypoints(['Graz@47.07412615053965,15.439953804016113','Saalfelden@47.424690305619905,12.856879234313965','Wien@48.209206,16.372778'],{"locale":this.gdir_language});
		} else {
			alert('Error: Cannot calculate route with ' + betweenPointsArr.length + ' locations.\nAt least 2 locations are necessary!');
		}
	}
}
DKGoogleMap.prototype.handleGDirOnLoad = function() {
	this.map.addOverlay(this.gdir.getPolyline());
}
DKGoogleMap.prototype.handleGDirErrorsEN = function() {
	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.");
	return;
	if (this.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: " + this.gdir.getStatus().code);
	} else if (this.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: " + this.gdir.getStatus().code);
	} else if (this.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: " + this.gdir.getStatus().code);
	} else if (this.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: " + this.gdir.getStatus().code);
	} else if (this.gdir.getStatus().code == G_GEO_BAD_REQUEST) {
		alert("A directions request could not be successfully parsed.\n Error code: " + this.gdir.getStatus().code);
	} else {
		alert("An unknown error occurred.\n Error code: " + this.gdir.getStatus().code);
	}
}
DKGoogleMap.prototype.handleGDirErrorsDE = function() {
	alert("Die gesuchte Adresse konnte leider nicht gefunden werden. Vielleicht handelt es sich um eine neue Adresse, die noch nicht von unserem System erfasst wurde.");
}
DKGoogleMap.prototype.hideMapCopyright = function() {
	if (this.map != null) {
		var element = this.map.getContainer();
		if (element != undefined) {
			var fiLstEL = element.firstChild;
			if (fiLstEL != undefined) {
				var secondEL = fiLstEL.nextSibling;
				if (secondEL != undefined) {
					secondEL.style.display = 'none';
				}
			}
		}
	}
}
DKGoogleMap.prototype.loadLargeMapControl = function() {
	if (this.map != null) {
		this.map.addControl(new GLargeMapControl());
	}
}
DKGoogleMap.prototype.loadSmallMapControl = function() {
	if (this.map != null) {
		this.map.addControl(new GSmallMapControl());
	}
}
DKGoogleMap.prototype.loadSmallZoomControl = function() {
	if (this.map != null) {
		this.map.addControl(new GSmallZoomControl());
	}
}
DKGoogleMap.prototype.loadMapTypeControl = function() {
	if (this.map != null) {
		this.map.addControl(new GMapTypeControl());
	}
}
DKGoogleMap.prototype.loadScaleControl = function() {
	if (this.map != null) {
		this.map.addControl(new GScaleControl());
	}
}
DKGoogleMap.prototype.loadOverviewControl = function() {
	if (this.map != null) {
		this.map.addControl(new GOverviewMapControl());
	}
}
DKGoogleMap.prototype.loadControl = function(loadLargeMap, loadSmallMap, loadMapType, loadSmallZoom, loadScale, loadOverview) {
	if (loadLargeMap) this.loadLargeMapControl();
	if (loadSmallMap) this.loadSmallMapControl();
	if (loadMapType) this.loadMapTypeControl();
	if (loadSmallZoom) this.loadSmallZoomControl();
	if (loadScale) this.loadScaleControl();
	if (loadOverview) this.loadOverviewControl();
}
/*
DKGoogleMap.prototype.geocodeAndSetLocationMarkerByFieldId = function(geocodeSearchStrFieldId) {
	var searchStrField = document.getElementById(geocodeSearchStrFieldId);
	if (!searchStrField) { return alert('error: no search str field found with id "' + geocodeSearchStrFieldId + '"'); }
	if (!this.geocoder) { return alert('error: geocoder object is not loaded'); }
	var obj = this;
	
	this.geocoder.getLatLng(searchStrField.value,
    	function(point) {
      		if (!point) {
        		alert(searchStrField.value + " not found.");
      		} else {
      			obj.addLocationMarker(point);
			}
		}
	);
}
*/
// -----------------------------------------------------------------------