!function(a,b,c,d){a.api=a.fn.api=function(c){var e,f,g=a.extend(!0,{},a.api.settings,c),h="function"!=typeof this?this:a("
"),i=g.stateContext?a(g.stateContext):a(h),j="object"==typeof this?a(h):i,k=j.data("module-"+g.namespace),l=j.get(),m=arguments[0],n="string"==typeof m,o=[].slice.call(arguments,1),p=g.className,q=g.metadata,r=g.errors;return f={initialize:function(){var c,e,h,k,l,m,n=(new Date).getTime(),o={},r={},s=g.errors;return g.serializeForm&&a(this).toJSON()!==d&&(o=f.get.formData(),f.debug("Adding form data to API Request",o),a.extend(!0,g.data,o)),c=a.proxy(g.beforeSend,j)(g),c===d||c?(k=f.get.url(f.get.templateURL()))?(h=a.Deferred().always(function(){g.stateContext&&i.removeClass(p.loading),a.proxy(g.complete,j)()}).done(function(b){f.debug("API request successful"),"json"==g.dataType?b.error!==d?a.proxy(g.failure,i)(b.error,g,j):a.isArray(b.errors)?a.proxy(g.failure,i)(b.errors[0],g,j):a.proxy(g.success,i)(b,g,j):a.proxy(g.success,i)(b,g,j)}).fail(function(b,c,e){var h,j=g.errors[c]!==d?g.errors[c]:e;if(b!==d)if(b.readyState!==d&&4==b.readyState){if(200!=b.status&&e!==d&&""!==e)f.error(s.statusMessage+e);else if("error"==c&&"json"==g.dataType)try{h=a.parseJSON(b.responseText),h&&h.error!==d&&(j=h.error)}catch(k){f.error(s.JSONParse)}i.removeClass(p.loading).addClass(p.error),g.errorLength>0&&setTimeout(function(){i.removeClass(p.error)},g.errorLength),f.debug("API Request error:",j),a.proxy(g.failure,i)(j,g,this)}else f.debug("Request Aborted (Most likely caused by page change)")}),a.extend(!0,r,g,{success:function(){},failure:function(){},complete:function(){},type:g.method||g.type,data:l,url:k,beforeSend:g.beforeXHR}),g.stateContext&&i.addClass(p.loading),g.progress&&(f.verbose("Adding progress events"),a.extend(!0,r,{xhr:function(){var c=new b.XMLHttpRequest;return c.upload.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(1e4*(b.loaded/b.total))/100+"%",a.proxy(g.progress,i)(c,b))},!1),c.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(1e4*(b.loaded/b.total))/100+"%",a.proxy(g.progress,i)(c,b))},!1),c}})),f.verbose("Creating AJAX request with settings: ",r),m=a.ajax(r).always(function(){e=g.loadingLength-((new Date).getTime()-n),g.loadingDelay=0>e?0:e}).done(function(a){var b=this;setTimeout(function(){h.resolveWith(b,[a])},g.loadingDelay)}).fail(function(a,b,c){var d=this;"abort"!=b?setTimeout(function(){h.rejectWith(d,[a,b,c])},g.loadingDelay):i.removeClass(p.error).removeClass(p.loading)}),g.stateContext&&j.data(q.promise,h).data(q.xhr,m),void 0):(f.error(s.missingURL),f.reset(),void 0):(f.error(s.beforeSend),f.reset(),void 0)},get:{formData:function(){return j.closest("form").toJSON()},templateURL:function(){var a,b=j.data(g.metadata.action)||g.action||!1;return b&&(f.debug("Creating url for: ",b),g.api[b]!==d?a=g.api[b]:f.error(r.missingAction)),g.url&&(a=g.url,f.debug("Getting url",a)),a},url:function(b,c){var e;return b&&(e=b.match(g.regExpTemplate),c=c||g.urlData,e&&(f.debug("Looking for URL variables",e),a.each(e,function(g,h){var i=h.substr(2,h.length-3),k=a.isPlainObject(c)&&c[i]!==d?c[i]:j.data(i)!==d?j.data(i):c[i];if(f.verbose("Looking for variable",i,j,j.data(i),c[i]),k===!1)f.debug("Removing variable from URL",e),b=b.replace("/"+h,"");else{if(k===d||!k)return f.error(r.missingParameter+i),b=!1,!1;b=b.replace(h,k)}}))),b}},reset:function(){j.data(q.promise,!1).data(q.xhr,!1),i.removeClass(p.error).removeClass(p.loading)},setting:function(a,b){return b===d?g[a]:(g[a]=b,void 0)},verbose:function(){g.verbose&&g.debug&&(f.verbose=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},debug:function(){g.debug&&(f.debug=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},error:function(){console.log!==d&&(f.error=Function.prototype.bind.call(console.error,console,g.moduleName+":"))},invoke:function(b,c,e){var g,h;return c=c||o,e=l||e,"string"==typeof b&&k!==d&&(b=b.split("."),g=b.length-1,a.each(b,function(b,c){return a.isPlainObject(k[c])&&b!=g?(k=k[c],!0):k[c]!==d?(h=k[c],!0):(f.error(r.method),!1)})),a.isFunction(h)?(k.verbose("Executing invoked function",h),h.apply(e,c)):h||!1}},n?(k===d&&f.initialize(),e=f.invoke(m)):(k!==d&&f.destroy(),f.initialize()),e?e:this},a.fn.apiButton=function(b){return a(this).each(function(){var c,d=a(this),e=a(this).selector||"",f=a.isFunction(b)?a.extend(!0,{},a.api.settings,a.fn.apiButton.settings,{stateContext:this,success:b}):a.extend(!0,{},a.api.settings,a.fn.apiButton.settings,{stateContext:this},b);c={initialize:function(){f.context&&""!==e?a(f.context).on(e,"click."+f.namespace,c.click):d.on("click."+f.namespace,c.click)},click:function(){f.filter&&0!==a(this).filter(f.filter).size()||a.proxy(a.api,this)(f)}},c.initialize()}),this},a.api.settings={moduleName:"API Module",namespace:"api",debug:!0,verbose:!0,api:{},beforeSend:function(a){return a},beforeXHR:function(){},success:function(){},complete:function(){},failure:function(){},progress:!1,errors:{missingAction:"API action used but no url was defined",missingURL:"URL not specified for the API action",missingParameter:"Missing an essential URL parameter: ",timeout:"Your request timed out",error:"There was an error with your request",parseError:"There was an error parsing your request",JSONParse:"JSON could not be parsed during error handling",statusMessage:"Server gave an error: ",beforeSend:"The before send function has aborted the request",exitConditions:"API Request Aborted. Exit conditions met"},className:{loading:"loading",error:"error"},metadata:{action:"action",promise:"promise",xhr:"xhr"},regExpTemplate:/\{\$([A-z]+)\}/g,action:!1,url:!1,urlData:!1,serializeForm:!1,stateContext:!1,method:"get",data:{},dataType:"json",cache:!0,loadingLength:200,errorLength:2e3},a.fn.apiButton.settings={filter:".disabled, .loading",context:!1,stateContext:!1}}(jQuery,window,document);