((function(){var __bind=function(a,b){return function(){return a.apply(b,arguments)}};(function($){var actions,bind,body,callbacks,currentPage,dispatchEvent,doPreTransitionActions,doTransition,enableActionLink,fetchImageUrls,fetchPage,finishTransition,folderUrl,getCurrentPageId,getCurrentPath,getCurrentTemplateId,getFolderPath,getPageInfo,gotoPage,incomingPage,init,initTransition,insertTransitionContent,isTransitioning,methods,nextPage,onHashChange,onKeyPress,onSwipeDetected,options,pageDimensions,pages,preloadImage,preloadImageQueue,preloadPlacementImages,prevPage,processPageData,public,setPageInfo,settings,tagUnloadedImages,templates,thePage,toggleActionLinks,transitionDirection,transitioning,updateNextPrevLinks,_ref;return settings={slideDuration:1e3,startDelay:0,easing:null,preloadImages:!0,showPreloaderProgess:!1,afterImageLoaded:function(){},navigationIn:null,navigationOut:null},(_ref=hpk.transitions)==null&&(hpk.transitions={}),pages={},templates={},callbacks={},isTransitioning=!1,transitionDirection="next",folderUrl=null,currentPage=null,incomingPage=null,body=null,thePage=null,actions=null,methods=[],$(function(){var method,_i,_len,_results;_results=[];for(_i=0,_len=methods.length;_i<_len;_i++)method=methods[_i],_results.push(hpk.transitions[method]=eval(method));return _results}),public=function(a){return methods.push(a)},public("init"),init=function(a,b){return folderUrl=a,body=$("body"),thePage=$("#the_page"),currentPage=pages[b.id]={data:b,placements:$(".page_placements").get(0),prevPage:getPageInfo("prev"),nextPage:getPageInfo("next"),initialised:!0},templates[b.template_id]={placements:$(".template_placements").get(0)},fetchPage(currentPage.nextPage!=null?"next":"prev"),actions=$(".transition_actions"),actions.width()===0&&actions.width(1),enableActionLink("next",currentPage.nextPage!=null),enableActionLink("prev",currentPage.prevPage!=null),IOS&&(hpk.touch.init(),hpk.touch.bind("swipe_detected",onSwipeDetected)),$(document).keyup(onKeyPress),setTimeout(function(){if(settings.preloadImages)return fetchImageUrls(getCurrentPath())},1e3),tagUnloadedImages($(".page_placements"))},public("options"),options=function(a){return $.extend(settings,a)},getCurrentPageId=function(){return parseInt($(".page_placements.current").attr("data-id"),10)},getCurrentTemplateId=function(){return parseInt($(".template_placements.current").attr("data-id"),10)},public("transitioning"),transitioning=function(){return isTransitioning},public("getFolderPath"),getFolderPath=function(){return folderUrl},public("getCurrentPath"),getCurrentPath=function(){return folderUrl+"/"+currentPage.data.path},public("nextPage"),nextPage=function(){return gotoPage("next")},public("prevPage"),prevPage=function(){return gotoPage("prev")},gotoPage=function(a){var b,c;if(isTransitioning)return;return b=getPageInfo(a),b!=null?(isTransitioning=!0,transitionDirection=a,incomingPage=null,c=pages[b.id],initTransition(a,b.id),c!=null?(insertTransitionContent(c),doTransition()):fetchPage(a,!0)):log("No more pages: "+a)},initTransition=function(a,b){var c,d,e,f,g,h,i;return log("Initialising transition wrappers ("+a+":"+b+")"),hpk.hub.clearRelativePageDimensions(),$(".page_container").addClass("transitioning"),c=pages[getCurrentPageId()].data,h=$("#page_transition_"+c.id).addClass("current"),i=$('<div id="page_transition_'+b+'" class="page transition_wrapper '+a+'"></div>'),thePage.append(i),g=$(window).width(),f=$(window).height(),d=pageDimensions(c),e=thePage.offset(),h.css({left:-e.left,top:-e.top,width:g,height:f,overflow:"hidden"}),$(".page_placements",h).css({left:e.left,top:e.top,width:d.width,height:d.height}),i.css({left:g*(a==="next"?1:-1),width:g,height:f,overflow:"hidden"}),thePage.css({top:c.data.padding})},insertTransitionContent=function(a){var b,c,d,e,f,g;incomingPage=a,c=a.data,e=$(a.placements),g=$("#page_transition_"+c.id),g.append(e),f=$(window).width(),b=pageDimensions(c),d={left:Math.round((f-b.width)/2),top:c.data.padding},g.css({top:-d.top,left:g.position().left-d.left}),e.css({left:d.left,top:d.top,width:b.width,height:b.height}),a.initialised||hpk.hub.init(g);if(settings.showPreloaderProgess)return tagUnloadedImages(e)},doTransition=function(a){var b,c,d,e,f,g,h,i;if(a!==!0){doPreTransitionActions();return}return e=transitionDirection,h=pages[getPageInfo(e).id].data,c=settings.slideDuration,i=$(window).width(),b=pageDimensions(h),d=Math.round((i-b.width)/-2),f=(i-pageDimensions(currentPage.data).width)/2,g=e==="next"?-i-f:i-f,thePage.animate({width:b.width,height:b.height,top:h.data.padding},c).css("overflow",""),$(".page.transition_wrapper.current").animate({left:g},c,settings.easing),$(".page.transition_wrapper."+e).animate({left:d},c,settings.easing,__bind(function(){return finishTransition()},this)),dispatchEvent("started")},doPreTransitionActions=function(){return dispatchEvent("starting"),toggleActionLinks(!1),setTimeout(function(){return doTransition(!0)},settings.startDelay)},finishTransition=function(){var a,b,c,d,e,f;return b=transitionDirection,$(".page.transition_wrapper.current").remove(),f=$(".page.transition_wrapper."+b),e=$(".page_placements",f),f.removeClass(b).addClass("current").attr("style",""),e.addClass("current").attr("style",""),c=pages[getCurrentPageId()],d=c.data,$(".page_container").removeClass("transitioning"),thePage.css({top:""}),$("head title").text(d.title),body.attr("style",d.background_style+d.padding_style),body.attr("class","hub "+d.classes.join(" ")),thePage.attr("style",d.content_style),location.hash="!"+d.path,$("#logged_in").length>0&&(a=$("#logged_in li.admin_design a"),a.attr("href",(""+folderUrl+"/"+d.path+"?design=1").replace(/\/\//,"/"))),updateNextPrevLinks(c.prevPage,c.nextPage),enableActionLink(b==="next"?"prev":"next",!0),(b==="next"?c.nextPage:c.prevPage)==null&&enableActionLink(b,!1),toggleActionLinks(!0),fetchPage(b),isTransitioning=!1,currentPage=c,setTimeout(hpk.hub.updateRelativePageDimensions,10),dispatchEvent("finished")},fetchPage=function(a,b){var c,d,e,f;return c=getPageInfo(a),c==null||pages[c.id]!=null?!1:(f=c.url,e=getCurrentTemplateId(),d={current_page_id:getCurrentPageId(),current_template_id:e,exclude_template:templates[e]!=null},$.getJSON(f,d,function(a){var c;log("SUCCESS: Page fetched"),c=processPageData(a);if(b)return insertTransitionContent(c),doTransition()}),!0)},processPageData=function(a){var b,c;return log("Processing page data: "+a.page.title),log(a),b=pages[a.page.id]={data:a.page,placements:a.page_placements},c=function(a){var b,c;return b=folderUrl.substr(-1,1)==="/"?"":"/",c=folderUrl+b+a.path,{id:a.id,templateId:a.template_id,url:c}},a.prev_page!=null&&(b.prevPage=c(a.prev_page)),a.next_page!=null&&(b.nextPage=c(a.next_page)),a.template_placements!=null&&(templates[a.page.template_id]={placements:a.template_placements}),settings.preloadImages&&preloadPlacementImages(b.placements),b},getPageInfo=function(a){var b,c;c=$("head link[class="+a+"_page]");if(c.length>0)return b={id:parseInt(c.attr("data-page"),10),templateId:parseInt(c.attr("data-template"),10),url:c.attr("href")},isNaN(b.id)?null:b},setPageInfo=function(a,b){var c;return c=$("head link[class="+a+"_page]"),b!=null?c.attr({"data-page":b.id,"data-template":b.templateId,href:b.url}):c.attr({"data-page":"","data-template":"",href:""})},updateNextPrevLinks=function(a,b){return setPageInfo("prev",a),setPageInfo("next",b)},pageDimensions=function(a){var b,c,d,e,f;return f=$(window).width(),e=$(window).height(),b=function(a){var b;return b=String(a).toLowerCase(),b==="1"||b==="yes"||b==="true"},d=b(a.data.relative_width)?f*(a.width/100):a.width,c=b(a.data.relative_height)?e*(a.height/100):a.height,{width:Math.round(d),height:Math.round(c)}},toggleActionLinks=function(a){return settings.startDelay===0?actions.toggle(a):a||a==null&&!actions.is(":visible")?settings.navigationIn!=null?settings.navigationIn.call(this,actions):actions.fadeIn(settings.startDelay):settings.navigationOut!=null?settings.navigationOut.call(this,actions):actions.fadeOut(settings.startDelay)},enableActionLink=function(a,b){return $("."+a+"_page",actions).toggleClass("disabled",!b)},public("fetchImageUrls"),fetchImageUrls=function(a){var b,c,d;return log("Transitions: -> fetchImageUrls"),d="/api/queue_transition_images",c={full_path:a,current_page_id:currentPage.data.id},b=$.getJSON(d,c,function(a){log("SUCCESS: Image URLs fetched");if(a!=null)return preloadImageQueue(a)}),b.error(function(a){return log("ERROR: Image URLS failed ("+a.status+")")}),null},preloadPlacementImages=function(a){var b,c,d,e,f,g,h,i,j;d='<img.*?src="([^"]+)"[^>]*>',e=new RegExp(d,"gm"),c=a.match(e);if(c!=null){f=new RegExp(d),j=[];for(h=0,i=c.length;h<i;h++)b=c[h],j.push((g=b.match(f))!=null?preloadImage(g[1]):void 0);return j}},preloadImageQueue=function(a){var b,c;c=0,b=function(d){c+=1;if(a[c]!=null)return preloadImage(a[c],b)};if(a[0]!=null)return preloadImage(a[0],b)},preloadImage=function(a,b){var c;log("Transitions: preloading "+a),c=new Image,b!=null&&(c.onload=function(){return log("  loaded"),b(c)}),c.src=a;if(c.complete){log("  already complete");if(b!=null)return b(c)}},public("tagUnloadedImages"),tagUnloadedImages=function(a){var b;return b=$("> .image_element",a),b.each(function(){var a,b;a=$(this),b=$("img",a);if(!b[0].complete)return a.addClass("image_loading"),b[0].onload=function(){return a.removeClass("image_loading"),settings.afterImageLoaded(a)}})},onSwipeDetected=function(a){var b;if(isTransitioning)return;b=a.target.swipeDirection();if(b==="left")return nextPage();if(b==="right")return prevPage()},onKeyPress=function(a){if(a.keyCode===37)return prevPage();if(a.keyCode===39)return nextPage()},onHashChange=function(a){var b;b=location.hash.replace(/^#!/,"");if(b!==currentPage.data.path)return null},dispatchEvent=function(a){var b,c,d,e,f;log("Dispatching event: "+a);if(callbacks[a]!=null){e=callbacks[a],f=[];for(c=0,d=e.length;c<d;c++)b=e[c],f.push(b.handler({target:hpk.transitions,data:b.data,currentPage:currentPage,incomingPage:incomingPage,direction:transitionDirection}));return f}},public("bind"),bind=function(a,b,c){var d,e;return(e=callbacks[a])==null&&(callbacks[a]=new Array),c==null&&(c=b),d={data:b,handler:c},callbacks[a].push(d)}})(jQuery)})).call(this)
