
/**
 * Type 0 = Startpunkt
 * Type 1 = Wegpunkt
 * Type 2 = Zielpunkt
 */
function MMWaypoint(point, type, id)
{	
	this.map = null;
	this.type = type;
	this.marker = new GMarker(point, {draggable: true, icon: this.GetFlagIcon(type) });
	this.marker.isMarker = true;
	
	this.id=id;
	
	this.listeners = new Array();	
}
MMWaypoint.prototype = new GOverlay();

MMWaypoint.prototype.initializeListeners = function(){
	
	this.listeners.push(GEvent.bind(this.marker, "dragend", this, function(){
		GEvent.trigger(this, "dragend", this);
	}));
	
	this.listeners.push(GEvent.bind(this.marker, "dragstart", this, function(){
		GEvent.trigger(this, "dragstart", this);
	}));
	
	this.listeners.push(GEvent.bind(this.marker, "mousedown", this, function(latlng){ 
		var point = this.map.fromLatLngToContainerPixel(latlng);
		GEvent.trigger(this, "rightclick", point);	
	}));
}


MMWaypoint.prototype.initialize = function(map){
	this.map = map;
	this.marker.initialize(map);
	this.initializeListeners();	
}


/**
 * 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.
 */
MMWaypoint.prototype.remove = function(){
	this.marker.remove();
	this.map = null;
		
	for(var i=0; i<this.listeners.length; i++){
		GEvent.removeListener(this.listeners[i]);
	}
	this.listeners = new Array();
}

/**
 * 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.
 */
MMWaypoint.prototype.redraw = function(force){
	this.marker.redraw(force);
}


/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMWaypoint.prototype.copy = function(map){
	return new MMWaypoint();
}


// Static Member
MMWaypoint.flagIcons = new Array();


MMWaypoint.prototype.GetFlagIcon = function(type)
{
      if (!MMWaypoint.flagIcons[type])
      {
				
		switch(type)
		{
			case 0:
				var iconUrl = "dateien/marker/flag_green.gif";
				break;
			case 1:
				var iconUrl = "dateien/marker/flag_yellow.gif";
				break;
			case 2:
				var iconUrl = "dateien/marker/flag_destination.gif";
				break;
			case 3:
				var iconUrl = "dateien/marker/station.png";
				break;
		}
		
		var icon = new GIcon({image: iconUrl});
		if(type<3) {
			icon.iconSize = new GSize(15, 29);
			icon.iconAnchor = new GPoint(1, 29);
			icon.shadow =  "dateien/marker/flag_shadow.png";
			icon.shadowSize = new GSize(44,29);
		} else if(type==3){
			icon.iconSize = new GSize(11,11);
			icon.iconAnchor = new GPoint(5,5);	
			icon.shadow = null;
		} 
        MMWaypoint.flagIcons[type] = icon;
      }
      
      return MMWaypoint.flagIcons[type];
}



/**
 * Returns the geographical coordinates at which this marker is anchored
 */
MMWaypoint.prototype.GetPoint = function()
{
	return this.marker.getPoint();
}


/**
 * Gets the flag type
 * Type 0 = Startpunkt
 * Type 1 = Wegpunkt
 * Type 2 = Zielpunkt
 */
MMWaypoint.prototype.GetType = function(){
	return this.type;	
}

/**
 * Sets the flag type
 * Type 0 = Startpunkt
 * Type 1 = Wegpunkt
 * Type 2 = Zielpunkt
 */
MMWaypoint.prototype.SetType = function(type)
{
	this.type = type;
	
	this.marker.remove();
	for(var i=0; i<this.listeners.length; i++){
		GEvent.removeListener(this.listeners[i]);
	}
	this.listeners = new Array();
	
	this.marker = new GMarker(this.marker.getPoint(), {draggable: true, icon: this.GetFlagIcon(type) });
	this.initializeListeners();
	
	this.marker.initialize(this.map);
	
	// to signal, that the flagtype has changed
	GEvent.trigger(this, "typeChanged");
}

