/**
 * @params minZoom: Ab diesem Zoomlevel werden die Icons geladen/angezeigt
 * @params bool openBettBikeLinkInParentWindow
 */
function MMPanoramioOverlay(minZoom)
{	
	this.minZoom = minZoom;
	this.map = null;
	this.currentRequest = null;
	
	this.markers = new Array();	
	this.events = new Array();
}
MMPanoramioOverlay.prototype = new GOverlay();


MMPanoramioOverlay.prototype.initialize = function(map){
	this.map = map;	
	
	this.events.push( GEvent.bind(this.map, 'moveend', this, this.LoadMarkers) );
	
	this.events.push( GEvent.bind(MMPanoramioOverlay, 'triggerPmio', this, function(){	
		this.RemoveAllMarkers();
		this.DrawMarkers();	
	}));
	
	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.
 */
MMPanoramioOverlay.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;	
}


MMPanoramioOverlay.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.
 */
MMPanoramioOverlay.prototype.redraw = function(force){ }

/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMPanoramioOverlay.prototype.copy = function(map){ return new MMPanoramioOverlay(); }


MMPanoramioOverlay.prototype.LoadMarkers = function()
{

	if(this.map.getZoom() >= this.minZoom)
	{
		var bounds = this.map.getBounds();

		var url = MMRegistry.urlPortal +"poi.php";
		var url ="http://www.panoramio.com/map/get_panoramas.php?";
		url += "miny="+ bounds.getSouthWest().lat();
		url += "&minx="+ bounds.getSouthWest().lng();
		url += "&maxy="+ bounds.getNorthEast().lat();
		url += "&maxx="+ bounds.getNorthEast().lng();
		url += "&order=popularity&set=public&callback=pmiohandler&from=0&to=15&size=small";

		var script = document.createElement("script");
		script.setAttribute("src",url);
		document.getElementsByTagName('head')[0].appendChild(script);
	}
	else
	{
		this.RemoveAllMarkers();
	}
}


MMPanoramioOverlay.prototype.RemoveAllMarkers = function()
{

	for(var i=0; i<this.markers.length; i++)
	{
		this.map.removeOverlay(this.markers[i]);
	}	
	this.markers = new Array();
}

MMPanoramioOverlay.prototype.DrawMarkers = function()
{		
	if(panoramioAnswer.photos)
	{
		for(var i=0; i<panoramioAnswer.photos.length; i++)
		{
			var pic = panoramioAnswer.photos[i];
			this.InitializeMarker(pic);
		}
	}
}

MMPanoramioOverlay.prototype.InitializeMarker = function(pic){
	
	var marker = new GMarker(new GLatLng(pic.latitude, pic.longitude), {icon: this.GetIcon() });
	this.markers.push(marker);
	this.map.addOverlay(marker);
			
	GEvent.bind(marker, 'click', this, function(){
		
		var html = '<div style="width:'+ pic.width +'px">';		
		html += '<table border="0">';		
		html += '<tr>';
		html += 	'<td>';		
		html += 		'<a href="http://www.panoramio.com/" target="_blank">';
		html += 		'<img src="'+MMRegistry.urlPortal+'dateien/logo-panoramio.gif" alt="Panoramio logo" width="119" height="25" border="0">';
		html += 		' </a>';
		html += 	'</td>';
		html +=	'</tr>';

		html += '<tr>';
		html += 	'<td>';
		html += 		'<font style="font-weight:700;font-size:medium;">'+ pic.photo_title +'</font>';
		html += 		'<br>';
		html += 		'<a id="photo_infowin" target="_blank" href="'+pic.photo_url+'">';
		html += 		'<img src="'+pic.photo_file_url+'" width="'+ pic.width +'" height="'+ pic.height +'"  border="0">';
		html += 		'</a>';
		html += 		'<br>';
		html += 		'von <a target="_blank" href="'+pic.owner_url+'"><font style="color:7777cc;font-size:11px;">'+pic.owner_name+'</font></a>';		
		html += 	'</td>'
		html += '</tr>';
		
		html += '</table>';
		html += '</div>';

		this.map.openInfoWindowHtml(new GLatLng(pic.latitude, pic.longitude), html);
	});
}

MMPanoramioOverlay.Icon = null;
MMPanoramioOverlay.prototype.GetIcon = function(type)
{
	if (!MMPanoramioOverlay.Icon)
	{
		var icon = new GIcon({image: "dateien/marker/panoramio-marker.png"});
		icon.iconSize = new GSize(18, 18);
		icon.iconAnchor = new GPoint(9, 9);
		
		MMPanoramioOverlay.Icon = icon;
	}
      
	return MMPanoramioOverlay.Icon;
}
