var globalObject
var timeout = 300
var max_batch = 30

var max_markers = 100000


function GMarkerFS(map, extra) 
{
  // we don't actually need the map variable but to be sure the Google Map API
  // is loaded
  this.map=map;
  this.width = map.getSize().width
  this.height = map.getSize().height
  this.extra = extra
  
  // a point is in one of the arrays below
  // at zoom-in/zoom-out, all points go to invalid and are re-rendered
  // at shift the inside points stay inside, 
  this.points_invalid = new Array()
  this.points_inside = new Array()   
  
  // 0 idle, 1 handling move, 2 handling zoom, 3 disabled
  // zoom overrules move
  this.state = 0
  this.counter = 0
  this.last_zoom = 0
  this.last_center_point = new GLatLng(0,0)
  this.RequesthandleMoveend = false
  
  GEvent.bind(this.map, "zoomend", this, this.handleZoomend);
  GEvent.bind(this.map, "moveend", this, this.handleMoveend);
}

GMarkerFS.prototype.addMarker = function(marker)
{
  if ( this.points_invalid.length + this.points_inside.length > max_markers) return
  var p = new GPointFG(this.map, marker)
  this.points_invalid.push(p)
}

GMarkerFS.prototype.addMarkerForced = function(marker)
{
  var p = new GPointFG(this.map, marker)
  this.points_inside.push(p)
  this.map.addOverlay(marker)
}

GMarkerFS.prototype.delMarker = function(marker)
{
	var n = this.points_invalid.length
	var i = 0
	for (i; i < n; i++)
	{
		if (this.points_invalid[i].gmarker == marker)
		{
			this.points_invalid.splice(i, 1)				
			return			
		}
	}
	
	n = this.points_inside.length
	i = 0
	for (i; i < n; i++)
	{
		if (this.points_inside[i].gmarker == marker)
		{
			this.points_inside.splice(i, 1)			
			this.map.removeOverlay(marker)
			return
		}
	}
}

GMarkerFS.prototype.checkInvalid = function()
{
	if (this.state == 3) return
	var i = 0
	while (this.counter >= 0 && i < max_batch)
	{
		var p = this.points_invalid[this.counter]
		p.updateXY()
		//alert('x,y=' + p.gpoint.x +',' + p.gpoint.y)
		if (	p.gpoint.x > -this.extra &&
				p.gpoint.x < this.width	+ this.extra &&
				p.gpoint.y > -this.extra &&
				p.gpoint.y < this.height + this.extra)
		{
			this.points_inside.push(p)
			this.points_invalid.splice(this.counter, 1)
			this.map.addOverlay(p.gmarker)	
			i = max_batch
		} 
		this.counter--
		i++
	}
	if (this.counter == -1)
	{
		this.state = 0
		document.getElementById('divDebug').innerHTML = ''
		if (this.RequesthandleMoveend)
		{
			this.handleMoveend()
		}
	}
	else
	{
	var nnn = this.points_inside.length + this.points_invalid.length
	document.getElementById('divDebug').innerHTML = 'Painting users: ' + (nnn-this.counter) + '/' + nnn + '   '
		globalObject = this
		setTimeout('globalObject.checkInvalid()', timeout)
	}
}

GMarkerFS.prototype.refresh = function()
{
	this.handleZoomend()
}

// disable / clean
GMarkerFS.prototype.disable = function()
{
	this.state = 3
	//alert('c0')
	this.map.clearOverlays()
	//alert('c1')
	this.points_invalid = this.points_invalid.concat(this.points_inside)
	this.points_inside.length = 0
	//alert('c3')	
}

GMarkerFS.prototype.enable = function()
{
	this.state = 0
}

GMarkerFS.prototype.handleZoomend = function()
{
	if (this.state == 3) return
	//if (this.state != 2)
	{
		this.disable()	
		this.counter = this.points_invalid.length - 1
	}
	this.state = 2
	this.checkInvalid()	
}

GMarkerFS.prototype.handleMoveend = function()
{
	if (this.state == 3) return
	if (this.state == 1 || this.state == 0)
	{
		if (this.state == 0)
		{			
			this.counter = this.points_invalid.length - 1
			this.RequesthandleMoveend = false
		}	
		else
		{
			this.RequesthandleMoveend = true
		}	
		this.state = 1
		this.checkInvalid()
	}
	else
	{
		globalObject = this
		setTimeout('globalObject.handleMoveend()', timeout)
	}	
}





