var FEATURE_NORMAL = 'normal';
var arrayCssRules = [];

Feature.FEATURE_UPDATE = "update";
Feature.FEATURE_NORMAL = 'normal';
Feature.FEATURE_DELETE = 'delete';
Feature.FEATURE_INSERT = 'insert';
	Feature.util = {
		cloneObject:function(object,properties){
			var elemClone =  {};
			if(object == {}){
				return object;
			}
			if(properties){
				for(var i in object){
					if(properties[i]){
					  elemClone[i] = object[i];
					}
					  
				}
			}else{
				for(var i in object){
					  elemClone[i] = object[i];		  
				}
			}
			return elemClone;
		},
		cloneOutProperties:function(object,properties){
			var elemClone =  {};
			if(object == {}){
				return object;
			}
			if(properties){
				for(var i in object){
					if(!properties[i] && !(object[i] instanceof Function)){
					  elemClone[i] = object[i];
					}	  
				}
			}else{
				for(var i in object){
					  if(!(object[i] instanceof Function))
					  	elemClone[i] = object[i];		  
				}
			}
			return elemClone;
		},
		cloneProperties:function(object,properties){
			var elemClone =  {};
			if(object == {}){
				return object;
			}
			if(properties){
				for(var i in object){
					if(properties[i] && !(object[i] instanceof Function)){
					  elemClone[i] = object[i];
					}
					  
				}
			}else{
				for(var i in object){
					  if(!(object[i] instanceof Function))
					  	elemClone[i] = object[i];		  
				}
			}
			return elemClone;
		},
		makeTips:function(atitle,textHTML,id_div){
			new Ext.ToolTip({
				target: id_div,
				title: atitle,
				width:200,
				html: textHTML,
				trackMouse:true
			 });
		},
		addLabel:function(aFeature){
		   var panel = new Ext.FormPanel({
			   labelAlign	: 'left',
		   	   buttonAlign	: 'right',
			   frame 	: true,
			   items	: [{
					       layout: 'column',
					       columnWidth: 1,
					       items:[{
							   columnWidth: 0.3,
							   html: "Nombre"
							},{
							   columnWidth: 0.7,
							   id: "edit-label-obj",
							   emptyText: aFeature.attributes.label,
							   xtype:'textfield'
						}]	
			   }]
		   });
            var win = new Ext.Window({
			id:'edit-win',
			title:'Crear Objeto',
		        renderTo    : Ext.getBody(),
		        layout      : 'fit',
		        width       : 220,
		        height      : 100,
		        closeAction :'hide',
		        plain       : true,
		        resizable   : false,
			modal	    : true,	
		        buttons: [{
				    text     : 'Guardar',
				    handler  : function(){
							var label = document.getElementById("edit-label-obj").value;
							aFeature.attributes.label = label; 
							win.close();
						}
				  },{
				    text     : 'Cancelar',
				    handler  : function(){	
							for(var i=0;i<mainmap.currentLayer.features.length;i++){
								if(mainmap.currentLayer.features[i].id == aFeature.id){
									mainmap.currentLayer.features[i].operation = Feature.FEATURE_DELETE;
									break;
								}
							}
						
							var div = document.getElementById("map"+"_"+aFeature.id);
							var parent = div.parentNode;
							parent.removeChild(div);
							win.close();
						}
		        }],
			items: panel
		     });
		     return win;
		},
        // cargar el grid con las propiedades de un objeto
        loadGrid:function(object){
            var frm = Ext.getCmp('pg');
            var store = frm.getStore();

            for (var propiedad in object){
                if(!(propiedad instanceof Function)){
                    if (propiedad == 'attributes'){
                        var elem = store.getById('labelname');
                        if(elem){
                            elem.data.value = object.attributes.label;
                        };
                    }
                    if (propiedad == 'style'){
                        var objProperty = object[propiedad];
                        for(var style in objProperty){
                        var elem = store.getById(style);
                        if(elem){
                            if(isNaN(parseInt(objProperty[style]))){
                                var x = objProperty[style].split('x')[0];
                                var y = parseInt(objProperty[style].split('x')[1]);
                                if(x='x' && !isNaN(y))
                                  elem.data.value = "#"+objProperty[style].split('x')[1];
                                else
                                  elem.data.value = objProperty[style];
                            } else
                            elem.data.value = objProperty[style];
                        };
                        }
                    }
                    if (propiedad == 'labelStyle'){
                        var objProperty = object[propiedad];
                        for(var style in objProperty){
                        var elem = store.getById("label"+style);
                        if(elem){
                            if(isNaN(parseInt(objProperty[style]))){
                                var x = objProperty[style].split('x')[0];
                                var y = parseInt(objProperty[style].split('x')[1]);
                                if(x='x' && !isNaN(y))
                                  elem.data.value = "#"+objProperty[style].split('x')[1];
                                else
                                  elem.data.value = objProperty[style];
                            } else
                            elem.data.value = objProperty[style];
                        };
                        }
                    }
                }
            }
            frm.getView().refresh();
        },
        getGrid:function(){
            var frm = Ext.getCmp('pg');
            var store = frm.getStore();
            var obj = {labelStyle:{},style:{},attributes:{}};
            var cantR = store.getCount();
            for (var i = 0; i < cantR; i++){
                var name = store.getAt(i).data.name;
                if(name.indexOf("label") != -1){
                    name = name.split("label")[1];
                    if(name == "name")
                        obj.attributes.label = store.getAt(i).data.value;
                    else
                        obj.labelStyle[name] = store.getAt(i).data.value;
                    }else{
                        obj.style[name] = store.getAt(i).data.value;
                }
            }
	    obj.layer = Ext.getCmp("cbxCapaActiva").getValue().toLowerCase();
            return obj;
        },
		addToCombo:function(comboBox,aFeature){
			var combo = Ext.getCmp(comboBox);
			var array_rec = {};
			array_rec['tipo'] = aFeature.attributes.dataId;
			array_rec['nombre'] = aFeature.attributes.label;
			var index = combo.store.find('tipo',array_rec['tipo']);

			if(index == -1){
				var rec = new Ext.data.Record(array_rec);
				combo.store.add(rec);
			}
// 			combo.setValue(aFeature.attributes.label);
		},
		clearCombo:function(comboBox){
			var combo = Ext.getCmp(comboBox);
			combo.store.removeAll();
		},
		addMenu: function(feature){
			var childs;
			var _menu = new Ext.menu.Menu({ 
			    id: "menu_"+feature.id, 
			    //ignoreParentClicks:true,
			    items:[{
					text: 'Propiedades', handler:function(){
						var display = mainmap.getDisplay("map");
						display.setFeaturePositionToXY(feature,-85,22);
					}
				  }] 
			});
			var elem = mainmap.getDisplay("map").getDisplayFeature(feature);
			dShape = elem.childNodes;
			for(var i=0;i<dShape.length;i++){
				var first = dShape[i];
				switch(first.id.substr(0,2)){
					case "pt":
						var elemShape = first.firstChild;
						elemShape.oncontextmenu = function(evt){ 
							    evt.stop();	
							    var target = evt.currentTarget;
							   _menu.showAt([target._display._posx + target.offsetLeft,target._display._posy + target.offsetTop]);
						};
					break
					default:
				}
			}
		}
	}
	Feature.util.client_dir = "images/icon/";
	Feature.util.server_dir = "../../../../htdocs/" + Feature.util.client_dir;
	
	//---------------------------------------------------------------------------------
	Feature.config = {};
	Feature.config.initialize = function(feature,config){
		if(config){
			feature.operation = (config.operation)?config.operation:Feature.FEATURE_INSERT;
			feature.layer = (config.layer)?config.layer:'';
			feature.style = {};
			if(config.style instanceof Object){
				if(config.style.transparency)
				   feature.style.transparency = config.style.transparency;
				else
				   feature.style.transparency = 75;

				if(config.style.color)
				  feature.style.color=feature.style.backgroundColor = config.style.color;
				else
				  feature.style.color=feature.style.backgroundColor = "x1e90ff";
				
				if(config.style.outlineColor)
				  feature.style.outlineColor = config.style.outlineColor
				else 
				  feature.style.outlineColor = "x00509f";

				feature.style.minscale = (config.style.minscale)?config.style.minscale:2000000;
				feature.style.maxscale = (config.style.maxscale)?config.style.maxscale:4000000;

				if(config.style.size)
				   feature.style.size = config.style.size;
				else
				   feature.style.size = 10;

				if(config.style.symbol){
				   if(config.style.symbol.indexOf(".png") || config.style.symbol.indexOf(".jpg")||
				      config.style.symbol.indexOf(".jpeg")|| config.style.symbol.indexOf(".gif")){
					var arr = config.style.symbol.split("/");
					if(!feature.attributes) feature.attributes = {};
					feature.attributes.image = arr[arr.length-1];
				   }
				   feature.style.symbol =  config.style.symbol;
				}else
				   feature.style.symbol = '-';
			
			}else{
		 	    feature.style = {
				transparency:75,
				backgroundColor:"x1e90ff",
				color:'x1e90ff',
				outlineColor:'x00509f',
				size:10,
				minscale:2000000,
				maxscale:4000000,
				symbol:"-"
			   }
			}
			if(config.labelStyle instanceof Object){
				feature.labelStyle = {};
				with(config.labelStyle){
				  feature.labelStyle.color = (color)?color:'x000000';
				  feature.labelStyle.outlineColor = (outlineColor)?outlineColor:'x4c4c4c';
				  feature.labelStyle.size = (size)?size:0;
				  feature.labelStyle.font = (font)?font:'msgothic';
				}
			}else{
			    feature.labelStyle = {
					color:'xFFFFFF',
					outlineColor:'x0054b4',
					size:'0',
					font :'msgothic'
			   }
			}
			if(config.attributes)
			  feature.attributes = config.attributes;	
			else
			  feature.attributes = {};	
			
		}else{
			feature.operation = Feature.FEATURE_INSERT;
			feature.layer = "editfeatures";
			feature.style = {
					transparency:75,
					backgroundColor:"x1e90ff",
					color:'x1e90ff',
					outlineColor:'x00509f',
					size:10,
					symbol:"/media/img1.png"
			}
			feature.labelStyle = {
					color:'x000000',
					outlineColor:'x4c4c4c',
					size:'10',
					font :'msgothic'
			}
			feature.attributes ={
					label:'Point',
					dataId:aFeature.id
			}
		}
		return feature;
	}
	Feature.config.toDefault = function(feature){
		var obj = {};
		obj.vertices = feature.vertices;
		obj.type = feature.type;
		obj.attributes = feature.attributes;
		obj.labelStyle = feature.labelStyle;
		obj.style = feature.style;
		obj.operation = feature.operation;
		obj.id = feature.id;
		obj.layer = feature.layer;
		return obj;
	}

