Ext.override(Ext.tree.TreeNodeUI, {
	grayedValue:null,
	onDisableChange :function(node, state){
		this.disabled = state;
		this[state ? 'addClass' :'removeClass']("x-tree-node-disabled");
	},
	initEvents :function(){
		
		
		this.node.on('contextmenu', function(node){


 var button1 = new Ext.menu.Item({
      baseCls: 'x-plain',
      text: "Metadados",

     icon: '../img/metadata.png'

     
});



button1.on('click', function() {
		var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        layout:'absolute',
        url:'save-form.php',
        defaultType: 'textfield',

        items: [{
            x:0,
            y: 60,
            xtype: 'label',
            name: 'metadados',
            anchor: '100% 100%'
        }]
    });

    

    var popup = new Ext.Window({
        title: 'Metadados',
        width: 500,
        height:300,
        minWidth: 300,
        minHeight: 200,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: form,
        autoScroll: 'true',
        autoLoad:'./metadata_popup.php?node='+node.id+'&text=' + node.text + '&par=' + json_par.par + '&module=' + json_module.value,

       buttons: [{
                    text: 'Fechar',
                    handler: function(){
                    
                        popup.close();
                    }
                }]


    });

  
    popup.show();
	
	});
	
	
if (node.isLeaf()){
	
  var button2 = new Ext.menu.Item({
      baseCls: 'x-plain',
      text: "Zoom para extensão de layer",
      icon: '../img/zoom.png'
     
  });	
	
  button2.on('click', function() {
  
  var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        layout:'absolute',
        url:'save-form.php',
        defaultType: 'textfield',
        autoLoad: map.get_layer_extent(node.id),
        items: [{
            x:0,
            y: 60,
            xtype: 'label',
            name: 'set map to layer extent',
            anchor: '100% 100%'
        }]
    });
  
	
	});	

}

//Create the context menu to hold the buttons
var contextMenu = new Ext.menu.Menu({
        baseCls: 'x-plain',
        plain:true,
        bodyStyle:'padding:1px;',
        buttonAlign:'center'
        
       
    });
    
 if (myNameSpace.data.funct.in_array('metadata'))
 {
  
	contextMenu.add(button1);	
 }
 
 contextMenu.add(button2);
 


//Show the menu
contextMenu.show(node.ui.getAnchor());
}),
		
		this.node.on("move", this.onMove, this);
		if(this.node.disabled){
			this.disabled = true;
			this.addClass("x-tree-node-disabled");
		}
		if(this.node.hidden){
			this.hide();
		}
		var ot = this.node.getOwnerTree();
		var dd = ot.enableDD || ot.enableDrag || ot.enableDrop;
		if(dd && (!this.node.isRoot || ot.rootVisible)){
			Ext.dd.Registry.register(this.elNode, {
				node:this.node,
				handles:this.getDDHandles(),
				isHandle:false
			});
		}
	},
	
	
	


	
	onDblClick :function(e){
		//alert ('onDblClick');
		//alert(this.node.getDepth() + ' ' + this.node.id + ' ' + this.node.ui);
		e.preventDefault();
		if(this.disabled){
			return;
		}
		if(!this.animating && this.node.isExpandable() && !e.getTarget('.x-tree-checkbox', 1)){
			this.node.toggle();
		}
		this.fireEvent("dblclick", this.node, e);
	},
	onCheckChange :function(){
		
		var checked = this.isChecked();
		//var randomnumber = Math.floor(Math.random()*11);
		//var el = this.tree.getTreeEl();
		
		//alert (this.node.isLeaf());
		//teste123();
		//var pai = this.node.parentNode;
		//alert (this.node.parentNode);
		/*alert (this.node.id);
		this.disabled = true;
		this.addClass("x-tree-node-disabled");*/
		
		if(checked !== this.node.attributes.checked){
			this.node.attributes.checked = checked;
			this.fireEvent('checkchange', this.node, checked);
		}
	},
	toggleCheck :function(checked){
		var cb = this.checkbox;
		if(!cb){
			return false;
		}
		if(checked === undefined){
			checked = this.isChecked() === false;
		}
		if(checked === true){
			Ext.fly(cb).replaceClass('x-tree-node-grayed', 'x-tree-node-checked');
		} else if(checked !== false){
			Ext.fly(cb).replaceClass('x-tree-node-checked', 'x-tree-node-grayed');
		} else {
			Ext.fly(cb).removeClass(['x-tree-node-checked', 'x-tree-node-grayed']);
		}
		this.onCheckChange();
		return checked;
	},
	onCheckboxClick:function() {
		if(!this.disabled){
			this.toggleCheck();
		}
	},
	onCheckboxOver:function() {
		this.addClass('x-tree-checkbox-over');
	},
	onCheckboxOut:function() {
		this.removeClass('x-tree-checkbox-over');
	},
	onCheckboxDown:function() {
		this.addClass('x-tree-checkbox-down');
	},
	onCheckboxUp:function() {
		this.removeClass('x-tree-checkbox-down');
	},
	renderElements :function(n, a, targetNode, bulkRender){
	//alert(a);
	
		this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() :'';
		var cb = a.checked !== undefined;
		var href = a.href ? a.href :Ext.isGecko ? "" :"#";
		var buf = ['<li class="x-tree-node"><div ext:tree-node-id="',n.id,'" class="x-tree-node-el x-tree-node-leaf x-unselectable ', a.cls,'" unselectable="on">',
			'<span class="x-tree-node-indent">',this.indentMarkup,"</span>",
			'<img src="', this.emptyIcon, '" class="x-tree-ec-icon x-tree-elbow" />',
			'<img src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" :""),(a.iconCls ? " "+a.iconCls :""),'" unselectable="on" />',
			cb ? ('<img src="'+this.emptyIcon+'" class="x-tree-checkbox'+(a.checked === true ? ' x-tree-node-checked' :(a.checked !== false ? ' x-tree-node-grayed' :''))+'" />') :'',
			'<a hidefocus="on" class="x-tree-node-anchor" href="',href,'" tabIndex="1" ',
			a.hrefTarget ? ' target="'+a.hrefTarget+'"' :"", '><span unselectable="on">',n.text,"</span></a></div>",
			'<ul class="x-tree-node-ct" style="display:none;"></ul>',
			"</li>"].join('');
		
    
    	
		var nel;
		if(bulkRender !== true && n.nextSibling && (nel = n.nextSibling.ui.getEl())){
			this.wrap = Ext.DomHelper.insertHtml("beforeBegin", nel, buf);
		}else{
			this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf);
		}
		this.elNode = this.wrap.childNodes[0];
		this.ctNode = this.wrap.childNodes[1];
		var cs = this.elNode.childNodes;
		this.indentNode = cs[0];
		this.ecNode = cs[1];
		this.iconNode = cs[2];
		var index = 3;
		if(cb){
			this.checkbox = cs[3];
			index++;
		}
		this.anchor = cs[index];
		this.textNode = cs[index].firstChild;
	},
	isChecked :function(){
		return this.checkbox
			? (Ext.fly(this.checkbox).hasClass('x-tree-node-checked')
				? true
				:Ext.fly(this.checkbox).hasClass('x-tree-node-grayed')
					? this.grayedValue
					:false)
			:false;
	},
	getChecked: function() {
		return this.node.attributes.checked;
	}
});
Ext.override(Ext.tree.TreeEventModel, {
	initEvents :function(e){
		var el = this.tree.getTreeEl();
		el.on('click', this.delegateClick, this);
		if(this.tree.trackMouseOver !== false){
			el.on('mouseover', this.delegateOver, this);
			el.on('mouseout', this.delegateOut, this);
		}
		el.on('mousedown', this.delegateDown, this);
		el.on('mouseup', this.delegateUp, this);
		el.on('dblclick', this.delegateDblClick, this);
  	el.on('contextmenu', this.delegateContextMenu, this);
		

	},
	delegateOver :function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		if(this.lastEcOver){
			this.onIconOut(e, this.lastEcOver);
			delete this.lastEcOver;
		}
		if(this.lastCbOver){
			this.onCheckboxOut(e, this.lastCbOver);
			delete this.lastCbOver;
		}
		if(e.getTarget('.x-tree-ec-icon', 1)){
			this.lastEcOver = this.getNode(e);
			this.onIconOver(e, this.lastEcOver);
		}
		else if(e.getTarget('.x-tree-checkbox', 1)){
			this.lastCbOver = this.getNode(e);
			this.onCheckboxOver(e, this.lastCbOver);
		}
		if(this.getNodeTarget(e)){
			this.onNodeOver(e, this.getNode(e));
		}
	},
	delegateOut :function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		var n;
		if(e.getTarget('.x-tree-ec-icon', 1)){
			n = this.getNode(e);
			this.onIconOut(e, n);
			if(n == this.lastEcOver){
				delete this.lastEcOver;
			}
		}
		else if(e.getTarget('.x-tree-checkbox', 1)){
			n = this.getNode(e);
			
			this.onCheckboxOut(e, n);
			if(n == this.lastCbOver){
				delete this.lastCbOver;
			}
		}
		t = this.getNodeTarget(e);
		if(t && !e.within(t, true)){
			this.onNodeOut(e, this.getNode(e));
		}
	},
	delegateDown :function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		if(e.getTarget('.x-tree-checkbox', 1)){
			this.onCheckboxDown(e, this.getNode(e));
		}
	},
	delegateUp :function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		if(e.getTarget('.x-tree-checkbox', 1)){
			this.onCheckboxUp(e, this.getNode(e));
		}
	},
	delegateClick :function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		if(e.getTarget('.x-tree-checkbox', 1)){
			this.onCheckboxClick(e, this.getNode(e));
		}
		else if(e.getTarget('.x-tree-ec-icon', 1)){
			this.onIconClick(e, this.getNode(e));
		}
		else if(this.getNodeTarget(e)){
			//console.log(this.getNodeTarget(e));
			this.onNodeClick(e, this.getNode(e));
		}
	},
	onCheckboxClick :function(e, node){
		node.ui.onCheckboxClick();
	},
	onCheckboxOver :function(e, node){
		node.ui.onCheckboxOver();
	},
	onCheckboxOut :function(e, node){
		node.ui.onCheckboxOut();
	},
	onCheckboxDown :function(e, node){
		node.ui.onCheckboxDown();
	},
	onCheckboxUp :function(e, node){
		node.ui.onCheckboxUp();
	}
});
Ext.override(Ext.tree.TreePanel, {
	getChecked : function(a, startNode){
		startNode = startNode || this.root;
		var r = [];
		var f = function(){
			if(this.ui.getChecked()){
				r.push(!a ? this : (a == 'id' ? this.id : this.attributes[a]));
			}
		};
		startNode.cascade(f);
		return r;
	}
});

