	/* 
		Licensed under the GPL (www.gnu.org/copyleft/gpl.html) 
		***** EDIT *****
	*/
	//-------------------------------------------------------------------------------
	Map.prototype.drawFeatures = function(pluginOutput)
	{
		if(mainmap)
		{
			var drawLayer;
			var display_layer = document.getElementById("map_edit_draw");
			var layers = [],pos=0;
			if(display_layer){
				var parent = display_layer.parentNode;
				display_layer.remove();
				for (i=0 ; i < this.layers.length ; i++) {
				    if (this.layers[i].id == "edit_draw") {
				      delete this.layers[i];
				      continue;
				    }
				    layers[pos++] = this.layers[i];
				}
				this.layers = layers;
				this.currentEditFeature = null;
			}
			
			drawLayer = new Layer("edit_draw");
			drawLayer.features = [];

			var arrayFeatures = Ext.decode(pluginOutput.variables['features_edit']);
			var temp_arr,cssText="";

// 			Feature.util.clearCombo('cbxGeometriaActiva');
// 			var combo = Ext.getCmp('cbxGeometriaActiva');
// 			var rec = new Ext.data.Record({nombre:'-geometrias-',tipo:'-'});
// 			combo.store.add(rec);
			

		 	for(var i = 0; i < arrayFeatures.length; i++)
			{
				var receivefeature = arrayFeatures[i];
				var feature = new Feature(receivefeature.WKTString);
				feature.attributes = receivefeature.attributes;
				feature.style = receivefeature.style;
				feature.labelStyle = receivefeature.labelStyle;
				feature.layer = receivefeature.layer;
				feature.operation = FEATURE_NORMAL;
				drawLayer.addFeature(feature);
				Feature.util.addToCombo('cbxGeometriaActiva',feature);
			}
// 			combo.setValue(combo.store.getAt(0).get("nombre"));
			mainmap.addLayer(mainmap,drawLayer);
			mainmap.setCurrentLayer("edit_draw");
			for(var i = 0; i < mainmap.currentLayer.features.length; i++)
			{
			  var elemFeature = mainmap.currentLayer.features[i];
			  Feature.util.addMenu(elemFeature); 
			  mainmap.paintFeature(elemFeature);
			}
		}
		enableTool('edit_move', true);
		//setActiveToolButton('edit_activate');
	}
	//-------------------------------------------------------------------------------
	Map.prototype.edit_clear = function() 
	{
		var aLayer = xGetElementById("map_draw");
		if (aLayer != null) {
			var parent = aLayer.parentNode;
			parent.removeChild(aLayer);		
		}
	}
	//-------------------------------------------------------------------------------
	Map.prototype.edit_activate = function(aDisplay) 
	{
		CartoWeb.trigger('Edit.Perform');
	}
	//-------------------------------------------------------------------------------
	Map.prototype.edit_save = function(aDisplay) 
	{
		//setActiveToolButton('edit_save');
		myform['edit_validate_all'].value = '0';  
		CartoWeb.trigger('Edit.Perform');
	}
	//-------------------------------------------------------------------------------
	Map.prototype.edit_point = function(aDisplay) 
	{
		this.resetMapEventHandlers();
		this.getDisplay(aDisplay).setTool('draw.point');
		this.setCurrentLayer("edit_draw");
		var globalobj = Feature.util.getGrid();

		this.onNewFeature = function(aFeature) {
			var cmp = Ext.getCmp("cbxCapaActiva");
			var obj = Feature.util.getGrid();
			obj.layer = cmp.getValue().toLowerCase();
			aFeature = Feature.config.initialize(aFeature,obj);
			this.setCurrentLayer("edit_draw");
		}
		this.onFeatureInput = function(aFeature) 
		{
			if(aFeature){
				aFeature.attributes.dataId = aFeature.id;
				this.currentLayer.features.push(aFeature);
				
				var obj = Feature.util.getGrid();
				aFeature.attributes.label = obj.attributes.label;
				aFeature.attributes.image = obj.style.symbol;

				var vertex = this.paintFeature(aFeature);
				var win = Feature.util.addLabel(aFeature);
				win.setPosition(vertex.x+aFeature.style.size/4,vertex.y+aFeature.style.size/4);
				win.show();
			}
			//reset
			Ext.getCmp("btnMoverGeometria").enable();
			Ext.getCmp("btnEliminarGeometria").enable();
			Ext.getCmp("btnAdicionarNodo").enable();
			Ext.getCmp("btnEliminarNodo").enable();
			Ext.getCmp("btnGuardar").enable();
			mainmap.edit_point('map');
		}
	};
	//----------------------------------------------------------------------------------
	Map.prototype.edit_polygon = function(aDisplay) 
	{
		this.resetMapEventHandlers();
		this.getDisplay(aDisplay).setTool('draw.poly');
		this.setCurrentLayer("edit_draw");

		this.onNewFeature = function(aFeature) 
		{
			var cmp = Ext.getCmp("cbxCapaActiva");
			var obj = Feature.util.getGrid();
			obj.layer = cmp.getValue().toLowerCase();
			aFeature = Feature.config.initialize(aFeature,obj);
			this.setCurrentLayer("edit_draw");
		}
		this.onFeatureInput = function(aFeature) 
		{
			if(aFeature){
				// new feature
				aFeature.attributes.dataId = aFeature.id;
				this.currentLayer.features.push(aFeature);
				
				var obj = Feature.util.getGrid();
				aFeature.attributes.label = obj.attributes.label;
				aFeature.attributes.image = obj.style.symbol;

				var vertex = this.paintFeature(aFeature);
				var win = Feature.util.addLabel(aFeature);
				win.setPosition(vertex.x,vertex.y);
				win.show();

				
			}
			// reset 
			Ext.getCmp("btnMoverGeometria").enable();
			Ext.getCmp("btnEliminarGeometria").enable();
			Ext.getCmp("btnAdicionarNodo").enable();
			Ext.getCmp("btnEliminarNodo").enable();
			Ext.getCmp("btnGuardar").enable();
			mainmap.edit_polygon('map');
		}
	};
	//---------------------------------------------------------------------------
	Map.prototype.edit_line = function(aDisplay) 
	{
		this.resetMapEventHandlers();
		this.getDisplay(aDisplay).setTool('draw.line');
		
		this.onNewFeature = function(aFeature) 
		{
			var cmp = Ext.getCmp("cbxCapaActiva");
			var obj = Feature.util.getGrid();
			obj.layer = cmp.getValue().toLowerCase();
			aFeature = Feature.config.initialize(aFeature,obj);
			this.setCurrentLayer("edit_draw");
		}
		this.onFeatureInput = function(aFeature) 
		{
			if(aFeature){
	
				// new feature
				aFeature.attributes.dataId = aFeature.id;
				this.currentLayer.features.push(aFeature);
				var vertex = this.paintFeature(aFeature);
				var obj = Feature.util.getGrid();
				aFeature.attributes.label = obj.attributes.label;

				var win = Feature.util.addLabel(aFeature);
				win.setPosition(vertex.x,vertex.y);
				win.show();

			}
			// reset the tool
			Ext.getCmp("btnMoverGeometria").enable();
			Ext.getCmp("btnEliminarGeometria").enable();
			Ext.getCmp("btnAdicionarNodo").enable();
			Ext.getCmp("btnEliminarNodo").enable();
			Ext.getCmp("btnGuardar").enable();
			mainmap.edit_line('map');
			
		}
	};

	//--------------------------------------------------------------------------------
	Map.prototype.edit_select = function(aDisplay) {

	  this.resetMapEventHandlers();
	  this.setCurrentLayer('drawing');
	  this.getDisplay(aDisplay).setTool('sel.box');

	  this.onSelBox = function(x1, y1, x2, y2) {
	    AjaxPlugins.Edit.params.edit_layers = Ext.getCmp("cbxCapaActiva").getValue().toLowerCase()+"|";
	    if(AjaxPlugins.Edit.params.edit_layers == "|"){
		AjaxPlugins.Edit.params.edit_layers = null;
	    }
	    AjaxPlugins.Edit.params.edit_selection = x1 + "," + y1 + "," + x2 + "," + y2;
	    CartoWeb.trigger('Edit.Perform', "doSubmit()");
        
           var tbrH = Ext.getCmp('tbarHerramientasEdicion');
           tbrH.enable();
           Ext.getCmp('cbxGeometriaActiva').enable();
           Feature.util.clearCombo('cbxGeometriaActiva');
           Ext.getCmp('btnMoverGeometria').toggle(true);
	  }
	};

 	//--------------------------------------------------------------------------------
	Map.prototype.edit_move = function(aDisplay) {
	  setActiveToolButton('edit_move');
	  this.resetMapEventHandlers();
	  this.getDisplay(aDisplay).setTool('move');
	  this.setCurrentLayer("edit_draw");
	  this.onFeatureChange = function(aFeature) {
	  Ext.getCmp('btnGuardar').enable(); // 
	  if(mainmap.currentEditFeature == aFeature)
			if(aFeature.type == "point")
				this.paintFeature(aFeature,"ffff00","ffff00");
			else
				this.paintFeature(aFeature,"ffff00");
		else
	   		this.paintFeature(aFeature);
	  }
	  this.onFeatureSelected = function(aFeature) {
		if(aFeature){
			if(mainmap.currentEditFeature && aFeature.id != mainmap.currentEditFeature.id)
				darkFeature(mainmap.currentEditFeature);

			mainmap.currentEditFeature = aFeature;
			Feature.util.addToCombo('cbxGeometriaActiva',aFeature);
			Feature.util.loadGrid(aFeature);
			hilightFeature(aFeature);
		}
	  }
	};

	//--------------------------------------------------------------------------------
	Map.prototype.edit_del_vertex = function(aDisplay) {
	  setActiveToolButton('edit_del_vertex');
	  this.resetMapEventHandlers();
	  this.getDisplay(aDisplay).setTool('delete.vertex');
	  this.onFeatureChange = function(aFeature) {
	   	if(mainmap.currentEditFeature == aFeature)
			if(aFeature.type == "point")
				this.paintFeature(aFeature,"ffff00","ffff00");
			else
				this.paintFeature(aFeature,"ffff00");
		else
	   		this.paintFeature(aFeature);

		// set enable to guardar
		Ext.getCmp('btnGuardar').enable();
	  }
	};
	//--------------------------------------------------------------------------------
	Map.prototype.edit_add_vertex = function(aDisplay) {
	  setActiveToolButton('edit_add_vertex');
	  this.resetMapEventHandlers();
	  this.getDisplay(aDisplay).setTool('add.vertex');
	  this.onFeatureChange = function(aFeature) {
		if(mainmap.currentEditFeature == aFeature)
			if(aFeature.type == "point")
				this.paintFeature(aFeature,"ffff00","ffff00");
			else
				this.paintFeature(aFeature,"ffff00");
		else
	   		this.paintFeature(aFeature);

		// set enable to guardar
		Ext.getCmp('btnGuardar').enable();
	  }
	};
  	//--------------------------------------------------------------------------------
	Map.prototype.edit_del_feature = function(aDisplay) {
	  setActiveToolButton('edit_del_feature');
	  _m_delete_feature = "Desea eliminar la geometria :";
	  this.resetMapEventHandlers();
	  this.getDisplay(aDisplay).setTool('delete.feature');
	  this.onFeatureChange = function(aFeature) 
	  {
		var div = document.getElementById("map"+"_"+aFeature.id);
		var parent = div.parentNode;
		parent.removeChild(div);
		var combo = Ext.getCmp('cbxGeometriaActiva');
		var index = combo.store.find("tipo",aFeature.attributes.dataId);
		combo.store.remove(combo.store.getAt(index));
		combo.clearInvalid();
		if(combo.store.getAt(0))
			combo.setValue(combo.store.getAt(0).get("nombre"));

		// set enable to guardar
		Ext.getCmp('btnGuardar').enable();
	  }
	};
	//--------------------------------------------------------------------------------
	Map.prototype.onUnselectFeatures = function() {
	  console.log("unselected");
	}
	//--------------------------------------------------------------------------------
	Map.prototype.paintFeature = function(aFeature,bgColor,highligth){
		var dShape = mainmap.getDisplay("map").getDisplayFeature(aFeature);
		dShape = dShape.childNodes;
		
		for(var i = 0; i < dShape.length; i++){
			var first = dShape[i];
			switch(first.id.substr(0,2)){
				case "pt":
					var elemShape = Ext.get(first.firstChild);
					if(aFeature.type == 'point'){
						if(aFeature.style.symbol == "-" && !(aFeature.attributes.image)){
							var color = aFeature.style.color.split("x")[1] || aFeature.style.color.split("#")[1]
						 	var outcolor = aFeature.style.outlineColor.split("x")[1]||aFeature.style.outlineColor.split("#")[1]
						 	elemShape.applyStyles({
								height:  3*aFeature.style.size/4,
								width:   3*aFeature.style.size/4,
								opacity: (aFeature.style.transparency/100),
								backgroundColor:highligth || color,
								borderWidth:'thin',
								borderColor:outcolor,
								borderStyle:'solid'
							  })
						}else{
							elemShape.applyStyles({
								height:  aFeature.style.size,
								width:   aFeature.style.size,
								opacity: (aFeature.style.transparency/100),
								backgroundColor: highligth ||"transparent",
								border :"3px solid red"
							})
							
							var src = Feature.util.client_dir + (aFeature.attributes.image);
							var img = document.createElement("img");
							img.src = src;
							img._display = this.getDisplay("map");
							img.style.cursor = 'pointer';
							img.id = "img"+elemShape.parent().id;
							if(!Ext.get(img.id))
							  elemShape.parent().appendChild(img);
							Ext.get(img).applyStyles(elemShape.getStyles("position","top","left","width","height"));
						  }
					} else {
						 elemShape.applyStyles({
							height:  8,
							width:   8,
							opacity: (aFeature.style.transparency/100),
							backgroundColor: bgColor || "transparent",
							borderWidth:'thin',
							borderColor:"1e90ff",
							borderStyle:'solid'
						  })
					}
				break;
				case "li":
					var childs = first.childNodes;
					for(var j=0;j<childs.length;j++){
						var elemShape = Ext.get(childs[j]);
						elemShape.applyStyles({
							opacity: aFeature.style.transparency/100,
							backgroundColor: aFeature.style.outlineColor,
							cursor:'pointer'
						})
					}
				break;
				case "pf":
					var childs = first.childNodes;
					for(var j=0;j<childs.length;j++){
						var elemShape = Ext.get(childs[j]);
						elemShape.applyStyles({
							opacity: aFeature.style.transparency/100,
							backgroundColor: aFeature.style.color,
							cursor:'pointer'
						})
					}
				break;
			}
		}
		var featureCentroid = aFeature.getCentroid();
		var map = this.getDisplay("map");
		
		var xpos = geo2Pix(featureCentroid.vertices[0].x, map._map.extent.xmin, map._map.extent.xmax, map._posx,map._posx + map._width);
		var ypos = geo2Pix(featureCentroid.vertices[0].y, map._map.extent.ymax, map._map.extent.ymin, map._posy,map._posy + map._height);
		
		return new Vertex(xpos,ypos);
		
	}	
	//--------------------------------------------------------------------------------	
	/******************************************/
	/* generic functions                      */
	/******************************************/
	function hilightFeature(feature) {
	  if (feature) {
		if(feature.type == "point"){
			var dShape = mainmap.getDisplay("map").getDisplayFeature(feature);
			var elemShape = Ext.get(dShape.firstChild.firstChild);
			elemShape.highlight("ffff00",
						{attr: "background-color",
						endColor:'ffff00',
						easing: 'easeIn',
    						duration: 1
					    });
		}else
		   mainmap.paintFeature(feature,"ffff00");
	  }
	}
	//--------------------------------------------------------------------------------
	function darkFeature(feature) {
		if(feature){
	      		//var dShape = mainmap.getDisplay("map").getDisplayFeature(feature);
			//var parent = dShape.parentNode;
			//parent.removeChild(dShape);
			mainmap.paintFeature(feature);
		}
	}
	//--------------------------------------------------------------------------------
	function array_unset(array,index) {
	  // unset $array[$index], shifting others values
	  var res = new Array();
	  var i = 0;
	  var l = array.length;
	  for (var i = 0; i < l; i++) {
	    if (i != index) {
	      res.push(array[i]);
	    }
	  }
	  return res;
	}
	//--------------------------------------------------------------------------------

