/**
 * Creates new Map overlay to keep track marker groups.
 */
function RyzomMapMarkerGroup(name, map){
	this.name=name;
	this.map_=map;
	// FIXME: this will probably make it slow 
	//this.mgr_=new MarkerManager(map, {trackMarkers: false, maxZoom:map.maxZoom});
	this.mgr_=mgr;
	//
	this.markers=[];  // GMarker
	this.overlays=[]; // GOverlay
	this.groups=[];   // RyzomMapMarkerGroup groups
	//
	this.hidden=true;

	// listener functions
	this.onShow=function(){ return; };
	this.onHide=function(){ return; };
}
RyzomMapMarkerGroup.prototype.getName=function(){
	return this.name;
};
RyzomMapMarkerGroup.prototype.processArray_=function(array, callback){
	for(var i=0;i<array.length;i++){
		callback(array[i]);
	}
};
RyzomMapMarkerGroup.prototype.show=function(){
	if(!this.hidden) return;
	
	var me=this;
	// with markers, we need to add them back to mgr
	this.processArray_(this.markers, function(m){
		me.mgr_.addMarker(m, false);
	});
	this.mgr_.redraw_(); // FIXME:
	
	var show_ = function(m){ m.show(); };
	this.processArray_(this.overlays, show_);
	this.processArray_(this.groups, show_);
	
	this.hidden=false;
	// 'listener' function
	this.onShow();
};
RyzomMapMarkerGroup.prototype.hide=function(){
	if(this.hidden) return;
	var me=this;
	// to hide a marker, we need to remove it from mgr
	this.processArray_(this.markers, function(m){
		me.mgr_.removeMarker(m);
	});
	this.mgr_.redraw_(); // FIXME:
	
	var hide_=function(m){ m.hide();};
	this.processArray_(this.overlays, hide_);
	this.processArray_(this.groups, hide_);
	
	this.hidden=true;
	// 'listener' function
	this.onHide();
};
RyzomMapMarkerGroup.prototype.removeAll=function(){
	for(var i=0;i<this.markers.length;i++){
		this.mgr_.removeMarker(this.markers[i]);
	}
	this.markers=[];

	for(var i=0;i<this.groups.length;i++){
		this.groups[i].removeAll();
	}
	this.groups=[];

	for(var i=0;i<this.overlays.length;i++){
		this.map_.removeOverlay(this.overlays[i]);
	}
	this.overlays=[];
};
RyzomMapMarkerGroup.prototype.addMarkers = function(markers, minZoom, maxZoom){
	for(var i=0;i<markers.length;i++){
		this.markers.push(markers[i]);
	}
	//this.mgr_.addMarkers(markers, minZoom, maxZoom);
};
RyzomMapMarkerGroup.prototype.addOverlay=function(overlay){
	this.overlays.push(overlay);
	this.map_.addOverlay(overlay);
};
RyzomMapMarkerGroup.prototype.addGroups=function(groups){
	for(var i=0;i<groups.length;i++){
		this.groups.push(groups[i]);
	}
};
// create markers/overlays list
RyzomMapMarkerGroup.prototype.createList=function(ul){
	
	function set_events(li, marker){
		$(li).click(function(e){
			map.panTo(marker.getLatLng());
		}).hover(function(e){
			var pos=map.fromLatLngToDivPixel(marker.getLatLng());
			$(this).addClass('hover');
			tooltip.show(pos.x, pos.y, marker.tooltip);
			marker.setOver(true);
		}, function(e){
			$(this).removeClass('hover');
			tooltip.hide();
			marker.setOver(false);
		});
	}
	
	for(var i=0;i<this.markers.length;i++){
		var li=document.createElement('li');
		li.className='alt'+(i%2);
		var m=this.markers[i];
		var img='';
		if(m.getIcon){
			img='<img src="'+(m.getIcon().image)+'" class="icon" /> ';
		}
		var title='-';
		if(m.getTitle){
			title=m.getTitle();
		}else{
			// ELabel probably - FIXME: skip for now
			continue;
		}
		// use tooltip instead ?
		if(!title){
			title = m.tooltip;
		}
		var span=document.createElement('span');
		span.innerHTML=img+title;
		span.style.display='block';
		span.style.cursor='pointer';
		set_events(span, m);

		li.appendChild(span);
		ul.appendChild(li);
	}
	// fixme: polygons
	/*for(var i=0;i<this.overlays.length;i++){
		var li=document.createElement('li');
		li.innerHTML='Overlay ['+i+']';
		ul.appendChild(li);
	}*/
	for(var i=0;i<this.groups.length;i++){
		var g=this.groups[i];
		
		var li=document.createElement('li');
		li.appendChild(document.createTextNode(g.getName()));
		
		var inner=document.createElement('ul');
		g.createList(inner);
		li.appendChild(inner);
		
		ul.appendChild(li);
	}
};


