/* 
 * Widgets for Social Network photo stream.
 * 
 * Author: Pixel Industry
 * Website: http://pixel-industry.com
 * Version: 1.4
 *
 */


!function(e){e.fn.socialstream=function(a){var t={socialnetwork:"flickr",username:"pixel-industry",limit:6,overlay:!0,apikey:!1,accessToken:"",picasaAlbumId:""},a=e.extend(t,a);return this.each(function(){function t(t){e.get("https://www.googleapis.com/youtube/v3/playlistItems",{part:"snippet",maxResults:a.limit,playlistId:t,key:a.apikey},function(t){var r='<ul class="youtube-list">';e.each(t.items,function(t,i){var s=i.snippet.thumbnails["default"].url,n="";if(!s)return!1;var l=(e("<div></div>"),"https://www.youtube.com/watch?v="+i.snippet.resourceId.videoId),c=i.snippet.title;if(a.overlay)var n='<div class="img-overlay"></div>';r+='<li><a target="_blank" href="'+l+'" title="'+c+'"><img src="'+s+'"/>'+n+"</a></li>"}),r+="</ul>",e(i).append(r)})}var i=e(this);switch(a.socialnetwork){case"flickr":i.append('<ul class="flickr-list"></ul>'),e.getJSON("https://api.flickr.com/services/rest/?method=flickr.people.findByUsername&username="+a.username+"&format=json&api_key=32ff8e5ef78ef2f44e6a1be3dbcf0617&jsoncallback=?",function(t){var r=t.user.nsid;e.getJSON("https://api.flickr.com/services/rest/?method=flickr.photos.search&user_id="+r+"&format=json&api_key=85145f20ba1864d8ff559a3971a0a033&per_page="+a.limit+"&page=1&extras=url_sq&jsoncallback=?",function(t){e.each(t.photos.photo,function(t,r){var s=r.owner,n=r.title,l=r.url_sq,c=r.id,o="https://www.flickr.com/photos/"+s+"/"+c,p=e("<img/>").attr({src:l,alt:n}),d=e("<a/>").attr({href:o,target:"_blank",title:n}),u=e(d).append(p);if(a.overlay){var v=e("<div/>").addClass("img-overlay");e(d).append(v)}var m=e("<li/>").append(u);e("ul",i).append(m)})})});break;case"pinterest":var r="http://pinterest.com/"+a.username+"/feed.rss",s="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+encodeURIComponent(r)+"&num="+a.limit+"&output=json_xml";e.getJSON(s,function(t){if(200==t.responseStatus){var r=t.responseData.feed,s="";if(!r)return!1;for(var n='<ul class="pinterest-list">',l=0;l<r.entries.length;l++){var c=r.entries[l],o=e("<div></div>");o.append(c.content);var p="http://www.pinterest.com"+o.find("a").attr("href"),d=o.find("img").attr("src"),u=o.find("p:nth-child(2)").html();if(a.overlay)var s='<div class="img-overlay"></div>';n+='<li><a target="_blank" href="'+p+'" title="'+u+'"><img src="'+d+'"/>'+s+"</a></li>"}n+="</ul>",e(i).append(n)}});break;case"instagram":if(i.append('<ul class="instagram-list"></ul>'),"undefined"==typeof a.accessToken||""==a.accessToken)return void console.warn("Instagram Access Token is not set. Please enter it in plugin init call.");var n=a.accessToken;r="https://api.instagram.com/v1/users/search?q="+a.username+"&access_token="+n+"&count=1&callback=?",e.getJSON(r,function(t){e.each(t.data,function(t,s){var l=s.username;if(l==a.username){var c=s.id;""!=c&&(r="https://api.instagram.com/v1/users/"+c+"/media/recent/?access_token="+n+"&count="+a.limit+"&callback=?",e.getJSON(r,function(t){e.each(t.data,function(t,r){var s=r.images.thumbnail.url,n=r.link,l="";null!=r.caption&&(l=r.caption.text);var c=e("<img/>").attr({src:s,alt:l}),o=e("<a/>").attr({href:n,target:"_blank",title:l}),p=e(o).append(c);if(a.overlay){var d=e("<div/>").addClass("img-overlay");e(o).append(d)}var u=e("<li/>").append(p);e("ul",i).append(u)})}))}})});break;case"dribbble":if(i.append('<ul class="dribbble-list"></ul>'),"undefined"==typeof a.accessToken||""==a.accessToken)return void console.warn("Dribbble Access Token is not set. Please enter it in plugin init call.");var n=a.accessToken;e.getJSON("https://api.dribbble.com/v1/users/"+a.username+"/shots?access_token="+n+"&callback=?",function(t){e.each(t.data,function(t,r){if(t<a.limit){var s=r.title,n=e("<img/>").attr({src:r.images.teaser,alt:s}),l=e("<a/>").attr({href:r.html_url,target:"_blank",title:s}),c=e(l).append(n);if(a.overlay){var o=e("<div/>").addClass("img-overlay");e(l).append(o)}var p=e("<li/>").append(c);e("ul",i).append(p)}})});break;case"deviantart":var r="http://backend.deviantart.com/rss.xml?type=deviation&q=by%3A"+a.username+"+sort%3Atime+meta%3Aall",s="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+encodeURIComponent(r)+"&num="+a.limit+"&output=json_xml";e.getJSON(s,function(t){if(200==t.responseStatus){var r=t.responseData.feed,s="";if(!r)return!1;for(var n='<ul class="deviantart-list">',l=0;l<r.entries.length;l++){var c=r.entries[l],o=e("<div></div>");o.append(c.content);var p=c.link,d=o.find("img").attr("src");if(!(d.indexOf("smile.gif")>=0)){var u=c.title.replace(/.jpg/g,"").replace(/-/g," ").replace(/_/g," ");if(a.overlay)var s='<div class="img-overlay"></div>';n+='<li><a target="_blank" href="'+p+'" title="'+u+'"><img src="'+d+'"/>'+s+"</a></li>"}}n+="</ul>",e(i).append(n)}});break;case"picasa":var r="https://picasaweb.google.com/data/feed/base/user/"+a.username+"/album/"+a.picasaAlbumId+"?kind=photo&access=public&alt=json-in-script&imgmax="+a.limit+"&callback=?";e.getJSON(r,function(t){if(t.feed.entry.length>0){var r=t.feed.entry,s="",n='<ul class="picasa-list">';e.each(r,function(e,t){var i=t.media$group.media$thumbnail[2].url,r=(t.media$group.media$description.$t,t.media$group.media$title.$t),s=t.link[1].href,l=r.replace(/.jpg/g,"").replace(/.JPG/g,"").replace(/-/g," ").replace(/_/g," ");if(a.overlay)var c='<div class="img-overlay"></div>';n+='<li><a target="_blank" href="'+s+'" title="'+l+'"><img src="'+i+'"/>'+c+"</a></li>"});for(var l=0;r>l;l++){var c=r[l],o=e("<div></div>");o.append(c.content);var p=c.link,d=o.find("img").attr("src"),u=c.title.replace(/.jpg/g,"").replace(/-/g," ").replace(/_/g," ");if(a.overlay)var s='<div class="img-overlay"></div>';n+='<li><a target="_blank" href="'+p+'" title="'+u+'"><img src="'+d+'"/>'+s+"</a></li>"}n+="</ul>",e(i).append(n)}});break;case"youtube":var l;a.apikey&&e.get("https://www.googleapis.com/youtube/v3/channels",{part:"contentDetails",id:a.username,key:a.apikey},function(a){e.each(a.items,function(e,a){l=a.contentDetails.relatedPlaylists.uploads,t(l)})});break;case"newsfeed":var s="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+encodeURIComponent(a.username)+"&num="+a.limit+"&output=json_xml";e.getJSON(s,function(t){if(200==t.responseStatus){var r=t.responseData.feed,s="";if(!r)return!1;for(var n='<ul class="social-feed">',l=0;l<r.entries.length;l++){var c=r.entries[l],o=e("<div></div>");o.append(c.content);var p=c.link,d=o.find("img").attr("src"),u=c.title.replace(/.jpg/g,"").replace(/-/g," ").replace(/_/g," ");if(a.overlay)var s='<div class="img-overlay"></div>';n+='<li><a target="_blank" href="'+p+'" title="'+u+'"><img src="'+d+'"/>'+s+"</a></li>"}n+="</ul>",e(i).append(n)}})}})}}(jQuery);