/**
 * @params minZoom: Ab diesem Zoomlevel werden die Icons geladen/angezeigt
 * @params bool openBettBikeLinkInParentWindow
 */
function MMBettBikeOverlay(minZoom, openBettBikeLinkInParentWindow)
{	
	this.minZoom = minZoom;
	this.map = null;
	this.currentRequest = null;
	this.markers = new Array();
	this.openBettBikeLinkInParentWindow = openBettBikeLinkInParentWindow;
	this.showKnr = false;
	
	this.events = new Array();
}
MMBettBikeOverlay.prototype = new GOverlay();


MMBettBikeOverlay.prototype.ShowInfoWindow = function(knr){
	this.showKnr = knr;
}

MMBettBikeOverlay.prototype.initialize = function(map){
	this.map = map;	
	this.events.push( GEvent.bind(this.map, 'moveend', this, this.LoadMarkers) );
	
	this.LoadMarkers();	
}


/**
 * 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.
 */
MMBettBikeOverlay.prototype.remove = function(){
	this.RemoveAllMarkers();
	
	for(var i=0; i<this.events.length; i++)
	{
		GEvent.removeListener(this.events[i]);
	}	
	this.events = new Array();
	
	this.map = null;	
}


MMBettBikeOverlay.tileCache = 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.
 */
MMBettBikeOverlay.prototype.redraw = function(force){ }

/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMBettBikeOverlay.prototype.copy = function(map){ return new MMBettBikeOverlay(); }


MMBettBikeOverlay.prototype.LoadMarkers = function()
{
	if(this.map.getZoom() >= this.minZoom)
	{
		var bounds = this.map.getBounds();
		
		var url = MMRegistry.urlPortal +"poi.php";
		var para = "?cmd=bettbike&bbox="+ bounds.getSouthWest().lat() +","+ bounds.getSouthWest().lng() +","+ bounds.getNorthEast().lat() +","+ bounds.getNorthEast().lng();
					
		this.currentRequest = new Ajax.Request(
				url,
				{
					method: 'get',	
					parameters: para,			
					onSuccess: function(transport)
					{	
						try
						{					
							var json = transport.responseText.evalJSON();						
							this.RemoveAllMarkers();						
							this.DrawJsonMarkers(json);
						}
						catch(e)
						{
							//alert(e);
						}
					}.bind(this)
				});
	}
	else
	{
		this.RemoveAllMarkers();
	}
}


MMBettBikeOverlay.prototype.RemoveAllMarkers = function()
{
	if(this.currentRequest)this.currentRequest.transport.abort();
	for(var i=0; i<this.markers.length; i++)
	{
		this.map.removeOverlay(this.markers[i]);
	}	
	this.markers = new Array();
}

MMBettBikeOverlay.prototype.DrawJsonMarkers = function(json)
{		
	if(json.pois)
	{
		for(var i=0; i<json.pois.length; i++)
		{
			var poi = json.pois[i];
			this.InitializeMarker(poi);			
		}
	}
}

MMBettBikeOverlay.prototype.InitializeMarker = function(poi){
	
	var marker = new GMarker(new GLatLng(poi.lat, poi.lng), {icon: this.GetIcon() });
	this.markers.push(marker);
	this.map.addOverlay(marker);
			
	GEvent.bind(marker, 'click', this, function(){ 			
		
		// show only at start
		this.showKnr = false;
				
		var html = '<div style="font-size:10pt;font-family:arial, sans-serif;">'		
		html += '<table border="0" cellpadding="0", cellspacing="0">'
		html += '<tr>'
		html +=   '<td colspan="2" valign="top"><span style="font-weight:700;font-size:medium;">'+ poi.name +'</span><br>';
		html += '</tr>'
		
		html += '<tr>'
		html += '<td>'
		html +=   '<span>';		
		
		
		if(poi.sterne > 0) html += '<img src="'+ MMRegistry.urlPortal +'dateien/mapicons/sterne'+ poi.sterne +'.gif">&nbsp;&nbsp;';
		/*
		if(this.openBettBikeLinkInParentWindow)
		{
			html += '<a target="_parent" href="'+ MMRegistry.urlBettBike +'bub/viewcompany.php?knr='+ poi.knr +'" style="font-size:11px;color:#7777cc;">mehr Infos&nbsp;&raquo;</a>';
		}
		else
		{
		*/
			html += '<a target="_blank" href="'+ MMRegistry.urlBettBike +'bub/viewcompany.php?knr='+ poi.knr +'" style="font-size:11px;color:#7777cc;">mehr Infos&nbsp;&raquo;</a>';
		//}
		
		html += '</span><br><br>';	
			
		
		html += '<table border="0">';
		html += '<tr><td>'+ poi.str +'</td></tr>';		
		html += '<tr><td>'+ poi.plz +' '+ poi.ort +'</td></tr>';		
		
		if(poi.vorwahl && poi.tel) html += '<tr><td>Tel:&nbsp;'+ poi.vorwahl +" / "+ poi.tel +'</td></tr>';
		else html += '<tr><td>&nbsp;</td></tr>';
		
		if(poi.internet) html += '<tr><td><a style="color:#008000;" target="_blank" href="http://'+ poi.internet +'">'+ poi.internet +'</a></td></tr>';				
		else html += '<tr><td>&nbsp;</td></tr>';
		
		html += '</table>';
		
		html += '</td>';
		
		html += '<td align="center">';
		html +=     '<br>&nbsp;';
		if(poi.bild){
			html +=     '<img width="90" height="90" src="'+ MMRegistry.urlBettBike +'bub_images/'+ poi.buland +'/size3/'+ poi.bild +'">';
		}
		html += '</td>';
		
		html += '</tr>';
		html += '</table>';
		html += '</div>';
		
		this.map.openInfoWindowHtml(new GLatLng(poi.lat, poi.lng), html);		
	});
	
	// show detailwindow if knr is set
	if(this.showKnr && poi.knr && poi.knr.toUpperCase() == this.showKnr.toUpperCase())	GEvent.trigger(marker, 'click');
}

MMBettBikeOverlay.Icon = null;
MMBettBikeOverlay.prototype.GetIcon = function(type)
{
	if (!MMBettBikeOverlay.Icon)
	{
		var icon = new GIcon({image: "dateien/marker/bett.png"});
		icon.iconSize = new GSize(35, 35);
		icon.iconAnchor = new GPoint(16, 13);
		 //icon.shadow = "dateien/marker/bett_shadow.png";
		 //icon.shadowSize = new GSize(59,32);
		 //icon.infoWindowAnchor =  new GPoint(16,15);
		
		MMBettBikeOverlay.Icon = icon;
	}
      
	return MMBettBikeOverlay.Icon;
}
