(function(){var t,e,i;function o(t,e){var i,o;if(this.options={target:"instafeed",get:"popular",resolution:"thumbnail",sortBy:"none",links:!0,mock:!1,useHttp:!1,afterLoad:null,api:"old"},"object"==typeof t)for(i in t)o=t[i],this.options[i]=o;this.context=null!=e?e:this,this.unique=this._genKey()}o.prototype.hasNext=function(){return"string"==typeof this.context.nextUrl&&0<this.context.nextUrl.length},o.prototype.next=function(){return!!this.hasNext()&&this.run(this.context.nextUrl)},o.prototype.run=function(t){var e,i;if("string"!=typeof this.options.accessToken)throw new Error("Missing accessToken.");return null!=this.options.before&&"function"==typeof this.options.before&&this.options.before.call(this),"undefined"!=typeof document&&null!==document&&((i=document.createElement("script")).id="instafeed-fetcher",i.src=t||this._buildUrl(),document.getElementsByTagName("head")[0].appendChild(i),e="instafeedCache"+this.unique,window[e]=new o(this.options,this),window[e].unique=this.unique),!0},o.prototype.parse=function(t){var e,i,o,n,s,r,a,p,l,c,u,h,d,m,f,g,y,w,v,_;if("object"!=typeof t){if(null!=this.options.error&&"function"==typeof this.options.error)return this.options.error.call(this,"Invalid JSON data"),!1;throw new Error("Invalid JSON response")}if(0===t.data.length){if(null!=this.options.error&&"function"==typeof this.options.error)return this.options.error.call(this,"No images were returned from Instagram"),!1;throw new Error("No images were returned from Instagram")}if(null!=this.options.success&&"function"==typeof this.options.success&&this.options.success.call(this,t),this.context.nextUrl="",null!=t.pagination&&(this.context.nextUrl=t.pagination.next_url),"none"!==this.options.sortBy)switch(y="least"===(w="random"===this.options.sortBy?["","random"]:this.options.sortBy.split("-"))[0],w[1]){case"random":t.data.sort(function(){return.5-Math.random()});break;case"recent":t.data=this._sortBy(t.data,"created_time",y);break;case"liked":t.data=this._sortBy(t.data,"likes.count",y);break;case"commented":t.data=this._sortBy(t.data,"comments.count",y);break;default:throw new Error("Invalid option for sortBy: '"+this.options.sortBy+"'.")}if("undefined"!=typeof document&&null!==document&&!1===this.options.mock){for(c=t.data,g=parseInt(this.options.limit,10),null!=this.options.limit&&c.length>g&&(c=c.slice(0,g)),s=document.createDocumentFragment(),null!=this.options.filter&&"function"==typeof this.options.filter&&(c=this._filter(c,this.options.filter)),u=r="",_=document.createElement("div"),a=0,d=c.length;a<d;a++){p=c[a],l="new"===this.options.api?p.media_url:p.images.standard_resolution.url,"VIDEO"===p.media_type&&(this.options.videos&&(u=p.media_url),l=p.thumbnail_url),0<=window.location.protocol.indexOf("http")&&!this.options.useHttp&&(l=l.replace(/https?:\/\//,"//"));var k=[!0];p.tags=this._extractTags(p.caption),""!==this.options.tagName&&this.options.tagName.forEach(function(t,e){t=t.toLowerCase(),-1===p.tags.indexOf(t)?k[e]=!1:k[e]=!0});var b=p.caption;p.caption&&""!==this.options.words&&(b=p.caption.split(/\s+/).slice(0,this.options.words).join(" "),b+="..."),-1!==k.indexOf(!0)&&(r+=this._makeTemplate({model:p,id:p.id,link:"old"===this.options.api?p.link:p.permalink,type:p.media_type,image:l,video:u,orientation:"square",caption:b,likes:"old"===this.options.api&&p.likes.count,comments:"old"===this.options.api&&p.comments.count,location:this._getObjectProperty(p,"location.name")}))}for(_.innerHTML=r,o=[],i=0,e=_.childNodes.length;i<e;)o.push(_.childNodes[i]),i+=1;for(this.options.afterLoad(),k=0,m=o.length;k<m;k++)f=o[k],s.appendChild(f);if("string"==typeof(v=this.options.target)&&(v=document.getElementById(v)),null==v)throw n='No element with id="'+this.options.target+'" on page.',new Error(n);v.appendChild(s),document.getElementsByTagName("head")[0].removeChild(document.getElementById("instafeed-fetcher")),h="instafeedCache"+this.unique,window[h]=void 0;try{delete window[h]}catch(t){}}return null!=this.options.after&&"function"==typeof this.options.after&&this.options.after.call(this),!0},o.prototype._buildUrl=function(){var t,e,i;switch(t="new"===this.options.api?"https://graph.instagram.com":"https://api.instagram.com/v1",this.options.get){case"popular":e="media/popular";break;case"user":e="new"===this.options.api?"me/media?fields=id,media_type,media_url,username,timestamp,permalink,caption,children,thumbnail_url&limit=200":"users/self/media/recent";break;default:throw new Error("Invalid option for get: '"+this.options.get+"'.")}return i=t+"/"+e,null!=this.options.accessToken&&(i+="new"===this.options.api?"&":"?",i+="access_token="+this.options.accessToken),i+="&callback=instafeedCache"+this.unique+".parse"},o.prototype._extractTags=function(t){var e=/#([^\s]+)/gi,i=/[~`!@#$%^&*\(\)\-\+={}\[\]:;"'<>\?,\./|\\\s]+/i,o=[];if("string"==typeof t)for(;null!==(match=e.exec(t));)!1===i.test(match[1])&&o.push(match[1].toLowerCase());return o},o.prototype._genKey=function(){var t;return""+(t=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)})()+t()+t()+t()},o.prototype._makeTemplate=function(t){var e,i,o,n,s;i=/(?:\{{2})([\w\[\]\.]+)(?:\}{2})/;var r=this.options.templateData;for(e='<div class="premium-insta-feed '+("VIDEO"===t.type?"premium-insta-video-wrap":"")+' premium-insta-box"><div class="premium-insta-feed-inner"><div class="premium-insta-feed-wrap"><div class="premium-insta-img-wrap"><img src="{{image}}"/>',"VIDEO"===t.type&&this.options.videos&&(e+="<video src={{video}} controls>"),e+='</div><div class="premium-insta-info-wrap"><div class="premium-insta-feed-interactions">'+r.likes+r.comments+r.description+"</div></div>"+r.link+"</div>"+r.share+"</div></div>";i.test(e);)n=e.match(i)[1],s=null!=(o=this._getObjectProperty(t,n))?o:"",e=e.replace(i,function(){return""+s});return e},o.prototype._getObjectProperty=function(t,e){var i,o;for(o=(e=e.replace(/\[(\w+)\]/g,".$1")).split(".");o.length;){if(i=o.shift(),!(null!=t&&i in t))return null;t=t[i]}return t},o.prototype._sortBy=function(t,n,s){var e;return e=function(t,e){var i,o;return i=this._getObjectProperty(t,n),o=this._getObjectProperty(e,n),s?o<i?1:-1:i<o?1:-1},t.sort(e.bind(this)),t},o.prototype._filter=function(t,e){var i,o,n,s;for(i=[],o=function(t){if(e(t))return i.push(t)},n=0,s=t.length;n<s;n++)o(t[n]);return i},t=o,e=this,i=function(){return t},"function"==typeof define&&define.amd?define([],i):"object"==typeof module&&module.exports?module.exports=i():e.Instafeed=i()}).call(this);