wf.PhotoView=wf.extend(wf.DataView,{
	onClick:function(e){
	}
});

if(wf.isIE){
	document.nativeGetElementById = document.getElementById;  
	document.getElementById = function(id){
		 var elem = document.nativeGetElementById(id);
		 if(elem){
		 	if(elem.attributes['id'].value == id){
				return elem;  
			}else{
				for(var i=1;i<document.all[id].length;i++){
					 if(document.all[id][i].attributes['id'].value == id){
					 	return document.all[id][i];  
					 }  
				}  
			}
			  
		 }
		 return null;   
	}  
}

wf.ItemView=wf.extend(wf.PhotoView,{
	gridStyle:true,
	gridTpl:null,
	listTpl:null,
	init:function(){
		if(this.gridStyle){
			this.tpl=this.gridTpl;
		}else{
			this.tpl=this.listTpl;
		}
		wf.ItemView.superclass.init.call(this);
	},
	changeTpl:function(gridStyle){
		if(this.gridStyle==gridStyle){
			return ;
		}
		if(gridStyle){
			this.tpl=this.gridTpl;
			this.el.replaceClass('item-list','item-view');
		}else{
			this.tpl=this.listTpl;
			this.el.replaceClass('item-view','item-list');
		}
		this.gridStyle=gridStyle;
		this.refresh();
	}
});

wf.SimplePanel=wf.extend(wf.BoxComponent,{
	init:function(){
		wf.SimplePanel.superclass.init.call(this);
		this.load();
	},
	load:function(url,params){
		var u = url||this.url;
        var p = params||this.params||{};
		this.oldParams=p;
		this.oldUrl=u;
        this.el.load({
            url: u,
            params: p,
            scripts: true,
            text: '装载中...'
        });
	},
	refresh:function(){
		this.load(this.oldUrl,this.oldParams);
	}
});

wf.SimpleForm=wf.extend(wf.Component,{
	afterReset:true,
	init:function(){
		 this.addEvents("beforeaction", "actionfailed", "actioncomplete");
		 this.formEl = this.el.child('form');
		 this.formEl.on('submit', this.onSubmit, this);
		 if(!this.fields){
		 	this.fields=[];
		 }else{
		 	for (var i = 0; i < this.fields.length; i++) {
				var f=this.fields[i];
				f.form=this;	 
			}
		 }
		  
	},
	isValid:function(){
		var valid = true;
        for (var i = 0; i < this.fields.length; i++) {
            var f = this.fields[i];
            if (!f.isValid()) {
                valid = false;
                break;
            }
        }
        return valid;
	},
	onSubmit: function(e){
        e.stopEvent();
    },
	submit: function(){
		if(this.fireEvent('beforeaction',this)){
			var o = this.options;
			var method = 'POST';
	        if (this.isValid()) {
	            wf.Ajax.request({
	                form: this.formEl,
	                url: this.url||this.formEl.action,
	                method: method,
	                params: this.params,
	                isUpload: this.fileUpload,
					success: this.success,
		            failure: this.failure,
		            scope: this
	            });
	        }
	        else {
	            this.afterAction(this, false);
	        }
		}
    },
	failure : function(response){
        this.response = response;
        this.afterAction(this, false);
    },
	afterAction: function(action, success, result){
        var o = action.options;
        if (success) {
            if (this.afterReset) {
                this.reset();
            }
            this.fireEvent('actioncomplete', this, result);
        }
        else {
            this.fireEvent('actionfailed', this, result);
        }
    },
	processResponse : function(response){
        this.response = response;
        if(!response.responseText){
            return true;
        }
        this.result =wf.decode(response.responseText);
        return this.result;
    },
	success: function(response){
        var result = this.processResponse(response);
        if (result === true || result.success) {
            this.afterAction(this, true, result);
            return;
        }
        if (result.errors) {
            this.markInvalid(result.errors);
        }
        this.afterAction(this, false, result);
    },
	markInvalid: function(errors){
		var s;
		for (id in errors) {
			 if (typeof errors[id] != 'function' && (field = this.findFieldByName(id))) {
                field.markInvalid(errors[id]);
            }
        }
	},
	 findFieldByName: function(name){
        var field;
        for (var i = 0; i < this.fields.length; i++) {
            var f = this.fields[i];
            if (f.getName() == name) {
                field = f;
                break;
            }
        }
        return field || null;
    },
    reset: function(){
        for (var i = 0; i < this.fields.length; i++) {
            var f = this.fields[i];
            f.reset();
        }
    }
});

wf.SimpleForm.Field=wf.extend(wf.Component,{
	blankText:'该输入项为必输项',
	minLengthText:'该输入项的最小长度是 {0}',
	maxLengthText : "该输入项的最大长度是 {0}",
	autoCommit:true,
	regex : null,
	regexText : "",
	allowBlank:true,
	init:function(){
		this.addEvents('blur','focus','change','specialkey');
		var el=this.getInputEl();
		if(this.readOnly||this.disabled){
            el.dom.readOnly = true;
        }
		el.on("focus", this.onFocus,  this);
		el.on("blur", this.onBlur,  this);
		el.on(wf.isIE ? "keydown" : "keypress", this.fireKey,  this);
		if(this.value){
			this.initValue();
		}
		if(this.autoCommit){
			this.on('specialkey',function(f,e){
				if(e.getKey()==e.ENTER){
					f.form.submit();
				}
			});
		}
	},
	onFocus : function(e){
        if(!this.hasFocus){
            this.hasFocus = true;
            this.startValue = this.getValue();
			this.fireEvent("focus", this);
        }
	},
	fireKey : function(e){
        if(e.isSpecialKey()){
            this.fireEvent("specialkey", this, e);
        }
    },
	onBlur : function(e){
        this.hasFocus = false;
        this.validate();
        var v = this.getValue();
        if(String(v) !== String(this.startValue)){
            this.fireEvent('change', this, v, this.startValue);
        }
		 this.fireEvent("blur", this);
	},
	getInputEl : function(){
		return this.el;
	},
	reset : function(){
		this.setValue(this.oldValue);
		this.clearInvalid();
	},
	getRawValue : function(){
		return this.getInputEl().dom.value;
	},
	isValid : function(){
		if(this.disabled){
            return true;
        }
        var v = this.validateValue(this.processValue(this.getValue()));
        return v;
	},
	validate : function(){
      	if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
            return true;
        }
        return false;
    },
	markInvalid:function(s){
		if(!this.errorEl){
			var id=wf.id();
			wf.DomHelper.insertHtml("afterend",this.el.dom,'<span style="color:red" id="'+id+'"></span>');
			this.errorEl=wf.get(id);
		}
		this.errorEl.update(s);
		this.errorEl.show();
	},
	clearInvalid : function(){
		if(this.errorEl){
			this.errorEl.hide();
		}
	},
	validateValue : function(value){
        if(value.length < 1){ // if it's blank
             if(this.allowBlank){
			 	 this.clearInvalid();
                 return true;
             }else{
                 this.markInvalid(this.blankText);
                 return false;
             }
        }
		if(value.length < this.minLength){
            this.markInvalid(String.format(this.minLengthText, this.minLength));
            return false;
        }
        if(value.length > this.maxLength){
            this.markInvalid(String.format(this.maxLengthText, this.maxLength));
            return false;
        }
		if(this.regex && !this.regex.test(value)){
            this.markInvalid(this.regexText);
            return false;
        }
		this.clearInvalid();
		return true;
    },
	processValue : function(value){
        return value;
    },
	initValue:function(){
		this.oldValue=this.value;
		this.setValue(this.value);
	},
	getValue : function(){
		var v = this.getRawValue();
        if(v === this.emptyText || v === undefined){
            v = '';
        }
		return v;
	},
	getName:function(){
		var el= this.getInputEl();
		return el.dom.name||el.id;
	},
	
	setValue : function(v){
		this.value = v;
		this.getInputEl().dom.value = (v === null || v === undefined ? '' : v);
		this.validate();
	}
});
wf.SimpleForm.email = /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/;
wf.SimpleForm.emailText='不是有效的Email格式';
wf.SimpleForm.url = /(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
wf.SimpleForm.urlText='不是有效的url格式';