/*
 * Ext JS Library 2.0
 * 
 */

/*
	Ext.extend
	
	Ext.form.ColorField

	Ext.AttachPicker
	Ext.menu.AttachItem
	Ext.menu.AttachMenu
	Ext.form.AttachField

	Ext.form.TreeField
	
	Ext.form.RadioBitField

	Ext.form.CheckboxBitField

 */


/*-------------------------------Ext.form.ColorField---------------------------*/

Ext.form.ColorField = function(config){
    Ext.form.ColorField.superclass.constructor.call(this, config);
};

Ext.extend(Ext.form.ColorField, Ext.form.TriggerField,{

	readOnly:true,

	getValue : function(){
        return Ext.form.ColorField.superclass.getValue.call(this)||"";
    },

	setValue : function(color){
        Ext.form.ColorField.superclass.setValue.call(this,color);
    },

	// private
    menuListeners : {
        select: function(palette,color){
            this.setValue(color);
        },
        show : function(){
            this.onFocus();
        },
        hide : function(){
            this.focus.defer(10, this);
            var ml = this.menuListeners;
            this.menu.un("select", ml.select,  this);
            this.menu.un("show", ml.show,  this);
            this.menu.un("hide", ml.hide,  this);
        }
    },

	onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        if(this.menu == null){
            this.menu = new Ext.menu.ColorMenu();
        };
        this.menu.on(Ext.apply({}, this.menuListeners, {
            scope:this
        }));
        this.menu.show(this.el);
    }

});
Ext.reg('colorfield', Ext.form.ColorField);


/*-------------------------------------Ext.AttachPicker----------------------------------------*/

Ext.AttachPicker = function(config){

    Ext.AttachPicker.superclass.constructor.call(this, config);
    this.addEvents({
        select: true
    });
};

Ext.extend(Ext.AttachPicker, Ext.Component, {

    value : null,

    ctype: 'Ext.AttachPicker',

    allowReselect : false,

    // private
    onRender : function(container, position){
        var el = document.createElement("div");
		
		var action=this.action;
		var params=this.params;
		var closeContainer=this.closeContainer;

        el.innerHTML ='<form method="POST" enctype="multipart/form-data" target="hideFrame">'+
					'<table><tr><td></td><td></td></tr></table></form>';
        container.dom.appendChild(el);

        this.el=Ext.get(el);  	
        var table=this.el.child('table').dom;        
        var uploadForm=this.el.child("form").dom;
        var attach=Ext.DomHelper.append(table.rows[0].cells[0],{tag: 'input',type: 'file',name:'attach'});

        var btnTd=table.rows[0].cells[1];

        var uploadBtn=new Ext.Button({
            text: '上传',
			renderTo:btnTd,	
            scope: this,
            handler:function(){
				if(attach.value!='')
				{	
					this.fireEvent('select',this,null);

					var isSuccess=false;

					uploadForm.action=action;
					uploadSuccess=function(response,option){
						var data=Ext.decode(response.responseText);
						if(data.success)
						{
							isSuccess=true;
							Ext.MessageBox.hide();
							closeContainer.hide();
						}	
						else	
							Ext.MessageBox.alert('警告','附件上传失败!');						
					};
					Ext.Ajax.request({
						url:uploadForm.action+'?'+params,
						method:'POST',
						form:uploadForm,
						isUpload:true,
						success:uploadSuccess
					});

 			        Ext.MessageBox.show({
						title: '请稍等...',
						msg: '上传...',
						width:200,
						progress:true,
						closable:false,
						animEl:btnTd
					});
					
					var startProgress=function(v){
						return function(){
							if(v == 11)	v=10;
							Ext.MessageBox.updateProgress(v/10,v*10+'%');
							if(v*10==100&&isSuccess)
							{
								isSuccess=false;
								Ext.MessageBox.hide();
								closeContainer.hide();
							}
						};
					};
					for(var i = 1; i < 12; i++){
						setTimeout(startProgress(i),i*300);
					}	

					this.fireEvent('select',this,getFileName(attach.value));
				}
				else Ext.MessageBox.alert('警告','请先选择要上传的文件!');
            }
        });
    },

    // private
    afterRender : function(){
        Ext.AttachPicker.superclass.afterRender.call(this);
        if(this.value){
            var s = this.value;
            this.value = null;
            this.select(s);
        }
    }
});

/*-------------------------------Ext.menu.AttachItem---------------------------*/
Ext.menu.AttachItem = function(config){
    Ext.menu.AttachItem.superclass.constructor.call(this, new Ext.AttachPicker(config), config);

    this.picker = this.component;
    this.addEvents({select: true});
    
    this.picker.on("render", function(picker){
        picker.getEl().swallowEvent("click");
    });
    this.picker.on("select", this.onSelect, this);


};
Ext.extend(Ext.menu.AttachItem, Ext.menu.Adapter, {
    onSelect : function(picker, file){
        this.fireEvent("select", this,file,picker);
        Ext.menu.AttachItem.superclass.handleClick.call(this);
    }
}); 

/*-------------------------------Ext.menu.AttachMenu---------------------------*/

Ext.menu.AttachMenu = function(config){
    Ext.menu.AttachMenu.superclass.constructor.call(this, config);
    this.plain = true;
    var ai = new Ext.menu.AttachItem(config);
    this.add(ai);
    this.picker = ai.picker;
    this.relayEvents(ai, ["select"]);
};
Ext.extend(Ext.menu.AttachMenu, Ext.menu.Menu);

/*-------------------------------Ext.form.AttachField---------------------------*/

Ext.form.AttachField = function(config){
    Ext.form.AttachField.superclass.constructor.call(this, config);
};

Ext.extend(Ext.form.AttachField, Ext.form.TriggerField,{

	getValue : function(){
        return Ext.form.AttachField.superclass.getValue.call(this)||"";
    },

	setValue : function(file){
        Ext.form.AttachField.superclass.setValue.call(this,file);
    },

	// private
    menuListeners : {
        select: function(picker,file){
            this.setValue(file);
        },
        show : function(){
            this.onFocus();
        },
        hide : function(){
            this.focus.defer(10, this);
            var ml = this.menuListeners;
            this.menu.un("select", ml.select,  this);
            this.menu.un("show", ml.show,  this);
            this.menu.un("hide", ml.hide,  this);
        }
    },

	onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        if(this.menu == null){
            this.menu = new Ext.menu.AttachMenu();
        };

        Ext.apply(this.menu.picker,  {
        	action:this.action,
        	params:this.params,
			closeContainer:this.closeContainer
        });
        this.menu.on(Ext.apply({}, this.menuListeners, {
            scope:this
        }));
        this.menu.show(this.el);
    }
});
Ext.reg('attachfield', Ext.form.AttachField);


/*-------------------------------Ext.form.TreeField---------------------------*/

Ext.form.TreeField = function(config){
    Ext.form.TreeField.superclass.constructor.call(this, config);
};

Ext.form.TreeField=Ext.extend(Ext.form.TriggerField,{

    displayField:'text',  

    valueField:'id',
		
	hiddenName:'',

	dataUrl:'',

	treeWidth:200,

	treeHeight:150,

	expandAll:false,

	leaf:true,

	readOnly:true,

	tree:null,

	defaultAutoCreate : {tag: "input", type: "text", size: "24", autocomplete: "off"},

	getValue : function(){
        return Ext.form.TreeField.superclass.getValue.call(this)||"";
    },

	setValue : function(node){
        Ext.form.TreeField.superclass.setValue.call(this,node[this.displayField]);
		this.value=node[this.valueField]; 
		this.hiddenField.value=node[this.valueField];
    },
	
	setTreeValue:function(id){
		if(this.tree){
			var node=this.tree.getNodeById(id);
			if(node){
				node.select();
				this.setValue(node);
			}
			else{
				this.value=id
				this.hiddenField.value=id;
			}
		}
	},

	setDisplayValue : function(text){
        Ext.form.TreeField.superclass.setValue.call(this,text);
    },

    onSelect:function(node){  
		this.setValue(node);  
		this.collapse();  
		this.fireEvent('select', this, node);  
    }, 

	expand : function(){  
		if(this.isExpanded() || !this.hasFocus){  
			return;  
		}  
		this.list.alignTo(this.wrap, this.listAlign);  
		this.list.show();  
		this.fireEvent('expand', this);  
	},  
		
	collapse : function(){  
		if(!this.isExpanded()){  
			return;  
		}  
		this.list.hide();  
		this.fireEvent('collapse', this);  
	},  

	isExpanded : function(){  
		return this.list && this.list.isVisible();  
	}, 

	onTriggerClick : function(){
        if(this.disabled){
            return;
        }
		if(this.isExpanded()){  
			this.collapse();  
		}else {  
			this.onFocus({});  
			this.expand();  
		}  
		this.el.focus();  
    },
	
	initList:function(){
		this.listWidth=this.treeWidth;
		this.list = new Ext.Layer({  
			shadow: this.shadow, cls: ['x-combo-selected', this.listClass].join(' '), constrain:false  
		});  
		var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);  
		this.list.setWidth(lw);                
		var innerList = this.list.createChild({cls:'x-combo-selected'});  
		innerList.setWidth(lw - this.list.getFrameWidth('lr'));  
		innerList.setHeight(this.layerHeight || this.minLayerHeight); 	
		return innerList;
	},
	
    onRender : function(container,position){

		Ext.form.TreeField.superclass.onRender.call(this,container,position); 

		var th=this;
		this.innerList=this.initList();

		var treeClick=function(node,event){
			if(th.leaf==node.isLeaf())
			{
				th.onSelect(node);
			}
		}
		var Tree=Ext.tree;    
		this.tree=new Tree.TreePanel({
			width:this.treeWidth,
			height:this.treeHeight,
			renderTo:this.innerList,	
			root:new Tree.AsyncTreeNode({
				text:'分类',
				id:'type'
			}),
			loader:new Tree.TreeLoader({dataUrl:this.dataUrl}),			
			rootVisible:false,
			autoScroll:true,
			lines:true
		});
		new Tree.TreeSorter(this.tree,{folderSort:true});
		if(this.expandAll) this.tree.expandAll();
		this.tree.on('click',treeClick);

		this.hiddenField=this.el.insertSibling({tag:'input',type:'hidden',name:this.hiddenName,id:this.hiddenName},'before',true);
    }
});
Ext.reg('treefield', Ext.form.TreeField); 


/*-------------------------------Ext.form.RadioBitField---------------------------*/

Ext.form.RadioBitField = function(config){
    Ext.form.RadioBitField.superclass.constructor.call(this, config);
};

Ext.form.RadioBitField=Ext.extend(Ext.form.TriggerField,{

	listArray:[],

    displayField:null,  

    valueField:null,
		
	hiddenName:'',

	radioWidth:200,

	radioHeight:150,

	readOnly:true,

	getValue : function(){
        return Ext.form.RadioBitField.superclass.getValue.call(this)||"";
    },

	setValue : function(value,text){
        Ext.form.RadioBitField.superclass.setValue.call(this,text);
		this.value=value; 
		this.hiddenField.value=value;
    },
	
	setRadioBitValue:function(value){
		for(var i=0;i<this.listArray.length;i++)
		{
			if(this.radioPanel.items.get(i).value==value)
			{
				this.setValue(value,this.listArray[i]);
				this.radioPanel.items.get(i).setValue(true);
				break;
			}
			else this.radioPanel.items.get(i).setValue(false);							
		}
		if(value<=0) this.setValue(value,'');
	},

    onSelect:function(value,text){  
		this.setValue(value,text);  
		this.collapse();  
		this.fireEvent('select', this,value,text);  
    }, 

	expand : function(){  
		if(this.isExpanded() || !this.hasFocus){  
			return;  
		}  
		this.list.alignTo(this.wrap, this.listAlign);  
		this.list.show();  
		this.fireEvent('expand', this);  
	},  
		
	collapse : function(){  
		if(!this.isExpanded()){  
			return;  
		}  
		this.list.hide();  
		this.fireEvent('collapse', this);  
	},  

	isExpanded : function(){  
		return this.list && this.list.isVisible();  
	}, 

	onTriggerClick : function(){
        if(this.disabled){
            return;
        }
		if(this.isExpanded()){  
			this.collapse();  
		}else {  
			this.onFocus({});  
			this.expand();  
		}  
		this.el.focus();  
    },

	initList:function(){
		this.listWidth=this.radioWidth;
		this.list = new Ext.Layer({  
			shadow: this.shadow, cls: ['x-combo-selected', this.listClass].join(' '), constrain:false  
		});  
		var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);  
		this.list.setWidth(lw);                
		var innerList = this.list.createChild({cls:'x-combo-selected'});  
		innerList.setWidth(lw - this.list.getFrameWidth('lr'));  
		innerList.setHeight(this.layerHeight || this.minLayerHeight); 	
		return innerList;
	},

    onRender : function(container,position){		
		Ext.form.RadioBitField.superclass.onRender.call(this,container,position); 

		th=this;
		this.innerList=this.initList();

		this.radioPanel=new Ext.Panel({
			width:this.radioWidth,
			height:this.radioHeight,
			renderTo:this.innerList	
		});
		for(var i=0;i<this.listArray.length;i++){
			var radioBit=new Ext.form.Radio({
				boxLabel:this.listArray[i],
				value:1<<i,
				id:'radioBit'+i,
				name:'radioBit'
			});
			radioBit.on('check',function(radio,checked){
				if(checked){
					th.onSelect(radio.value,radio.boxLabel);
				}			
			});
			this.radioPanel.add(radioBit);		
		}
		this.radioPanel.doLayout();
		this.hiddenField=this.el.insertSibling({tag:'input',type:'hidden',name:this.hiddenName,id:this.hiddenName},'before',true);

    }

});
Ext.reg('radiobitfield', Ext.form.RadioBitField); 


/*-------------------------------Ext.form.CheckboxBitField---------------------------*/

Ext.form.CheckboxBitField = function(config){
    Ext.form.CheckboxBitField.superclass.constructor.call(this, config);
};

Ext.form.CheckboxBitField=Ext.extend(Ext.form.TriggerField,{

	listArray:[],

    displayField:null,  

    valueField:null,
		
	hiddenName:'',

	checkboxWidth:200,

	checkboxHeight:150,

	readOnly:true,

	value:0,
		
	getValue : function(){
        return Ext.form.CheckboxBitField.superclass.getValue.call(this)||"";
    },

	setValue : function(value){
		if(value!==undefined)
		{
			Ext.form.CheckboxBitField.superclass.setValue.call(this,value);
			this.value=value; 
		}
    },
	
    onSelect:function(value){  
		this.setValue(value);  
//		this.collapse();  
		this.fireEvent('select', this,value);  
    },
		
	setCheckboxBitValue:function(value){
		var v=0;
		for(var i=0;i<this.listArray.length;i++)
		{
			if(this.checkboxPanel.items.get(i).checked)
			{
				v=v+this.checkboxPanel.items.get(i).value;
			}
		}
		this.onSelect(v);
	},

	expand : function(){  
		if(this.isExpanded() || !this.hasFocus){  
			return;  
		}  
		this.list.alignTo(this.wrap, this.listAlign);  
		this.list.show();  
		this.fireEvent('expand', this);  
	},  		

	collapse : function(){  
		if(!this.isExpanded()){  
			return;  
		}  
		this.list.hide();  
		this.fireEvent('collapse', this);  
	},  
	
	isExpanded : function(){  
		return this.list && this.list.isVisible();  
	}, 

	decToBin:function(value){
		var dec=value;
		var bin="";
		while(dec>0){
			if(dec%2!=0)
				bin="1"+bin;
			else 
				bin="0"+bin;
			dec=parseInt(dec/2);
		}
		return bin;
	},

	setCheckboxBitChecked:function(value){
		var bin=this.decToBin(value);
		var binArray=bin.split("");

		for(var i=0;i<this.listArray.length;i++)
			this.checkboxPanel.items.get(i).setValue(false);

		for(var i=0;i<binArray.length;i++){
			if(binArray[i]=='1'){
				var j=binArray.length-1-i;
				this.checkboxPanel.items.get(j).setValue(true);
			}
		}

	},

	onTriggerClick : function(){
        if(this.disabled){
            return;
        }
		if(this.isExpanded()){  
			this.collapse();  
		}else {  
			this.onFocus({});  
			this.expand();  
		}  
		this.el.focus();  
		this.setCheckboxBitChecked(this.value);
    },

	initList:function(){
		this.listWidth=this.checkboxWidth;
		this.list = new Ext.Layer({  
			shadow: this.shadow, cls: ['x-combo-selected', this.listClass].join(' '), constrain:false  
		});  
		var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);  
		this.list.setWidth(lw);                
		var innerList = this.list.createChild({cls:'x-combo-selected'});  
		innerList.setWidth(lw - this.list.getFrameWidth('lr'));  
		innerList.setHeight(this.layerHeight || this.minLayerHeight); 	
		return innerList;
	},

    onRender : function(container,position){		
		Ext.form.CheckboxBitField.superclass.onRender.call(this,container,position); 

		th=this;
		this.innerList=this.initList();

		this.checkboxPanel=new Ext.Panel({
			width:this.checkboxWidth,
			height:this.checkboxHeight,
			renderTo:this.innerList	
		});

		for(var i=0;i<this.listArray.length;i++){
			var checkboxBit=new Ext.form.Checkbox({
				boxLabel:this.listArray[i],
				value:1<<i,
				id:'checkboxBit'+i,
				name:'checkboxBitBit'
			});
			checkboxBit.on('check',function(checkbox,checked){
				th.onSelect(th.setCheckboxBitValue(checkbox.value));
			});
			this.checkboxPanel.add(checkboxBit);		
		}

		this.checkboxPanel.doLayout();
    }

});
Ext.reg('checkboxbitfield', Ext.form.CheckboxBitField); 
