
function MMNetSelector(){	
	this.map;
	
	// polyline, marker, etc...
	this.selectedOverlays = new Array();
	
	this.infoLoadingProcess = new MMHTMLTooltip("Daten werden geladen...");
	
	//stored to demark segment on repeated click
	this.lastSelectedSegmentID = 0;	
}
MMNetSelector.prototype = new GOverlay();

/**
 * Returns the Trackobject ID of the last Selection
 */
MMNetSelector.prototype.GetCurrentSelection = function()
{
	return this.lastSelectedSegmentID;
}

/**
 * Called by the map after the overlay is added to the map using GMap2.addOverlay(). The overlay object 
 * can draw itself into the different panes of the map that can be obtained using GMap2.getPane().
 */
MMNetSelector.prototype.initialize = function(map){
	this.map = map;
	this.map.addControl(this.infoLoadingProcess);
}


/**
 * Called by the map after the overlay is removed from the map using GMap2.removeOverlay() or GMap2.clearOverlays(). 
 * The overlay must remove itself from the map panes here.
 */
MMNetSelector.prototype.remove = function(){
	this.map = null;	
	
	if(this.infoLoadingProcess)this.map.removeControl(this.infoLoadingProcess);
	this.Clear();
}

/**
 * Called by the map when the map display has changed. The argument force will be true if the zoom level or 
 * the pixel offset of the map view has changed, so that the pixel coordinates need to be recomputed.
 */
MMNetSelector.prototype.redraw = function(force){ }

/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMNetSelector.prototype.copy = function(map){ return new MMNetSelector(); }


/**
 * Removes all Overlays (polylines, markers, ...)
 */
MMNetSelector.prototype.Clear = function()
{
	// cancel any running request
	if(this.currentRequest)this.currentRequest.transport.abort();
	this.infoLoadingProcess.hide();
	
	for(var i=0; i<this.selectedOverlays.length; i++)
	{
		this.map.removeOverlay(this.selectedOverlays[i]);
	}
	this.selectedOverlays = new Array();
}

/**
 * Läd asynchron einen Trackobject-Pfad und übergibt ihn der Callbackfunktion als Parameter vom Typ GPolyline.
 */
MMNetSelector.LoadPolyline = function(trackobjectID, callback)
{
	var url = MMRegistry.urlPortal +"commands.php?cmd=jsonPolyline&id="+ trackobjectID;	
	
	new Ajax.Request(
		url,
		{
			method: 'get',	
			onSuccess: function(transport)
			{					
				var json = transport.responseText.evalJSON();
				var polyline = GPolyline.fromEncoded(json);
				callback.call(this, polyline);
			
			}.bind(this)
		});
}


/**
 * Selects nearest Segment, displays start/endnode and neighbour segments
 */
MMNetSelector.prototype.SelectSegmentByLatLng = function(point)
{
	// cancel any running request
	if(this.currentRequest)this.currentRequest.transport.abort();
	
	var url = MMRegistry.urlPortal +"commands.php?cmd=jsonSegmentByLatLng&lat="+ point.lat() +"&lng="+ point.lng();		
	this.infoLoadingProcess.show({x:60,y:17});
	this.currentRequest = new Ajax.Request(
		url,
		{
			method: 'get',	
			onFailure: function(transport){			
				// finish async loading
				this.currentRequest = null;
				this.infoLoadingProcess.hide();
			},
			onSuccess: function(transport)
			{					
				var json = transport.responseText.evalJSON();
				
				if(this.lastSelectedSegmentID != json.segment.id)
				{
					this.lastSelectedSegmentID = json.segment.id;

					// segment
					var polyline = GPolyline.fromEncoded(json.segment.polyline);
					this.selectedOverlays.push(polyline);
					if(this.map)this.map.addOverlay(polyline);
					
					// startneighbours
					for(var i=0; i<json.startpoint.NeighbourSegments.length; i++)
					{
						var polyline = GPolyline.fromEncoded(json.startpoint.NeighbourSegments[i]);
						this.selectedOverlays.push(polyline);
						if(this.map)this.map.addOverlay(polyline);
					}
					
					// endneighbours
					for(var i=0; i<json.endpoint.NeighbourSegments.length; i++)
					{
						var polyline = GPolyline.fromEncoded(json.endpoint.NeighbourSegments[i]);
						this.selectedOverlays.push(polyline);
						if(this.map)this.map.addOverlay(polyline);
					}
					

					var nodeIconUrl = "dateien/marker/station.png";
					var nodeIcon = new GIcon({image: nodeIconUrl});
					nodeIcon.iconSize = new GSize(11,11);
					nodeIcon.iconAnchor = new GPoint(5,5);	
					nodeIcon.infoWindowAnchor =  new GPoint(5,5);
					nodeIcon.shadow = null;

					// startnode
					var startmarker = new GMarker(new GLatLng(json.startpoint.lat, json.startpoint.lng), {icon: nodeIcon });	
					this.selectedOverlays.push(startmarker);
					if(this.map)this.map.addOverlay(startmarker);
					
					// endnode
					var endmarker = new GMarker(new GLatLng(json.endpoint.lat, json.endpoint.lng), {icon: nodeIcon });	
					this.selectedOverlays.push(endmarker);
					if(this.map)this.map.addOverlay(endmarker);
					
					
					startmarker.bindInfoWindowHtml('<div style:"text-align:middle">'
						+'<iframe height="130px" frameborder="0" scrolling="no" src="'+ MMRegistry.urlPortal +'commands.php?cmd=editTrackpointSpecial&id='+ json.startpoint.id +'"></iframe>'
					+'</div>');
					
					endmarker.bindInfoWindowHtml('<div>'
						+'<iframe height="130px" frameborder="0" scrolling="no" src="'+ MMRegistry.urlPortal +'commands.php?cmd=editTrackpointSpecial&id='+ json.endpoint.id +'"></iframe>'
					+'</div>');
					
				}
				else
				{
					this.lastSelectedSegmentID = 0;
				}
				
				// finish async loading
				this.currentRequest = null;
				this.infoLoadingProcess.hide();
			
			}.bind(this)
		});
}
