/*! Gallery Viewer - v0.1.0 - 2012-10-22 * https://github.com/bgcom/gallery-viewer * Copyright (c) 2012 BGcom; Licensed MIT */ (function(a,b,c,d,e,f){var g="",h={};a.widget("bgcom.galleryViewer",{version:"1.0",data:{},_handlers:{},nbElem:0,_indexes:[],_curIdx:NaN,options:{template:g,someValue:null,zoomFactor:1.2,selectors:{thumbList:".galleryViewer-ThumbnailList",thumbElement:".galleryViewer-ThumbnailContainer",thumbLink:".galleryViewer-ThumbnailLink",button:{next:".galleryViewer-next",previous:".galleryViewer-previous",zoomIn:".galleryViewer-zoomIn",zoomOut:".galleryViewer-zoomOut",close:".galleryViewer-close"},main:".galleryViewer-main",mainContainer:".galleryViewer-main-container",photoContainer:".galleryViewer-photo-container",sideContainer:".galleryViewer-side-container"}},changeImage:function(b){var d=0;switch(b.data.type){case"previous":d=h._curIdx===0?h._indexes.length-1:h._curIdx-1;break;case"next":d=h._curIdx===h._indexes.length-1?0:h._curIdx+1;break;case"none":d=h.data[a(b.currentTarget).attr("key")].index-1}var e=h._indexes[d];c.pushState({state:e,rand:Math.random()},h._getTitle(e),e),h._trigger("imageChanged",b)},close:function(b){h._goRootUrl(),h.element.html(""),a("html").css("overflow","auto"),a("body").off("keydown.galleryViewer",h._handler.keyboard),h._trigger("viewerClosed",b)},zoom:function(b){var c=a("img",h.options.selectors.photoContainer),d=c.width(),e=c.height(),f=0;b.data.type==="in"?f=h.options.zoomFactor:b.data.type==="out"&&(f=1/h.options.zoomFactor),c.width(d*f),c.height(e*f)},destroy:function(){h.close(),h._unbindAll(),a.Widget.prototype.destroy.call(this)},suspend:function(){h._unbindAll()},restart:function(){h.data={},h.nbElem=0,h._indexes=[],h._curIdx=NaN,a(h.options.selectors.thumbLink,h.options.selectors.thumbList).each(h._appendElement),a(h.options.selectors.thumbLink,h.options.selectors.thumbList).on("click.galleryViewer",{type:"none"},h._handler.openViewer)},_create:function(){h=this,h._initWidget(),h._bindAll()},_bindAll:function(){c.Adapter.bind(d,"statechange",function(){h._changeImageFromUrl()}),a(h.options.selectors.thumbLink,h.options.selectors.thumbList).on("click.galleryViewer",{type:"none"},h._handler.openViewer)},_unbindAll:function(){a(h.options.selectors.thumbLink,h.options.selectors.thumbList).off("click.galleryViewer")},_initWidget:function(){h.template=b.compile(h.options.template),a(h.options.selectors.thumbLink,h.options.selectors.thumbList).each(h._appendElement),h._changeImageFromUrl()},_appendElement:function(c,d){var e=a(d).attr("key"),f=a.parseJSON(a(d).attr("data"));h._indexes[c]=e,h.data[e]=f,h.data[e].description=new b.SafeString(f.description),h.data[e].index=c+1,h.data[e].imageObj=new Image,h.data[e].imageObj.src=h.data[e].src},_changeImageFromUrl:function(){var a=d.location.pathname.split("/"),b=a[a.length-1];typeof h.data[b]!="undefined"?(h._displayImage(b),h.data[b].imageObj.onload=function(a){h._sizeImage()}):typeof h.data[b]=="undefined"&&b!==""&&h._goRootUrl()},_goRootUrl:function(){var a=location.href.split("/");a.pop();var b=a.join("/");c.pushState({state:b,rand:Math.random()},h._getTitle(),b+"/")},_displayImage:function(b){h._curIdx=h.data[b].index-1,a("body").off("keydown.galleryViewer",h._handler.keyboard),a("body").on("keydown.galleryViewer",h._handler.keyboard),h._render(b),a(h.options.selectors.button.next,h.element).on("click.galleryViewer",{type:"next"},h._handler.changeImage),a(h.options.selectors.button.previous,h.element).on("click.galleryViewer",{type:"previous"},h._handler.changeImage),a(h.options.selectors.button.close,h.element).one("click.galleryViewer",h._handler.close),a(h.options.selectors.button.zoomIn,h.element).on("click.galleryViewer",{type:"in"},h._handler.zoom),a(h.options.selectors.button.zoomOut,h.element).on("click.galleryViewer",{type:"out"},h._handler.zoom),a(h.options.selectors.mainContainer).on("click.galleryViewer",function(a){a.stopPropagation()}),a(h.options.selectors.main).one("click.galleryViewer",h._handler.close),a(d).on("resize.galleryViewer",h._handler.resize),h.element.focus()},_render:function(b){a("html").css("overflow-x","hidden"),a("html").css("overflow-y","hidden");var c=h.data[b];h.element.html(h.template({elem:c,totalElements:h._indexes.length})),a("img",h.element).draggable({}),h._trigger("imageDisplayed"),h._resize()},_resize:function(){var b=h.options.selectors,c=a(b.main);c.height(a(d).height()),c.width(a(d).width()),a(b.photoContainer).width(c.width()-a(b.sideContainer).outerWidth(!0)),d.scrollTo(0,0),h._sizeImage()},_sizeImage:function(){var b=a(h.options.selectors.photoContainer,h.element),c=a("img",b),d=h.data[h._indexes[h._curIdx]].imageObj,e={height:b.height()/d.height,width:b.width()/d.width},f;e.width<1&&e.height<1?f=Math.min(e.width,e.height):e.width>1&&e.height>1?f=Math.max(e.width,e.height):e.width<1?f=e.width:e.height<1&&(f=e.height),c.width(d.width*f),c.height(d.height*f)},_getTitle:function(a){var b=e.title.split("|");return typeof a=="undefined"&&b.length>3?b.splice(0,1):typeof h.data[a]!="undefined"?(b[0]=" "+b[0],b.length>3?b.splice(0,1,h.data[a].title+" "):b=[h.data[a].title+" "].concat(b)):typeof a!="undefined"&&typeof h.data[a]=="undefined"&&(b.splice(0,1),b[0]=b[0].substr(1)),b.join("|")},_setOption:function(a,b){},_handler:{openViewer:function(a){a.preventDefault(),h.changeImage(a),h._trigger("viewerOpened")},changeImage:function(a){a.preventDefault(),h.changeImage(a)},keyboard:function(a){switch(a.keyCode?a.keyCode:a.which){case 37:a.preventDefault(),a.data={type:"previous"},h.changeImage(a);break;case 39:a.preventDefault(),a.data={type:"next"},h.changeImage(a);break;case 27:a.preventDefault(),h.close(a);break;case 38:a.preventDefault(),a.data={type:"in"},h.zoom(a);break;case 40:a.preventDefault(),a.data={type:"out"},h.zoom(a)}},zoom:function(a){h.zoom(a)},resize:function(a){h._resize()},close:function(a){h.close(a)}}})})(jQuery,Handlebars,History,window,document);