janitorMap = function(pMapDomElement,pJsonUri,pInitialCenter)
{
	this.map;
	this.initialCenter = pInitialCenter;
	this.map_dom_element = pMapDomElement;
	this.marker_managers = {};
	this.polyline_managers = {};
	this.json_uri = pJsonUri;
};

janitorMap.prototype.init = function()
{	
	/**
	 * Check for Browser Compatibility
	 */
	if (GBrowserIsCompatible()) {
		
		/**
		 * Instantiate the map
		 */
		this.map = new GMap2(document.getElementById(this.map_dom_element));
		
		/**
		 * Add some controls
		 */
		var zoomControl = new GLargeMapControl(); 
		var typeControl = new GMapTypeControl();
		this.map.addControl(zoomControl);
		this.map.addControl(typeControl);

		/**
		 * Set the center
		 */
		this.map.setCenter(new GLatLng(this.initialCenter.lat,this.initialCenter.lng),this.initialCenter.zoom);
		if(this.initialCenter.marker) {
			pMarker = this._createMarker(new GLatLng(this.initialCenter.lat,this.initialCenter.lng), this.initialCenter.marker.config, this.initialCenter.marker.options);
			this.map.addOverlay(pMarker);
		}
		
	} else {
		alert('Karte konnte nicht geladen werden da der von Ihnen verwendete Browser die benötigten Funktionen nicht unterstützt.'); 
	}
};

janitorMap.prototype.showAddress = function(pAddress)
{
	this._geocodeAddress(pAddress, this._displayAddress);
};

janitorMap.prototype.saveGeocodeForAddress = function(pAddress)
{
	this._geocodeAddress(pAddress, this._saveAddress);
};

janitorMap.prototype._saveAddress = function(pResponse,pScope)
{
	alert(pResponse.Placemark[0].Point.coordinates[1]);
};

janitorMap.prototype._displayAddress = function(pResponse,pScope)
{
	if (!pResponse || pResponse.Status.code != 200) {
	    alert("\"" + pResponse.name + "\" konnte nicht gefunden werden");
	} else {
		if(pResponse.Placemark.length>1) {
			alert('Es wurden insgesamt ' + pResponse.Placemark.length + ' Koordinaten zu der Adresse "' + pResponse.name + '" gefunden.\nEs wird der erste Treffer angezeigt.');
		}
		var pPlace = pResponse.Placemark[0];
		var pPoint = new GLatLng(pPlace.Point.coordinates[1],pPlace.Point.coordinates[0]);
		var pMarker = pScope._createMarker(pPoint, {item_id: 123}, {draggable: true});
		GEvent.addListener(pMarker,'dragend',function(pPoint) {
			pScope._debug(pPoint.lat());
			pScope._debug(pPoint.lng());
		});
		GEvent.addListener(pMarker,'click',function(pPoint) {
			pScope._debug(pPoint.lat());
			pScope._debug(pPoint.lng());
			pScope._debug(pMarker.config.item_id);
		});
		pScope.map.setCenter(pPoint,12);
		pScope.map.clearOverlays();
		pScope.map.addOverlay(pMarker);
	}
};

janitorMap.prototype._geocodeAddress = function(pAddress, pCallback)
{
	var scope = this;
	var pGeocoder = new GClientGeocoder();
	pGeocoder.getLocations(pAddress,function(pResponse) {
		pCallback(pResponse,scope);
	});
};

janitorMap.prototype._reverseGeocode = function(pPoint, pCallback)
{
	var scope = this;
	var pGeocoder = new GClientGeocoder();
	pGeocoder.getLocations(pPoint,function(pResponse) {
		pCallback(pResponse,scope);
	});
};

janitorMap.prototype._createMarker = function(pPoint, pConfig, pMarkerOptions)
{
	if(pConfig.icon) {
		pMarkerOptions.icon = this._createIcon(pConfig.icon);
		delete(pConfig.icon);
	}
	var pMarker = new GMarker(pPoint, pMarkerOptions);
	pMarker.config = pConfig;
	return pMarker;
};

janitorMap.prototype._createIcon = function(pOptions)
{
	var baseIcon = new GIcon();
	if(pOptions.iconSize) {
		baseIcon.iconSize = new GSize(pOptions.iconSize.x, pOptions.iconSize.y);
	}
	if(pOptions.shadow) {
		baseIcon.shadow = pOptions.shadow;
	}
	if(pOptions.iconAnchor) {
		baseIcon.iconAnchor = new GPoint(pOptions.iconAnchor.x, pOptions.iconAnchor.y);
	} else if(pOptions.iconSize) {
		baseIcon.iconAnchor = new GPoint(pOptions.iconSize.x / 2, pOptions.iconSize.y / 2);
	}
	if(pOptions.image) {
		baseIcon.image = image; 
	}
	return baseIcon;
};

janitorMap.prototype._loadGpxFile = function(pUrl)
{
	
};

janitorMap.prototype._loadGpxFileOnSuccess = function()
{
	
};

janitorMap.prototype._loadGpxFileOnFailure = function()
{
	
};

janitorMap.prototype._parseGpxFile = function(pFile)
{
	
};

janitorMap.prototype._plotPolylines = function(pFile)
{
	
};

janitorMap.prototype._debug = function(pDebug)
{
	if(typeof console != 'undefined') {
		console.info(pDebug);
	}
};
