/*	MarkerManagerLite

	Stuff to do:
	an unload method to free all refs ?
	function closure on clusterMarker arguments - to check
	make clusterMarker event and icon customisable
	addMarkers to add markers instead of replacing any existing markers: 	this._markers.concat(this._markers, _markers);
	check for memory leaks - remove event handles
	finally optimise for speed
*/

MarkerManager=function(map) {
	this._map=map;
	this._markers=[];
	this._markers_name=[];
	this._clusterMarkers=[];
	this._events=[];
	this._bounds=[];
	GEvent.bind(this._map, "load", this, this.refresh);
	GEvent.bind(this._map, "moveend", this, this.refresh);
	GEvent.bind(this._map, "zoomend", this, this.refresh);
};

MarkerManager.prototype.addMarkers=function(_markers) {
	for (var i=0; i<_markers.length; i++) {
		_markers[i]._isVisible=false;
		
	}
	this._markers=_markers;
	this.refresh();
	
};

MarkerManager.prototype.clearMarkers=function() {
	this._markers=[];
	this._clusterMarkers=[];
	this._events=[];
	this._bounds=[];
	this._map.clearOverlays();
};

MarkerManager.prototype.refresh=function() {
	var i, j;
	for (i=0; i<this._clusterMarkers.length; i++) {
		this._map.removeOverlay(this._clusterMarkers[i]);
	}
	this._clusterMarkers=[];
	this._containedMarkers();
	if (this._map.getZoom()<this._map.getCurrentMapType().getMaximumResolution() && this._markers.length>0) {
		this._nonIntersectingMarkers();
	}
	
	for (i=0; i<this._markers.length; i++) {
		if (this._markers[i]._makeVisible && !this._markers[i]._isVisible) {
			this._map.addOverlay(this._markers[i]);
			this._markers[i]._isVisible=true;
		}
		if (!this._markers[i]._makeVisible && this._markers[i]._isVisible) {
			this._map.removeOverlay(this._markers[i]);
			this._markers[i]._isVisible=false;
		}
	}
	
	
	
	for (i=0; i<this._clusterMarkers.length; i++) {
		this._map.addOverlay(this._clusterMarkers[i]);
	}
};

MarkerManager.prototype._containedMarkers=function() {
	var _bounds=this._map.getBounds();
	var sw=_bounds.getSouthWest();
	var _swPoint=this._map.getCurrentMapType().getProjection().fromLatLngToPixel(sw, this._map.getZoom());
	var _newSwPoint=new GPoint(_swPoint.x-256, _swPoint.y+256);
	sw=this._map.getCurrentMapType().getProjection().fromPixelToLatLng(_newSwPoint, this._map.getZoom(), true);
	var ne=_bounds.getNorthEast();
	var _nePoint=this._map.getCurrentMapType().getProjection().fromLatLngToPixel(ne, this._map.getZoom());
	var _newNePoint=new GPoint(_nePoint.x+256, _nePoint.y-256);
	ne=this._map.getCurrentMapType().getProjection().fromPixelToLatLng(_newNePoint, this._map.getZoom(), true);
	_bounds=new GLatLngBounds(sw, ne);
	for (var i=0; i<this._markers.length; i++) {
		this._markers[i]._makeVisible=_bounds.contains(this._markers[i].getPoint())?true:false;
	}
};

MarkerManager.prototype._nonIntersectingMarkers=function() {
	var _clusterGroup, i, j, z='_'+this._map.getZoom();
	
	if (typeof(this._bounds[z])=='undefined') {
		this._bounds[z]=this._markersBoundsArray(this._markers);
	}
	for (i=0; i<this._markers.length; i++)
	{
		if (this._markers[i]._makeVisible) {
			_clusterGroup=[];
			for (j=i+1; j<this._markers.length; j++) {
				if (this._markers[j]._makeVisible && this._bounds[z][i].intersects(this._bounds[z][j])) {
					_clusterGroup.push(j);
				}
			}
			if (_clusterGroup.length!==0) {
				for (j=0; j<_clusterGroup.length; j++) {
					this._markers[_clusterGroup[j]]._makeVisible=false;
				}
				_clusterGroup.push(i);
				this._markers[i]._makeVisible=false;
				this._clusterMarkers.push(this._centerAndZoomMarker(_clusterGroup, this._map));
			}
		}
	}
	
};

MarkerManager.prototype._markersBoundsArray=function(_markers) {
	var _bounds=[], _anchorPoint, _boundsSw, _boundsNe, _icon, _iconWidth, _iconHeight, _iconAnchorX, _iconAnchorY, i, j, k, z, intersectMargin;
	intersectMargin=0;	//	maximum number of pixels to overlap before icon clustered - add to constructor?
	for (i=0; i<_markers.length; i++) {
		_icon=_markers[i].getIcon();
		j=_icon.iconSize;
		_iconWidth=j.width;
		_iconHeight=j.height;
		j=_icon.iconAnchor;
		_iconAnchorX=j.x;
		_iconAnchorY=j.y;
		j=_markers[i].getPoint();
		k=this._map.getCurrentMapType().getProjection();
		z=this._map.getZoom();
		_anchorPoint=k.fromLatLngToPixel(new GLatLng(j.lat(),j.lng()), z);
		_boundsSw=k.fromPixelToLatLng(new GPoint(_anchorPoint.x-_icon.iconAnchor.x+intersectMargin, _anchorPoint.y-_iconAnchorY+_iconHeight-intersectMargin), z);
		_boundsNe=k.fromPixelToLatLng(new GPoint(_anchorPoint.x-_icon.iconAnchor.x+_icon.iconSize.width-intersectMargin, _anchorPoint.y-_icon.iconAnchor.y+intersectMargin), z);
		_bounds[i]=new GLatLngBounds(_boundsSw, _boundsNe);
	}
	return _bounds;
};

//	following function unused and can be deleted if not needed
MarkerManager.prototype._markerBounds=function(_marker) {
	var _anchorPoint, _boundsSw, _boundsNe, _icon, _iconWidth, _iconHeight, _iconAnchorX, _iconAnchorY, i, j, k, z;
	_icon=_marker.getIcon();
	j=_icon.iconSize;
	_iconWidth=j.width;
	_iconHeight=j.height;
	j=_icon.iconAnchor;
	_iconAnchorX=j.x;
	_iconAnchorY=j.y;
	j=_marker.getPoint();
	k=map.getCurrentMapType().getProjection();
	z=map.getZoom();
	_anchorPoint=k.fromLatLngToPixel(new GLatLng(j.lat(),j.lng()), z);
	_boundsSw=k.fromPixelToLatLng(new GPoint(_anchorPoint.x-_icon.iconAnchor.x, _anchorPoint.y-_iconAnchorY+_iconHeight), z, true);
	_boundsNe=k.fromPixelToLatLng(new GPoint(_anchorPoint.x-_icon.iconAnchor.x+_icon.iconSize.width, _anchorPoint.y-_icon.iconAnchor.y), z, true);
	return new GLatLngBounds(_boundsSw, _boundsNe);
};

MarkerManager.prototype._centerAndZoomMarker=function(_clusterGroup, _map) {
	var _number=_clusterGroup.length;
	var _point=this._markers[_clusterGroup[_clusterGroup.length-1]].getPoint();
	var _bounds=new GLatLngBounds(_point, _point);
	for (var i=0; i<_clusterGroup.length-1; i++) {
		_bounds.extend(this._markers[_clusterGroup[i]].getPoint());
	}
		
	var tablo_nom = [];
	var tablo_icon = [];
	var tabl_im = [];
	var tabl_pl = [];
	
	this._clusterIcon=new GIcon();
	this._clusterIcon.image="./icons/" +_number+ ".png";
	//this._clusterIcon.shadow = "./icons/s.png";
	this._clusterIcon.iconSize=new GSize(23, 23);
	this._clusterIcon.iconAnchor=new GPoint(10, 10);
	this._clusterIcon.shadowSize=new GSize(32,21);
	this._clusterIcon.infoWindowAnchor=new GPoint(10,10);
	
	var _marker=new PdMarker(_point, this._clusterIcon);
	_marker.setTooltip('<center>Il y a '+_number+' lieux ici<br/>- Cliquer pour détailler -</center>');
	_marker.setHoverImage('./icons/'+_number+'_over.png');
	_marker.setOpacity(90);

	for (var i=0; i<_clusterGroup.length; i++) {
		tablo_nom[i] = this._markers[_clusterGroup[i]].title;
	}
	
	for (var i=0; i<_clusterGroup.length; i++) {
		tabl_im[i] = this._markers[_clusterGroup[i]].th;
	}
	
	for (var i=0; i<_clusterGroup.length; i++) {
		tablo_icon[i] = this._markers[_clusterGroup[i]].type;
	}
	
	for (var i=0; i<_clusterGroup.length; i++) {
		tabl_pl[i] = this._markers[_clusterGroup[i]].pl;
	}
		
	GEvent.addListener(_marker, 'click', function() {
		
		//GLog.write('Debut transfert');
		var alpha = _map;
		var beta = _bounds;
		zoomb = _map.getBoundsZoomLevel(_bounds);
		centre = _bounds.getCenter();
		//GLog.write('Fin transfert');
		
		var maximum_par_feuille = 10;
		var maximum_de_feuille = 3;
		
		if (tablo_nom.length < 15)
		{
			var html = '<center><table border="0">';
			for (var i=0; i<tablo_nom.length; i++) {
		
				var tmp = tablo_nom[i]; 
				var ic = tablo_icon[i];
				var url="http://www.limoges360.com/place-";
				url += tabl_pl[i];
				url += ".php";				
				html += '<tr height="20">';
				html += '<td width="23">';
				html += '<img src="./icons/';
				html += ic;
				html += '.png"></td><td><a style="color: black; font-size:14px" href="';
				html += url;
				html += '" onClick="" onMouseOver=\"au_dessus(';
				html += tabl_im[i];
				//GLog.write('Et hop on envoie '+tabl_im[i]+' à au-dessus()');
				html += ');\" onMouseOut=\"plus_au_dessus();\">'
				html += tmp;
				html += '</a></td></tr>';
			}


			html += '</table>';
			html += '<a style="color: black; font-size:14px" href="javascript:zoom(centre,zoomb)'
			html += ';">Cliquer ici pour zoomer</a>';
		
			_map.closeInfoWindow();
			_map.openInfoWindowHtml(_point,html);
			
		}
		
		else if ( tablo_nom.length <= maximum_de_feuille*maximum_par_feuille) {
		
			var tab = new Array();
			
			for (var i=0; i< tablo_nom.length; i += maximum_par_feuille)
			{
				var fin = Math.min(i+maximum_par_feuille,tablo_nom.length);
				var html = '<div style="width:'+maximum_de_feuille*140+'px height:400px"></div>';
				html += '<div><ol type="1" start="'+(i+1)+'">';
				for (var j = i; j < fin; j++) {
					
					var tmp = tablo_nom[j]; 
					var ic = tablo_icon[j];
					html += '<li><img src="./icons/';
					html += ic;
					html += '.png">&nbsp;&nbsp;<a href="" style="color: black; font-size:14px" onMouseOver=\"au_dessus(';
					html += tabl_im[j];
					html += ');\" onMouseOut=\"plus_au_dessus();\">'
					html += tmp;
					html += '</a>';
					
				}
				html += '</ol>';
				html += '<a style="color: black; font-size:14px" href="javascript:zoom(centre,zoomb)'
				html += ';">Cliquer ici pour zoomer</a></div>';
				var label = '' + (i+1) + '-' + (fin);
				tab[tab.length]= new GInfoWindowTab(label,html);
			}
			
			_marker.openInfoWindowTabsHtml(tab, {maxWidth:700});
			
		}
		
		else
		{
		
			zoom(centre,zoomb);
		
		}
	});
	
	
	return _marker;
};



MarkerManager.prototype.redrawVisibleMarkers=function() {
	for (i=0; i<this._markers.length; i++) {
		if (this._markers[i]._isVisible) {
			this._map.removeOverlay(this._markers[i]);
			this._map.addOverlay(this._markers[i]);
		}
	}
};

