if(typeof CalameoWidgets=="undefined")var CalameoWidgets=Class.create({}); CalameoWidgets.Library=Class.create({initialize:function(a,b){this.cache=[];this.feedUrl="http://data.calameo.com/library/";this.frameUrl="http://widget.calameo.com/library/";this.types=new Array("featured","account","subscription","favorites","group","category");this.sortBy={latestPublished:{o:0,w:"desc"},latestIssue:{o:5,w:"desc"},mostViews:{o:7,w:"desc"},mostPages:{o:2,w:"desc"},title:{o:1,w:"asc"}};this.callback="_jsonp";this.maxShelves=5;this.page=0;this.Config=new Hash;this.Config.set("rows", 1);this.Config.set("type","featured");this.Config.set("sortBy","latestPublished");this.Config.set("id","");this.parseConfig(b);if(typeof this.Config.get("feedUrl")!="undefined")this.feedUrl=this.Config.get("feedUrl");if(typeof this.Config.get("frameUrl")!="undefined")this.frameUrl=this.Config.get("frameUrl");this.parseQueryStringConfig();this.sanitizeConfig();this.Config.set("rows",Math.min(this.maxShelves,Math.max(1,this.Config.get("rows"))));if(typeof this.Config.get("thumbSize")!="undefined"&& this.Config.get("thumbSize")=="small"){this.Config.set("truncateLength",21);this.Config.set("thumbWidth",70);this.Config.set("cellWidth",110);this.Config.set("cellHeight",150)}else{this.Config.set("truncateLength",26);this.Config.set("thumbWidth",100);this.Config.set("cellWidth",130);this.Config.set("cellHeight",185)}if(typeof this.Config.get("linkTarget")=="undefined"||this.Config.get("linkTarget")=="")this.Config.set("linkTarget","_parent");if(this.Container=$(a)){this.build();if(this.types.indexOf(this.Config.get("type"))== -1)return this.triggerError("Unknown library type");typeof this.sortBy[this.Config.get("sortBy")]=="undefined"&&this.Config.set("sortBy","latestPublished");this.Config.set("order",this.sortBy[this.Config.get("sortBy")]);this.loadData()}},parseConfig:function(a){typeof a!="undefined"&&typeof a=="object"&&$H(a).each(function(b){this.Config.set(b.key,b.value)},this)},parseQueryStringConfig:function(){var a=new String(window.location);$H(a.toQueryParams()).each(function(b){this.Config.set(b.key,b.value)}, this)},sanitizeConfig:function(){this.Config.each(function(a){if(a.value=="true")this.Config.set(a.key,true);else a.value=="false"&&this.Config.set(a.key,false)},this)},triggerError:function(a){if(typeof this.Error!="undefined"){typeof this.Loading!="undefined"&&this.hideLoading();if(typeof a=="undefined"||a==null||a=="")a="Unable to load library";this.Error.down("span").update(a);this.Error.setStyle({left:Math.round((this.Element.getWidth()-this.Error.getWidth())/2)+"px",top:Math.round((this.Element.getHeight()- this.Error.getHeight())/2)+"px"});this.Error.show()}},loadData:function(){this.Error.hide();this.Config.set("step",Math.floor((this.Element.getWidth()-this.Next.getWidth()-this.Previous.getWidth())/this.Config.get("cellWidth"))*this.Config.get("rows"));var a=this.getCachedData();if(a)return this.parseData(a);this.showLoading();this.hideNavigation();$$("script").each(function(b){Element.readAttribute(b,"src")!=null&&Element.readAttribute(b,"src").startsWith(this.feedUrl)&&Element.remove(b)},this); a=new Hash;a.set("type",this.Config.get("type"));a.set("id",this.Config.get("id"));a.set("order",this.Config.get("order").o);a.set("way",this.Config.get("order").w);a.set("page",this.page);a.set("step",this.Config.get("step"));this.loadJSONP(this.feedUrl,a)},loadJSONP:function(a,b){if(typeof b=="undefined")b=new Hash;b.set("callback",this.callback);var c=document.createElement("script");c.src=a+"?"+b.toQueryString();c.type="text/javascript";$$("head")[0].insert(c)},parseData:function(a){var b=null; this.hideLoading();if(typeof a.status!="undefined")if(a.status=="ok"){this.cacheData(a);this.Data=a.content;if(this.Data.poweredBy==undefined)this.Element.down(".powered-by").hide();else{this.Element.down(".powered-by span").update(this.Data.poweredBy);this.Element.down(".powered-by").show().href=this.Data.poweredByLink}this.renderBooks();return}else if(a.status=="error")b=a.msg;this.triggerError(b)},build:function(){if(typeof document.Libraries=="undefined")document.Libraries=[];this.id=document.Libraries.size(); document.Libraries.push(this);window[this.callback]=this.parseData.bind(this);this.Container.addClassName("library");typeof this.Config.get("theme")!="undefined"&&this.Config.get("theme")!=null&&this.Config.get("theme")!=""?this.Container.addClassName(this.Config.get("theme")):this.Container.addClassName("white");typeof this.Config.get("showShadow")!="undefined"&&this.Config.get("showShadow")==true&&this.Container.addClassName("show-shadow");typeof this.Config.get("showGloss")!="undefined"&&this.Config.get("showGloss")== true&&this.Container.addClassName("show-gloss");typeof this.Config.get("showTitle")!="undefined"&&this.Config.get("showTitle")==true&&this.Container.addClassName("show-title");if(typeof this.Config.get("showInfo")!="undefined")switch(this.Config.get("showInfo")){case "pages":this.Container.addClassName("show-pages");break;case "views":this.Container.addClassName("show-views");break;case "account":this.Container.addClassName("show-account");break;case "date":this.Container.addClassName("show-date"); break}this.Element=Builder.node("div",{className:"library-inner"});typeof this.Config.get("bgColor")!="undefined"&&this.Config.get("bgColor")!=""&&this.checkHexColor(this.Config.get("bgColor"))&&this.Element.setStyle({background:"#"+this.Config.get("bgColor")});typeof this.Config.get("thumbSize")!="undefined"&&this.Config.get("thumbSize")=="small"?this.Element.addClassName("small-thumb"):this.Element.addClassName("normal-thumb");this.Container.insert(this.Element);this.Shelves=Builder.node("div", {className:"shelves"});this.Shelves.setStyle({height:this.Config.get("cellHeight")*this.Config.get("rows")+"px"});this.ShelvesContent=Builder.node("div",{className:"shelves-inner"});this.ShelvesContent.hide();this.Shelves.insert(this.ShelvesContent);this.Element.insert(this.Shelves);this.Next=Builder.node("a",{href:"javascript:document['Libraries']["+this.id+"].goNext();",className:"navigation next"},Builder.node("span"));this.Next.hide();this.Element.insert(this.Next);this.Previous=Builder.node("a", {href:"javascript:document['Libraries']["+this.id+"].goPrevious();",className:"navigation previous"},Builder.node("span"));this.Previous.hide();this.Element.insert(this.Previous);this.Loading=Builder.node("div",{className:"loading"},Builder.node("span"));this.Element.insert(this.Loading);this.Error=Builder.node("div",{className:"error"},Builder.node("span"));this.Error.hide();this.Element.insert(this.Error);this.PoweredBy=Builder.node("a",{href:"javascript:void(0);",className:"powered-by"},Builder.node("span")); this.PoweredBy.hide();this.Element.insert(this.PoweredBy)},renderBooks:function(){Element.update(this.ShelvesContent,"");if(this.Data.books.size()==0)return this.triggerError("Library is empty");this.Data.books.each(function(a){if(typeof a.date=="undefined")a.date="";var b=this.Config.get("thumbWidth"),c=a.height/a.width*b,d=typeof this.Config.get("linkTo")!="undefined"&&this.Config.get("linkTo")=="view"?a.viewUrl:a.publicUrl;d=typeof this.Config.get("handler")!="undefined"?"javascript:"+this.Config.get("handler")+ "('"+a.id+"');":d;a=Builder.node("div",{className:"book"},Builder.node("div",{className:"book-inner"},Builder.node("div",{className:"book-content"},[Builder.node("a",{href:d,target:this.Config.get("linkTarget"),className:"title"},a.name.truncate(this.Config.get("truncateLength"))),Builder.node("div",{className:"info"},[Builder.node("a",{href:a.accountUrl,target:this.Config.get("linkTarget"),className:"account"},a.accountName.truncate(16)),Builder.node("div",{className:"pages"},this.formatNumber(Math.round(a.numPages), 0,",","",""," pages","","")),Builder.node("div",{className:"views"},this.formatNumber(Math.round(a.numViews),0,",","",""," views","","")),Builder.node("div",{className:"date"},a.date)]),Builder.node("div",{className:"thumb"},[Builder.node("div",{className:"drop-shadow"}),Builder.node("img",{src:a.thumbUrl,alt:"",width:b+"px",height:c+"px"}),Builder.node("a",{href:d,target:this.Config.get("linkTarget"),className:"gloss-effect"})])])));a.setStyle({width:this.Config.get("cellWidth")+"px"});this.ShelvesContent.insert(a)}, this);this.Shelves.show();this.updateNavigation();if(!Prototype.Browser.IE)if(typeof this.Config.get("transition")=="undefined"||this.Config.get("transition")!=false){this.ShelvesContent.hide();new Effect.Appear(this.ShelvesContent,{duration:0.5});return}this.ShelvesContent.show()},updateNavigation:function(){(this.page+1)*this.Config.get("step")>=this.Data.total?this.Next.hide():this.Next.show();this.page==0?this.Previous.hide():this.Previous.show()},hideNavigation:function(){this.Next.hide();this.Previous.hide()}, goNext:function(){this.page++;this.loadData()},goPrevious:function(){this.page--;this.loadData()},showLoading:function(){this.Loading.setStyle({left:Math.round((this.Element.getWidth()-this.Loading.getWidth())/2)+"px",top:Math.round((this.Element.getHeight()-this.Loading.getHeight())/2)+"px"});this.Loading.show()},hideLoading:function(){this.Loading.hide()},cacheData:function(a){this.cache[this.page+"-"+this.Config.get("step")]=a},getCachedData:function(){if(typeof this.cache[this.page+"-"+this.Config.get("step")]!= "undefined")return this.cache[this.page+"-"+this.Config.get("step")];return false},formatNumber:function(a,b,c,d,h,i,e,f){a=Math.round(a*Math.pow(10,b));if(a>=0)e=f="";a=(""+Math.abs(a)).split("");b=a.length-b;b<0&&b--;for(var g=b;g<0;g++)a.unshift("0");a.splice(b,0,d);for(a[0]==d&&a.unshift("0");b>3;){b-=3;a.splice(b,0,c)}return h+e+a.join("")+f+i},checkHexColor:function(a){return/^([0-9a-f]{1,2}){3}$/i.test(a)},getUrl:function(){var a=new Array("type","id","rows","sortBy","theme","bgColor","thumbSize", "showTitle","showShadow","showGloss","showInfo","linkTo"),b=new Hash;a.each(function(c){typeof this.Config.get(c)!="undefined"&&b.set(c,this.Config.get(c))},this);return this.frameUrl+"?"+b.toQueryString()},getHtml:function(){return'<iframe src="'+this.getUrl()+'" width="100%" height="'+this.Config.get("rows")*this.Config.get("cellHeight")+'" frameborder="0"></iframe>'}});
