/**
 * Picasa Webalbum Integration jQuery plugin
 * This library was inspired aon pwa by Dieter Raber
 * @name jquery.pwi.js
 * @author Jeroen Diderik - http://www.jdee.nl/
 * @revision 1.2.1
 * @date March 17, 2010
 * @copyright (c) 2009 Jeroen Diderik(www.jdee.nl)
 * @license Creative Commons Attribution-Share Alike 3.0 Netherlands License - http://creativecommons.org/licenses/by-sa/3.0/nl/
 * @Visit http://pwi.googlecode.com/ for more informations, duscussions etc about this library
 */
(function(c){var b,a={};c.fn.pwi=function(d){var n,g={};d=c.extend({},c.fn.pwi.defaults,d);b=this;function k(){g=d;ts=new Date().getTime();g.id=ts;n=c("<div id='pwi_"+ts+"'/>").appendTo(b);n.addClass("pwi_container");h();return false}function h(){if(g.username===""){alert("Make sure you specify at least your username.\nSee http://pwi.googlecode.com for more info");return}switch(g.mode){case"latest":r();break;case"album":m();break;case"keyword":m();break;default:s();break}}function t(v){var w=new Date(Number(v)),u=w.getUTCFullYear();if(u<1000){u+=1900}return(g.months[(w.getUTCMonth())]+" "+w.getUTCDate()+", "+u)}function q(u){var v=new Date(Number(u));$year=v.getUTCFullYear();if($year<1000){$year+=1900}if(v=="Invalid Date"){return u}else{return(v.getUTCDate()+"-"+(v.getUTCMonth()+1)+"-"+$year+" "+v.getUTCHours()+":"+(v.getUTCMinutes()<10?"0"+v.getUTCMinutes():v.getUTCMinutes()))}}function e(w){var u,x="",z="",y=w.content.src,v=w.gphoto$id.$t,z=(w.summary?w.summary.$t:"");if(g.showPhotoDate){if(w.exif$tags.exif$time){x=q(w.exif$tags.exif$time.$t)}else{if(w.gphoto$timestamp){x=q(w.gphoto$timestamp.$t)}else{x=q(w.published.$t)}}x+=" "}x+=z.replace(new RegExp("'","g"),"&#39;");u=c("<div class='pwi_photo' style='height:"+(g.thumbSize+1)+"px;cursor: pointer;'/>");u.append("<a href='"+y+"?imgmax="+g.photoSize+"' rel='lb-"+g.username+"' title='"+x+"'><img src='"+y+"?imgmax="+g.thumbSize+"&crop="+g.thumbCrop+"'/></a>");if(g.showPhotoCaption){if(g.showPhotoCaptionDate&&g.showPhotoDate){z=x}if(g.photoCaptionLength>0){z=z.substring(0,g.photoCaptionLength)}u.find("a").append("<br/>"+z)}return u}function j(w){var v=c("<div/>"),x=0;while(x<g.albumMaxResults&&x<w.feed.entry.length){var u=w.feed.entry[x].gphoto$name.$t,y=t(w.feed.entry[x].gphoto$timestamp.$t),z=w.feed.entry[x].media$group.media$thumbnail[0].url.replace(new RegExp("/s160-c/","g"),"/");if(c.inArray(u,g.albums)>-1||g.albums.length===0){$scAlbum=c("<div class='pwi_album'/>");$scAlbum.bind("click.pwi",u,function(A){A.stopPropagation();g.page=1;g.album=A.data;if(typeof(g.onclickAlbumThumb)==="function"){g.onclickAlbumThumb(A,g);return false}else{m();return false}});$scAlbum.append("<img src='"+z+"?imgmax="+g.albumThumbSize+"&crop="+g.albumCrop+"'/>");g.showAlbumTitles?$scAlbum.append("<br/>"+w.feed.entry[x].title.$t+"<br/>"+(g.showAlbumdate?y:"")+(g.showAlbumPhotoCount?"&nbsp;&nbsp;&nbsp;&nbsp;"+w.feed.entry[x].gphoto$numphotos.$t+" "+g.labels.photos:"")):false;v.append($scAlbum)}x++}v.append("<div style='clear: both;height:0px;'/>");g.albumstore=w;l(false,v)}function f(B){var K,G,H=B.feed.openSearch$totalResults.$t,v="",x="",y=B.feed.gphoto$location===undefined?"":B.feed.gphoto$location.$t,I=B.feed.subtitle===undefined?"":B.feed.subtitle.$t,F=t(B.feed.gphoto$timestamp===undefined?"":B.feed.gphoto$timestamp.$t),A=(H=="1")?false:true;v=(B.feed.title==="undefined"||g.albumTitle.length>0)?g.albumTitle:B.feed.title.$t;K=c("<div/>");if(g.mode!="album"){var D=c("<div class='pwi_album_backlink'>"+g.labels.albums+"</div>").bind("click.pwi",function(L){L.stopPropagation();s();return false});K.append(D)}if(g.showAlbumDescription){G=c("<div class='pwi_album_description'/>");G.append("<div class='title'>"+v+"</div>");G.append("<div class='details'>"+H+" "+(A?g.labels.photos:g.labels.photo)+(g.showAlbumdate?", "+F:"")+(g.showAlbumLocation&&y?", "+y:"")+"</div>");G.append("<div class='description'>"+I+"</div>");if(g.showSlideshowLink){if(g.mode==="keyword"){}else{G.append("<div><a href='http://picasaweb.google.com/"+g.username+"/"+B.feed.gphoto$name.$t+""+((g.authKey!="")?"?authkey="+g.authKey:"")+"#slideshow/"+B.feed.entry[0].gphoto$id.$t+"' rel='gb_page_fs[]' target='_new' class='sslink'>"+g.labels.slideshow+"</a></div>")}}K.append(G)}if(H>g.maxResults){$pageCount=(H/g.maxResults);var J=c("<div class='pwi_prevpage'/>").text(g.labels.prev),u=c("<div class='pwi_nextpage'/>").text(g.labels.next);x=c("<div class='pwi_pager' style='clear:left;'/>");if(g.page>1){J.addClass("link").bind("click.pwi",function(L){L.stopPropagation();g.page=(parseInt(g.page)-1);m();return false})}x.append(J);for(var w=1;w<$pageCount+1;w++){if(w==g.page){D="<div class='pwi_pager_current'>"+w+"</div> "}else{D=c("<div class='pwi_pager_page'>"+w+"</div>").bind("click.pwi",w,function(L){L.stopPropagation();g.page=L.data;m();return false})}x.append(D)}if(g.page<$pageCount){u.addClass("link").bind("click.pwi",function(L){L.stopPropagation();g.page=(parseInt(g.page)+1);m();return false})}x.append(u);x.append("<div style='clear: both;height:0px;'/>")}if(x.length>0&&(g.showPager==="both"||g.showPager==="top")){K.append(x)}var C=((g.page-1)*g.maxResults);while(C<(g.maxResults*g.page)&&C<H){var z=e(B.feed.entry[C]);K.append(z);C++}if(x.length>0&&(g.showPager==="both"||g.showPager==="bottom")){K.append(x.clone(true))}g.photostore[g.album]=B;var E=c(".pwi_photo",K).css(g.thumbCss);if(typeof(g.popupExt)==="function"){g.popupExt(E.find("a[rel='lb-"+g.username+"']"))}else{if(typeof(g.onclickThumb)==="function"){E.find("a[rel='lb-"+g.username+"']").bind("click.pwi",o)}else{if(typeof(g.onclickThumb)!="function"&&c.slimbox){E.find("a[rel='lb-"+g.username+"']").slimbox(g.slimbox_config)}}}l(false,K)}function i(x){var v=c("<div/>"),w=x.feed?x.feed.entry.length:0,z=0;while(z<g.maxResults&&z<w){var y=e(x.feed.entry[z]);v.append(y);z++}v.append("<div style='clear: both;height:0px;'> </div>");var u=c("div.pwi_photo",v).css(g.thumbCss);if(typeof(g.popupExt)==="function"){g.popupExt(u.find("a[rel='lb-"+g.username+"']"))}else{if(typeof(g.onclickThumb)==="function"){u.find("a[rel='lb-"+g.username+"']").bind("click",o)}else{if(typeof(g.onclickThumb)!="function"&&c.slimbox){u.find("a[rel='lb-"+g.username+"']").slimbox(g.slimbox_config)}}}l(false,v)}function p(u){g.onclickAlbumThumb(u);return false}function o(){g.onclickThumb.call(this);return false}function s(){if(g.albumstore.feed){j(g.albumstore)}else{l(true,"");var u="http://picasaweb.google.com/data/feed/api/user/"+g.username+"?kind=album&access="+g.albumTypes+"&alt=json";gdata={io:{handleScriptLoaded:function(v){j(v)}}};c.getJSON(u,"callback=?",j)}return n}function m(){if(g.photostore[g.album]){f(g.photostore[g.album])}else{var u=((g.page-1)*g.maxResults)+1;if(g.mode==="keyword"){var v="http://picasaweb.google.com/data/feed/api/user/"+g.username+"?alt=json&kind=photo&tag="+g.keyword+"&max-results="+g.maxResults+"&start-index="+u+((g.authKey!="")?"&authkey="+g.authKey:"")}else{var v="http://picasaweb.google.com/data/feed/api/user/"+g.username+"/album/"+g.album+"?kind=photo&alt=json"+((g.authKey!="")?"&authkey="+g.authKey:"")}l(true,"");gdata={io:{handleScriptLoaded:function(w){f(w)}}};c.getJSON(v,"callback=?",f)}return n}function r(){l(true,"");var u="http://picasaweb.google.com/data/feed/api/user/"+g.username+(g.album!=""?"/album/"+g.album:"")+"?kind=photo&max-results="+g.maxResults+"&alt=json&q="+((g.authKey!="")?"&authkey="+g.authKey:"");gdata={io:{handleScriptLoaded:function(v){i(v)}}};c.getJSON(u,"callback=?",i);return n}function l(v,u){if(v){document.body.style.cursor="wait"}else{document.body.style.cursor="default";n.html(u)}}k()};c.fn.pwi.defaults={mode:"albums",username:"",album:"",authKey:"",albums:[],albumCrop:1,albumTitle:"",albumThumbSize:160,albumMaxResults:999,albumsPerPage:10,albumStartIndex:1,albumTypes:"public",page:1,photoSize:800,maxResults:50,showPager:"bottom",thumbSize:72,thumbCrop:0,thumbCss:{margin:"5px"},onclickThumb:"",onclickAlbumThumb:"",popupExt:"",showAlbumTitles:true,showAlbumdate:true,showAlbumPhotoCount:true,showAlbumDescription:true,showAlbumLocation:true,showSlideshowLink:true,showPhotoCaption:false,showPhotoCaptionDate:false,photoCaptionLength:0,showPhotoDownload:false,showPhotoDate:true,labels:{photo:"photo",photos:"photos",albums:"Back to albums",slideshow:"Display slideshow",loading:"PWI fetching data...",page:"Page",prev:"Previous",next:"Next",devider:"|"},months:["January","February","March","April","May","June","July","August","September","October","November","December"],slimbox_config:{loop:false,overlayOpacity:0.6,overlayFadeDuration:400,resizeDuration:400,resizeEasing:"swing",initialWidth:250,initlaHeight:250,imageFadeDuration:400,captionAnimationDuration:400,counterText:"{x}/{y}",closeKeys:[27,88,67,70],prevKeys:[37,80],nextKeys:[39,83]},blockUIConfig:{message:"<div class='lbLoading pwi_loader'>loading...</div>",css:"pwi_loader"},albumstore:{},photostore:{},token:""}})(jQuery);
