/*
****************************************************
*	File : 		script_image.js
*	Author : 	O.Gayte,F.Mauro,F.Marty
*	Company :    VEREMES - www.veremes.com
*	Credits :      Th. Beaufreton (data-image)
*	                    Walter zorn's wz_jsgraphics graphic library
*	This file is a part of VEREMAP a free mapserver application designed and distributed by Veremes.
*	Copyright (c) 2004-2009 Veremes
****************************************************
*/

/**
* \file scripts_image.js
* \brief Ce fichier JavaScript contient les traitements liés à l'utilisation de la carte dans Veremap.pro.
*
* scripts_image.js permet d'activer les actions JavaScript liées à l'utilisation de la carte (IMAGE WEB) 
*
* C'est notamment dans ce fichier JavaScript que sont traités les évènements souris liés à la carte.
*
* \author Olivier Gayte <olivier.gayte@veremes.com>
* \author Frédéric Mauro <frederic.mauro@veremes.com>
* \author Fabien Marty <fabien.marty@veremes.com>
*/

/**
* Le navigateur est Netscape/Mozilla (OUI ou NON).
*/
var isNav;
/**
* Le navigateur est Internet Explorer (OUI ou NON).
*/
var isIE;
/**
* Objet stockant l'évènement en cours
*/
var oEvent;
/**
* bWheelUsed est vrai (1) si l'utilisateur est en train d'utiliser la roulette.
* c'est à dire si un evenement onmousewheel a été généré et que l'utilisateur ne s'est pas interrompu depuis xx millisecondes
*/
var bWheelUsed=0;
/**
* date en millisecondes du dernier appel à la fonction wheel().
*/
var iLastWheelEvent;
/**
* Délai en millisecondes au bout duquel on considère que l'utilisateur a arrêté de tourner la molette.
*/
var iEndWheelDelay=1000;
/**
* Ce paramètre permet d'accentuer  ou atténuer le facteur de zoom appliquer à chaque action de la molette de la souris
* dWheelZoomFactor=10 par defaut
*/
var dWheelZoomFactor=10;
/**
* Epaisseur de la bordure de la carte.
*/
var iMapBorderWidth=2;
/**
* Objet layout Yui de la carte
*/
var mapLayout;
/**
* Objet layout Yui de l'en-tete
*/
var headerLayout;
/**
* Objet mapImage de la première carte
*/
var oMap;
/**
* Objet mapImage de la deuxième carte
*/
var oCompareMap;

/**
* Cette fonction permet de créer un objet DIV au sein de le DIV qui contient la carte.
* \param sParamDivName Identifiant du DIV permettant de créer l'objet mapImage.
*/
function mapImage (sParamDivName){
	/**
	* Id du div de la carte.
	*/
	this.sDivName=sParamDivName;
	/**
	* Position de la souris en X (pixels).
	*/
	this.mouseX=0;
	/**
	* Position de la souris en Y (pixels).
	*/
	this.mouseY=0;
	/**
	* Etat de la souris (Down ou Up).
	*/
	this.mouseD=false;
	/**
	* Position de la souris en X (Terrain).
	*/
	this.coordx=0;
	/**
	* Position de la souris en Y (Terrain).
	*/
	this.coordy=0;
	/**
	* Zoom activé (OUI ou NON).
	*/
	this.zooming=false;
	/**
	* Position en X de la boîte de zoom.
	*/
	this.x1=0;
	/**
	* Position en Y de la boîte de zoom.
	*/
	this.y1=0;
	/**
	* Position en X de la boîte de zoom.
	*/
	this.x2=0;
	/**
	* Position en Y de la boîte de zoom.
	*/
	this.y2=0;
	/**
	* Variable de calcul pour l'utilisation de l'outil "pan" (déplacement en x).
	*/
	this.iXDiff;
	/**
	* Variable de calcul pour l'utilisation de l'outil "pan" (déplacement en y).
	*/
	this.iYDiff;
	/**
	* Variable de calcul pour l'utilisation de l'outil "pan" (départ en x).
	*/
	this.iMouseXStart;
	/**
	* Variable de calcul pour l'utilisation de l'outil "pan" (départ en y).
	*/
	this.iMouseYStart;
	/**
	* Objets de dessin points.
	*/
	this.oGfxPoint;
	/**
	* Tableau d'objets de dessin points .
	*/
	this.aGfxMultiPoint=new Array();
	/**
	* Objet de dessin ligne.
	*/
	this.oGfxLine;
	/**
	* Objet de dessin polygone.
	*/
	this.oGfxPolygon;
	/**
	* Objet de dessin cercle.
	*/
	this.oGfxCircle;
	/**
	* Tableau d'objets de dessin annotations pour la ligne.
	*/
	this.aGfxLineText=new Array();
	/**
	* Tableau d'objets de dessin annotations pour le polygone.
	*/
	this.aGfxPolyText=new Array();
	/**
	* Objet de dessin quelquonque en édition.
	*/
	this.oGfxGeom;
	/**
	* Dessin ponctuel représentant 
	* le vertex en cours d'édition sur lequel une opération de déplacement est effectuée.
	*/
	this.oGfxGeomVertex;
	/**
	* Index du vertex en cours de modification (suppression ou déplacement).
	*/
	this.iEditedVertex;
	/**
	* Objet graphique des outils.
	*/
	this.oBoxLayer;
	/**
	* Objet graphique des outils d'impression.
	*/
	this.oPrintLayer;
	/**
	* Objet graphique pour la saisie.
	*/
	this.oGfxLayer;
	/**
	* Largeur de la carte (Pixels). 
	* La valeur de mapwidth est définie après le chargement dans initSize()
	*/
	this.mapwidth;
	/**
	* Hauteur de la carte (Pixels).
	*/
	this.mapheight;
	/**
	* Valeur minimum en X de la carte (Terrain).
	* L'étendue est définie après la création de la première carte
	*/
	this.envxmin;
	/**
	* Valeur minimum en Y de la carte (Terrain).
	*/
	this.envymin;
	/**
	* Valeur maximum en X de la carte (Terrain).
	*/
	this.envxmax;
	/**
	* Valeur maximum en Y de la carte (Terrain).
	*/
	this.envymax;
	/**
	* Contenu HTML par défaut du layer carte.
	*/
	this.mapLayerDefHTML="<img id='"+sParamDivName+"1' name='map' src='IMAGE' usemap='#"+sParamDivName+"tooltip' border='0' onload='parent.testImgSize(\""+sParamDivName+"\");'>";
	/**
	* Contenu HTML courant du layer carte.
	*/
	this.mapLayerCurHTML;
	/**
	* METHODES
	*/
	this.initMap=initMap;
	this.startUp=startUp;
	this.createLayer=createLayer;
	this.setLayerVisibility=setLayerVisibility;
	this.getLayerVisibility=getLayerVisibility;
	this.resizeLayer=resizeLayer;
	this.traitementEnCours=traitementEnCours;
	this.traitementTermine=traitementTermine;
	this.setImageEvent=setImageEvent;
	this.unsetImageEvent=unsetImageEvent;
	this.setMapLayerHTML=setMapLayerHTML;
	this.setCursor=setCursor;
	this.setMapLayerHTML=setMapLayerHTML;
	this.calcCoordGeo=calcCoordGeo;
	this.setFormPoint=setFormPoint;
	this.setFormRect=setFormRect;
	this.getImageXY=getImageXY;
	this.startZoomBox=startZoomBox;
	this.stopZoomBox=stopZoomBox;
	this.setClip=setClip;
	this.changeExtent=changeExtent;
	this.gfx_changeExtent=gfx_changeExtent;
	this.initGfxGeom=initGfxGeom;
	this.drawCircle=drawCircle;
	this.drawPrintRect=drawPrintRect;
	this.createPoint=createPoint;
	this.deleteLastPoint=deleteLastPoint;
}

	/**
	* Cette fonction initialise tous les objets DIV contenus dans le DIV qui contient la carte.
	*/
	function initMap(){
		// Type de Navigateur utilisé
		setBrowserType();
		this.mapwidth=getClientWidth();
		this.mapheight=getClientHeight();
		var iTop=0;
		var iLeft=0;
		if(this.sDivName=="divMapImage2"){
			var iLeft=this.mapwidth+iMapBorderWidth-(iMapBorderWidth/2);
		}
		// Couche de la carte
		var content=this.mapLayerDefHTML.replace(/IMAGE/,"images/null.png");
		if(isIE){
			// IE n'intègre pas l'épaisseur de la bordure
			var iHeight=this.mapheight+(iMapBorderWidth*2);
			var iWidth=this.mapwidth+(iMapBorderWidth*2);
		}else{
			var iHeight=this.mapheight;
			var iWidth=this.mapwidth;
		}
		this.createLayer("MapLayer",iTop,iLeft,iWidth,iHeight,true,content);
		// Couche légèrement opaque noir
		iLeft=iLeft+iMapBorderWidth;
		iTop=iTop+iMapBorderWidth;
		content="";
		this.createLayer("MapMask",iTop,iLeft,this.mapwidth,this.mapheight,true,content);
		// Couche de la barre de chargement
		content="<img src='images/progbar.gif' alt='Chargement en cours'>";
		var iXcentral=iLeft+Math.round(this.mapwidth/2);
		var iYcentral=iTop+Math.round(this.mapheight/2);
		this.createLayer("ProgressBarLayer",(iYcentral-7),(iXcentral-75),150,15,true,content,2);
		// Couches graphique des outils (zoom,selection,..)
		content="";
		this.createLayer("BoxLayer",iTop,iLeft,this.mapwidth,this.mapheight,false,content);
		this.createLayer("PrintLayer",iTop,iLeft,this.mapwidth,this.mapheight,false,content);
		this.createLayer("EditLayer",iTop,iLeft,this.mapwidth,this.mapheight,false,content);
		// Objets WalterZorn
		this.oBoxLayer=new jsGraphics(this.sDivName+"BoxLayer");
		this.oBoxLayer.setStroke(2);
		this.oBoxLayer.setColor("red");
		this.oPrintLayer=new jsGraphics(this.sDivName+"PrintLayer");
		this.oPrintLayer.setStroke(4);
		this.oPrintLayer.setColor("red");
		this.oGfxLayer=new GraphicLayer();
		// A améliorer ! En mode comparaison le curseur doit reprendre celui de la première carte !
		this.setCursor('nw-resize');
	}

	/**
	* Cette fonction est appelée une fois que le DIV contenant la carte à été mise en place.
	* Elle permet d'initialiser la carte (étendue maximale).
	*/
	function startUp(){
		if(parent.map_toolbar.document.mapForm){
			this.setImageEvent();
			if(parent.sXmlFile){
				//appel http request
				//peut être supprimer la valeur fichier
			}else{
				parent.frames.map_toolbar.document.mapForm.mf_tool.value="mapinit";
				parent.frames.map_toolbar.map_refresh();
			}
		}
	}

	/**
	* Cette fonction permet de créer un objet DIV au sein de le DIV qui contient la carte.
	* \param sName Nom de l'objet DIV.
	* \param iIntop Décalage vers le bas de l'objet DIV.
	* \param iInleft Décalage vers la droite de l'objet DIV.
	* \param iWidth Largeur de l'objet DIV.
	* \param iHeight Hauteur de l'objet DIV.
	* \param bVisible L'objet DIV est visible (OUI ou NON).
	* \param sContent Le contenu de l'objet DIV.
	* \param iZIndex position du DIV (Facultatif : 1 par défaut).
	*/
	function createLayer(sName,iIntop,iInleft,iWidth,iHeight,bVisible,sContent,iZIndex){
		if(!iZIndex) iZIndex=1;
		var sDivContent=document.getElementById(this.sDivName).innerHTML;
		sDivContent+="<div id='"+this.sDivName+sName+"' class='"+sName+"' style='position:absolute;overflow:hidden;left:"+iInleft+"px;top:"+iIntop+"px;width:"+iWidth+"px;height:"+iHeight+"px;z-index:"+iZIndex+";visibility:"+(bVisible? "visible;":"hidden;")+"'>"+sContent+"</div>";
		document.getElementById(this.sDivName).innerHTML=sDivContent;
	}
	/**
	* Cette fonction permet de rendre un layer visible ou invisible
	* \param sLayer Id du DIV.
	* \param bVisible état (true ou false).
	*/
	function setLayerVisibility(sLayer,bVisible){
		document.getElementById(this.sDivName+sLayer).style.visibility=(bVisible==true) ? "visible" : "hidden";
	}
	/**
	* Cette fonction permet de retourner l'information sur la visibilité du layer
	* \param sLayer Id du DIV.
	* \return true ou false
	*/
	function getLayerVisibility(sLayer){
		//alert(document.getElementById(this.sDivName+sLayer).style.visibility);
		if(document.getElementById(this.sDivName+sLayer).style.visibility=="visible"){ return true; }else{ return false;}
	}

	/**
	* Cette fonction permet de redimensionner les différents objets DIV contenus dans le DIV qui contient la carte.
	* \param sLayerId Identifiant du DIV à redimensionner.
	*/
	function resizeLayer(sLayerId){
		this.mapwidth=getClientWidth();
		this.mapheight=getClientHeight();
		var iLeft=0;
		var iTop=0;
		var sWidth=this.mapwidth+'px';
		var sHeight=this.mapheight+'px';
		if(this.sDivName=="divMapImage2"){
			iLeft=this.mapwidth+iMapBorderWidth;
		}
		if(sLayerId != "MapLayer"){
			iLeft=iLeft+iMapBorderWidth;
			iTop=iMapBorderWidth;
		} else if(isIE){
			var sWidth=parseInt(this.mapwidth+(iMapBorderWidth*2))+'px';
			var sHeight=parseInt(this.mapheight+(iMapBorderWidth*2))+'px'
		}
		if(sLayerId=="ProgressBarLayer"){
			iLeft=iLeft+Math.round(this.mapwidth/2)-75;
			iTop=iTop+Math.round(this.mapheight/2)-7;
		}else{
			// Redéfinit la taille de l'objet layer (DIV) à partir de la largeur et hauteur du layout divMap.
			document.getElementById(this.sDivName+sLayerId).style.width=sWidth;
			document.getElementById(this.sDivName+sLayerId).style.height=sHeight;
		}
		// Repositionne l'objet layer (DIV).
		document.getElementById(this.sDivName+sLayerId).style.left=iLeft+'px';
		document.getElementById(this.sDivName+sLayerId).style.top=iTop+'px';
	}

	/**
	* Cette fonction affiche la barre de progression lors du chargement de la carte.
	*/
	function traitementEnCours(){
		this.setLayerVisibility("ProgressBarLayer",true);
		this.setLayerVisibility("MapMask",true);
	}
	/**
	* Cette fonction cache la barre de progression lorsque le chargement de la carte est terminé.
	*/
	function traitementTermine(){
		this.setLayerVisibility("ProgressBarLayer",false);
		this.setLayerVisibility("MapMask",false);
	}

	/**
	* Cette fonction permet d'associer des fonctions JavaScript à des evénements JavaScript :
	* \li \c onmousemove Sur déplacement de la souris on utilise la fonction mouseMove();.
	* \li \c onmousedown Sur activation de la souris on utilise la fonction mouseDown();.
	* \li \c onmouseup Sur désactivation de la souris on utilise la fonction mouseUp();.
	* \li \c onresize Sur redimensionnement du layout divMap on utilise la fonction mapResize();.
	* \li \c onmousewheel Sur mouvement de la molette (zoom+ou zoom -) on appele la fonction wheel();.
	*/
	function setImageEvent(){
		document.getElementById(this.sDivName).onmousemove=mouseMove;
		document.getElementById(this.sDivName).onmouseup=mouseUp;
		document.getElementById(this.sDivName).onmousedown=mouseDown;
		/*Active le redimensionnement
		if(window.name=="map_image"){
			window.onresize=mapResize;
		}*/
		if(isIE){
			document.onmousewheel=wheel;
		}else{
			if(window.addEventListener){
				window.addEventListener('DOMMouseScroll',wheel,true);
			}
		}
	}

	/**
	* Cette fonction permet d'annuler les associations créées avec  setImageEvent();.
	*/
	function unsetImageEvent(){
		document.getElementById(this.sDivName).onmousemove="";
		document.getElementById(this.sDivName).onmouseup="";
		document.getElementById(this.sDivName).onmousedown="";
		/* Désactive le redimensionnement
		if(window.name=="map_image"){
			window.onresize="";
		}*/
		if(isIE){
			document.onmousewheel="";
		}else{
			if(window.removeEventListener){
				window.removeEventListener("DOMMouseScroll",wheel,false);
			}
		}
	}

	/**
	* Cette fonction permet de modifier le curseur de l'utilisateur selon l'outil utilisé.
	* \param sCursor Le type de curseur que l'on veut afficher.
	*/
	function setCursor(sCursor){
		if(isIE){
			parent.document.getElementById(this.sDivName).getAttribute("style").setAttribute("cursor",sCursor);
		}else{
			parent.document.getElementById(this.sDivName).setAttribute("style","cursor: "+sCursor);
		}
	}

	/**
	* Cette fonction permet de mettre à jour le contenu HTML du layer carte (Image et Tooltip).
	*/
	function setMapLayerHTML(sContent){
		document.getElementById(this.sDivName+"MapLayer").innerHTML=sContent;
	}

	/**
	* Cette fonction permet de calculer les coordonnées terrain de la souris.
	* \param iMouseX coordonnées images en X.
	* \param iMouseY coordonnées images en Y.
	* \return Les coordonnées en X et Y (Terrain) de la souris.
	*/
	function calcCoordGeo(iMouseX,iMouseY){
		var coordX_geo=this.envxmin+((this.envxmax-this.envxmin)*(((iMouseX)/this.mapwidth)*100))/100;
		var coordY_geo=this.envymin+((this.envymax-this.envymin)*(100-(((iMouseY)/this.mapheight)*100)))/100;
		return (new Array(coordX_geo,coordY_geo));
	}

	/**
	* Cette fonction permet de valoriser les attributs "point" (mf_x et mf_y) du formulaire principal.
	* Cette opération est basée sur les coordonnées du click souris.
	*/
	function setFormPoint(){
		var oMapForm=parent.frames.map_toolbar.document.mapForm;
		if(oMapImage.aGfxMultiPoint.length==0){
			var aParam=this.calcCoordGeo(this.mouseX,this.mouseY);
			oMapForm.mf_x.value=aParam[0];
			oMapForm.mf_y.value=aParam[1];
		}else{
			var sSelectShape="";
			for (var i=0; i<oMapImage.aGfxMultiPoint.length; i++){
				if(sSelectShape!="") sSelectShape+=" ";
				sSelectShape+=oMapImage.aGfxMultiPoint[i].toStringXY().replace(/ /g, '@');
			}
			oMapForm.mf_selectShape.value=sSelectShape;
		}
	}

	/**
	* Cette fonction valorise l'attribut mf_selectShape du formulaire principal.
	* Cet attribut stocke l'étendue dans laquelle est faite la sélection.
	*/
	function setFormRect(){
		var oMapForm=parent.frames.map_toolbar.document.mapForm;
		var aParam=this.calcCoordGeo(oMapForm.mf_imageXMin.value,oMapForm.mf_imageYMax.value);
		var sSelectShape=shapeStringConcat(aParam[0],aParam[1]);
		aParam=this.calcCoordGeo(oMapForm.mf_imageXMax.value,oMapForm.mf_imageYMin.value);
		sSelectShape=sSelectShape+shapeStringConcat(aParam[0],aParam[1]);
		oMapForm.mf_selectShape.value=sSelectShape;
	}

	/**
	* Cette fonction permet de récupérer les coordonnées X et Y (Pixels) de la souris.
	* \return Les coordonnées en X et Y (Pixels) de la souris.
	*/
	function getImageXY(){
		var iX=oEvent.clientX-iMapBorderWidth;
		var iY=oEvent.clientY-headerLayout.get('height')-iMapBorderWidth;
		if(this.sDivName=="divMapImage2"){
			iX=iX+iMapBorderWidth-parseInt(parent.document.getElementById(this.sDivName+"MapLayer").style.left);
		}
		this.mouseX=iX;
		this.mouseY=iY;
		return new Array(iX,iY);
	}

	/**
	* Cette fonction permet d'initialiser la boîte de zoom.
	* \return false.
	*/
	function startZoomBox(){
		this.mouseD=true;
		this.getImageXY();
		if(this.zooming){
			this.stopZoomBox();
		}else{
			this.x1=this.mouseX;
			this.y1=this.mouseY;
			this.x2=this.x1+1;
			this.y2=this.y1+1;
			this.setClip();
			this.zooming=true;
		}
		return false;
	}
	/**
	* Cette fonction permet de stopper (détruire) la boîte de zoom.
	* \return false.
	*/
	function stopZoomBox(){
		this.zooming=false;
		this.oBoxLayer.clear();
		return false;
	}
	/**
	* Cette fonction assure le dessin de la boîte de zoom.
	* Cette fonction est appelée lorsque le dessin d'un boite élastique doit être affiché (sur des outils tels que 
	* "Zoom avant" ou "Sélection par boite élastique").
	*/
	function setClip(){
		this.oBoxLayer.clear();
		this.oBoxLayer.drawPolyline(new Array(this.x1,this.x2,this.x2,this.x1,this.x1), new Array(this.y1,this.y1,this.y2,this.y2,this.y1));
		this.oBoxLayer.paint();
	}

	/**
	* Cette fonction modifie l'étendue de la carte lorsqu'un zoom est effectué (à la fin du dessin d'une boite de zoom).
	* Les attributs d'étendue image du formulaire principal sont modifiés.
	*/
	function changeExtent(){
		// iTolerance=taille minimum de la boite du zoom élastique, en deça, on force le zoom à dZoomFactor * la taille de l'écran
		var oMapForm=parent.frames.map_toolbar.document.mapForm;
		var iTolerance=2;
		var dZoomFactor=0.5;
		var valeur=0;
		if(this.x2 < this.x1){
			valeur=this.x1;
			this.x1=this.x2;
			this.x2=valeur;
		}
		if(this.y2 < this.y1){
			valeur=this.y1;
			this.y1=this.y2;
			this.y2=valeur;
		}
		if(this.x2-this.x1 <= iTolerance && this.y2-this.y1 <= iTolerance && oMapForm.mf_tool.value=="zoom_in"){
			var width=Number(oMapForm.mf_imageWidth.value);
			var height=Number(oMapForm.mf_imageHeight.value);
			var newX1=((this.x1+this.x2)/2)-((width*dZoomFactor)/2);
			var newX2=((this.x1+this.x2)/2)+((width*dZoomFactor)/2);
			var newY1=((this.y1+this.y2)/2)-((height*dZoomFactor)/2);
			var newY2=((this.y1+this.y2)/2)+((height*dZoomFactor)/2);
			oMapForm.mf_imageXMin.value=newX1;
			oMapForm.mf_imageYMin.value=newY1;
			oMapForm.mf_imageXMax.value=newX2;
			oMapForm.mf_imageYMax.value=newY2;
		}else{
			oMapForm.mf_imageXMin.value=this.x1;
			oMapForm.mf_imageYMin.value=this.y1;
			oMapForm.mf_imageXMax.value=this.x2;
			oMapForm.mf_imageYMax.value=this.y2;
		}
		this.stopZoomBox();
	}
	/**
	* Cette fonction modifie l'étendue associée à l'objet GraphicLayer en cours
	*/
	function gfx_changeExtent(){
		this.oGfxLayer.changeShapesExtent(new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight));
		if(parent.frames.map_toolbar.dCurrentScale <= this.oGfxLayer.iZoomLimit){
			this.oGfxLayer.drawShapes();
		}else{
			this.oGfxLayer.cleanShapes(); //Masque les objets si il y a un seuil de zoom dans la vm_table
		}
	}

	/**
	* Cette fonction permet d'initialiser les objets 'Vm_gfx' permettant par la suite d'éditer en JavaScript 
	* la géométrie de n'importe quel objet. Attention, le dessin JavaScript / Walter Zorn n'est pas mis en place ici.
	* \param sType Type de géométrie à dessiner en JavaScript.
	* \param aXY Tableau contenant la liste des coordonnées de l'objet à dessiner
	*/
	function initGfxGeom(sType,aXY){
		this.setLayerVisibility("EditLayer",true);
		this.oGfxGeom=new VmShape(this.sDivName+"EditLayer", new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight), sType);
		this.oGfxGeomVertex=new VmShape(this.sDivName+"EditLayer", new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight), "point");
		switch(sType){
			case "point" :
				this.oGfxLayer.addShape(this.oGfxGeom);
				this.oGfxGeom.setVmPoint("#393",6,"square");
				this.oGfxGeom.addXY(new VmVertex(aXY[0][0],aXY[0][1]));
			break;
			case "line" :
				this.oGfxGeom.setVmLine(2,"#393",parent.aMeasure);
				this.oGfxGeom.setVmLineVertex(6,"#339");
			case "polygon" :
				this.oGfxGeom.setVmPolygon(2,"#393",parent.aMeasure);
				this.oGfxGeom.setVmPolygonVertex(6,"#339");
			default :
				this.oGfxLayer.addShape(this.oGfxGeom);
				var iXYlg=aXY.length-1;
				for(var i=0; i<=iXYlg; i++){
					this.oGfxGeom.addXY(new VmVertex(aXY[i][0],aXY[i][1]));
				}
			break;
		}
		// DEPRECATED (this.oGfxLayer.iZoomLimit='1.7976931348623157e+308') module saisie2 2009_02
		if((parent.frames.map_toolbar.dCurrentScale<=parseInt(this.oGfxLayer.iZoomLimit)) || (this.oGfxLayer.iZoomLimit='1.7976931348623157e+308')){
			this.oGfxLayer.drawShapes();
		}else{
			alert('L\'échelle à laquelle vous vous situez ne permet pas la modification de la géométrie.\nVous devez vous situez en dessous du 1/'+parseInt(this.oGfxLayer.iZoomLimit)+"ème.");
		}
	}

	/**
	* Retourne la géométrie pour l'impression (uniquement si la géométrie a été saisie avec les outils "Mesures et sélections").
	* \param sGeom géométrie.
	* \param sGeomType Type de géométrie.
	* \return la géométrie pour l'impression.
	*/
	function setPrintableGeom(sGeom,sGeomType){
		if(parent.YAHOO.veremap.oDivDraw && parent.YAHOO.veremap.oDivDraw.cfg.getProperty('visible')){
			if(sGeom != ""){
				var aGeom=sGeom.split(/\s+/g);
				for (var i=0; i<aGeom.length; i++){
					if(i==0){
						sGeom=aGeom[i];
					}else{
						if((i % 2) == 0){
							sGeom += "|"+aGeom[i];
						}else{
							sGeom += "@"+aGeom[i];
						}
					}
				}
				return sGeom += "|"+sGeomType;
			}else{
				return sGeom;
			}
		}
	}

	/**
	* Traitement de la géométrie pour la selection (uniquement si la géométrie a été saisie avec les outils "Mesures et sélections").
	* \param sGeom géométrie.
	* \param sGeomType Type de géométrie.
	*/
	function setSelectTableGeom(sGeom,sGeomType){
		if(parent.YAHOO.veremap.oDivDraw && parent.YAHOO.veremap.oDivDraw.cfg.getProperty('visible')){
			if(sGeom != ""){
				var aGeom=sGeom.split(/\s+/g);
				for (var i=0; i<aGeom.length; i++){
					if(i==0){
						sGeom=aGeom[i];
					}else{
						if((i % 2) == 0){
							sGeom += " "+aGeom[i];
						}else{
							sGeom += "@"+aGeom[i];
						}
					}
				}
				return sGeom;
			}else{
				return sGeom;
			}
		}
	}

	/**
	* Dessine un cercle à partir des coordonnées du centre et d'un point du cercle.
	* \param aXYCenter Coordonnées images du centre du cercle.
	* \param aXY Coordonnées images d'un point du cercle.
	* \return la géométrie pour l'impression.
	*/
	function drawCircle(aXYCenter,aXY){
		parent.map_toolbar.buttonClean();
		this.setLayerVisibility("EditLayer",true);
		this.oGfxCircle=new VmShape(this.sDivName+"EditLayer", new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight), "circle");
		this.oGfxLayer.addShape(this.oGfxCircle);
		this.oGfxCircle.addIJ(new VmVertex(aXYCenter[0],aXYCenter[1]));
		this.oGfxCircle.addIJ(new VmVertex(aXY[0],aXY[1]));
		this.oGfxCircle.setVmCircle(2,"#f00",parent.aMeasure);
		this.oGfxLayer.drawShapes();
		parent.frames.frameDrawTools.enableTools("select_circle");
		// Outils de mesure
		parent.YAHOO.veremap.oDivMeasure.setBody(this.oGfxCircle.getHTMLSummary());
		// Récupération de la géométrie pour l'impression et la sélection.
		parent.frames.map_toolbar.document.mapForm.mf_selectShape.value=setSelectTableGeom(this.oGfxCircle.toStringXY(),"polygon");
		return setPrintableGeom(this.oGfxCircle.toStringXY(),"circle");
	}
	/**
	* Dessinne un rectangle dans la couche "PrintLayer".
	* \param iXmin Coordonnée X minimun en pixel du rectangle.
	* \param iYmin Coordonnée Y minimun en pixel du rectangle.
	* \param iXmax Coordonnée X maximun en pixel du rectangle.
	* \param iYmax Coordonnée Y maximun en pixel du rectangle.
	*/
	function drawPrintRect(iXmin,iYmin,iXmax,iYmax){
		this.setLayerVisibility("PrintLayer",true);
		this.oPrintLayer.clear();
		this.oPrintLayer.drawPolyline(new Array(iXmin,iXmax,iXmax,iXmin,iXmin), new Array(iYmin,iYmin,iYmax,iYmax,iYmin));
		this.oPrintLayer.paint();
	}
	/**
	* fonction qui permet de créer un objet point.
	* \return un objet point.
	*/
	function createPoint(){
		var oPoint=new VmShape(this.sDivName+"EditLayer", new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight),"point");
		oPoint.setVmPoint("#00f",6,"circle");
		oPoint.addIJ(new VmVertex(this.mouseX,this.mouseY));
		return oPoint;
	}
	/**
	* Supprime le dernier point tracé (mise à jour du tableau de mesures et de la géométrie pour la sélection et l'impression).
	*/
	function deleteLastPoint(){
		var sMeasure=sPrintableGeom=sType="";
		var oMapForm=parent.frames.map_toolbar.document.mapForm;
		var bCleanAll=false;
		if(this.oGfxCircle){
			var aCoordCenter=new Array(this.oGfxCircle.aIJ[0].x,this.oGfxCircle.aIJ[0].y);
			parent.frames.frameDrawTools.buttonClean();
			this.setLayerVisibility("EditLayer",true);
			this.oGfxPoint=new VmShape(this.sDivName+"EditLayer", new VmRectangle(new VmVertex(this.envxmin,this.envymin), new VmVertex(this.envxmax,this.envymax), this.mapwidth, this.mapheight),"point");
			this.oGfxPoint.setVmPoint("#00f",6,"circle");
			this.oGfxPoint.addIJ(new VmVertex(aCoordCenter[0],aCoordCenter[1]));
			this.oGfxLayer.addShape(this.oGfxPoint);
			this.oGfxLayer.drawShapes();
			parent.frames.map_toolbar.openStandardWindow(YAHOO.veremap.oDivRaduisCircle);
			sMeasure=oMapImage.oGfxPoint.getHTMLSummary();
			sPrintableGeom="";
			oMapForm.mf_selectShape.value="";
		}else if(this.oGfxPolygon){
			if(this.oGfxPolygon.aIJ.length>0){
				if(this.oGfxPolygon.aIJ.length==1){
					bCleanAll=true;
				}else{
					if(this.oGfxPolygon.aXY[0].x==this.oGfxPolygon.aXY[this.oGfxPolygon.aXY.length-1].x && this.oGfxPolygon.aXY[0].y==this.oGfxPolygon.aXY[this.oGfxPolygon.aXY.length-1].y){
						parent.frames.frameDrawTools.desableTools();
						this.oGfxPolygon.closed=false;
					}else{
						this.aGfxPolyText.pop();
						this.oGfxLayer.aShape.pop();
					}
					this.oGfxPolygon.dropLastVertex();
					this.oGfxLayer.drawShapes();
					sMeasure=this.oGfxPolygon.getHTMLSummary();
					sPrintableGeom=setPrintableGeom(this.oGfxPolygon.toStringXY(),"line");
					oMapForm.mf_selectShape.value=setSelectTableGeom(this.oGfxPolygon.toStringXY(),"line");
				}
			}
		}else if(this.oGfxLine){
			if(this.oGfxLine.aIJ.length>0){
				if(this.oGfxLine.aIJ.length==1){
					bCleanAll=true;
				}else{
					this.oGfxLine.dropLastVertex();
					this.aGfxLineText.pop();
					this.oGfxLayer.aShape.pop();
					this.oGfxLayer.drawShapes();
					sMeasure=this.oGfxLine.getHTMLSummary();
					sPrintableGeom=setPrintableGeom(this.oGfxLine.toStringXY(),"line");
					oMapForm.mf_selectShape.value=setSelectTableGeom(this.oGfxLine.toStringXY(),"line");
				}
			}
		}else if(this.aGfxMultiPoint.length>0){
			this.aGfxMultiPoint.pop();
			this.oGfxLayer.aShape.pop();
			this.oGfxLayer.drawShapes();
			if(this.aGfxMultiPoint.length==1){
				this.oGfxPoint=this.aGfxMultiPoint[0];
				this.aGfxMultiPoint=new Array();
				oMapForm.mf_x.value=this.oGfxPoint.aXY[0].x;
				oMapForm.mf_y.value=this.oGfxPoint.aXY[0].y;
				sMeasure=this.oGfxPoint.getHTMLSummary();
				sPrintableGeom=this.oGfxPoint.toStringXY();
				sPrintableGeom=setPrintableGeom(sPrintableGeom,"point");
			}else{
				this.setFormPoint();
				for (var i=0; i<this.aGfxMultiPoint.length; i++){
					sMeasure+=this.aGfxMultiPoint[i].getHTMLSummary();
					sPrintableGeom+=" "+this.aGfxMultiPoint[i].toStringXY();
				}
				sPrintableGeom=setPrintableGeom(sPrintableGeom.replace(/^\s+/,""),"multipoint");
			}
		}else if(this.oGfxPoint){
			bCleanAll=true;
		}
		if(bCleanAll){
			parent.frames.frameDrawTools.buttonClean();
		}else{
			// Outils de mesure et récupération de la géométrie pour l'impression.
			parent.YAHOO.veremap.oDivMeasure.setBody(sMeasure);
			oMapForm.mf_printDrawing.value=sPrintableGeom;
		}
	}

/**
* Cette fonction permet d'activer différents traitements lors du déplacement de la souris sur la carte.
*
* Elle est liée à l'évènement JavaScript "onMouseMove".
* \return FAUX.
*/
function mouseMove(e){
	isIE ? oEvent=event : oEvent=e;
	oMapImage=getObjectMapImage();
	oMapImage.getImageXY();
	if(oMapImage.mouseX < 0 || oMapImage.mouseY < 0 ) return false;
	var param=new Array();
	param=oMapImage.calcCoordGeo(oMapImage.mouseX,oMapImage.mouseY);
	oMapImage.coordx=param[0];
	oMapImage.coordy=param[1];
	param=null;
	var sTool=parent.map_toolbar.document.mapForm.mf_tool.value;

	var u=Math.pow(10,2);
	var a_coordx=Math.round(oMapImage.coordx*u)/u;
	var a_coordy=Math.round(oMapImage.coordy*u)/u;
	var sCursorPosition="Outil : <span class=\"mapInfo\">"+sTool+"</span> Curseur : ("+a_coordx.toString()+", "+a_coordy.toString()+") ("+oMapImage.mouseX.toString()+", "+oMapImage.mouseY.toString()+")";
	document.getElementById("cursorPosition").innerHTML=sCursorPosition;

	if((oMapImage.mouseX > oMapImage.mapwidth) || (oMapImage.mouseY > oMapImage.mapheight) || (oMapImage.mouseX <= 0) || (oMapImage.mouseY <=0)){
		// Si le curseur quitte la carte on lance mouseUp();
		mouseUp(oEvent);
	}else{
		switch (sTool){
			case "pan" :
				if(oMapImage.mouseD == true){
					oMapImage.iXDiff=oMapImage.mouseX-oMapImage.iMouseXStart;
					oMapImage.iYDiff=oMapImage.mouseY-oMapImage.iMouseYStart;
					document.getElementById(oMapImage.sDivName+"1").style.left=oMapImage.iXDiff+"px";
					document.getElementById(oMapImage.sDivName+"1").style.top=oMapImage.iYDiff+"px";
				}
			break;
			case "etablissement_rect":
			case "habitat_precaire_rect":
			case "select_rect":
			case "zoom_in":
				if(oMapImage.zooming){
					oMapImage.x2=oMapImage.mouseX;
					oMapImage.y2=oMapImage.mouseY;
					oMapImage.setClip();
				}
			break;
			case "draw_poly" :
				if(oMapImage.oGfxPolygon){
					if(oMapImage.oGfxPolygon.aIJ[0] && (Math.abs(oMapImage.mouseX-oMapImage.oGfxPolygon.aIJ[0].x) <= 2) && (Math.abs(oMapImage.mouseY-oMapImage.oGfxPolygon.aIJ[0].y) <= 2)){
						oMapImage.setCursor("pointer");
					}else{
						oMapImage.setCursor("crosshair");
					}
				}
			break;
			case "draw_circle" :
				if(oMapImage.oGfxPoint){
					var iXCenter=oMapImage.oGfxPoint.aXY[0].x;
					var iYCenter=oMapImage.oGfxPoint.aXY[0].y;
					// Calcul du rayon.
					if(iXCenter > a_coordx.toString()){
						var iAC=iXCenter-a_coordx.toString();
					}else{
						var iAC=a_coordx.toString()-iXCenter;
					}
					if(iYCenter > a_coordy.toString()){
						var iBC=iYCenter-a_coordy.toString();
					}else{
						var iBC=a_coordy.toString()-iYCenter;
					}
					document.getElementById('raduisCircleDist').innerHTML=Math.round(Math.sqrt((iAC*iAC)+(iBC*iBC)));
				}
			break;
			case "geom_move_vertex" :
				// DEPRECATED (this.oGfxLayer.iZoomLimit='1.7976931348623157e+308') module saisie2 2009_02
				if((parent.frames.map_toolbar.dCurrentScale<=parseInt(oMapImage.oGfxLayer.iZoomLimit)) || (oMapImage.oGfxLayer.iZoomLimit='1.7976931348623157e+308')){
				if(oMapImage.mouseD && (oMapImage.iEditedVertex != null) && (oMapImage.iEditedVertex != -1)){
					oMapImage.oGfxLayer.cleanShapes();
					oMapImage.oGfxGeomVertex.cleanShape();

					oMapImage.oGfxGeom.aIJ[oMapImage.iEditedVertex].setX(oMapImage.mouseX);
					oMapImage.oGfxGeom.aIJ[oMapImage.iEditedVertex].setY(oMapImage.mouseY);
					oMapImage.oGfxGeom.aXY[oMapImage.iEditedVertex].setX(oMapImage.coordx);
					oMapImage.oGfxGeom.aXY[oMapImage.iEditedVertex].setY(oMapImage.coordy);
					if((oMapImage.oGfxGeom.type == "polygon") && (oMapImage.iEditedVertex == 0)){
						oMapImage.oGfxGeom.aIJ[oMapImage.oGfxGeom.aIJ.length-1].setX(oMapImage.mouseX);
						oMapImage.oGfxGeom.aIJ[oMapImage.oGfxGeom.aIJ.length-1].setY(oMapImage.mouseY);
						oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].setX(oMapImage.coordx);
						oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].setY(oMapImage.coordy);
					}
					oMapImage.oGfxGeom.redrawSegments(oMapImage.iEditedVertex);

					// Dessin du vertex sélectionné
					oMapImage.oGfxGeomVertex.aIJ[0].setX(oMapImage.mouseX);
					oMapImage.oGfxGeomVertex.aIJ[0].setY(oMapImage.mouseY);
					oMapImage.oGfxGeomVertex.aXY[0].setX(oMapImage.coordx);
					oMapImage.oGfxGeomVertex.aXY[0].setY(oMapImage.coordy);
					oMapImage.oGfxGeomVertex.drawShape();
				}}
			break;
		} // fin du switch
	}
	return false;
}

/**
* Cette fonction permet d'activer différents traitements lors d'un clique sur la carte (bouton cliqué).
* Elle est liée à l'évènement JavaScript "onMouseDown".
* \return false.
*/
function mouseDown(e){
	isIE ? oEvent=event : oEvent=e;
	oMapImage=getObjectMapImage();
	oMapImage.getImageXY();
	if(oMapImage.mouseX < 0 || oMapImage.mouseY < 0 ) return false;
	var sGeomForPrint='';
	var oMapForm=parent.map_toolbar.document.mapForm;
	var aTool=oMapForm.mf_tool.value;
	oMapForm.mf_eventLocation.value=oMapImage.sDivName;
	switch (aTool){
		case "zoom_in":
			oMapImage.setLayerVisibility("BoxLayer",true);
			oMapImage.startZoomBox();
		break;
		case "zoom_out":
			oMapImage.mouseD=true;
			oMapForm.mf_imageX.value=oMapImage.mouseX;
			oMapForm.mf_imageY.value=oMapImage.mouseY;
			setExtent(oMapImage.envxmin,oMapImage.envymin,oMapImage.envxmax,oMapImage.envymax);
		break;
		case "pan":
			oMapImage.mouseD=true;
			document.getElementById(oMapImage.sDivName+"1").style.position="absolute";
			oMapImage.iMouseXStart=oMapImage.mouseX;
			oMapImage.iMouseYStart=oMapImage.mouseY;
			oMapImage.oGfxLayer.cleanShapes();
		break;
		case "info" :
			oMapImage.mouseD=true;
		break;
		case "etablissement_rect" :
		case "habitat_precaire_rect" :
		case "select_rect":
			oMapImage.setLayerVisibility("BoxLayer",true);
			oMapImage.startZoomBox();
		break;
		// Outils de dessins standards
		case "draw_point":
			var sType="point";
			var sMeasure=sPrintableGeom="";
			if(parent.YAHOO.veremap.oDivDraw.cfg.getProperty('visible') && (oMapImage.oGfxPoint || oMapImage.aGfxMultiPoint.length>1) && !oMapImage.oGfxPolygon && !oMapImage.oGfxLine && !oMapImage.oGfxCircle){
				if(oMapImage.aGfxMultiPoint.length==0){
					oMapImage.aGfxMultiPoint.push(oMapImage.oGfxPoint);
					oMapImage.oGfxPoint="";
				}
				var oPoint=oMapImage.createPoint();
				oMapImage.aGfxMultiPoint.push(oPoint);
				sType="multipoint";
				for (var i=0; i<oMapImage.aGfxMultiPoint.length; i++){
					sMeasure+=oMapImage.aGfxMultiPoint[i].getHTMLSummary();
					sPrintableGeom+=" "+oMapImage.aGfxMultiPoint[i].toStringXY();
				}
				//oMapForm.mf_selectShape.value=setSelectTableGeom(sPrintableGeom.replace(/^\s+/,""),sType);
			}else{
				parent.map_toolbar.buttonClean();
				oMapImage.setLayerVisibility("EditLayer",true);
				var oPoint=oMapImage.createPoint();
				oMapImage.oGfxPoint=oPoint;
				sMeasure=oMapImage.oGfxPoint.getHTMLSummary();
				sPrintableGeom=oMapImage.oGfxPoint.toStringXY();
			}
			oMapImage.oGfxLayer.addShape(oPoint);
			oMapImage.oGfxLayer.drawShapes();
			oMapImage.setFormPoint();
			parent.frames.frameDrawTools.enableTools("select_point");
			// Module de SAISIE2 DEPRECATED 2008_02
			if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed)
				parent.map_toolbar.oSaisieTableWin.focus();
			// Outils de mesure et récupération de la géométrie pour l'impression.
			parent.YAHOO.veremap.oDivMeasure.setBody(sMeasure);
			sGeomForPrint=setPrintableGeom(sPrintableGeom.replace(/^\s+/,""),sType);
		break;
		case "draw_line":
			if(!oMapImage.oGfxLine){
				if(oMapImage.oGfxPolygon || oMapImage.oGfxPoint || oMapImage.oGfxCircle|| oMapImage.aGfxMultiPoint.length>0) parent.map_toolbar.buttonClean();
				oMapImage.setLayerVisibility("EditLayer",true);
				oMapImage.oGfxLine=new VmShape(oMapImage.sDivName+"EditLayer", new VmRectangle(new VmVertex(oMapImage.envxmin,oMapImage.envymin), new VmVertex(oMapImage.envxmax,oMapImage.envymax), oMapImage.mapwidth, oMapImage.mapheight), "line");
				oMapImage.oGfxLine.setVmLine(2,"#f00",parent.aMeasure);
				oMapImage.oGfxLayer.addShape(oMapImage.oGfxLine);
			}
			oMapImage.oGfxLine.addIJ(new VmVertex(oMapImage.mouseX,oMapImage.mouseY));
			oMapImage.oGfxLine.drawLastSegment();
			oMapImage.aGfxLineText[oMapImage.aGfxLineText.length]=new VmShape(oMapImage.sDivName+"EditLayer", new VmRectangle(new VmVertex(oMapImage.envxmin,oMapImage.envymin), new VmVertex(oMapImage.envxmax,oMapImage.envymax), oMapImage.mapwidth, oMapImage.mapheight), "text");
			oMapImage.aGfxLineText[oMapImage.aGfxLineText.length-1].setVmText("arial","12px","#f00",Font.PLAIN,oMapImage.aGfxLineText.length,5);
			oMapImage.oGfxLayer.addShape(oMapImage.aGfxLineText[oMapImage.aGfxLineText.length-1]);
			oMapImage.aGfxLineText[oMapImage.aGfxLineText.length-1].addIJ(new VmVertex(oMapImage.mouseX,oMapImage.mouseY));				
			oMapImage.aGfxLineText[oMapImage.aGfxLineText.length-1].drawShape();
			parent.frames.frameDrawTools.enableTools("select_line");
			// Outils de mesure
			parent.YAHOO.veremap.oDivMeasure.setBody(oMapImage.oGfxLine.getHTMLSummary());
			// Module de SAISIE2 DEPREDATED 2008_02
			if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed)
				parent.map_toolbar.oSaisieTableWin.focus();
			// Récupération de la géométrie pour l'impression et pour la sélection.
			sGeomForPrint=setPrintableGeom(oMapImage.oGfxLine.toStringXY(),"line");
			oMapForm.mf_selectShape.value=setSelectTableGeom(oMapImage.oGfxLine.toStringXY(),"line");
		break;
		case "draw_poly":
			var bAddVertex=true;
			var iObjectX,iObjectY;
			if(!oMapImage.oGfxPolygon){
				if(oMapImage.oGfxLine || oMapImage.oGfxPoint || oMapImage.oGfxCircle|| oMapImage.aGfxMultiPoint.length>0) parent.map_toolbar.buttonClean();
				oMapImage.setLayerVisibility("EditLayer",true);
				oMapImage.oGfxPolygon=new VmShape(oMapImage.sDivName+"EditLayer", new VmRectangle(new VmVertex(oMapImage.envxmin,oMapImage.envymin), new VmVertex(oMapImage.envxmax,oMapImage.envymax), oMapImage.mapwidth, oMapImage.mapheight), "polygon");
				oMapImage.oGfxPolygon.setVmPolygon(2,"FF0000",parent.aMeasure);
				oMapImage.oGfxLayer.addShape(oMapImage.oGfxPolygon);
				oMapImage.oGfxPoint=new VmShape(oMapImage.sDivName+"EditLayer", new VmRectangle(new VmVertex(oMapImage.envxmin,oMapImage.envymin), new VmVertex(oMapImage.envxmax,oMapImage.envymax), oMapImage.mapwidth, oMapImage.mapheight), "point");
				oMapImage.oGfxPoint.setVmPoint("#00f",6,"circle");
				oMapImage.oGfxLayer.addShape(oMapImage.oGfxPoint);
			}
			if(oMapImage.oGfxPolygon.aIJ[0] && (Math.abs(oMapImage.mouseX-oMapImage.oGfxPolygon.aIJ[0].x) <= 2) && (Math.abs(oMapImage.mouseY-oMapImage.oGfxPolygon.aIJ[0].y) <= 2)){
				bAddVertex=oMapImage.oGfxPolygon.addXY(new VmVertex(oMapImage.oGfxPolygon.aXY[0].x,oMapImage.oGfxPolygon.aXY[0].y));
				oMapImage.oGfxPolygon.drawLastSegment();
				oMapImage.oGfxPoint.drawShape();
				parent.frames.frameDrawTools.enableTools("select_poly");
			}else{
				bAddVertex=oMapImage.oGfxPolygon.addIJ(new VmVertex(oMapImage.mouseX,oMapImage.mouseY));
				oMapImage.oGfxPoint.addIJ(new VmVertex(oMapImage.mouseX,oMapImage.mouseY));
				if(bAddVertex){
					oMapImage.oGfxPolygon.drawLastSegment();
					oMapImage.oGfxPoint.drawShape();
					oMapImage.aGfxPolyText[oMapImage.aGfxPolyText.length]=new VmShape(oMapImage.sDivName+"EditLayer", new VmRectangle(new VmVertex(oMapImage.envxmin,oMapImage.envymin), new VmVertex(oMapImage.envxmax,oMapImage.envymax), oMapImage.mapwidth, oMapImage.mapheight), "text");
					oMapImage.aGfxPolyText[oMapImage.aGfxPolyText.length-1].setVmText("arial","12px","#f00",Font.PLAIN,oMapImage.aGfxPolyText.length,5);
					oMapImage.oGfxLayer.addShape(oMapImage.aGfxPolyText[oMapImage.aGfxPolyText.length-1]);
					oMapImage.aGfxPolyText[oMapImage.aGfxPolyText.length-1].addIJ(new VmVertex(oMapImage.mouseX,oMapImage.mouseY));
					oMapImage.aGfxPolyText[oMapImage.aGfxPolyText.length-1].drawShape();
				}
			}
			// Outils de mesure
			if(bAddVertex){
				parent.YAHOO.veremap.oDivMeasure.setBody(oMapImage.oGfxPolygon.getHTMLSummary());
			}
			// Module de SAISIE2 DEPRECATED 2008_02
			if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed)
				parent.map_toolbar.oSaisieTableWin.focus();
			// Récupération de la géométrie pour l'impression et la sélection.
			sGeomForPrint=setPrintableGeom(oMapImage.oGfxPolygon.toStringXY(), "polygon");
			oMapForm.mf_selectShape.value=setSelectTableGeom(oMapImage.oGfxPolygon.toStringXY(), "polygon");
		break;
		case "draw_circle":
			if(!oMapImage.oGfxPoint || oMapImage.oGfxPolygon || oMapImage.oGfxLine || oMapImage.oGfxCircle|| oMapImage.aGfxMultiPoint.length>0){
				if(oMapImage.oGfxPolygon || oMapImage.oGfxLine || oMapImage.oGfxCircle|| oMapImage.aGfxMultiPoint.length>0) parent.map_toolbar.buttonClean();
				oMapImage.setLayerVisibility("EditLayer",true);
				oMapImage.oGfxPoint=oMapImage.createPoint();
				oMapImage.oGfxLayer.addShape(oMapImage.oGfxPoint);
				oMapImage.oGfxLayer.drawShapes();
				parent.frames.map_toolbar.openStandardWindow(YAHOO.veremap.oDivRaduisCircle);
			}else{
				sXYCenter=oMapImage.oGfxPoint.toStringIJ();
				aXYCenter=sXYCenter.split(/\s+/g);
				aXY=[oMapImage.mouseX,oMapImage.mouseY];
				sGeomForPrint=oMapImage.drawCircle(aXYCenter,aXY);
			}
		break;
		// -------
		case "geom_add_vertex" :
		case "draw_geom_add_vertex" :
		case "geom_cut_line" :
		case "draw_geom_cut_line" :
			oMapImage.mouseD=true;
		break;
		case "geom_move_vertex" :
			oMapImage.mouseD=true;
			var bMoveVertex=true;
		case "geom_drop_vertex" :
			if(parent.map_toolbar.dCurrentScale <= oMapImage.oGfxLayer.iZoomLimit){
				oMapImage.iEditedVertex=-1;
				var aXY=oMapImage.calcCoordGeo(oMapImage.mouseX,oMapImage.mouseY);
				var oVertex=new VmVertex(aXY[0],aXY[1]);
				var dTempDistance,dTolerance,iVertexToEdit=0;
				var dDistance=oVertex.distanceTo(oMapImage.oGfxGeom.getShapeVertexXY(0));

				for(var i=1; i<oMapImage.oGfxGeom.aXY.length; i++){
					dTempDistance=oVertex.distanceTo(oMapImage.oGfxGeom.getShapeVertexXY(i));
					if(dTempDistance < dDistance){
						dDistance=dTempDistance;
						iVertexToEdit=i;
					}
				}

				// --- Module de SAISIE2
				if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed)
					//DEPRECATED 2008_02
					dTolerance=((oMapImage.envxmax-oMapImage.envxmin) / oMapImage.mapwidth) * parent.map_toolbar.oSaisieTableWin.mainFrame.iGfxGeomEditTolerance;
				if(parent.YAHOO.veremap.oDivModuleSaisie && parent.document.getElementById('divModuleSaisie_c').style.visibility == 'visible'){
					dTolerance=((oMapImage.envxmax-oMapImage.envxmin) / oMapImage.mapwidth) * parent.frames.frameModuleSaisie.mainFrame.iGfxGeomEditTolerance;
				}
				// --- Module de DESSIN
				if(parent.YAHOO.veremap.oDivModuleDessin && parent.YAHOO.veremap.oDivModuleDessin.cfg.getProperty('visible'))
					dTolerance=((oMapImage.envxmax-oMapImage.envxmin) / oMapImage.mapwidth) * parent.frames.frameModuleDessin.mainFrame.iGfxGeomEditTolerance;				
				// --- Module PDIPR
				if(parent.YAHOO.veremap.oDivPdipr && parent.YAHOO.veremap.oDivPdipr.cfg.getProperty('visible'))
					dTolerance=((oMapImage.envxmax-oMapImage.envxmin) / oMapImage.mapwidth) * parent.frames.framePdipr.mainFrame.iGfxGeomEditTolerance;
				// --- Module PATRIMOINE
				if(parent.YAHOO.veremap.oDivModulePatrimoine && parent.YAHOO.veremap.oDivModulePatrimoine.cfg.getProperty('visible'))
					dTolerance=((oMapImage.envxmax-oMapImage.envxmin) / oMapImage.mapwidth) * parent.frames.frameModulePatrimoine.mainFrame.iGfxGeomEditTolerance;
				// ---
				if(dTolerance >= dDistance) oMapImage.iEditedVertex=iVertexToEdit;
				if(!bMoveVertex){
					if(oMapImage.iEditedVertex != -1){
						var bDropped=oMapImage.oGfxGeom.dropVertex(oMapImage.iEditedVertex);
					}
					oMapImage.oGfxLayer.drawShapes();
					// --- Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
					// ---
				}else{
					if(oMapImage.iEditedVertex != -1){
						oMapImage.oGfxGeomVertex.setVmPoint("#f00",10,"square");
						oMapImage.oGfxGeomVertex.addIJ(new VmVertex(oMapImage.oGfxGeom.aIJ[oMapImage.iEditedVertex].x,oMapImage.oGfxGeom.aIJ[oMapImage.iEditedVertex].y));
						oMapImage.oGfxGeomVertex.drawShape();
					}
				}
			}else{
				// --- Module de SAISIE2 DEPRECATED 2008_02
				if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
					parent.map_toolbar.oSaisieTableWin.focus();
				// ---
			}
			break;
		case "geom_extend_line" :
			if(parent.map_toolbar.dCurrentScale <= oMapImage.oGfxLayer.iZoomLimit){
				if(oMapImage.oGfxGeom.type != "line"){
					// --- Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
					// ---
				}else{
					oMapImage.mouseD=true;
					var aXY=oMapImage.calcCoordGeo(oMapImage.mouseX,oMapImage.mouseY);
					var oVertex=new VmVertex(aXY[0],aXY[1]);
					var sExtendLineType='null';

					// --- Module de SAISIE2
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed){	
						// Initialisation  DEPRECATED 2008_02
						if(!parent.map_toolbar.oSaisieTableWin.mainFrame.bExtendLine || parent.map_toolbar.oSaisieTableWin.mainFrame.bExtendLine == null){
							parent.map_toolbar.oSaisieTableWin.mainFrame.bExtendLine=true;
							var dFirstVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[0].x,oMapImage.oGfxGeom.aXY[0].y));
							var dLastVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].x,oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].y));
							(dLastVertexDistance <= dFirstVertexDistance) ? 
							parent.map_toolbar.oSaisieTableWin.mainFrame.sExtendLineType="end" :
							parent.map_toolbar.oSaisieTableWin.mainFrame.sExtendLineType="start";
						}
						// Prolongation
						sExtendLineType=parent.map_toolbar.oSaisieTableWin.mainFrame.sExtendLineType;
					}
					if(parent.YAHOO.veremap.oDivModuleSaisie && parent.document.getElementById('divModuleSaisie_c').style.visibility == 'visible'){
						// Initialisation  
						if(!parent.frames.frameModuleSaisie.mainFrame.bExtendLine || parent.frames.frameModuleSaisie.mainFrame.bExtendLine == null){
							parent.frames.frameModuleSaisie.mainFrame.bExtendLine=true;
							var dFirstVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[0].x,oMapImage.oGfxGeom.aXY[0].y));
							var dLastVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].x,oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].y));
							(dLastVertexDistance <= dFirstVertexDistance) ? 
							parent.frames.frameModuleSaisie.mainFrame.sExtendLineType="end" :
							parent.frames.frameModuleSaisie.mainFrame.sExtendLineType="start";
						}
						// Prolongation
						sExtendLineType=parent.frames.frameModuleSaisie.mainFrame.sExtendLineType;
					}
					// --- Module de DESSIN
					if(parent.YAHOO.veremap.oDivModuleDessin && parent.YAHOO.veremap.oDivModuleDessin.cfg.getProperty('visible')){	
						// Initialisation
						if(!parent.frames.frameModuleDessin.mainFrame.bExtendLine || parent.frames.frameModuleDessin.mainFrame.bExtendLine == null){
							parent.frames.frameModuleDessin.mainFrame.bExtendLine=true;
							var dFirstVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[0].x,oMapImage.oGfxGeom.aXY[0].y));
							var dLastVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].x,oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].y));
							(dLastVertexDistance <= dFirstVertexDistance) ? 
							parent.frames.frameModuleDessin.mainFrame.sExtendLineType="end" :
							parent.frames.frameModuleDessin.mainFrame.sExtendLineType="start";
						}
						// Prolongation
						sExtendLineType=parent.frames.frameModuleDessin.mainFrame.sExtendLineType;
					}
					// --- Module PDIPR
					if(parent.YAHOO.veremap.oDivPdipr && parent.YAHOO.veremap.oDivPdipr.cfg.getProperty('visible')){
						// Initialisation
						if(!parent.frames.framePdipr.mainFrame.bExtendLine || parent.frames.framePdipr.mainFrame.bExtendLine == null){
							parent.frames.framePdipr.mainFrame.bExtendLine=true;
							var dFirstVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[0].x,oMapImage.oGfxGeom.aXY[0].y));
							var dLastVertexDistance=oVertex.distanceTo(new VmVertex(oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].x,oMapImage.oGfxGeom.aXY[oMapImage.oGfxGeom.aXY.length-1].y));
							(dLastVertexDistance <= dFirstVertexDistance) ? 
							parent.frames.framePdipr.mainFrame.sExtendLineType="end" :
							parent.frames.framePdipr.mainFrame.sExtendLineType="start";
						}
						// Prolongation
						sExtendLineType=parent.frames.framePdipr.mainFrame.sExtendLineType;
					}
					// ---
					
					if(sExtendLineType == "end"){
						oMapImage.oGfxGeom.addXY(oVertex);
					}else{
						oMapImage.oGfxGeom.reverseShape();
						oMapImage.oGfxGeom.addXY(oVertex);
						oMapImage.oGfxGeom.reverseShape();
					}
					oMapImage.oGfxLayer.drawShapes();

					// --- Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
				}
			}
		break;
		// Module CADASTRE
		case "cadastre_fiche_urb" :
		case "cadastre_rp_menu" :
		case "cadastre_prc_info" :
			sGeomForPrint=oMapForm.mf_printDrawing.value;
			oMapImage.mouseD=true;
		break;
		// Module PDIPR,DESSIN
		case "dessin_info" :
		case "pdipr_add_segment" :
			oMapImage.mouseD=true;
		break;
		// ---
	}

	parent.frames.map_toolbar.document.mapForm.mf_printDrawing.value=sGeomForPrint;
	parent.frames.map_toolbar.document.mapForm.mf_printAddDrawing.value='false';
	//------
	return false;
}

/**
* Cette fonction permet d'activer différents traitements lorsque la souris n'est plus utilisée (bouton décliqué).
*
* Elle est liée à l'évènement JavaScript "onMouseUp".
*/
function mouseUp(e){
	isIE ? oEvent=event : oEvent=e;
	oMapImage=getObjectMapImage();
	oMapImage.getImageXY();
	if(oMapImage.mouseX < 0 || oMapImage.mouseY < 0 ) return false;
	var oMapForm=parent.frames.map_toolbar.document.mapForm;
	var aTool=oMapForm.mf_tool.value;
	oMapForm.mf_eventLocation.value=oMapImage.sDivName;

	if(oMapImage.mouseD){
		oMapImage.mouseD=false;
		switch (aTool){
			case "zoom_in":
				oMapImage.setLayerVisibility("BoxLayer",false);
				oMapImage.changeExtent();
				parent.frames.map_toolbar.map_refresh();
			break;
			case "pan":
				oMapForm.mf_imageX.value=Math.round(getClientWidth()/2)-oMapImage.iXDiff;
				oMapForm.mf_imageY.value=Math.round(getClientHeight()/2)-oMapImage.iYDiff;
				setExtent(oMapImage.envxmin,oMapImage.envymin,oMapImage.envxmax,oMapImage.envymax);
				parent.frames.map_toolbar.map_refresh();
			break;
			case "zoom_out":
				parent.frames.map_toolbar.map_refresh();
			break;
			case "info":
				oMapImage.setFormPoint();
				parent.frames.map_toolbar.map_refresh();
			break;
			case "etablissement_rect" :
			case "habitat_precaire_rect" :
			case "select_rect":
				oMapImage.setLayerVisibility("BoxLayer",false);
				oMapImage.changeExtent();
				oMapImage.setFormRect();
				parent.frames.map_toolbar.map_refresh();
			break;
			case "geom_cut_line" :
			case "draw_geom_cut_line" :
				if(oMapImage.oGfxGeom.type != "line"){
					// Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
					break;
				}
			case "geom_add_vertex" :
			case "draw_geom_add_vertex" :
				if(parent.map_toolbar.dCurrentScale <= oMapImage.oGfxLayer.iZoomLimit){
					oMapImage.setFormPoint();
					// Module de SAISIE2
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed){
						// DEPRECATED 2008_02
						oMapForm.mf_geom_tolerance.value=parent.map_toolbar.oSaisieTableWin.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.map_toolbar.oSaisieTableWin.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);	
					}
					if(parent.YAHOO.veremap.oDivModuleSaisie && parent.document.getElementById('divModuleSaisie_c').style.visibility == 'visible'){
						oMapForm.mf_geom_tolerance.value=parent.frames.frameModuleSaisie.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.frames.frameModuleSaisie.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);
					}
					// Module de DESSIN
					if(parent.YAHOO.veremap.oDivModuleDessin && parent.YAHOO.veremap.oDivModuleDessin.cfg.getProperty('visible')){
						oMapForm.mf_geom_tolerance.value=parent.frames.frameModuleDessin.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.frames.frameModuleDessin.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);	
					}					
					// Module PDIPR
					if(parent.YAHOO.veremap.oDivPdipr && parent.YAHOO.veremap.oDivPdipr.cfg.getProperty('visible')){
						oMapForm.mf_geom_tolerance.value=parent.frames.framePdipr.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.frames.framePdipr.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);								
					}
					// Module PATRIMOINE
					if(parent.YAHOO.veremap.oDivModulePatrimoine && parent.YAHOO.veremap.oDivModulePatrimoine.cfg.getProperty('visible')){
						oMapForm.mf_geom_tolerance.value=parent.frames.frameModulePatrimoine.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.frames.frameModulePatrimoine.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);								
					}
					// ---
					// Module PATRIMOINE
					if(parent.YAHOO.veremap.oDivModulePatrimoine && parent.YAHOO.veremap.oDivModulePatrimoine.cfg.getProperty('visible')){
						oMapForm.mf_geom_tolerance.value=parent.frames.frameModulePatrimoine.mainFrame.iGfxGeomEditTolerance * 
						((oMapForm.mf_xMax.value-oMapForm.mf_xMin.value) / oMapForm.mf_imageWidth.value);
						oMapForm.mf_geom_wkt.value=parent.frames.frameModulePatrimoine.mainFrame.setGfxGeomWKT(oMapImage.oGfxGeom.type);								
					}
					// ---
					parent.frames.map_toolbar.map_refresh();
				}
			break;
			case "geom_move_vertex" :
				if(parent.map_toolbar.dCurrentScale <= oMapImage.oGfxLayer.iZoomLimit){
					if(oMapImage.iEditedVertex != -1){
						oMapImage.oGfxGeomVertex.cleanShape();
						oMapImage.oGfxGeomVertex.clearShape();
					}
					oMapImage.oGfxLayer.drawShapes();
					// Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
				}else{
					// Module de SAISIE2 DEPRECATED 2008_02
					if(parent.map_toolbar.oSaisieTableWin && !parent.map_toolbar.oSaisieTableWin.closed) 
						parent.map_toolbar.oSaisieTableWin.focus();
				}
			break;
			// Module CADASTRE
			case "cadastre_fiche_urb" :
			case "cadastre_rp_menu" :
			case "cadastre_prc_info" :
				oMapImage.setFormPoint();
				oMapForm.mf_cadastre_id_par.value="";
				parent.frames.map_toolbar.map_refresh();
			break;
			// Module PDIPR,DESSIN
			case "dessin_info" :
			case "pdipr_add_segment":
				oMapImage.setFormPoint();
				parent.map_toolbar.map_refresh();
			break;
		} 
	}
}

//-------------------------------------
// Récupération du navigateur
//-------------------------------------
/**
* Valorise le type de navigateur en cours d'utilisation.
* L'application fonctionne FireFox et IE 6.x et plus.
*/
function setBrowserType(){
	if(getBrowserInfo() =="Microsoft Internet Explorer"){
		isNav=false;
		isIE=true;
	}else if(getBrowserInfo() =="Netscape"){
		isNav=true;
		isIE=false;
	}else{
		alert("Navigateur non supporté : "+navigator.appName+"-"+navigator.appVersion);
		// Par défaut on considère que l'on est sur Firefox
		isNav=true;
		isIE=false;
	}
}

/**
* Cette fonction permet de récupérer des informations sur le navigateur web utilisé.
* \return Le nom du navigateur web utilisé.
*/ 
function getBrowserInfo(){
	return navigator.appName;
}

//----------------------------------------------
// Dimension de la carte
//----------------------------------------------

/**
* Cette fonction renvoit la largeur en pixel du DIV de la carte.
* \return La largeur en pixel du DIV de la carte.
*/
function getClientWidth(){
	var iWidth=parseInt(mapLayout.get('width'));
	if(iMapBorderWidth != 0){
		iWidth=iWidth-(iMapBorderWidth * 2);
	}
	if(typeof(oCompareMap) == 'object'){
		iWidth=parseInt((iWidth-iMapBorderWidth) / 2);
	}
	return iWidth;
}

/**
* Cette fonction renvoit la hauteur en pixel du DIV de la carte.
* \return La hauteur en pixel du DIV de la carte.
*/
function getClientHeight(){
	if(iMapBorderWidth == 0){
		return (mapLayout.get('height'));
	}else{
		return (mapLayout.get('height')-(iMapBorderWidth*2));
	}
}

//----------------------------------------------
// Redimensionnement de la carte
//----------------------------------------------
/**
* Cette fonction permet de redimensionner les différents objets DIV contenus dans la frame qui contient la carte de comparaison.
*/
function mapCompareResize(){
	oCompareMap.resizeLayer('MapLayer'); 
	oCompareMap.resizeLayer('MapMask');
	oCompareMap.resizeLayer('ProgressBarLayer');
	oCompareMap.resizeLayer('BoxLayer');
	oCompareMap.resizeLayer('PrintLayer');
	oCompareMap.resizeLayer('EditLayer');
}

/**
* Cette fonction permet de redimensionner la carte.
* Elle est activée par l'evénement "onresize".
*/
function mapResize(){
	if(parent.frames.map_toolbar.bWait == true) return false;

	if(parent.frames.map_toolbar.document.mapForm){
		var oMapForm=parent.frames.map_toolbar.document.mapForm;
		if(oMapForm.mf_modeCompare.value == 1 && oMapForm.mf_imageWidth.value == getClientWidth() &&  oMapForm.mf_imageHeight.value == getClientHeight()) return false;
		if((oMapForm.mf_imageWidth.value != getClientWidth()) || (oMapForm.mf_imageHeight.value != getClientHeight())){
			oMap.resizeLayer('MapLayer');
			oMap.resizeLayer('BoxLayer');
			oMap.resizeLayer('PrintLayer');
			oMap.resizeLayer('EditLayer');
			oMap.resizeLayer('MapMask');
			oMap.resizeLayer('ProgressBarLayer');
			oMap.mapwidth=getClientWidth();
			oMap.mapheight=getClientHeight();
			oMapForm.mf_scale.value=parent.frames.map_toolbar.dCurrentScale;
			oMapForm.mf_tool.value="scale";
			parent.map_toolbar.map_refresh();
		}
		if(oMapForm.mf_modeCompare.value==1 && oMapForm.mf_initCompare.value==0){
			mapCompareResize();
		}
	}
}

/**
* Cette fonction permet récupérer l'objet correspondant à la carte qui a été cliqué par un utilisateur.
* \return l'objet mapImage de la carte cliqué.
*/
function getObjectMapImage(){
	if(typeof(oCompareMap)=='undefined'){
		return oMap;
	}else{
		if(oEvent.target){
			var oMapImage=oEvent.target.parentNode;
		} else if(oEvent.srcElement){	
			var oMapImage=oEvent.srcElement.parentNode;
		}
		if(oMapImage){
			if(oMapImage.id==""){
				oMapImage=oMapImage.parentNode;
			}
			var sTest=oMapImage.id.substring(0,11);
			if(sTest=="divMapImage"){
				sTest=oMapImage.id.substring(0,12);
				if(sTest=="divMapImage2"){
					return oCompareMap;
				}else{
					return oMap;
				}
			}
		}
		return oMap; // si on ne trouve pas,on retourne la première carte.
	}
}

/**
* Cette fonction permet de tester la taille (en pixels) de l'image qui vient d'être chargée.
* Si la taille de cette image ne correspond pas à la taille du cadre dans lequel elle est contenue,on la rafraîchit.
* \param sDivName Nom du Div de référence.
*/
function testImgSize(sDivName){
	if(sDivName == "divMapImage"){
		if(document.getElementById(sDivName+'1').width != getClientWidth() || document.getElementById(sDivName+'1').height != getClientHeight()){
			if(parent.map_toolbar.document.mapForm.mf_tool.value!="mapinit" && parent.map_toolbar.document.mapForm.mf_tool.value!=""){
				parent.map_toolbar.document.mapForm.mf_scale.value=parent.frames.map_toolbar.dCurrentScale;
				parent.map_toolbar.document.mapForm.mf_tool.value="scale";
				parent.map_toolbar.map_refresh();
			}
		}
		//On permet l'utilisation des boutons si on est en mode "carte simple"
		if(typeof(oCompareMap) != 'object'){
			parent.frames.map_toolbar.bWait=false;
		}
	}
}

/**
* Cette fonction permet de structurer une chaîne de caractères dans le but de la stocker dans l'attribut mf_selectShape 
* du formulaire principal.
* \return La chaîne structurée.
*/   
function shapeStringConcat(sX,sY){
	var sConcat="";
	sConcat=sConcat+sX+"@"+sY+" "; 
	return sConcat;
}

/**
* Cette fonction permet de valoriser les attributs d'étendue du formulaire principal 
* en se basant sur la valeur des quatres paramètres passés.
* \param iEnvxmin Valeur minimum en X de la carte (Terrain).
* \param iEnvymin Valeur minimum en Y de la carte (Terrain).
* \param iEnvxmax Valeur maximum en X de la carte (Terrain).
* \param iEnvymax Valeur maximum en Y de la carte (Terrain).
*/
function setExtent(iEnvxmin,iEnvymin,iEnvxmax,iEnvymax){
	parent.map_toolbar.document.mapForm.mf_xMin.value=iEnvxmin;
	parent.map_toolbar.document.mapForm.mf_yMin.value=iEnvymin;
	parent.map_toolbar.document.mapForm.mf_xMax.value=iEnvxmax;
	parent.map_toolbar.document.mapForm.mf_yMax.value=iEnvymax;
}

/**
* Cette fonction permet de gérer le zoom sur la carte à l'aide de la molette de la souris.
*/
function wheel(e){
	var sDivMap=oMap.sDivName+"1";
	if(isIE){
		e=window.event;
		delta=e.wheelDelta/120;
	}else{
		delta=-e.detail/3;
	}
	delta=delta * dWheelZoomFactor;	
	bWheelUsed=1;
	var dStamp=new Date();
	iLastWheelEvent=(dStamp.getSeconds()*1000)+dStamp.getMilliseconds();
	if( document.getElementById(sDivMap).naturalHeight ){
		fHauteurDiv=document.getElementById(sDivMap).naturalHeight;
	}else{
		lgi=new Image();
		lgi.src=document.getElementById(sDivMap).src;
		fHauteurDiv=lgi.height;
	}
	fHauteurImage=document.getElementById(sDivMap).height * ((100-delta)/100);
	fTop=(fHauteurDiv-fHauteurImage)/2;
	if( document.getElementById(sDivMap).naturalWidth ){
		fLargeurDiv=document.getElementById(sDivMap).naturalWidth;
	}else{
		lgi=new Image();
		lgi.src=document.getElementById(sDivMap).src;
		fLargeurDiv=lgi.width;
	}
	fLargeurImage=document.getElementById(sDivMap).width * ((100-delta)/100);
	fLeft=(fLargeurDiv-fLargeurImage)/2;
	document.getElementById(sDivMap).style.width=fLargeurImage+"px";
	document.getElementById(sDivMap).style.height=fHauteurImage+"px";
	document.getElementById(sDivMap).style.marginTop=fTop+"px";
	document.getElementById(sDivMap).style.marginLeft=fLeft+"px";

	window.setTimeout('delayedRefresh()',iEndWheelDelay+1);
}

/**
* Cette fonction permet de rafraîchir la fenêtre carte en fonction de la taille de l'image obtenue 
* après avoir utilisé le zoom de la molette de la souris.  
*/
function delayedRefresh(){
	var sDivMap=oMap.sDivName+"1";
	var dStamp=new Date();
	var iCurrentDate=(dStamp.getSeconds()*1400)+dStamp.getMilliseconds();
	if((bWheelUsed==1) && (iCurrentDate >= (iLastWheelEvent+iEndWheelDelay))){
		bWheelUsed=0;
		parent.map_toolbar.document.mapForm.mf_tool.value="redraw";
		
		fOriginXmin=parseFloat(parent.map_toolbar.document.mapForm.mf_xMin.value);
		fOriginYmin=parseFloat(parent.map_toolbar.document.mapForm.mf_yMin.value);
		fOriginXmax=parseFloat(parent.map_toolbar.document.mapForm.mf_xMax.value);
		fOriginYmax=parseFloat(parent.map_toolbar.document.mapForm.mf_yMax.value);
		fMilieuImageX=(fOriginXmax+fOriginXmin)/2;
		fMilieuImageY=(fOriginYmax+fOriginYmin)/2;
		
		fLargeurOriginTerrain=fOriginXmax-fOriginXmin;
		fHauteurOriginTerrain=fOriginYmax-fOriginYmin;
		
		if(document.getElementById(sDivMap).naturalWidth){
			fLargeurOriginImage=document.getElementById(sDivMap).naturalWidth;
		}else{
			lgi=new Image();
			lgi.src=document.getElementById(sDivMap).src;
			fLargeurOriginImage=lgi.width;
		}
		if(document.getElementById(sDivMap).naturalHeight){
			fHauteurOriginImage=document.getElementById(sDivMap).naturalHeight;
		}else{
			lgi=new Image();
			lgi.src=document.getElementById(sDivMap).src;
			fHauteurOriginImage=lgi.height;
		}
		fLargeurNewImage=(document.getElementById(sDivMap).width);
		fHauteurNewImage=(document.getElementById(sDivMap).height);
		fPourcentReducImage=fLargeurOriginImage / fLargeurNewImage;
		fLargeurTerrain=fLargeurOriginTerrain * fPourcentReducImage;
		fHauteurTerrain=fHauteurOriginTerrain * fPourcentReducImage;
		fNewXmin=fMilieuImageX-(fLargeurTerrain/2);
		fNewYmin=fMilieuImageY-(fHauteurTerrain/2);
		fNewXmax=fMilieuImageX+(fLargeurTerrain/2);
		fNewYmax=fMilieuImageY+(fHauteurTerrain/2);
		parent.map_toolbar.document.mapForm.mf_xMin.value=fNewXmin;
		parent.map_toolbar.document.mapForm.mf_yMin.value=fNewYmin;
		parent.map_toolbar.document.mapForm.mf_xMax.value=fNewXmax;
		parent.map_toolbar.document.mapForm.mf_yMax.value=fNewYmax;
		parent.map_toolbar.map_refresh();
	}
}

/**
* fonction qui permet de dessiner un cercle à partir de la longeur du rayon.
* \param  sRaduis Longeur du rayon en mètres (décimal).
*/
function setRaduis(sRaduis){
	if(sRaduis!=""){
		var iRaduis=parseFloat(sRaduis);
		if(isNaN(iRaduis)){
			alert("Valeur incorrecte. Vous devez saisir un nombre décimal ou un nombre entier.");
		}else{
			oMapImage=oMap;
			if(!oMapImage.oGfxPoint) oMapImage=oCompareMap;
			sXYCenter=oMapImage.oGfxPoint.toStringIJ();
			aXYCenter=sXYCenter.split(/\s+/g);
			//calcul du deuxième point
			sXY=oMapImage.oGfxPoint.toStringXY();
			aXY=sXY.split(/\s+/g);
			var unitsPerDotX=(oMapImage.envxmax-oMapImage.envxmin)/oMapImage.mapwidth;
			var unitsPerDotY=(oMapImage.envymax-oMapImage.envymin)/oMapImage.mapheight;
			aXY[0]=((parseFloat(aXY[0])+iRaduis)-oMapImage.envxmin)/unitsPerDotX;
			aXY[1]=(oMapImage.envymax-aXY[1])/unitsPerDotY;
			parent.frames.map_toolbar.document.mapForm.mf_printDrawing.value=oMapImage.drawCircle(aXYCenter,aXY);
		}
	}else{
		alert("Veuillez saisir une valeur.");
	}
}
