!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.macy=n()}(this,function(){"use strict";function t(t,n){var e=void 0;return function(){e&&cleartimeout(e),e=settimeout(t,n)}}function n(t,n){for(var e=t.length,r=e,o=[];e--;)o.push(n(t[r-e-1]));return o}function e(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(window.promise)return a(t,n,e);t.recalculate(!0,!0)}function r(t){for(var n=t.options,e=t.responsiveoptions,r=t.keys,o=t.docwidth,i=void 0,s=0;s=a&&(i=n.breakat[a],o(i,e))}return e}function o(t){for(var n=t.options,e=t.responsiveoptions,r=t.keys,o=t.docwidth,i=void 0,s=r.length-1;s>=0;s--){var a=parseint(r[s],10);o<=a&&(i=n.breakat[a],o(i,e))}return e}function i(t){var n=t.usecontainerforbreakpoints?t.container.clientwidth:window.innerwidth,e={columns:t.columns};b(t.margin)?e.margin={x:t.margin.x,y:t.margin.y}:e.margin={x:t.margin,y:t.margin};var i=object.keys(t.breakat);return t.mobilefirst?r({options:t,responsiveoptions:e,keys:i,docwidth:n}):o({options:t,responsiveoptions:e,keys:i,docwidth:n})}function s(t){return i(t).columns}function a(t){return i(t).margin}function c(t){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],e=s(t),r=a(t).x,o=100/e;if(!n)return o;if(1===e)return"100%";var i="px";if("string"==typeof r){var c=parsefloat(r);i=r.replace(c,""),r=c}return r=(e-1)*r/e,"%"===i?o-r+"%":"calc("+o+"% - "+r+i+")"}function u(t,n){var e=s(t.options),r=0,o=void 0,i=void 0;if(1===++n)return 0;i=a(t.options).x;var u="px";if("string"==typeof i){var l=parsefloat(i,10);u=i.replace(l,""),i=l}return o=(i-(e-1)*i/e)*(n-1),r+=c(t.options,!1)*(n-1),"%"===u?r+o+"%":"calc("+r+"% + "+o+u+")"}function l(t){var n=0,e=t.container,r=t.rows;v(r,function(t){n=t>n?t:n}),e.style.height=n+"px"}function p(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],o=s(t.options),i=a(t.options).y;m(t,o,e),v(n,function(n){var e=0,o=parseint(n.offsetheight,10);isnan(o)||(t.rows.foreach(function(n,r){n2&&void 0!==arguments[2]&&arguments[2],r=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],o=s(t.options),i=a(t.options).y;m(t,o,e),v(n,function(n){t.lastcol===o&&(t.lastcol=0);var e=c(n,"height");e=parseint(n.offsetheight,10),isnan(e)||(n.style.position="absolute",n.style.top=t.rows[t.lastcol]+"px",n.style.left=""+t.cols[t.lastcol],t.rows[t.lastcol]+=isnan(e)?0:e+i,t.lastcol+=1,r&&(n.dataset.macycomplete=1))}),r&&(t.tmprows=null),l(t)}var h=function t(n,e){if(!(this instanceof t))return new t(n,e);if(n&&n.nodename)return n;if(n=n.replace(/^\s*/,"").replace(/\s*$/,""),e)return this.bycss(n,e);for(var r in this.selectors)if(e=r.split("/"),new regexp(e[1],e[2]).test(n))return this.selectors[r](n);return this.bycss(n)};h.prototype.bycss=function(t,n){return(n||document).queryselectorall(t)},h.prototype.selectors={},h.prototype.selectors[/^\.[\w\-]+$/]=function(t){return document.getelementsbyclassname(t.substring(1))},h.prototype.selectors[/^\w+$/]=function(t){return document.getelementsbytagname(t)},h.prototype.selectors[/^\#[\w\-]+$/]=function(t){return document.getelementbyid(t.substring(1))};var v=function(t,n){for(var e=t.length,r=e;e--;)n(t[r-e-1])},m=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];this.running=!1,this.events=[],this.add(t)};m.prototype.run=function(){if(!this.running&&this.events.length>0){var t=this.events.shift();this.running=!0,t(),this.running=!1,this.run()}},m.prototype.add=function(){var t=this,n=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return!!n&&(array.isarray(n)?v(n,function(n){return t.add(n)}):(this.events.push(n),void this.run()))},m.prototype.clear=function(){this.events=[]};var d=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.instance=t,this.data=n,this},y=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];this.events={},this.instance=t};y.prototype.on=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return!(!t||!n)&&(array.isarray(this.events[t])||(this.events[t]=[]),this.events[t].push(n))},y.prototype.emit=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!t||!array.isarray(this.events[t]))return!1;var e=new d(this.instance,n);v(this.events[t],function(t){return t(e)})};var g=function(t){return!("naturalheight"in t&&t.naturalheight+t.naturalwidth===0)||t.width+t.height!==0},e=function(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return new promise(function(t,e){if(n.complete)return g(n)?t(n):e(n);n.addeventlistener("load",function(){return g(n)?t(n):e(n)}),n.addeventlistener("error",function(){return e(n)})}).then(function(n){e&&t.emit(t.constants.event_image_load,{img:n})}).catch(function(n){return t.emit(t.constants.event_image_error,{img:n})})},w=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return n(e,function(n){return e(t,n,r)})},a=function(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return promise.all(w(t,n,e)).then(function(){t.emit(t.constants.event_image_complete)})},i=function(n){return t(function(){n.emit(n.constants.event_resize),n.queue.add(function(){return n.recalculate(!0,!0)})},100)},n=function(t){if(t.container=h(t.options.container),t.container instanceof h||!t.container)return!!t.options.debug&&console.error("error: container not found");t.container.length&&(t.container=t.container[0]),t.options.container=t.container,t.container.style.position="relative"},t=function(t){t.queue=new m,t.events=new y(t),t.rows=[],t.resizer=i(t)},l=function(t){var n=h("img",t.container);window.addeventlistener("resize",t.resizer),t.on(t.constants.event_image_load,function(){return t.recalculate(!1,!1)}),t.on(t.constants.event_image_complete,function(){return t.recalculate(!0,!0)}),t.options.useownimageloader||e(t,n,!t.options.waitforimages),t.emit(t.constants.event_initialized)},_=function(t){n(t),t(t),l(t)},b=function(t){return t===object(t)&&"[object array]"!==object.prototype.tostring.call(t)},o=function(t,n){b(t)||(n.columns=t),b(t)&&t.columns&&(n.columns=t.columns),b(t)&&t.margin&&!b(t.margin)&&(n.margin={x:t.margin,y:t.margin}),b(t)&&t.margin&&b(t.margin)&&t.margin.x&&(n.margin.x=t.margin.x),b(t)&&t.margin&&b(t.margin)&&t.margin.y&&(n.margin.y=t.margin.y)},c=function(t,n){return window.getcomputedstyle(t,null).getpropertyvalue(n)},m=function(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(t.lastcol||(t.lastcol=0),t.rows.length<1&&(e=!0),e){t.rows=[],t.cols=[],t.lastcol=0;for(var r=n-1;r>=0;r--)t.rows[r]=0,t.cols[r]=u(t,r)}else if(t.tmprows){t.rows=[];for(var r=n-1;r>=0;r--)t.rows[r]=t.tmprows[r]}else{t.tmprows=[];for(var r=n-1;r>=0;r--)t.tmprows[r]=t.rows[r]}},v=function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],e=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],r=n?t.container.children:h(':scope > *:not([data-macy-complete="1"])',t.container);r=array.from(r).filter(function(t){return null!==t.offsetparent});var o=c(t.options);return v(r,function(t){n&&(t.dataset.macycomplete=0),t.style.width=o}),t.options.trueorder?(f(t,r,n,e),t.emit(t.constants.event_recalculated)):(p(t,r,n,e),t.emit(t.constants.event_recalculated))},r=function(){return!!window.promise},x=object.assign||function(t){for(var n=1;n0&&void 0!==arguments[0]?arguments[0]:k;if(!(this instanceof t))return new t(n);this.options={},x(this.options,k,n),this.options.cancellegacy&&!r()||_(this)};return q.init=function(t){return console.warn("depreciated: macy.init will be removed in v3.0.0 opt to use macy directly like so macy({ /*options here*/ }) "),new q(t)},q.prototype.recalculateonimageload=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return e(this,h("img",this.container),!t)},q.prototype.runonimageload=function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=h("img",this.container);return this.on(this.constants.event_image_complete,t),n&&this.on(this.constants.event_image_load,t),e(this,r,n)},q.prototype.recalculate=function(){var t=this,n=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return e&&this.queue.clear(),this.queue.add(function(){return v(t,n,e)})},q.prototype.remove=function(){window.removeeventlistener("resize",this.resizer),v(this.container.children,function(t){t.removeattribute("data-macy-complete"),t.removeattribute("style")}),this.container.removeattribute("style")},q.prototype.reinit=function(){this.recalculate(!0,!0),this.emit(this.constants.event_initialized),window.addeventlistener("resize",this.resizer),this.container.style.position="relative"},q.prototype.on=function(t,n){this.events.on(t,n)},q.prototype.emit=function(t,n){this.events.emit(t,n)},q.constants={event_initialized:"macy.initialized",event_recalculated:"macy.recalculated",event_image_load:"macy.image.load",event_image_error:"macy.image.error",event_image_complete:"macy.images.complete",event_resize:"macy.resize"},q.prototype.constants=q.constants,q});