if(typeof UI==="undefined"||!UI){var UI={}
}(function(){var a=false;
var e=Backbone.Model.extend({initialize:function(){var j=this.get("formField");
if(!j){this.clear();
return false
}var k=j.attr("input_id");
var g=$(document.getElementById(k));
var f=j.find(".error-field");
var i=j.find(".error-field-above");
this.set({inputField:g,errorField:f,errorFieldAbove:i,id:k});
var h=g.attr("child_for");
if(h){this.set({parent:h})
}this.bind("change:status",function(){var l=this.get("status"),m=this.get("inputField");
m.unbind("keypress keyup keydown");
if(l=="focus"){m.keyup(_T.scope(this.onKeyUp,this));
m.keypress(_T.scope(function(n){if(n.which==13){$.publish("formfield/enterKey",[this.get("inputField")])
}},this))
}if(l=="pause"){m.keypress(_T.scope(this.onKeyPress,this));
m.keydown(_T.scope(function(n){if(n.which==86){this.onKeyPress(n)
}},this))
}})
},onKeyUp:function(f){if(!this.isEmpty()){return false
}this.setStatus("pause");
this.toggleDefaultText()
},onKeyPress:function(f){this.toggleDefaultText();
this.setStatus("focus")
},setStatus:function(f){var g=this.get("formField");
this.clearStatus();
this.set({status:f});
g.addClass(f)
},clearStatus:function(){if(!this.has("status")){return false
}this.get("formField").removeClass(this.get("status"));
this.set({status:""})
},name:function(){return this.get("inputField").attr("name")
},focus:function(){if(this.has("status")&&this.get("status")=="focus"){return false
}var f=this.get("inputField");
var g=_T.scope(function(){f[0].focus()
},this);
if(a){clearTimeout(a)
}a=setTimeout(g,2);
if(this.isDefault()){this.setStatus("pause")
}else{this.setStatus("focus")
}},unfocus:function(){var f=this.get("inputField");
f.unbind();
if(this.get("status")!="pause"){this.toggleDefaultText()
}this.clearStatus();
$.publish("formfield/tabKey",[f])
},error:function(){this.setStatus("error")
},readonly:function(){this.get("inputField").prop("readonly",true)
},unReadonly:function(){this.get("inputField").removeAttr("readonly")
},value:function(h){var f=this.get("inputField");
if(!h){return f.val()
}this.toggleDefaultText();
var g=f.val(h);
return g
},isActive:function(){return(!this.isEmpty()&&!this.get("formField").hasClass("default-text"))
},isEmpty:function(){var f=this.value();
if(f==""||this.get("inputField").hasClass("default-text")){return true
}return false
},clear:function(){this.clearStatus();
this.value("")
},reset:function(){this.clear();
this.toggleDefaultText()
},remove:function(){return this.collection.remove(this)
},toggleDefaultText:function(){var f=this.get("inputField");
if(f.hasClass("default-text")){f.removeClass("default-text");
this.get("formField").children("p.label").addClass("no-display")
}else{if(f.val()==""){f.addClass("default-text");
this.get("formField").children("p.label").removeClass("no-display")
}}},isDefault:function(){return this.get("inputField").hasClass("default-text")
}});
var b=Backbone.Collection.extend({model:e,initialize:function(){},invalid:function(){_.each(this.models,function(g,f){g.error()
})
},valid:function(){_.each(this.models,function(g,f){g.clearStatus()
})
},clear:function(f){var g=(!f)?this.models:f;
_.each(g,function(i,h){i.get("inputField").attr("value","")
},this)
}});
var d=Backbone.Collection.extend({model:e,initialize:function(){}});
var c={CLASS:{formField:"form-field"},model:null,collection:null,errorCollection:null,focusCollection:null,init:function(){this.model=e;
this.collection=new b;
this.errorCollection=new d;
this.focusCollection=new Backbone.Collection;
this.collection.bind("add",this.add);
var f=$("."+this.CLASS.formField);
_.each(f,function(j,h){if(!j){return
}var k=f.filter(j);
var g={formField:k};
this.collection.add(g);
var i=this.collection.at(h);
if(!i.isEmpty()){i.get("formField").children("p.label").addClass("no-display")
}if(k.hasClass("error")){this.triggerError(i);
this.errorCollection.add(i.clone(),{silent:true})
}},this)
},add:function(f){this._connect(f)
},_connect:function(f){f.get("formField").bind("focusin click",this.focus)
},_disconnect:function(f){f.get("formField").unbind()
},blur:function(f){var g=f.data;
g.unfocus();
var h=this.focusCollection.get(g.name());
if(g.has("parent")&&g.value()!=h.get("value")){$.publish(g.get("parent")+"/edited",[g.get("parent")])
}this.focusCollection.remove(h);
this._disconnect(g);
this._connect(g)
},focus:function(f){var j=$(f.currentTarget),g=j.attr("input_id");
var h=this.collection.get(g);
if(!h){return
}this._disconnect(h);
var i=h.get("formField");
if(h.get("status")=="error"){this.clearError(h)
}this.focusCollection.add({id:h.name(),value:h.value()});
h.focus();
i.bind("focusout",h,this.blur)
},hasFocus:function(){if(this.focusCollection.length!=0){return this.focusCollection.at(0)
}return false
},hasErrors:function(){return(this.errorCollection.length!=0)
},get:function(g){if(g===null){return false
}var f=false;
if(!g.tagName){f=g
}if(g.tagName&&g.tagName.toUpperCase()=="INPUT"){f=g.id
}return f?(this.collection.get(f)||false):false
},reset:function(){if(this.errorCollection.length==0){return false
}var f;
while((f=this.errorCollection.length)>0){var g=this.errorCollection.at(f-1).id;
this.clearError(UI.FormField.get(g));
this.errorCollection.remove(this.errorCollection.get(g),{silent:true})
}},_triggerError:function(f,h,g){f.get("errorField"+g).removeClass("no-display").html(h);
f.error()
},triggerError:function(f,g){this._triggerError(f,g,"")
},triggerErrorAbove:function(f,g){this._triggerError(f,g,"Above")
},_clearError:function(f,h){var g=this.errorCollection.get(f);
if(g){this.errorCollection.remove(g)
}f.get("errorField"+h).addClass("no-display").html("");
f.clear()
},clearError:function(f){this._clearError(f,"");
this._clearError(f,"Above")
}};
_.bindAll(c);
UI.FormField=c;
UI.FormField.init()
})();
