var _mapclient = null;

function loadEvent(func) 
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') { window.onload = func; } 
	else { window.onload = function() { oldonload(); func(); } }
}

function ClientMapServer(id)
{	
	this.oMap     = xGetElementById(id);
	this.oZoomBox = xGetElementById('zoomrect');
	//this.oSpinner = xGetElementById('chargement');
	this.controler = top.controler;
	
	this.currentTool = 'PAN';
	this.setMapSize = function(w, h) 
	{
		this.nMapWidth = w; this.nMapHeight = h; 
	}
	
	this.setMapGeoExtent = function(minx, miny, maxx, maxy) 
	{
		this.fGeoMinX = minx; this.fGeoMinY = miny; this.fGeoMaxX = maxx; this.fGeoMaxY = maxy;
	}
	
	this.setCursor = function()
	{
		if (modeInfo == 1) // ajout anfr
		{	if (this.oMap.style) this.oMap.style.cursor = 'default';
			return;
		}
		else if (this.currentTool == 'ZOOM_IN' || this.currentTool == 'ZOOM_OUT')
		{	if (this.oMap.style) this.oMap.style.cursor = 'crosshair'; 
			return;
		}
		else if (this.currentTool == 'PAN')
		{	if (this.oMap.style) this.oMap.style.cursor = 'move';
		}
	}
	
	this.fGeoMinX = this.fGeoMinY = this.fGeoMaxX = this.fGeoMaxY = 0; //this.fGeoMinW = xGetElementById('MAP_EXTENT_MINX').value;   
	
	// Pt screen onMouseDown (x1/y1), onMouseMove/Up (x2/y2)
	this.x1 = this.y1 = this.x2 = this.y2 = -1;
	this.drag = false;
	
	// Map position
	this.nMapPosLeft = 0;
	this.nMapPosTop  = 0;
	this.nMapWidth   = 0;
	this.nMapHeight  = 0;

	this.oMap.style.position = 'absolute';
	this.oMap.style.top  = this.nMapPosTop;
	this.oMap.style.left = this.nMapPosLeft;
	
	var o = this;
	var m = o.oMap;
	
	m.onmouseover = function(e) {o._handleMouseEnter(e)};
	m.onmousedown = function(e) {o._handleMouseDown(e); return false; };
	m.onmousemove = function(e) {o._handleMouseMove(e)};
	m.onmouseup   = function(e) {o._handleMouseUp(e)};
	m.onmouseout  = function(e) {o._handleMouseOut(e)};  
	
	this.oZoomBox.onmousemove = function(e) {o._handleMouseMove(e)};
	this.oZoomBox.onmouseup = function(e) {o._handleMouseUp(e)};
}

var p = ClientMapServer.prototype;

p._handleMouseEnter = function(e)
{	
	if (this.currentTool == 'PAN')
	{	
		//this.oMap.style.cursor = 'move';
	}
}

p.transformPix2Geo = function(nPixPos, dfPixMin, dfPixMax, dfGeoMin, dfGeoMax, nInversePix) 
{	
	dfWidthGeo = dfGeoMax - dfGeoMin;
	dfWidthPix = dfPixMax - dfPixMin;
	
	dfPixToGeo = dfWidthGeo / dfWidthPix;
	
	if (Math.abs(dfPixToGeo) < 1) dNbDecimal = Math.abs(Math.log(dfPixToGeo))/Math.log(10)+1;
	else dNbDecimal = 0;
	
	if (nInversePix == 0) { dfDeltaPix = nPixPos - dfPixMin; }
	else { dfDeltaPix = dfPixMax - nPixPos; }
	
	dfDeltaGeo = dfDeltaPix * dfPixToGeo;
	
	dfPosGeo = new String(new Number(dfGeoMin) + new Number(dfDeltaGeo));
	aPosGeo = dfPosGeo.split(".");
	
	if (dNbDecimal > 0 && aPosGeo.length == 2) { dfPosGeo = aPosGeo[0]+"."+aPosGeo[1].substr(0, dNbDecimal); }
	else { dfPosGeo = aPosGeo[0]; }
	
	szttt =  ""+"pixelpos = "+nPixPos+" result ="+dfPosGeo;
	return dfPosGeo;
}

// Gestion des etiquettes
var CM1='';var OkDisplay=-1;var ol_offsetx=0;var ol_offsety=0;var olCheckMouseCapture=false;

p._handleMouseMove = function(e)
{	
	if (typeof xEvent != 'function') return;
	
	var evt = new xEvent(e);
	this.x2 = evt.pageX;
	this.y2 = evt.pageY;
	
	this.setCursor();
	
	//// MODE PAN
	if (this.currentTool == 'PAN' && this.drag)
	{
		var left = this.x2 - this.x1 + this.nMapPosLeft;
		var top  = this.y2 - this.y1  + this.nMapPosTop;
		xMoveTo(this.oMap, left, top);
		
		var t = this.y1 - this.y2;
		var r = this.x1 - this.x2 + this.nMapWidth;
		var b = this.y1 - this.y2 + this.nMapHeight;
		var l = this.x1 - this.x2;
		
		xClip(this.oMap, t, r, b, l);
		
		return false; // Fix for IE5
	}
		
	//// MODE ZOOM_IN ou SELECT
	else if ((this.currentTool == 'ZOOM_IN') && this.drag)
	{	if(this.x1==this.x2 && this.y1==this.y2) { xHide(this.oZoomBox); }
		else
		{
			var x = Math.min(this.x1,  this.x2);
			var y = Math.min(this.y1,  this.y2);
			var w = Math.abs(this.x1 - this.x2) + 1;
			var h = Math.abs(this.y1 - this.y2) + 1;
			
			xResizeTo(this.oZoomBox, w, h);
			xMoveTo(this.oZoomBox, x, y);
			xShow(this.oZoomBox);
			
			return false;
		}
	}
	
	// Affichage des etiquettes
	else
	{	var blnShow = 0;
		var offsetY = 3;//15;
		var offsetX = 3;//15;
		
		var blnOk = 1;
		if (document.layers)
		{	blnOk = 0;
		}
		if (!document.all)
		{	if (document.getElementById)
			{	blnOk = 0;
			}
		}
		
		ol_width=160;
		ol_fgcolor="#FFFFFF";
		ol_bgcolor="#00233E";
		ol_bgcolor="#333333";
		
		if (modeInfo == 0)
		{	if (blnOk == 1)
			{	//ol_offsetx=this.x2+10;
				//ol_offsety=this.y2+20;
				ol_offsetx=10;
				ol_offsety=20;
			}
			else
			{	ol_offsetx=10;
				ol_offsety=20;
			}
			return overlib("Cliquer pour zoomer ou cliquer/déposer pour déplacer", WIDTH, 125);
		}
		
		var nbInfos = 0; 
		if (tabInfos != null)
		{	nbInfos = tabInfos.length;
		}
		
		for (var i = 0 ; i < nbInfos; i++)
		{	if ((this.x2>(tabX[i]-lngBuffer/2+offsetX)) && (this.x2<(tabX[i]+lngBuffer/2+offsetX)))
			{	if ((this.y2>(tabY[i]-lngBuffer/2+offsetY)) && (this.y2<(tabY[i]+lngBuffer/2+offsetY)))
				{	tabHTML = tabInfos[i];
					strHTML = '<div align="left" class="txt7">';
					for (j=0; j<1; j++)
					{	if (tabHTML[j] != '')
						{	strHTML +=  tabHTML[j]+'<br>';
						}
					}
					strHTML += '</div>';
					
					return showEtiq(blnOk, strHTML, this.x2, this.y2);
					i=nbInfos;
				}
			}
		}
		
		// masquer l'etiquette
		if (blnShow != 1)
		{	nd();
		}
	}
}

p._handleMouseOut = function(e) 
{	
	this.oMap.style.cursor = this.oZoomBox.style.cursor = "default";
	
	nd();
}

p._handleMouseDown = function(e) 
{	
	var evt = new xEvent(e);
	
	this.x1 = this.x2 = evt.pageX;
	this.y1 = this.y2 = evt.pageY;
	
	if (modeInfo == 0)
	{	this.drag = true;
	}
	else
	{	var blnShow = 0;
		var offsetY = 3;//15;
		var offsetX = 3;//15;

		if (window.Event)
		{	button=evt.which;
		}
		else
		{	button=event.button;
		}
		if (button > 1)
		{	return;
		}

		if (modeInfo == 0)
		{	f = xGetElementById("Formulaire");
			f.x.value=this.x1;
			f.y.value=this.y1;
			f.submit();
			return;
		}
		var nbInfos = 0; 
		if (tabInfos != null)
		{	nbInfos = tabInfos.length;
		}

		for (var i = 0 ; i < nbInfos; i++)
		{	if ((this.x1>(tabX[i]-lngBuffer/2+offsetX)) && (this.x1<(tabX[i]+lngBuffer/2+offsetX)))
			{	if ((this.y1>(tabY[i]-lngBuffer/2+offsetY)) && (this.y1<(tabY[i]+lngBuffer/2+offsetY)))
				{	// affichage de la fiche d'infos
					tabHTML = tabInfos[i];
					f(tabX[i],tabY[i]);
					
					return;
				}
			}
		}
	}
}

p._handleMouseUp = function(e) 
{	
	var evt = new xEvent(e);
	
	if (this.drag)
	{	this.drag = false;
	
		// Skip right mouse down button
		var rightclick = false;
		if (!e) var e = window.event;
		if (e.which) rightclick = (e.which == 3 || evt.keyCode == 0);
		else if (e.button) rightclick = (e.button == 2);
		if (rightclick) return true;
		
		this.x1 -= this.nMapPosLeft;
		this.x2 -= this.nMapPosLeft;
		this.y1 -= this.nMapPosTop;
		this.y2 -= this.nMapPosTop;
		
		if (this.currentTool == 'ZOOM_IN'  || this.currentTool == 'ZOOM_OUT')
		{
			xResizeTo(this.oZoomBox, 1, 1);
			xMoveTo(this.oZoomBox, 1, 1);
			xHide(this.oZoomBox);
			
			document.Formulaire.x1.value = this.x1 - xPageX(this.oMap);
			document.Formulaire.x2.value = this.x2 - xPageX(this.oMap);
			document.Formulaire.y1.value = this.y1 - xPageY(this.oMap);
			document.Formulaire.y2.value = this.y2 - xPageY(this.oMap);
			document.Formulaire.submit();
		}
		else if (this.currentTool == 'PAN')
		{
			if (this.x1 != this.x2 || this.y1 != this.y2)
			{	var x = this.x1 - (this.x2 - this.nMapWidth / 2);
				var y = this.y1 - (this.y2 - this.nMapHeight / 2);
				document.Formulaire.cmd.value = this.currentTool;
			}
			else
			{	var x = this.x1 - xPageX(this.oMap);
				var y = this.y1 - xPageY(this.oMap);
			}
			
			document.Formulaire.x1.value = x;
			document.Formulaire.y1.value = y;
			document.Formulaire.x2.value = x;
			document.Formulaire.y2.value = y;
			document.Formulaire.submit();
		}
	}
}
