/*!
    Copyright 2009 5to1.com
*/

Package("login");login.LoginViewController=Class.create(coherent.ViewController,{LOGIN_ENDPOINT:"/api/gallery/login",PREFETCH_SCRIPTS:[app.VIRTUAL_ROOT+"/res/latest/5to1-gallery.js"],PREFETCH_CSS:[app.VIRTUAL_ROOT+"/res/latest/5to1-gallery.css"],XHR_OPTIONS:{responseContentType:"application/json",contentType:"application/json",headers:{accept:"application/json"}},__structure__:{"fieldset input[type=text]":views.BetterTextField({valueBinding:{keypath:"username",nullValuePlaceholder:"User Name"},sendsActionOnEndEditing:false,action:"performLogin"}),"fieldset input[type=password]":views.BetterTextField({valueBinding:{keypath:"password",nullValuePlaceholder:"Password"},sendsActionOnEndEditing:false,action:"performLogin"}),"fieldset button":coherent.Button({action:"performLogin",enabledBinding:"loading(not)"}),".server-error":coherent.View({visibleBinding:{keypath:"statusCode",transformedValue:function(a){a=parseInt(a||0,10);return a>=500}},animated:true}),".credentials-error":coherent.View({visibleBinding:{keypath:"statusCode",transformedValue:function(a){a=parseInt(a||0,10);return(400<=a)&&(a<500)}},animated:true})},init:function(){this.setValueForKey("","username");this.setValueForKey("","password");Event.observe(window,"load",this.prefetchResources.bind(this));var a=this.viewWithSelector("fieldset input[type=text]");a.focus()},prefetchResources:function(){var c=document.getElementsByTagName("head")[0];function b(f){var e=document.createElement("script");e.src=f;e.type="text/javascript";e.charset="utf-8";c.appendChild(e)}function d(e){var f=document.createElement("link");f.rel="stylesheet";f.type="text/css";f.charset="utf-8";f.href=e;c.appendChild(f)}this.PREFETCH_SCRIPTS.forEach(b);this.PREFETCH_CSS.forEach(d);this.context=new model.ManagedObjectContext();this.context.defaultEndpoint="/api/model/${__typename__}/${id}";this.context.defaultAttributeEndpoint="/api/model/${__typename__}/${id}/${__attr__}";this.context.endpoints[model.KeywordCollection.__typename__]="/api/gallery/keywords";var a=this.context.fetch(model.KeywordCollection)},performLogin:function(){var b={username:this.username,password:this.password};var a={body:JSON.stringify(b)};Object.applyDefaults(a,this.XHR_OPTIONS);var c=XHR.post(this.LOGIN_ENDPOINT,null,a);c.addMethods(this.loginCompleted.bind(this),this.loginFailed.bind(this));this.setValueForKey(true,"loading")},loginCompleted:function(a){var b=new Cookie("LoginInfo");b.setObjectValue(a,{path:"/"});window.location=app.LOGIN_DESTINATION},loginFailed:function(a){this.setValueForKey(a.status,"statusCode");this.setValueForKey(false,"loading")}});