

var Mappler = {};

Mappler.MapFront = function(options) {
	
	this.classname = "Mappler.MapFront";
	
	this.DBG = true;

	/* Options */	
	this.canvasId = null;
	this.formId = null;
	this.directionsPanelId = null;
	this.onGDirectionsLoadFunc = options.onGDirectionsLoadFunc
	this.onGDirectionsErrorFunc = options.onGDirectionsErrorFunc
	this.showControls = false;

	/* Member Vars */
	this.options = {};
	this.map = null;
	this.mapType = 13;
	this.gdir = null;
	this.locale = 'de';
	
	this.init = function() {
		if(this.DBG) wtaxi.writeOut("MapFront.init");
		if(GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById(this.canvasId));
			
			if(this.showControls) {
				this.map.addControl(new GLargeMapControl());
			}
			
			if(this.options.center) {
				this.map.setCenter(
					new GLatLng(
						this.options.center.lat, 
						this.options.center.lng), 
						this.mapType);
			} else {
				this.map.setCenter(new GLatLng(48.826786, 9.300785), this.myMapType);
			}
		}
		if(this.options.optInitFunc) {
			this.options.optInitFunc(this);
		}
	}

	this.getDirections = function(from, to, over) {
		if(this.gdir == null) {
			var elDirPanel = null;
			if(this.directionsPanelId != null) {
				elDirPanel = document.getElementById(this.directionsPanelId);
			}
	        this.gdir = new GDirections(this.map, elDirPanel);
	        GEvent.addListener(this.gdir, "load", Mappler.onGDirectionsLoad);
	        GEvent.addListener(this.gdir, "error", Mappler.onGDirectionsError);
	        this.gdir._mapfront = this;
		}
		if(over == null) {
			this.gdir.load("from: " + from + " to: " + to,
		    	{ "locale": this.locale });
		} else {
			this.gdir.load("from: " + from + " to: " + over + " to: " + to,
		    	{ "locale": this.locale });
		}
	}

	this.onGDirectionsLoad = function(gdir) {
		if(this.DBG) wtaxi.writeOut("Directions loaded <div>" + gdir.getSummaryHtml() + "</div>");
		if(this.onGDirectionsLoadFunc) this.onGDirectionsLoadFunc(this);
	}
	
	this.onGDirectionsError = function(gdir) {
		if(this.DBG) wtaxi.writeOut("Directions error <div>" + gdir.getSummaryHtml() + "</div>");
		var errTxt = null;
		if(gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			errTxt = "Adresse konnte nicht gefunden werden";
			//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)
			errTxt = "Ein unbekannter Fehler ist aufgetreten ('processing 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)
			errTxt = "Interner Fehler ('q parameter missing')";
			//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_GEO_BAD_KEY)
			errTxt = "Interner Fehler ('key invalid')";
			//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)
			errTxt = "Interner Fehler ('parser error')";
			//alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		else 
			errTxt = "Unbekannter Fehler";
			//alert("An unknown error occurred.");
		if(this.onGDirectionsErrorFunc) this.onGDirectionsErrorFunc(this, errTxt, gdir.getStatus().code);
	}
	
	if(this.DBG) wtaxi.writeOut("created");
	
	if(options != undefined) {
		this.canvasId = options.canvasId;
		this.formId = options.formId;
		this.directionsPanelId = options.directionsPanelId;
		this.onGDirectionsLoadFunc = options.onGDirectionsLoadFunc;
		this.onGDirectionsErrorFunc = options.onGDirectionsErrorFunc;
		this.showControls = options.showControls;
	}
	this.options = options;
	return this;
}

Mappler.onGDirectionsLoad = function() {
	this._mapfront.onGDirectionsLoad(this);
}

Mappler.onGDirectionsError = function() {
	this._mapfront.onGDirectionsError(this);
}