!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=this,l=(new Date).getTime(),m=[],n=j.selector||"",o=g.namespace+"-module",p=g.className,q=g.metadata,r=g.error,s=j.data(o),t=arguments[0],u=s!==d&&"string"==typeof t,v=[].slice.call(arguments,1);return e={initialize:function(){var c,f,h,k,l,m,n=(new Date).getTime(),o={},s={};return g.serializeForm&&a(this).toJSON()!==d&&(o=e.get.formData(),e.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=e.get.url(e.get.templateURL()))?(h=a.Deferred().always(function(){g.stateContext&&i.removeClass(p.loading),a.proxy(g.complete,j)()}).done(function(b){e.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,f){var h,j=g.error[c]!==d?g.error[c]:f;if(b!==d)if(b.readyState!==d&&4==b.readyState){if(200!=b.status&&f!==d&&""!==f)e.error(k.statusMessage+f);else if("error"==c&&"json"==g.dataType)try{h=a.parseJSON(b.responseText),h&&h.error!==d&&(j=h.error)}catch(k){e.error(k.JSONParse)}i.removeClass(p.loading).addClass(p.error),g.errorLength>0&&setTimeout(function(){i.removeClass(p.error)},g.errorLength),e.debug("API Request error:",j),a.proxy(g.failure,i)(j,g,this)}else e.debug("Request Aborted (Most likely caused by page change)")}),a.extend(!0,s,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&&(e.verbose("Adding progress events"),a.extend(!0,s,{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}})),e.verbose("Creating AJAX request with settings: ",s),m=a.ajax(s).always(function(){f=g.loadingLength-((new Date).getTime()-n),g.loadingDelay=0>f?0:f}).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):(e.error(r.missingURL),e.reset(),void 0):(e.error(r.beforeSend),e.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&&(e.debug("Creating url for: ",b),g.api[b]!==d?a=g.api[b]:e.error(r.missingAction)),g.url&&(a=g.url,e.debug("Getting url",a)),a},url:function(b,c){var f;return b&&(f=b.match(g.regExpTemplate),c=c||g.urlData,f&&(e.debug("Looking for URL variables",f),a.each(f,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(e.verbose("Looking for variable",i,j,j.data(i),c[i]),k===!1)e.debug("Removing variable from URL",f),b=b.replace("/"+h,"");else{if(k===d||!k)return e.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(b,c){return c===d?g[b]:(a.isPlainObject(b)?a.extend(!0,g,b):g[b]=c,void 0)},internal:function(b,c){return c===d?e[b]:(a.isPlainObject(b)?a.extend(!0,e,b):e[b]=c,void 0)},debug:function(){g.debug&&(g.performance?e.performance.log(arguments):(e.debug=Function.prototype.bind.call(console.info,console,g.moduleName+":"),e.debug.apply(console,arguments)))},verbose:function(){g.verbose&&g.debug&&(g.performance?e.performance.log(arguments):(e.verbose=Function.prototype.bind.call(console.info,console,g.moduleName+":"),e.verbose.apply(console,arguments)))},error:function(){e.error=Function.prototype.bind.call(console.error,console,g.moduleName+":"),e.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;g.performance&&(b=(new Date).getTime(),d=l||b,c=b-d,l=b,m.push({Element:k,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(e.performance.timer),e.performance.timer=setTimeout(e.performance.display,100)},display:function(){var b=g.moduleName+":",c=0;clearTimeout(e.performance.timer),l=!1,a.each(m,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",n&&(b+=" '"+n+"'"),(console.group!==d||console.table!==d)&&m.length>0&&(console.groupCollapsed(b),console.table?console.table(m):a.each(m,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),m=[]}},invoke:function(b,c,g){var h,i,j;return c=c||v,g=k||g,"string"==typeof b&&s!==d&&(b=b.split(/[\. ]/),h=b.length-1,a.each(b,function(c,f){var g=c!=h?f+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(s[f])&&c!=h)s=s[f];else{if(!a.isPlainObject(s[g])||c==h)return s[f]!==d?(i=s[f],!1):s[g]!==d?(i=s[g],!1):(e.error(r.method),!1);s=s[g]}})),a.isFunction(i)?j=i.apply(g,c):i!==d&&(j=i),a.isArray(f)?f.push(j):"string"==typeof f?f=[f,j]:j!==d&&(f=j),i}},u?(s===d&&e.initialize(),e.invoke(t)):(s!==d&&e.destroy(),e.initialize()),f!==d?f: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={name:"API",namespace:"api",debug:!0,verbose:!0,performance:!0,api:{},beforeSend:function(a){return a},beforeXHR:function(){},success:function(){},complete:function(){},failure:function(){},progress:!1,error:{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);