(function(){var n;n=function(){function n(n,t){var i,r;if(this.options={target:"instafeed",get:"popular",resolution:"thumbnail",sortBy:"none",links:!0,mock:!1,useHttp:!1},typeof n=="object")for(i in n)r=n[i],this.options[i]=r;this.context=t!=null?t:this;this.unique=this._genKey()}return n.prototype.hasNext=function(){return typeof this.context.nextUrl=="string"&&this.context.nextUrl.length>0},n.prototype.next=function(){return this.hasNext()?this.run(this.context.nextUrl):!1},n.prototype.run=function(t){var u,r,i;if(typeof this.options.clientId!="string"&&typeof this.options.accessToken!="string")throw new Error("Missing clientId or accessToken.");if(typeof this.options.accessToken!="string"&&typeof this.options.clientId!="string")throw new Error("Missing clientId or accessToken.");return this.options.before!=null&&typeof this.options.before=="function"&&this.options.before.call(this),typeof document!="undefined"&&document!==null&&(i=document.createElement("script"),i.id="instafeed-fetcher",i.src=t||this._buildUrl(),u=document.getElementsByTagName("head"),u[0].appendChild(i),r="instafeedCache"+this.unique,window[r]=new n(this.options,this),window[r].unique=this.unique),!0},n.prototype.parse=function(n){var s,ft,h,c,lt,f,o,et,nt,l,a,t,r,tt,u,i,v,y,p,at,w,it,b,k,ot,st,ht,ct,rt,d,ut,e,g;if(typeof n!="object"){if(this.options.error!=null&&typeof this.options.error=="function")return this.options.error.call(this,"Invalid JSON data"),!1;throw new Error("Invalid JSON response");}if(n.meta.code!==200){if(this.options.error!=null&&typeof this.options.error=="function")return this.options.error.call(this,n.meta.error_message),!1;throw new Error("Error from Instagram: "+n.meta.error_message);}if(n.data.length===0){if(this.options.error!=null&&typeof this.options.error=="function")return this.options.error.call(this,"No images were returned from Instagram"),!1;throw new Error("No images were returned from Instagram");}if(this.options.success!=null&&typeof this.options.success=="function"&&this.options.success.call(this,n),this.context.nextUrl="",n.pagination!=null&&(this.context.nextUrl=n.pagination.next_url),this.options.sortBy!=="none"){ut=this.options.sortBy==="random"?["","random"]:this.options.sortBy.split("-");d=ut[0]==="least"?!0:!1;switch(ut[1]){case"random":n.data.sort(function(){return.5-Math.random()});break;case"recent":n.data=this._sortBy(n.data,"created_time",d);break;case"liked":n.data=this._sortBy(n.data,"likes.count",d);break;case"commented":n.data=this._sortBy(n.data,"comments.count",d);break;default:throw new Error("Invalid option for sortBy: '"+this.options.sortBy+"'.");}}if(typeof document!="undefined"&&document!==null&&this.options.mock===!1){if(i=n.data,rt=parseInt(this.options.limit,10),this.options.limit!=null&&i.length>rt&&(i=i.slice(0,rt)),o=document.createDocumentFragment(),this.options.filter!=null&&typeof this.options.filter=="function"&&(i=this._filter(i,this.options.filter)),this.options.template!=null&&typeof this.options.template=="string"){for(nt="",tt="",at="",g=document.createElement("div"),a=0,ot=i.length;a<ot;a++){if(t=i[a],r=t.images[this.options.resolution],typeof r!="object")throw f="No image found for resolution: "+this.options.resolution+".",new Error(f);w=r.width;y=r.height;p="square";w>y&&(p="landscape");w<y&&(p="portrait");u=r.url;l=window.location.protocol.indexOf("http")>=0;l&&!this.options.useHttp&&(u=u.replace(/https?:\/\//,"//"));tt=this._makeTemplate(this.options.template,{model:t,id:t.id,link:t.link,type:t.type,image:u,width:w,height:y,orientation:p,caption:this._getObjectProperty(t,"caption.text"),likes:t.likes.count,comments:t.comments.count,location:this._getObjectProperty(t,"location.name")});nt+=tt}for(g.innerHTML=nt,c=[],h=0,ft=g.childNodes.length;h<ft;)c.push(g.childNodes[h]),h+=1;for(b=0,st=c.length;b<st;b++)ct=c[b],o.appendChild(ct)}else for(k=0,ht=i.length;k<ht;k++){if(t=i[k],v=document.createElement("img"),r=t.images[this.options.resolution],typeof r!="object")throw f="No image found for resolution: "+this.options.resolution+".",new Error(f);u=r.url;l=window.location.protocol.indexOf("http")>=0;l&&!this.options.useHttp&&(u=u.replace(/https?:\/\//,"//"));v.src=u;this.options.links===!0?(s=document.createElement("a"),s.href=t.link,s.appendChild(v),o.appendChild(s)):o.appendChild(v)}if(e=this.options.target,typeof e=="string"&&(e=document.getElementById(e)),e==null)throw f='No element with id="'+this.options.target+'" on page.',new Error(f);e.appendChild(o);et=document.getElementsByTagName("head")[0];et.removeChild(document.getElementById("instafeed-fetcher"));it="instafeedCache"+this.unique;window[it]=void 0;try{delete window[it]}catch(vt){lt=vt}}return this.options.after!=null&&typeof this.options.after=="function"&&this.options.after.call(this),!0},n.prototype._buildUrl=function(){var i,n,t;i="https://api.instagram.com/v1";switch(this.options.get){case"popular":n="media/popular";break;case"tagged":if(!this.options.tagName)throw new Error("No tag name specified. Use the 'tagName' option.");n="tags/"+this.options.tagName+"/media/recent";break;case"location":if(!this.options.locationId)throw new Error("No location specified. Use the 'locationId' option.");n="locations/"+this.options.locationId+"/media/recent";break;case"user":if(!this.options.userId)throw new Error("No user specified. Use the 'userId' option.");n="users/"+this.options.userId+"/media/recent";break;default:throw new Error("Invalid option for get: '"+this.options.get+"'.");}return t=i+"/"+n,t+=this.options.accessToken!=null?"?access_token="+this.options.accessToken:"?client_id="+this.options.clientId,this.options.limit!=null&&(t+="&count="+this.options.limit),t+="&callback=instafeedCache"+this.unique+".parse",t},n.prototype._genKey=function(){var n;return n=function(){return((1+Math.random())*65536|0).toString(16).substring(1)},""+n()+n()+n()+n()},n.prototype._makeTemplate=function(n,t){for(var u,f,e,r=/(?:\{{2})([\w\[\]\.]+)(?:\}{2})/,i=n;r.test(i);)f=i.match(r)[1],e=(u=this._getObjectProperty(t,f))!=null?u:"",i=i.replace(r,function(){return""+e});return i},n.prototype._getObjectProperty=function(n,t){var i,r;for(t=t.replace(/\[(\w+)\]/g,".$1"),r=t.split(".");r.length;){if(i=r.shift(),!(n!=null&&i in n))return null;n=n[i]}return n},n.prototype._sortBy=function(n,t,i){var r;return r=function(n,r){var u,f;return u=this._getObjectProperty(n,t),f=this._getObjectProperty(r,t),i?u>f?1:-1:u<f?1:-1},n.sort(r.bind(this)),n},n.prototype._filter=function(n,t){var r,u,i,f,e;for(r=[],u=function(n){if(t(n))return r.push(n)},i=0,e=n.length;i<e;i++)f=n[i],u(f);return r},n}(),function(n,t){return typeof define=="function"&&define.amd?define([],t):typeof module=="object"&&module.exports?module.exports=t():n.Instafeed=t()}(this,function(){return n})}).call(this)