BwUseManualWidgetLoadedNotification(),bwdefine("JigsawPuzzleUI",["widgetstorage","jigsawpuzzle","bw/widget","jquery","bw/tr","bw/dialog","bw/rewardUtils","bw/uriEncodeImage","bw/config","bw/ProgressLogger"],function(e,t,n,i,a,o,r,s,c,u){"use strict";i(function(){c.getConfig().then(function(c){function l(){m=i("#canvas").parent().outerWidth(),b=i("#canvas").parent().outerHeight(),[p,h].forEach(function(e){e.width=m,e.height=b,e.style.width=m+"px",e.style.height=b+"px"}),[p,h].forEach(function(e){var t=e.getContext("2d"),n=window.devicePixelRatio||1,i=t.webkitBackingStorePixelRatio||t.mozBackingStorePixelRatio||t.msBackingStorePixelRatio||t.oBackingStorePixelRatio||t.backingStorePixelRatio||1;if(i!==n){var a=e.width,o=e.height;x=n/i,e.width=a*x,e.height=o*x,t.scale(x,x)}})}function d(){var e=i("body");return Math.min(parseInt(Math.max(e.width(),e.height())/100)/10,1)}n.makeWidget({layout:"title_toolbar_layout",title:c.title,"no-user-select":!0});var w=r.createRewardWord(i("#reward"),c.rewardWord),f=u(),g=new Image;g.src=c.imagefile;var h=i("#canvas-move")[0],v=h.getContext("2d"),p=i("#canvas")[0],m=i("#canvas").parent().outerWidth(),b=i("#canvas").parent().outerHeight(),y=i("#canvas").offset().top,x=1;l(),g.onload=function(){function r(){l(),P.updateCanvasSize(m,b),P.draw(),w.adjustPosition()}function u(e){var t=e.data;t.hasOwnProperty("msg")&&"requestlogin"===t.msg&&(BwNotifyWidgetLoaded(),window.removeEventListener("message",u))}var h=d(),z=parseInt(c.piecesX,10)*parseInt(c.piecesY,10),P=new t.Puzzle({piecesX:parseInt(c.piecesX,10),piecesY:parseInt(c.piecesY,10),canvasWidth:m,canvasHeight:b,image:g,imageRatio:h,canvas:p,canvasRatio:x,seed:c.title+c.imagefile,finishedCB:function(){w.show(),f.setProgress(z,z)},updateCB:function(e){f.setProgress(z-e,z-1)}});window.onresize=r;var k=!1;window.addEventListener("message",u),e.init({type:"Jigsaw Puzzle",group:"_no_name_",name:""===c.title?"Jigsaw Puzzle":c.title,id:c.uuid2,singleKey:!1,updateCallback:function(t){"yes"!==c.save_answers&&(t={}),P.load(t,function(t,u){if(!t&&u||(P.shuffle(),"yes"===c.save_answers&&e.write(P.save())),P.draw(),!k){k=!0;var l=function(){P.shuffle(),"yes"===c.save_answers&&(e.write(P.save()),f.restart()),P.draw(),i("#reward").hide(),i("#reward").removeClass("animated bounceIn")};i("#clear").tapOrClick(function(){o.showConfirmationDialog(a("The pieces will be shuffled, this can not be undone. Are you sure?"),a("Shuffle the jigsaw pieces and start over?"),l,null,a("Yes, clear"),a("No, keep"))}),"yes"===c.showHint&&(i("#hint-container").css({"background-image":"url("+s(c.imagefile)+")","background-size":Math.floor(g.width*h)+"px "+Math.floor(g.height*h)+"px"}),i("#hint").tapOrClick(function(){i("#hint-container").fadeToggle(200)}),i("#hint").removeClass("bw-icon-hidden"),n.centerTitle());var d=null,w=function(){d&&(d.drop(),"yes"===c.save_answers&&e.write(P.save()),window.requestAnimationFrame(function(){v.clearRect(0,0,m,b),P.draw()}),d=null)},x=function(e,t){d&&(console.warn("Lost selection somewhere"),w()),d=P.getSelection(e,t),d&&window.requestAnimationFrame(function(){P.draw(),d&&d.draw(v)})},z=function(e,t,n){d&&(d.move(e,t),window.requestAnimationFrame(function(){d&&(v.clearRect(0,0,m,b),d.draw(v))}))};i("#container").on("touchstart",function(e){}),i("#canvas-move").on("touchstart",function(e){e.preventDefault(),x(e.originalEvent.targetTouches[0].pageX,e.originalEvent.targetTouches[0].pageY-y)}).on("touchmove",function(e){e.preventDefault(),z(e.originalEvent.targetTouches[0].pageX,e.originalEvent.targetTouches[0].pageY-y,e)}).on("mousedown",function(e){e.preventDefault();var t=i("#canvas-move").offset();x(e.clientX-t.left,e.clientY-t.top)}).on("mouseup touchend",function(e){e.preventDefault(),w()}).on("mousemove",function(e){e.preventDefault();var t=i("#canvas-move").offset();z(e.clientX-t.left,e.clientY-t.top,e)}),p.style.width="0px",window.setTimeout(function(){p.style.width=m+"px",r()},100),BwNotifyWidgetLoaded()}})}})}})})}),bwdefine.finalize();