/**
		-------------------------------------------------------
		Classe acai_carto_gazeetter
		-------------------------------------------------------
		Description : 
*/
function acai_carto_gazeetter(pNom, pLeft, pTop, pWidth, pHeight) {
	var fAction;
	var fMapObject;
	var fGazeetterObject;
	var fProjection;
	var newXmin = 0;
	var newYmin = 0;
	var newXmax = 0;
	var newYmax = 0;
	var fInitLevel = 0;
	var fNumReg = "00";
	var fNumDeptReg = "00";
	var fNumDept = "-1";
	var fIsIE = (navigator.userAgent.indexOf("MSIE") != -1) ? true : false;
	var fRegOK = true;
	var fDeptOK = true;
	var fComOK = true;

	this.setProjection = function(pProjection) {
		fProjection = pProjection;
	};
	
	this.InitRegions =function(numReg) {
		// chargement du fichier <projection>regdep.js
		try {
			var regionsFile = VIEWER_LOCATION + fProjection + "/" + fProjection +'reg.js';
			acai_carto_utils.ajax_TextLoader(regionsFile,false);

			var regions = '<select name="lstRegions" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\" onChange="' + fGazeetterObject + '.RefreshDepartements();">';
			if (numReg == "00") {
				regions += '<option selected value=\"00\">' + GAZEETTER_CHOICE_LEVEL1 + '</option>';
				for (var i=0 ; i<aReg.length ; i++) {
					regions += '<option value=\"' + aReg[i][REG_CODE_REG] + '\">' + aReg[i][REG_LIBELLE] + '</option>';
				}
			} else {
				for (var i=0 ; i<aReg.length ; i++) {
					if (aReg[i][REG_CODE_REG] == numReg) {
						regions += '<option selected value=\"' + aReg[i][REG_CODE_REG] + '\">' + aReg[i][REG_LIBELLE] + '</option>';
					}
				}
			}
			regions += '</select>';
			return regions;
		} catch (e) {
			fRegOK = false;
			return ""
		}
	};

	this.InitDepartements = function(numReg,numDept) {
		if ( (! fRegOK) || numReg=="00") {
			numDept = "00";
		}
		try {
			var departementsFile = VIEWER_LOCATION + fProjection + "/" + fProjection +'dept.js';
			acai_carto_utils.ajax_TextLoader(departementsFile,false);

			try {
				var depts = '<select name="lstDepartements" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\" onChange="' + fGazeetterObject + '.RefreshCommunes();">';
				if (numDept == "00" && fRegOK) {
					depts += '<option selected value=\"00\">' + GAZEETTER_CHOICE_LEVEL2 + '</option>';
					for (var i=0 ; i<aDept.length ; i++) {
						if (aDept[i][DEP_CODE_REG]==numReg) {
							depts += '<option value=\"' + aDept[i][DEP_CODE_DEP] + '\">' + aDept[i][DEP_CODE_DEP] + ' - ' + aDept[i][DEP_LIBELLE]+ '</option>';
						}
					}
				} else if (numDept == "00" && !fRegOK){
					depts += '<option selected value=\"00\">' + GAZEETTER_CHOICE_LEVEL2 + '</option>';
					for (var i=0 ; i<aDept.length ; i++) {
						depts += '<option value=\"' + aDept[i][DEP_CODE_DEP] + '\">' + aDept[i][DEP_CODE_DEP] + ' - ' + aDept[i][DEP_LIBELLE]+ '</option>';
					}
				} else {
					for (var i=0 ; i<aDept.length ; i++) {
						if (aDept[i][DEP_CODE_DEP]==numDept) {
							depts += '<option selected value=\"' + aDept[i][DEP_CODE_DEP] + '\">' + aDept[i][DEP_CODE_DEP] + ' - ' + aDept[i][DEP_LIBELLE]+ '</option>';
						}
					}
				}
				depts += '</select>';
				fDeptOK = true;
				return depts;
			} catch (e) {
				fDeptOK = false;
				return ""
			}
		} catch (e) {
			fDeptOK = false;
			return ""
		}
	};

	this.InitCommunes = function(numDept) {
		if ( ! fDeptOK) {
			fComOK = false;
			return "";
		}
		if (numDept != "00" && numDept !="-1") {
			try {
				var communesFile = VIEWER_LOCATION + fProjection + "/" + fProjection +'com' + numDept.toString() + '.js';
				acai_carto_utils.ajax_TextLoader(communesFile,false);
	
				var communes = '<select name="lstCommunes" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\">';
				communes += '<option value=\"00\">' + GAZEETTER_CHOICE_LEVEL3 + '</option>';
				for (var i=0 ; i<aCom.length ; i++) {
					communes += '<option value=\"' + aCom[i][COM_CODE_COM] + '\">' + aCom[i][COM_LIBELLE] + '</option>';
				}
				communes += '</select>';
				fComOK = true;
				return communes;
			} catch (e) {
				fComOK = false;
				return ""
			}
		} else {
			fComOK = false;
			var communes = '<select name="lstCommunes" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\">';
			communes += '<option value=\"00\">' + GAZEETTER_CHOICE_LEVEL3 + '</option>';
			communes += '</select>';
			return communes;
		}
	};

	this.RefreshDepartements = function() {
		fDeptOK = false;
		document.getElementById("idDepartements").innerHTML = "";
		document.getElementById("idCommunes").innerHTML = "";
		try {
			var numReg = document.forms["composant"].lstRegions[document.forms["composant"].lstRegions.selectedIndex].value;
			var depts = '<select name="lstDepartements" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\" onChange="' + fGazeetterObject + '.RefreshCommunes();">'
				+ '<option value=\"00\">' + GAZEETTER_CHOICE_LEVEL2 + '</option>';
			for (var i=0 ; i<aDept.length ; i++) {
				if (aDept[i][DEP_CODE_REG]==numReg) {
					depts += '<option value=\"' + aDept[i][DEP_CODE_DEP] + '\">' + aDept[i][DEP_CODE_DEP] + ' - ' + aDept[i][DEP_LIBELLE]+ '</option>';
				}
			}
			depts += '</select>';
			document.getElementById("idDepartements").innerHTML = depts;
			fDeptOK = true;
			this.RefreshCommunes();
		} catch (e) {
			fDeptOK = false;
			document.getElementById("idDepartements").innerHTML = "";
			this.RefreshCommunes();
		}
	};

	this.RefreshCommunes = function() {
		var communesVide = '<select name="lstCommunes" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\">';
		communesVide += '<option value=\"00\">' + GAZEETTER_CHOICE_LEVEL3 + '</option>';
		communesVide += '</select>';
		if ( ! fDeptOK) {
			fComOK = false;
			document.getElementById("idCommunes").innerHTML = communesVide;
		} else {
			numDept=document.forms["composant"].lstDepartements[document.forms["composant"].lstDepartements.selectedIndex].value;
			if (numDept != "00") {
				try {
					var communesFile = VIEWER_LOCATION + fProjection + "/" + fProjection +'com' + numDept.toString() + '.js';
					acai_carto_utils.ajax_TextLoader(communesFile,false);
					
					var communes = '<select name="lstCommunes" style=\"color:' + this.getColor() + ';width:' + CONTROLS_WIDTH +'px;font-family:Arial;font-size:8pt;\">';
					communes += '<option value=\"00\">' + GAZEETTER_CHOICE_LEVEL3 + '</option>';
					for (var i=0 ; i<aCom.length ; i++) {
						communes += '<option value=\"' + aCom[i][COM_CODE_COM] + '\">' + aCom[i][COM_LIBELLE] + '</option>';
					}
					communes += '</select>';
					fComOK = true;
					document.getElementById("idCommunes").innerHTML = communes;
				} catch (e) {
					document.getElementById("idCommunes").innerHTML = communesVide;
					fComOK = false;
				}
			} else {
				document.getElementById("idCommunes").innerHTML = communesVide;
			}
		}
	};
		
	this.init = function(pGazeetterObject,pMapObject) {
		fGazeetterObject = pGazeetterObject;
		fMapObject = pMapObject;
	};
		
	this.configure = function() {
		var theFormHTML = '<br/><form id="composant" name="composant">'
			+ '<div id="idRegions"' + ((fIsIE) ? '' : ' onclick="document.forms[' + '\'composant\'' + '].lstRegions.focus();" ') + ( (fInitLevel>0) ? "style=display:none": "") +'>'
			+ this.InitRegions(fNumReg)
			+ '</div>'
			+ '<div id="idDepartements"' + ((fIsIE) ? '' : ' onclick="document.forms[' + '\'composant\'' + '].lstDepartements.focus();" ') + ( (fInitLevel>1) ? "style=display:none": "") +'>'
			+ this.InitDepartements(fNumDeptReg,fNumDept)
			+ '</div>'
			+ '<div id="idCommunes"' + ((fIsIE) ? '' : ' onclick="document.forms[' + '\'composant\'' + '].lstCommunes.focus();"') + '>'
			+ this.InitCommunes(fNumDept)
			+ '</div>'
			+ '<div align=\'center\'>'
			+ '<input type="button" style="color:' + this.getBgColor() + ';background-color:' + this.getColor() + ';font-family:Arial;font-size:8pt;" value="' + GAZEETTER_BUTTON_TEXT + '" onclick="' + fGazeetterObject + '.execute();"/>'
			+ '</div></form>';
		this.setContenu(theFormHTML);
	};

	this.setInitialLevel = function(pInitLevel,pInitValue) {
		fInitLevel = pInitLevel;
		if (pInitLevel == GAZEETTER_LEVEL_ALL) {
				fNumReg = "00";
				fNumDeptReg = "00";
				fNumDept = "00";
		}
		if (pInitLevel == GAZEETTER_LEVEL_REG) {
				fNumReg = pInitValue;
				fNumDeptReg = pInitValue;
				fNumDept = "00";
		}
		if (pInitLevel == GAZEETTER_LEVEL_DEP) {
				fNumReg = "-1";
				fNumDeptReg = "00";
				fNumDept = pInitValue;
		}
		this.configure();
	};
	
	this.getLevel = function() {
		return fInitLevel;
	};
	
	this.execute = function() {
		var numReg = document.getElementById("composant").lstRegions.selectedIndex;
		var numDept = document.getElementById("composant").lstDepartements.selectedIndex;
		var numCom = 0;
		try {
			numCom = document.getElementById("composant").lstCommunes.selectedIndex;
		} catch (e) {
		}
		var newXmin;
		var newXmax;
		var newYmin;
		var newYmax;
		if (numCom <= 0 && numDept <= 0 && numReg > 0) {
				newXmin = aReg[numReg-1][REG_XMIN_BOUNDS];
				newXmax = aReg[numReg-1][REG_XMAX_BOUNDS];
				newYmin = aReg[numReg-1][REG_YMIN_BOUNDS];
				newYmax = aReg[numReg-1][REG_YMAX_BOUNDS];
		}
				
		if (numCom <= 0 && numDept > 0) {
				for (var i=0 ; i<aDept.length ; i++) {
					if (aDept[i][DEP_CODE_DEP]==document.getElementById("composant").lstDepartements[numDept].value) {
						newXmin = aDept[i][DEP_XMIN_BOUNDS];
						newXmax = aDept[i][DEP_XMAX_BOUNDS];
						newYmin = aDept[i][DEP_YMIN_BOUNDS];
						newYmax = aDept[i][DEP_YMAX_BOUNDS];
					}
				}
		}
				
		if (numCom > 0) {
				newXmin = aCom[numCom-1][COM_XMIN_BOUNDS];
				newXmax = aCom[numCom-1][COM_XMAX_BOUNDS];
				newYmin = aCom[numCom-1][COM_YMIN_BOUNDS];
				newYmax = aCom[numCom-1][COM_YMAX_BOUNDS];
		}
		if (numCom > 0 || numDept > 0 || numReg > 0) {
				// adaptation du zoom au rectangle de l'image (memes proportions...)
				var fCurrentWidth = eval(fMapObject+".getWidth()");
				var fCurrentHeight = eval(fMapObject+".getHeight()");
				var aNewBounds = acai_carto_utils.adaptRectangleToMap(fCurrentWidth, fCurrentHeight, newXmin, newXmax, newYmin, newYmax);
				newXmin = aNewBounds[0];
				newXmax = aNewBounds[1];
				newYmin = aNewBounds[2];
				newYmax = aNewBounds[3];

				// augmentation de la nouvelle emprise
				//ratio = 1.5;
				var geoWidth = (newXmax - newXmin);
				var geoHeight = (newYmax - newYmin);
				var centerX = geoWidth/2 + newXmin;
				var centerY = geoHeight/2 + newYmin;
				newXmin = centerX - geoWidth/2*LOCALISATION_BOUNDS_TOLERANCE;
				newXmax = centerX + geoWidth/2*LOCALISATION_BOUNDS_TOLERANCE;
				newYmin = centerY - geoHeight/2*LOCALISATION_BOUNDS_TOLERANCE;
				newYmax = centerY + geoHeight/2*LOCALISATION_BOUNDS_TOLERANCE;

				eval(fMapObject + ".gotoBounds(" + newXmin + "," + newXmax + "," + newYmin + "," + newYmax + "," + true + ")" );
		}
		
	};
	
	this.show = function() {
		this.redraw();
	};
	
	this.isPopulated = function() {
		return (fRegOK || fDeptOK || fComOK);
	};

	this.setNom(pNom);
	this.setPosition(pLeft,pTop,pWidth,pHeight);
}

acai_carto_gazeetter.prototype = new acai_commons_genericCouche;


