!function(a,b,c,d){a.fn.state=function(b){var c,e=a(this),f=e.selector||"",g=arguments[0],h=[].slice.call(arguments,1),i=(new Date).getTime(),j=[];return e.each(function(){var k,l=a(this),m=a.extend(!0,{},a.fn.state.settings,b),n=this,o=l.data("module-"+m.namespace),p="string"==typeof g,q=m.namespace,r=m.metadata,s=m.className,t=m.states,u=m.text;k={initialize:function(){k.verbose("Initializing module",n),m.automatic&&k.add.defaults(),m.context&&""!==f?(k.allows("hover")&&a(n,m.context).on(f,"mouseenter."+q,k.hover.enable).on(f,"mouseleave."+q,k.hover.disable),k.allows("pressed")&&a(n,m.context).on(f,"mousedown."+q,k.pressed.enable).on(f,"mouseup."+q,k.pressed.disable),k.allows("focus")&&a(n,m.context).on(f,"focus."+q,k.focus.enable).on(f,"blur."+q,k.focus.disable),a(m.context).on(f,"mouseenter."+q,k.text.change).on(f,"mouseleave."+q,k.text.reset).on(f,"click."+q,k.toggle)):(k.allows("hover")&&l.on("mouseenter."+q,k.hover.enable).on("mouseleave."+q,k.hover.disable),k.allows("pressed")&&l.on("mousedown."+q,k.pressed.enable).on("mouseup."+q,k.pressed.disable),k.allows("focus")&&l.on("focus."+q,k.focus.enable).on("blur."+q,k.focus.disable),l.on("mouseenter."+q,k.text.change).on("mouseleave."+q,k.text.reset).on("click."+q,k.toggle)),l.data("module-"+q,k)},destroy:function(){k.verbose("Destroying previous module",n),l.off("."+q)},refresh:function(){k.verbose("Refreshing selector cache",n),l=a(n)},add:{defaults:function(){var c=b&&a.isPlainObject(b.states)?b.states:{};a.each(m.defaults,function(b,e){k.is[b]!==d&&k.is[b]()&&(k.verbose("Adding default states",b,n),a.extend(m.states,e,c))})}},is:{active:function(){return l.hasClass(s.active)},loading:function(){return l.hasClass(s.loading)},inactive:function(){return!l.hasClass(s.active)},enabled:function(){return!l.is(m.filter.active)},disabled:function(){return l.is(m.filter.active)},textEnabled:function(){return!l.is(m.filter.text)},button:function(){return l.is(".button:not(a, .submit)")},input:function(){return l.is("input")}},allow:function(a){k.debug("Now allowing state",a),t[a]=!0},disallow:function(a){k.debug("No longer allowing",a),t[a]=!1},allows:function(a){return t[a]||!1},enable:function(a){k.allows(a)&&l.addClass(s[a])},disable:function(a){k.allows(a)&&l.removeClass(s[a])},textFor:function(a){return u[a]||!1},focus:{enable:function(){l.addClass(s.focus)},disable:function(){l.removeClass(s.focus)}},hover:{enable:function(){l.addClass(s.hover)},disable:function(){l.removeClass(s.hover)}},pressed:{enable:function(){l.addClass(s.pressed).one("mouseleave",k.pressed.disable)},disable:function(){l.removeClass(s.pressed)}},toggle:function(){var a=l.data(r.promise);k.allows("active")&&k.is.enabled()&&(k.refresh(),a!==d?k.listenTo(a):k.change())},listenTo:function(b){k.debug("API request detected, waiting for state signal",b),b?(u.loading&&k.text.update(u.loading),a.when(b).then(function(){"resolved"==b.state()?(k.debug("API request succeeded"),m.activateTest=function(){return!0},m.deactivateTest=function(){return!0}):(k.debug("API request failed"),m.activateTest=function(){return!1},m.deactivateTest=function(){return!1}),k.change()})):(m.activateTest=function(){return!1},m.deactivateTest=function(){return!1})},change:function(){k.debug("Determining state change direction"),k.is.inactive()?k.activate():k.deactivate(),m.sync&&k.sync(),a.proxy(m.onChange,n)()},activate:function(){a.proxy(m.activateTest,n)()&&(k.debug("Setting state to active"),l.addClass(s.active),k.text.update(u.active)),a.proxy(m.onActivate,n)()},deactivate:function(){a.proxy(m.deactivateTest,n)()&&(k.debug("Setting state to inactive"),l.removeClass(s.active),k.text.update(u.inactive)),a.proxy(m.onDeactivate,n)()},sync:function(){k.verbose("Syncing other buttons to current state"),k.is.active()?e.not(l).state("activate"):e.not(l).state("deactivate")},text:{get:function(){return m.selector.text?l.find(m.selector.text).text():l.html()},flash:function(a,b){var c=k.text.get();a=a||m.text.flash,b=b||m.flashDuration,k.text.update(a),setTimeout(function(){k.text.update(c)},b)},change:function(){k.verbose("Checking if text should be changed"),k.is.textEnabled()&&(k.is.active()?u.hover?(k.verbose("Changing text to hover text",u.hover),k.text.update(u.hover)):u.disable&&(k.verbose("Changing text to disable text",u.disable),k.text.update(u.disable)):u.hover?(k.verbose("Changing text to hover text",u.disable),k.text.update(u.hover)):u.enable&&(k.verbose("Changing text to enable text",u.disable),k.text.update(u.enable)))},reset:function(){var a=u.active||l.data(r.storedText),b=u.inactive||l.data(r.storedText);k.is.textEnabled()&&(k.is.active()&&a?(k.verbose("Resetting active text",a),k.text.update(a)):b&&(k.verbose("Resetting inactive text",a),k.text.update(b)))},update:function(a){var b=k.text.get();a&&a!==b&&(k.debug("Updating text",a),m.selector.text?l.data(r.storedText,a).find(m.selector.text).text(a):l.data(r.storedText,a).html(a))}},setting:function(a,b){return b===d?m[a]:(m[a]=b,void 0)},performance:{log:function(a){var b,c;m.performance&&(b=(new Date).getTime(),c=b-i,i=b,j.push({Name:a,"Execution Time":c}),clearTimeout(k.performance.timer),k.performance.timer=setTimeout(k.performance.display,100))},display:function(){var b=m.moduleName+" Performance ("+f+")";m.moduleName+": "+f+"("+e.size()+" elements)",console.group!==d&&j.length>0&&(console.groupCollapsed(b),console.table?console.table(j):a.each(j,function(a,b){console.log(b.Name+":"+b["Execution Time"])}),console.groupEnd(),j=[])}},verbose:function(){m.verbose&&m.debug&&(k.performance.log(arguments[0]),k.verbose=Function.prototype.bind.call(console.info,console,m.moduleName+":"))},debug:function(){m.debug&&(k.performance.log(arguments[0]),k.debug=Function.prototype.bind.call(console.info,console,m.moduleName+":"))},error:function(){console.log!==d&&(k.error=Function.prototype.bind.call(console.error,console,m.moduleName+":"))},invoke:function(b,c,e){var f,g;return e=e||[].slice.call(arguments,2),"string"==typeof b&&o!==d&&(b=b.split("."),f=b.length-1,a.each(b,function(b,c){return a.isPlainObject(o[c])&&b!=f?(o=o[c],!0):o[c]!==d?(g=o[c],!0):(k.error(m.errors.method),!1)})),a.isFunction(g)?g.apply(c,e):g}},p?c=k.invoke(g,this,h):(o!==d&&k.destroy(),k.initialize())}),c!==d?c:this},a.fn.state.settings={moduleName:"State Module",debug:!0,verbose:!1,namespace:"state",performance:!1,onActivate:function(){},onDeactivate:function(){},onChange:function(){},activateTest:function(){return!0},deactivateTest:function(){return!0},automatic:!0,sync:!1,flashDuration:3e3,filter:{text:".loading, .disabled",active:".disabled"},context:!1,errors:{method:"The method you called is not defined."},metadata:{promise:"promise",storedText:"stored-text"},className:{focus:"focus",hover:"hover",pressed:"down",active:"active",loading:"loading"},selector:{text:!1},defaults:{input:{hover:!0,focus:!0,pressed:!0,loading:!1,active:!1},button:{hover:!0,focus:!1,pressed:!0,active:!1,loading:!0}},states:{hover:!0,focus:!0,pressed:!0,loading:!1,active:!1},text:{flash:!1,hover:!1,active:!1,inactive:!1,enable:!1,disable:!1}}}(jQuery,window,document);