/* turn.js | turnjs.com | (c) 2012 Emmanuel Garcia | MIT Licensed  */
(function(f){var E=Math.PI,C=E/2,H="WebKitCSSMatrix"in window&&"m11"in new WebKitCSSMatrix,t="Touch"in window,L={backward:["bl","tl"],forward:["br","tr"],all:["tl","bl","tr","br"]},M={page:1,shadows:!0,duration:600,acceleration:!0},N={back:null,corners:"forward",cornerSize:100,shadows:!0,duration:600,acceleration:!0},u=function(a,b,c,d,e,g){return{css:{position:"absolute",top:a,left:b,width:c,height:d,"z-index":e||"auto",overflow:g||"hidden"}}},J=function(a,b,c,d,e){var g=1-e,B=g*g*g;mu3=e*e*e;return j(Math.round(B*
a.x+3*e*g*g*b.x+3*e*e*g*c.x+mu3*d.x),Math.round(B*a.y+3*e*g*g*b.y+3*e*e*g*c.y+mu3*d.y))},j=function(a,b){return{x:a,y:b}},o=function(a,b,c){return H&&c?" translate3d("+a+"px,"+b+"px, 0px) ":" translate("+a+"px, "+b+"px) "},r=function(a){return" rotate("+a+"deg) "},i={init:function(a){var b,c,d=this.data();c=this.children();var e=c.length,a=f.extend({},M,a);d.opt=a;d.pageObjs={};d.pages={};d.pageWrap={};d.pagePlace={};d.pageMv=[];d.totalPages=e;this.css({position:"relative"});H&&!t&&a.acceleration&&
this.transform(o(0,0,!0));for(b=1;b<=e;b++)d.pagePlace[b]=b,d.pageObjs[b]=f(c[b-1]).addClass("turn-page").addClass("p"+b),d.pageWrap[b]=f("<div/>",{"class":"turn-page-wrapper",css:{position:"absolute",width:d.pageObjs[b].width(),height:d.pageObjs[b].height()}}).attr("page",b).appendTo(this).prepend(d.pageObjs[b]);for(b=1;b<=e;b++)c=b%2==0,d.pages[b]=d.pageWrap[b].css(c?{top:0,left:0}:{top:0,right:0}).children(":first").flip({next:c?b-1:b+1,page:b,turn:this,duration:a.duration,acceleration:a.acceleration,
corners:c?"backward":"forward",back:c?d.pageObjs[b-1]:d.pageObjs[b+1],backShadow:a.shadows&&b!=2&&b!=e-1,frontShadow:a.shadows}).bind("pressed",i._pressed).bind("released",i._released).bind("start",i._start).bind("end",i._end).bind("flip",i._flip);i.page.apply(this,[a.page]);d.done=!0;return this},_visiblePages:function(a){a=a||this.data().page;return a%2==0?[a,a+1]:[a-1,a]},_removeMv:function(a){var b,c=this.data();for(b=0;b<c.pageMv.length;b++)c.pageMv[b]==a&&(c.pageMv.splice(b,1),b--)},_addMv:function(a){var b=
this.data();i._removeMv.apply(this,[a]);b.pageMv.push(a)},view:function(a){var b=this.data(),a=i._visiblePages.apply(this,[a]);return[b.pages[a[0]]?a[0]:0,b.pages[a[1]]?a[1]:0]},stop:function(){var a=this.data(),b;a.pageMv=[];if(a.tpage)a.page=a.tpage,delete a.tpage;this.turn("update");for(b=1;b<=a.totalPages;b++){var c=a.pages[b].data().pageFlip.opt;a.pages[b].flip("hideThumbIndex");h._moveBackPage.apply(a.pages[b],[null]);a.pagePlace[c.next]=c.next;if(c.force)c.next=c.page%2==0?c.page-1:c.page+
1,a.pages[b].flip("setBackPage",a.pageObjs[c.next]),delete c.force}return this},page:function(a){var b=this.data(),c=this.turn("view");if(pg=b.pages[a])if(!b.done||(!c[0]||a>=c[0])&&(!c[1]||a<=c[1]))b.tpage=a,this.turn("stop"),this.trigger("turned",[a,pg]);else{b.tpage=a;this.turn("stop");var d,e,g=this.turn("view",a);c[1]&&a>c[1]?(d=c[1],e=g[0]):c[0]&&a<c[0]&&(d=c[0],e=g[1]);a=b.pages[d].data().pageFlip.opt;b.tpage=e;if(a.next!=e)a.next=e,b.pagePlace[a.next]=a.page,a.force=!0,h._moveBackPage.apply(b.pages[d],
[!1]),b.pages[d].flip("setBackPage",b.pageObjs[e]);b.pages[d].flip("turnPage")}else return c[0]||c[1];return this},next:function(){i._moveTo.apply(this,[1]);return this},previous:function(){i._moveTo.apply(this,[-1]);return this},_moveTo:function(a){var b,c=this.data(),d=i._visiblePages.apply(this,[c.tpage||c.page])[a==1?1:0],a=d+a,e=function(a){return c.pages[a].data().pageFlip};for(b=0;b<c.pageMv.length;b++)if(e(c.pageMv[b]).opt.force){this.turn("stop");break}if(c.pages[a]&&c.pages[a])if(c.pages[a].flip("moving")||
c.pagePlace[d]==a)b=e(a).opt,i._removeMv.apply(this,[b.pageMv]),i._addMv.apply(this,[d]),c.tpage=a,b.pageMv=d,c.pages[a].flip("hideThumbIndex",!0),c.pages[d].trigger("flip"),this.turn("update");else{c.tpage=a;if(e(d).fwrapper.is(":visible"))b=e(d).opt,i._removeMv.apply(this,[b.pageMv]),i._addPage.apply(c.pages[d]);c.pages[d].flip("turnPage")}},_addPage:function(){var a=f(this).data().pageFlip.opt,b=a.turn,c=b.data();a.pageMv=a.page;i._addMv.apply(b,[a.pageMv]);c.pagePlace[a.next]=a.page;b.turn("update")},
_start:function(a){var b=f(this).data().pageFlip.opt;a.stopPropagation();i._addPage.apply(this);b.turn.trigger("start",[b.page])},_end:function(a,b){a.stopPropagation();var c=f(this),d=c.data().pageFlip.opt,e=d.turn,g=e.data();if(b||g.tpage){if(g.tpage==d.next||g.pageMv.length==0)g.page=g.tpage||d.next,delete g.tpage,e.turn("page",g.page);if(d.force)d.next=d.page%2==0?d.page-1:d.page+1,c.flip("setBackPage",e.data().pageObjs[d.next]),delete d.force}else i._removeMv.apply(e,[d.pageMv]),e.turn("update");
e.trigger("end",[d.page,this])},resize:function(a){var b=this.data();for(p=1;p<=b.totalPages;p++)b.pages[p].flip("resize",a)},calculateZ:function(a){var b=this,c=this.data(),d,e,g,B=c.totalPages,h=a.length,f={pageZ:{},partZ:{},pageV:{}},j=function(a){a=b.turn("view",a);a[0]&&(f.pageV[a[0]]=!0);a[1]&&(f.pageV[a[1]]=!0)};g=this.turn("view");for(var i=g[0]||g[1],k=0;k<h;k++)d=a[k],e=c.pages[d].data().pageFlip.opt.next,g=c.pagePlace[d],j(d),j(e),d=c.pagePlace[e]==e?e:d,e=c.totalPages-Math.abs(i-d),f.pageZ[d]=
e,f.partZ[g]=c.totalPages*2+Math.abs(i-d),d%2!=0&&c.pages[d-1]?(e-=h-1,f.pageZ[d-1]=e):d%2==0&&c.pages[d+1]&&(e-=d-1,f.pageZ[d+1]=e),e<B&&(B=e);for(var m in f.pageV)f.pageZ[m]||(f.pageZ[m]=--B);return f},update:function(){var a,b=this.data();if(b.pageMv.length){var c=this.turn("calculateZ",b.pageMv),d=this.turn("view",b.tpage),e;b.pagePlace[d[0]]==d[0]?e=d[0]:b.pagePlace[d[1]]==d[1]&&(e=d[1]);for(a=1;a<=b.totalPages;a++)b.pageWrap[a].css({"z-index":c.pageZ[a]||0,display:c.pageV[a]?"":"none"}),b.pages[a].flip("z",
c.partZ[a]||null),c.pageV[a]&&b.pages[a].flip("resize"),b.tpage&&b.pages[a].flip("disable",a!=e)}else{d=this.turn("view");for(a=1;a<=b.totalPages;a++)(c=a==d[0]||a==d[1])?b.pageWrap[a].css({"z-index":b.totalPages,display:""}):a==d[0]-2||a==d[1]+2?b.pageWrap[a].css({"z-index":b.totalPages-1,display:""}):b.pageWrap[a].css({"z-index":0,display:"none"}),b.pages[a].flip("z",null),b.pages[a].flip("disable",!c)}},_pressed:function(){var a,b=f(this).data().pageFlip,c=b.opt.page,b=b.opt.turn.data().pages;
for(a in b)a!=c&&b[a].flip("disable",!0);return this.time=(new Date).getTime()},_released:function(a,b){var c=f(this),d=(new Date).getTime()-this.time,e=c.data().pageFlip;if(d<200||b.x<0||b.x>f(this).width())a.stopPropagation(),e.opt.turn.data().tpage=e.opt.next,e.opt.turn.turn("update"),f(c).flip("turnPage")},_flip:function(){var a=f(this).data().pageFlip.opt;a.turn.trigger("turning",[a.next])},disable:function(a){var b=this.data(),a=typeof a=="undefined"?!0:a===!0;for(p=1;p<=b.totalPages;p++)b.pages[p].flip("disable",
a)}},h={init:function(a){if(a.shadows)a.frontShadow=!0,a.backShadow=!0;h.setData.apply(this,[{opt:f.extend({},N,a)}]);h._addEvents.apply(this);h._addPageWrapper.apply(this);return this},setData:function(a){var b=this.data();b.pageFlip=f.extend(b.pageFlip||{},a)},_cAllowed:function(){return L[this.data().pageFlip.opt.corners]||this.data().pageFlip.opt.corners},_cornerActivated:function(a){var a=t?a.originalEvent.touches:[a],b;b=this.data().pageFlip;var c=b.parent.offset(),d=this.width(),e=this.height(),
g=Math.max(0,a[0].pageX-c.left),a=Math.max(0,a[0].pageY-c.top),f=b.opt.cornerSize,c=h._cAllowed.apply(this);if(!b.opt.back||g<=0||a<=0||g>=d||a>=e)b=!1;else if(g<=f&&a<=f)b="tl";else if(g>=d-f&&a<=f)b="tr";else if(g<=f&&a>=e-f)b="bl";else if(g>=d-f&&a>=e-f)b="br";else return!1;return jQuery.inArray(b,c)!=-1?{corner:b,x:g,y:a}:!1},_c:function(a,b){b=b||0;return{tl:j(b,b),tr:j(this.width()-b,b),bl:j(b,this.height()-b),br:j(this.width()-b,this.height()-b)}[a]},_c2:function(a){return{tl:j(this.width()*
2,0),tr:j(-this.width(),0),bl:j(this.width()*2,this.height()),br:j(-this.width(),this.height())}[a]},z:function(a){var b=this.data().pageFlip;b.opt["z-index"]=a;b.fwrapper.css({"z-index":a||parseInt(b.parent.css("z-index"))||0})},resize:function(){var a=this.data().pageFlip;a.parent.is(":visible")&&(a.fwrapper.css({top:a.parent.offset().top,left:a.parent.offset().left}),a.opt.turn&&a.fparent.css({top:-a.opt.turn.offset().top,left:-a.opt.turn.offset().left}));this.flip("z",a.opt["z-index"])},_addPageWrapper:function(){var a=
this.data().pageFlip,b=this.parent();if(!a.wrapper){var c=this.css("left"),d=this.css("top"),e=this.width(),g=this.height(),j=Math.round(Math.sqrt(Math.pow(e,2)+Math.pow(g,2)));a.fparent=a.opt.turn?a.opt.turn.data().fparent:f("#turn-fwrappers");if(!a.fparent){var i=f("<div/>").hide();i.data().flips=0;a.opt.turn?(i.css(u(-a.opt.turn.offset().top,-a.opt.turn.offset().left,"auto","auto","auto","visible").css).appendTo(a.opt.turn),a.opt.turn.data().fparent=i):i.css(u(0,0,"auto","auto","auto","visible").css).attr("id",
"turn-fwrappers").appendTo(f("body"));a.fparent=i}a.parent=b;this.css({position:"absolute",top:0,left:0,bottom:"auto",right:"auto"});a.wrapper=f("<div/>",u(d,c,j,j,this.css("z-index"))).appendTo(b).prepend(this);a.fwrapper=f("<div/>",u(b.offset().top,b.offset().left,j,j)).hide().appendTo(a.fparent);a.fpage=f("<div/>",{fpage:1,css:{width:g,height:e,cursor:"default"}}).appendTo(f("<div/>",u(0,0,e,g,0,"visible")).appendTo(a.fwrapper));if(a.opt.frontShadow)a.ashadow=f("<div/>",u(0,0,g,e,1)).appendTo(a.fpage);
if(a.opt.backShadow)a.bshadow=f("<div/>",u(0,0,e,g,1)).css({position:""}).appendTo(b);h.setData.apply(this,[a]);h.resize.apply(this)}},_displayCorner:function(a){var b=this,c=0,d=0,e,g,f,i,F,w=j(0,0),I=j(0,0),k=j(0,0),m=this.width(),q=this.height(),l=this.data().pageFlip,v=l.opt.acceleration,u=l.wrapper.height(),t=h._c.apply(this,[a.corner]),A=a.corner.substr(0,1)=="t",x=a.corner.substr(1,1)=="l",D=function(){var n=j(t.x?t.x-a.x:a.x,t.y?t.y-a.y:a.y),y=Math.atan2(n.y,n.x),s;d=C-y;c=d/E*180;s=j(x?m-
n.x/2:a.x+n.x/2,n.y/2);var z=Math.max(0,Math.sin(d-Math.atan2(s.y,s.x))*Math.sqrt(Math.pow(s.x,2)+Math.pow(s.y,2)));k=j(z*Math.sin(d),z*Math.cos(d));if(d>C&&(k.x+=Math.abs(k.y*Math.tan(y)),k.y=0,Math.round(k.x*Math.tan(E-d))<q)){a.y=Math.sqrt(Math.pow(q,2)+2*s.x*n.x);if(A)a.y=q-a.y;return D()}e=Math.round(k.y/Math.tan(d)+k.x);x&&(e=m-e);n=x?e:m-e;y=n*Math.cos(d*2);s=n*Math.sin(d*2);n*=Math.sin(d);z=h._c2.apply(b,[a.corner]);z=Math.sqrt(Math.pow(z.x-a.x,2)+Math.pow(z.y-a.y,2));I=j(Math.round(e+(x?
-y:y)),Math.round(A?s:q-s));F=z<m?z/m:1;if(d>C){y=E-d;s=u-q/Math.sin(y);w=j(Math.round(s*Math.cos(y)),Math.round(s*Math.sin(y)));if(x)w.x=-w.x;if(A)w.y=-w.y}if(l.opt.frontShadow){ppcen=n/(d<Math.atan2(m,q)?q/Math.cos(d):m/Math.sin(d))*100;i=n>100?(n-100)/n:0;g=j(n*Math.sin(C-d)/q*100,n*Math.cos(C-d)/m*100);if(A)g.y=100-g.y;if(x)g.x=100-g.x}if(l.opt.backShadow){f=j(n*Math.sin(d)/m*100,n*Math.cos(d)/q*100);if(!x)f.x=100-f.x;if(!A)f.y=100-f.y}k.x=Math.round(k.x);k.y=Math.round(k.y);return!0},G=function(a,
c,d,e){var h=["0","auto"],j=(m-u)*d[0]/100,k=(q-u)*d[1]/100,d=d[0]+"% "+d[1]+"%",c={left:h[c[0]],top:h[c[1]],right:h[c[2]],bottom:h[c[3]]};b.css(c).transform(r(e)+o(a.x,a.y,v),d);l.fpage.parent().css(c);l.wrapper.transform(o(-a.x+j,-a.y+k,v)+r(-e),d);l.fwrapper.transform(o(-a.x+w.x+j,-a.y+w.y+k,v)+r(-e),d);l.fpage.parent().transform(r(e)+o(a.x+I.x-w.x,a.y+I.y-w.y,v),d);l.opt.frontShadow&&l.ashadow.css({"background-image":"-webkit-gradient(linear, "+(x?100:0)+"% "+(A?100:0)+"%, "+g.x+"% "+g.y+"%, color-stop("+
i+",rgba(0,0,0,0)), color-stop("+((1-i)*0.8+i)+",rgba(0,0,0,"+0.2*F+")), to(rgba(255,255,255,"+0.2*F+")) )"});l.opt.backShadow&&l.bshadow.css({"background-image":"-webkit-gradient(linear, "+(x?0:100)+"% "+(A?0:100)+"%, "+f.x+"% "+f.y+"%,  color-stop(0.8,rgba(0,0,0,0)), color-stop(1, rgba(0,0,0,"+0.2*F+")), to(rgba(0,0,0,0)) )"})};switch(a.corner){case "tl":a.x=Math.max(a.x,1);D();G(k,[1,0,0,1],[100,0],c);l.fpage.transform(o(-q,-m,v)+r(90-c*2),"100% 100%");l.opt.back.transform(r(90)+o(0,-q,v),"0% 0%");
break;case "tr":a.x=Math.min(a.x,m-1);D();G(j(-k.x,k.y),[0,0,0,1],[0,0],-c);l.fpage.transform(o(0,-m,v)+r(-90+c*2),"0% 100%");l.opt.back.transform(r(270)+o(-m,0,v),"0% 0%");break;case "bl":a.x=Math.max(a.x,1);D();G(j(k.x,-k.y),[1,1,0,0],[100,100],-c);l.fpage.transform(o(-q,0,v)+r(-90+c*2),"100% 0%");l.opt.back.transform(r(270)+o(-m,0,v),"0% 0%");break;case "br":a.x=Math.min(a.x,m-1),D(),G(j(-k.x,-k.y),[0,1,1,0],[0,100],c),l.fpage.transform(r(90-c*2),"0% 0%"),l.opt.back.transform(r(90)+o(0,-q,v),"0% 0%")}l.p=
a},setBackPage:function(a){var b=this.data().pageFlip;b.opt.back=a;b.backParent=a.parent()},_moveBackPage:function(a){var b=this.data().pageFlip;if(b.opt.back)if(a){if(!((b.ashadow?"1":"0")in b.fpage.children()))h.setData.apply(this,[{backParent:b.opt.back.parent()}]),b.fpage.prepend(b.opt.back)}else b.opt.back.transform("","0% 0%"),b.backParent&&b.backParent.prepend(b.opt.back)},_showThumbIndex:function(a,b){var c=this.data(),d=c.pageFlip;if(d.opt.back){if(b){var e=this,c=d.p||h._c.apply(this,[a.corner,
1]);this.animatef({from:[c.x,c.y],to:[a.x,a.y],duration:500,frame:function(b){h._displayCorner.apply(e,[{corner:a.corner,x:b[0],y:b[1]}])}})}else h._displayCorner.apply(this,[a]),c.effect&&!c.effect.turning&&this.animatef(!1);d.fwrapper.is(":visible")||(d.fparent.show().data().flips++,h._moveBackPage.apply(this,[!0]),d.fwrapper.show(),d.opt.backShadow&&d.bshadow.show(),this.trigger("start"))}},hide:function(){var a=this.data().pageFlip;--a.fparent.data().flips==0&&a.fparent.hide();this.css({left:0,
top:0,right:"auto",bottom:"auto"}).transform("","0% 100%");a.wrapper.transform("","0% 100%");a.fwrapper.hide();a.opt.backShadow&&a.bshadow.hide();a.opt.back.transform("","0% 0%")},hideThumbIndex:function(a){var b=this.data().pageFlip;if(b.p){var c=this,d=b.p,e=function(){b.p=null;c.flip("hide");c.trigger("end",[!1])};if(a){var g,f,i=h._c.apply(this,[d.corner]),a=d.corner.substr(0,1)=="t",o=Math.abs((d.y-i.y)/2);g=j(d.x,d.y+o);f=j(i.x,a?i.y+o:i.y-o);this.animatef({from:0,to:1,frame:function(a){a=J(d,
g,f,i,a);a.corner=d.corner;h._displayCorner.apply(c,[a])},complete:e,duration:800,hiding:!0})}else this.animatef(!1),e()}},turnPage:function(){var a=this,b=this.data().pageFlip,c=b.cornerActivated?b.cornerActivated.corner:h._cAllowed.apply(this)[0],d=b.p||h._c.apply(this,[c]),e=h._c2.apply(this,[c]);this.trigger("flip");this.animatef({from:0,to:1,frame:function(b){b=J(d,d,e,e,b);b.corner=c;h._showThumbIndex.apply(a,[b])},complete:function(){a.trigger("end",[!0])},duration:b.opt.duration,turning:!0});
b.cornerActivated=null},moving:function(){return"effect"in this.data()},isTurning:function(){return this.flip("moving")&&this.data().effect.turning},_addEvents:function(){var a=this,b=t?{start:"touchstart",move:"touchmove",end:"touchend"}:{start:"mousedown",move:"mousemove",end:"mouseup"};f(document).bind(b.start,function(){return h._eventStart.apply(a,arguments)}).bind(b.move,function(){h._eventMove.apply(a,arguments)}).bind(b.end,function(){h._eventEnd.apply(a,arguments)})},_eventStart:function(a){var b=
this.data().pageFlip;if(!b.disabled&&!this.flip("isTurning")&&(b.cornerActivated=h._cornerActivated.apply(this,[a]),b.cornerActivated))return h._moveBackPage.apply(this,[!0]),this.trigger("pressed",[b.p]),!1},_eventMove:function(a){var b=this.data(),c=b.pageFlip,a=t?a.originalEvent.touches:[a];c.disabled||(c.cornerActivated?(b=c.parent.offset(),h._showThumbIndex.apply(this,[{corner:c.cornerActivated.corner,x:a[0].pageX-b.left,y:a[0].pageY-b.top}])):!b.effect&&!t&&((corner=h._cornerActivated.apply(this,
[a[0]]))?(a=h._c.apply(this,[corner.corner,c.opt.cornerSize/2]),h._showThumbIndex.apply(this,[{corner:corner.corner,x:a.x,y:a.y},!0])):h.hideThumbIndex.apply(this,[!0])))},_eventEnd:function(){var a=this.data().pageFlip;if(!a.disabled&&a.cornerActivated){var b=jQuery.Event("released");this.trigger(b,[a.p]);b.isPropagationStopped()||h.hideThumbIndex.apply(this,[!0])}a.cornerActivated=null},disable:function(a){h.setData.apply(this,[{disabled:a}])}},K=function(a,b,c){if(!c[0]||typeof c[0]=="object")return b.init.apply(a,
c);else if(b[c[0]]&&c[0].toString().substr(0,1)!="_")return b[c[0]].apply(a,Array.prototype.slice.call(c,1));else throw c[0]+" is an invalid value";};f.extend(f.fn,{flip:function(a,b){return K(this,h,arguments)},turn:function(a){return K(this,i,arguments)},transform:function(a,b){b&&this.css({"transform-origin":b,"-moz-transform-origin":b,"-o-transform-origin":b,"-webkit-transform-origin":b,"-ms-transform-origin":b});return this.css({transform:a,"-moz-transform":a,"-o-transform":a,"-webkit-transform":a,
"-ms-transform":a})},animatef:function(a){var b=this.data();b.effect&&clearInterval(b.effect.handle);if(a){if(!a.to.length)a.to=[a.to];if(!a.from.length)a.from=[a.from];if(!a.easing)a.easing=function(a,b,c,d,e){return d*Math.sqrt(1-(b=b/e-1)*b)+c};var c,d=[],e=a.to.length,f=this,h=a.fps||30,i=-h,j=function(){var c,j=[];i=Math.min(a.duration,i+h);for(c=0;c<e;c++)j.push(a.easing(1,i,a.from[c],d[c],a.duration));a.frame(e==1?j[0]:j);i==a.duration&&(clearInterval(b.effect.handle),delete b.effect,f.data(b),
a.complete&&a.complete())};for(c=0;c<e;c++)d.push(a.to[c]-a.from[c]);b.effect=a;b.effect.handle=setInterval(j,h);this.data(b);j()}else delete b.effect}});f.has3d=H;f.isTouch=t})(jQuery);