/** 
 * @fileoverview Classe che gestisce l'interazione con l'editor di geometrie 
 * in tecnologia Macromedia Flash MX
 *
 * @author Salvi Andrea - Globo srl
 */
 
/**
 * @class Classe che gestisce l'interazione con l'applicazione Macromedia 
 * Flash MX di nome FlashFeatureEditor.swf
 * @constructor
 */
function FMX_FlashFeatureEditor() {
}

FMX_FlashFeatureEditor.prototype = new FlashMXObject();

/**
 * Imposta l'extent geografico
 * 
 * @param {Number} minX 
 * @param {Number} maxX  
 * @param {Number} minY  
 * @param {Number} maxY    
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric
 */
FMX_FlashFeatureEditor.prototype.setGeographicExtent = function(minX,maxX,minY,maxY) {  
  return this.callMethod('setGeographicExtent',[minX,maxX,minY,maxY]);
}

/**
 * Aggiorna il contenuto dell'oggetto flash
 * 
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.refresh = function() {
  this.callMethod('refresh');
}

/**
 * Mostra una area colorata semitrasparente che coincide con le dimensioni 
 * dell'applicazione
 * 
 * @param {boolean} state se true mostra altrimenti nasconde
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */ 
FMX_FlashFeatureEditor.prototype.showFill = function(state) {
  this.callMethod('showFill',[state]);
}

/**
 * Carica immagine mappa specificata dall'url
 * 
 * @param {String} urlImage url dell'immagine 
 * @param {Number} width larghezza che si vuole dell'immagine
 * @param {Number} height altezza che si vuole dell'immagine
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.loadMapImage = function(urlImage,width,height) {
  this.callMethod('loadMapImage',[urlImage,width,height]);
}

/**
 * Ridimensiona immagine mappa
 * 
 * @param {String} width larghezza che si vuole dell'immagine
 * @param {String} height altezza che si vuole dell'immagine
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.resizeMapImage = function(width,height) {
  this.callMethod('resizeMapImage',[width,height]);
}

FMX_FlashFeatureEditor.prototype.initialized = false;

/**
 * Sospende/riprende funzionamento editor
 * 
 * @param {boolean} state se true sospende altrimenti riprende
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.suspend = function(state) {
  this.callMethod('suspend',[state]);
}

/**
 * Imposta tipo di geometria da editare: punto, linea, poligono
 * 
 * @param {Number} featureClass vedere costanti con inizio GEOTYPE_
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.setFeatureClass = function(featureClass) {
  this.callMethod('setFeatureClass',[featureClass]);
}

/**
 * Imposta tipo di modalità di funzionamento dell'editor selezionando una
 * specifica geometria
 * 
 * @param {String} userMode vedere costanti con inizio MOD_
 * @param {String} geoId identificativo della geometria, già presente, 
 * specificato in precedenza
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.setUserMode = function(userMode,geoId) {
  return this.callMethod('setUserMode',[userMode,geoId]);
}

/**
 * Restituisce tipo di modalità corrente di funzionamento dell'editor
 * 
 * @return restituisce una delle costanti con inizio MOD_
 * @type Number
 */
FMX_FlashFeatureEditor.prototype.getUserMode = function() 
{
  return this.callMethod('getUserMode');
}

/**
 * Ripulisce editor
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.resetGeometry = function() {
  this.callMethod('resetGeometry');
}

/**
 * Imposta l'editor in modalità editazione sulla geometria specificata che si
 * suppone sia già presente
 * 
 * @param {String} geoId identificativo della geometria da editare
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.activateGeometry = function(geoId) {
  return this.callMethod('activateGeometry',[geoId]);
}

/**
 * Imposta geometrie in formato AXL
 * 
 * @param {String} axlGeometry insieme delle geometrie da impostare specificate 
 * in formato AXL
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.setGeometry = function(axlGeometry) {
  return this.callMethod('setGeometry',[axlGeometry]);
}

/**
 * Restituisce la geometria specificata in formato AXL
 * 
 * @param {String} geoId identificativo della geometria 
 * in formato AXL
 * @return AXL della geometria
 * @type String 
 */
FMX_FlashFeatureEditor.prototype.getGeometry = function(geoId) {
  return this.callMethod('getGeometry',[geoId]);
}

/**
 * Restituisce la splitline in formato AXL
 * 
 * @return AXL della geometria della splitline
 * @type String 
 */
FMX_FlashFeatureEditor.prototype.getSplitLine = function() {
  return this.callMethod('getSplitLine');
}

/**
 * Restituisce versione dell'applicazione
 * 
 * @return versione dell'applicazione
 * @type String 
 */
FMX_FlashFeatureEditor.prototype.getApplVersion = function() {
  return this.callMethod('getApplVersion');
}

/**
 * Aggiunge una geometria
 *
 * @param {String} axlGeometry insieme delle geometrie da aggiungere specificate 
 * in formato AXL 
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric 
 */
FMX_FlashFeatureEditor.prototype.addGeometry = function(axlGeometry) {
  return this.callMethod('addGeometry',[axlGeometry]);
}

/**
 * Determina se una determinata geometria è cambiata
 *
 * @param {String} geoId identificativo della geometria 
 * @return true se cambiata, false altrimenti
 * @type boolean 
 */
FMX_FlashFeatureEditor.prototype.isChanged = function(geoId) {
  return this.callMethod('isChanged',[geoId]);
}

/**
 * Imposta stato di modifica di una determinata geometria
 *
 * @param {String} geoId identificativo della geometria  
 * @param {boolean} state se true geometria cambiata false altrimenti
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric  
 */
FMX_FlashFeatureEditor.prototype.setIsChanged = function(geoId,state) {
  return this.callMethod('setIsChanged',[geoId,state]);
}

/**
 * Mostra/nasconde etichette geomerie
 * 
 * @param {boolean} state se true mostra label se false non la mostra
 */
FMX_FlashFeatureEditor.prototype.showLabel = function(state) {
  //alert("showLabel()");
  this.callMethod('showLabel',[state]);
}

/**
 * Sostituisce id geoemetria
 *
 * @param {String} oldGeoId identificativo corrente della geometria 
 * @param {String} newGeoId nuovo identificativo della geometria 
 */
FMX_FlashFeatureEditor.prototype.substituteGeoId = function(oldGeoId,newGeoId) {
  this.callMethod('substituteGeoId',[oldGeoId,newGeoId]);
}

/**
 * Imposta la label di una determinata geometria
 *
 * @param {String} geoId identificativo della geometria  
 * @param {String} text testo della label
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric  
 */
FMX_FlashFeatureEditor.prototype.setLabel = function(geoId,text) 
{
  return this.callMethod('setLabel',[geoId,text]);
}

/**
 * Associa una funzione all'evento di modifica punto
 *
 * @param {String} functionName nome della funzione da richiamare 
 * @return esito dell'operazione, vedere costanti che iniziano per ERR_
 * @type Numeric  
 */
FMX_FlashFeatureEditor.prototype.setEventChangedPoint = function(functionName) {
  this.callMethod('setEventChangedPoint',[functionName]);
}

/* 
 * Costanti per userMode 
 */
 
/** 
 * Imposta modalità di modifica geometria
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_EDIT  = 100;
/** 
 * Imposta modalità di inserimento nuova geometria
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_NEW   = 101;
/** 
 * Imposta modalità di nessuna operazione impostata
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_STOP  = 102;
/** 
 * Imposta modalità di unione geometrie
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_UNION = 103;
/** 
 * Imposta modalità di divisione geometrie
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_SPLIT = 104;
/** 
 * Imposta modalità di inserimento parti
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_PART  = 105;
/** 
 * Imposta modalità di inserimento buchi
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.MOD_HOLE  = 106;

/*
 * Costanti per featureClass
 */
/** 
 * Imposta multipunto come tipo di geometria in editazione 
 * @type Numeric  
 */  
FMX_FlashFeatureEditor.prototype.GEOTYPE_MULTIPOINT = 200;	
/** 
 * Imposta polilinea come tipo di geometria in editazione 
 * @type Numeric  
 */  
FMX_FlashFeatureEditor.prototype.GEOTYPE_POLYLINE   = 201;	
/** 
 * Imposta poligono multipart come tipo di geometria in editazione 
 * @type Numeric  
 */  
FMX_FlashFeatureEditor.prototype.GEOTYPE_POLYGON    = 202;	

/*
 * Costanti per gestione errori
 */
/** 
 * Nessun errore
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_OK              = 200;	
/** 
 * Identificativo della geometria non trovato
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_IDNOTFOUND      = 201;	
/** 
 * Identificativo della geometria già utilizzato
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_IDUSED          = 202;	
/** 
 * Azione richiesta non possibile per lo stato dell'editor
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_FORBIDDENACTION = 203;	
/** 
 * Extent impostato in modo non corretto
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_BADEXTENT       = 204;	
/** 
 * Non è stato impostato l'id della geometria in relazione 
 * all'operazione richiesta
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_IDUNDEFINED     = 205;	
/** 
 * L'AXL fornito non è corretto
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_AXLMALFORMED    = 206;	
/** 
 * Informa che la geometria è stata modificata
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_CHANGED         = 207;	
/** 
 * Informa che la geometria non è stata modificata
 * @type Numeric  
 */ 
FMX_FlashFeatureEditor.prototype.ERR_UNCHANGED       = 208;	


//************************
//    FUNZIONI VARIE
//+++++++++++++++++++++++++
// funzione di inizializzazione dell'oggetto
function initFMX_FlashFeatureEditor() {
	
  if (typeof(fFeatureEditorObj) == "undefined") return;
  //alert("initFMX_FlashFeatureEditor()");
  fFeatureEditorObj.initialized = true;
  fFeatureEditorObj.setGeographicExtent(document.processa.minX.value,document.processa.maxX.value,document.processa.minY.value,document.processa.maxY.value);
  fFeatureEditorObj.loadMapImage(document.laMappa.src,document.processa.MapImageX.value,document.processa.MapImageY.value);
  fFeatureEditorObj.resize(document.processa.MapImageX.value,document.processa.MapImageY.value);
  fFeatureEditorObj.showFill(false);
  fFeatureEditorObj.show(false);
  fFeatureEditorObj.showLabel(false);
	fFeatureEditorObj.refresh();  
  //fFeatureEditorObj.setEventChangedPoint("handler_EvChPoint");
}

function handler_EvChPoint(idGeoemtry) 
{
  alert("handler_EvChPoint(" + idGeoemtry + ")");
}

// funzione di aggiornamento dell'oggetto
function refreshFMX_FlashFeatureEditor() {
  //alert("refreshFMX_FlashFeatureEditor()");
  if (typeof(fFeatureEditorObj) != "undefined") {
    //alert(fFeatureEditorObj.initialized);
	  if (fFeatureEditorObj.initialized) 
	  {
		  fFeatureEditorObj.setGeographicExtent(document.processa.minX.value,document.processa.maxX.value,document.processa.minY.value,document.processa.maxY.value);
		  fFeatureEditorObj.loadMapImage(document.laMappa.src,document.processa.MapImageX.value,document.processa.MapImageY.value);
		  fFeatureEditorObj.refresh();
	  }    
  }			
}

// funzione di gestione del resize dell'editor
function resizeFMX_FlashFeatureEditor()
{
  //alert("resizeFMX_FlashFeatureEditor()");
  if (typeof(fFeatureEditorObj) != "undefined") {
    //alert(fFeatureEditorObj.initialized);
	  if (fFeatureEditorObj.initialized) 
	  {
      fFeatureEditorObj.resize(mappaWidth,mappaHeight);
	    fFeatureEditorObj.resizeMapImage(mappaWidth,mappaHeight);
	    fFeatureEditorObj.refresh();
		  //
		  if (!fFeatureEditorObj.showed) fFeatureEditorObj.show(false);	    
	  }    
  }	
}

//
function getErroMessage(code)
{
	var message="";
	
	switch(code) 
	{
		case (""+fFeatureEditorObj.ERR_OK) :
			message = "ERR_OK";
			break;
		case (""+fFeatureEditorObj.ERR_IDNOTFOUND) :
			message = "ERR_IDNOTFOUND";
			break;
		case (""+fFeatureEditorObj.ERR_IDUSED) :
			message = "ERR_IDUSED";
			break;
		case (""+fFeatureEditorObj.ERR_FORBIDDENACTION) :
			message = "ERR_FORBIDDENACTION";
			break;
		case (""+fFeatureEditorObj.ERR_BADEXTENT) :
			message = "ERR_BADEXTENT";
			break;
		case (""+fFeatureEditorObj.ERR_IDUNDEFINED) :
			message = "ERR_IDUNDEFINED";
			break;
		case (""+fFeatureEditorObj.ERR_AXLMALFORMED) :
			message = "ERR_AXLMALFORMED";
			break;
		case (""+fFeatureEditorObj.ERR_CHANGED) :
			message = "ERR_CHANGED";
			break;	
		case (""+fFeatureEditorObj.ERR_UNCHANGED) :
			message = "ERR_UNCHANGED";
			break;																												
	}
	
	return message;			
}

// verifica se c'? qualcosa in eitazione
function fFeatureEditorIsOnEditing()
{
  if (typeof(fFeatureEditorObj) == "undefined") return;
  var userMode = fFeatureEditorObj.getUserMode();
  //alert(userMode);
  return ((userMode == fFeatureEditorObj.MOD_EDIT) ||
      (userMode == fFeatureEditorObj.MOD_NEW));
}