// ZIM js Interactive Media modules by Dan Zen http://danzen.com (c) 2016
// zimcreate.js adds functionality to CreateJS for digidos (Interactive Features) http://zimjs.com
// free to use - donations welcome of course! http://zimjs.com/donate
// functions in this module require createjs namespace to exist and in particular easel.js and tween.js
// available at http://createjs.com
if("undefined"==typeof zog)document.write(''),document.write('');else var zim=function(e){return zon&&zog("ZIM CREATE Module"),e.drag=function(t,o,r,a,i,n,s,c){function l(){z=0,f=0,x=s?e.boundsToGlobal(t.parent,o):o,p=t.parent.localToGlobal(t.x,t.y),d(t,p.x,p.y)}function d(e,t,o){if(e.parent&&e.getStage()){var r=e.parent.globalToLocal(t,o),a=u(e,r.x-z,r.y-f);e.x=a.x,e.y=a.y,e.getStage().update()}}function u(e,t,r){if(o){var a=e.parent.localToGlobal(t,r);t=Math.max(x.x,Math.min(x.x+x.width,a.x)),r=Math.max(x.y,Math.min(x.y+x.height,a.y)),a=e.parent.globalToLocal(t,r),t=a.x,r=a.y}return{x:t,y:r}}var g,h="obj, rect, overCursor, dragCursor, currentTarget, swipe, localBounds, onTop";if(g=zob(e.drag,arguments,h))return g;if(!zot(t)&&t.on){t.cursor=zot(r)?"pointer":r,zot(o)&&(s=!1),zot(i)&&(i=!1),zot(n)&&(n=!1),zot(s)&&(s=!1),zot(c)&&(c=!0),e.setSwipe(t,n);var z,f,p,x;return t.zimAdded=t.on("added",l,null,!0),t.parent&&l(),t.zimDown=t.on("mousedown",function(r){var n=i?r.currentTarget:r.target;c&&(n.parent.setChildIndex(n,n.parent.numChildren-1),n.getStage().update());var l=n.parent.globalToLocal(r.stageX,r.stageY);z=l.x-n.x,f=l.y-n.y,x=s?e.boundsToGlobal(r.target.parent,o):o,t.cursor=zot(a)?"move":a},!0),t.zimMove=t.on("pressmove",function(e){var t=i?e.currentTarget:e.target;d(t,e.stageX,e.stageY)},!0),t.zimUp=t.on("pressup",function(e){t.cursor=zot(r)?"pointer":r},!0),t}},e.noDrag=function(t){return!zot(t)&&t.on?(t.cursor="default",e.setSwipe(t,!0),t.off("added",t.zimAdded),t.off("mousedown",t.zimDown),t.off("pressmove",t.zimMove),t.off("pressup",t.zimUp),t):void 0},e.setSwipe=function(e,t){function o(t){for(var r,a=t.getNumChildren(),i=0;a>i;i++)r=t.getChildAt(i),r.zimNoSwipe=e.zimNoSwipe,r instanceof createjs.Container&&o(r)}!zot(e)&&e.on&&(e.zimNoSwipe=t?null:!0,e instanceof createjs.Container&&o(e))},e.hitTestPoint=function(e,t,o){if(!zot(e)&&e.globalToLocal){var r=e.globalToLocal(t,o);return e.hitTest(r.x,r.y)}},e.hitTestReg=function(e,t){if(!zot(e)&&!zot(t)&&e.localToLocal&&t.localToLocal){var o=t.localToLocal(t.regX,t.regY,e);return e.hitTest(o.x,o.y)}},e.hitTestRect=function(e,t,o){if(!zot(e)&&!zot(t)&&e.hitTest&&t.getBounds){zot(o)&&(o=0);var r=t.getBounds();if(!r)return void zog("zim create - hitTestRect():\n please setBounds() on param b object");for(var a,i,n,s=0;o>=s;s++){if(a=r.width*(s+1)/(o+1),i=r.height*(s+1)/(o+1),n=t.localToLocal(r.x+a,r.y,e),e.hitTest(n.x,n.y))return!0;if(n=t.localToLocal(r.x+r.width,r.y+i,e),e.hitTest(n.x,n.y))return!0;if(n=t.localToLocal(r.x+r.width-a,r.y+r.height,e),e.hitTest(n.x,n.y))return!0;if(n=t.localToLocal(r.x,r.y+r.height-i,e),e.hitTest(n.x,n.y))return!0}}},e.hitTestCircle=function(e,t,o){if(!zot(e)&&!zot(t)&&e.hitTest&&t.getBounds){zot(o)&&(o=8);var r=t.getBounds();if(!r)return void zog("zim create - hitTestCircle():\n please setBounds() on param b object");for(var a,i,n,s,c=r.x+r.width/2,l=r.y+r.height/2,d=(r.width+r.height)/2/2,u=0;o>u;u++)if(a=u/o*2*Math.PI,i=c+d*Math.cos(a),n=l+d*Math.sin(a),s=t.localToLocal(i,n,e),e.hitTest(s.x,s.y))return!0}},e.hitTestBounds=function(t,o,r){function a(e,t){return e.x>=t.x+t.width||e.x+e.width<=t.x||e.y>=t.y+t.height||e.y+e.height<=t.y?!1:!0}if(!zot(t)&&!zot(o)&&t.getBounds&&o.getBounds){var i=!1;r&&r.graphics&&(i=!0);var n=t.getBounds(),s=o.getBounds();if(!n||!s)return void zog("zim create - hitTestBounds():\n please setBounds() on both objects");var c=e.boundsToGlobal(t),l=e.boundsToGlobal(o);if(i){var d=r.graphics;d.clear(),d.setStrokeStyle(1).beginStroke("blue"),d.drawRect(c.x,c.y,c.width,c.height),d.beginStroke("green"),d.drawRect(l.x,l.y,l.width,l.height),r.getStage().update()}return a(c,l)}},e.boundsToGlobal=function(e,t){if(!zot(e)&&e.getBounds){var o=e.getBounds();if(!o&&zot(t))return void zog("zim create - boundsToGlobal():\n please setBounds() on object (or a rectangle)");t&&(o=t);var r=e.localToGlobal(o.x,o.y),a=e.localToGlobal(o.x+o.width,o.y),i=e.localToGlobal(o.x+o.width,o.y+o.height),n=e.localToGlobal(o.x,o.y+o.height),s=Math.min(r.x,a.x,i.x,n.x),c=Math.min(r.y,a.y,i.y,n.y),l=Math.max(r.x,a.x,i.x,n.x),d=Math.max(r.y,a.y,i.y,n.y);return new createjs.Rectangle(s,c,l-s,d-c)}},e.hitTestGrid=function(e,t,o,r,a,i,n,s,c,l,d,u,g){if(!zot(e)&&!u){var h=e.globalToLocal(i,n);i=h.x,n=h.y}zot(s)&&(s=0),zot(c)&&(c=0),zot(l)&&(l=0),zot(d)&&(d=0);var z=t/r,f=o/a,p=Math.min(r-1,Math.max(0,Math.floor((i-s)/z))),x=Math.min(a-1,Math.max(0,Math.floor((n-c)/f)));if(!(i-s>z*(p+1)-l||z*p>i-s||n-c>f*(x+1)-d||f*x>n-c)){var v=x*r+p;return zot(g)||"index"==g?v:"col"==g?p:"row"==g?x:"array"==g?[v,p,x]:void 0}},e.scale=function(e,t){return!zot(e)&&e.scaleX?(zot(t)&&(t=1),e.scaleX=e.scaleY=t,e):void 0},e.scaleTo=function(t,o,r,a,i){var n,s="obj, boundObj, percentX, percentY, type";if(n=zob(e.scaleTo,arguments,s))return n;if(zot(t)||!t.getBounds||!t.getBounds())return void zog("zim create - scaleTo(): please provide an object (with setBounds) to scale");if(zot(o)||!o.getBounds||!o.getBounds())return void zog("zim create - scaleTo(): please provide a boundObject (with setBounds) to scale to");if(zot(r)&&(r=-1),zot(a)&&(a=-1),-1==r&&-1==a)return t;zot(i)&&(i="smallest");var c=o.getBounds().width*r/100,l=o.getBounds().height*a/100;if((-1==r||-1==a)&&"both"!=i&&"stretch"!=i)return-1==r?e.scale(t,l/t.getBounds().height):e.scale(t,c/t.getBounds().width),t;if("both"==i||"stretch"==i)return t.scaleX=-1!=r?c/t.getBounds().width:t.scaleX,t.scaleY=-1!=a?l/t.getBounds().height:t.scaleY,t;if("biggest"==i||"largest"==i||"outside"==i)var d=Math.max(c/t.getBounds().width,l/t.getBounds().height);else var d=Math.min(c/t.getBounds().width,l/t.getBounds().height);return e.scale(t,d),t},e.move=function(t,o,r,a,i,n,s,c,l,d,u){var g,h="target, x, y, time, ease, call, params, wait, props, fps, ticker";return(g=zob(e.move,arguments,h))?g:e.animate(t,{x:o,y:r},a,i,n,s,c,l,d,u)},e.animate=function(t,o,r,a,i,n,s,c,l,d){function u(){if(i&&"function"==typeof i&&i(n),c.loop){if(!(x>0))return;if(x>v)return void v++}p.setPaused(!0),d&&createjs.Ticker.off("tick",j)}function g(){T&&"function"==typeof T&&T(B)}var h,z="target, obj, time, ease, call, params, wait, props, fps, ticker";if(h=zob(e.animate,arguments,z))return h;if(!zot(t)&&t.on&&!zot(o)&&t.getStage()){var f=r;zot(f)&&(f=1e3),zot(a)&&(a="quadInOut"),zot(s)&&(s=0),zot(c)&&(c={override:!0}),zot(n)&&(n=t),zot(l)&&(l=30),zot(d)&&(d=!0),zot(o.scale)||(o.scaleX=o.scaleY=o.scale,delete o.scale);var p;if(c.loop&&!zot(c.count)){var x=c.count;delete c.count;var v=1}if(c.rewind){if(a){var m=a;-1==m.indexOf("InOut")&&(-1!=m.indexOf("Out")?m=m.replace("Out","In"):-1!=m.indexOf("In")&&(m=m.replace("In","Out")))}var y={},b=0;for(var w in o)y[w]=t[w];if(delete c.rewind,c.rewindWait&&(b=c.rewindWait,delete c.rewindWait),c.rewindCall){var T=c.rewindCall,B=c.rewindParams;zot(B)&&(B=t),delete c.rewindCall,delete c.rewindParams,p=createjs.Tween.get(t,c).wait(s).to(o,f,createjs.Ease[a]).call(g).wait(b).to(y,f,createjs.Ease[m]).call(u)}else p=createjs.Tween.get(t,c).wait(s).to(o,f,createjs.Ease[a]).wait(b).to(y,f,createjs.Ease[m]).call(u)}else p=createjs.Tween.get(t,c).wait(s).to(o,f,createjs.Ease[a]).call(u);if(d){var j=createjs.Ticker.on("tick",t.getStage());createjs.Ticker.setFPS(l)}return t}},e.fit=function(t,o,r,a,i,n){var s,c="obj, left, top, width, height, inside";if(s=zob(e.fit,arguments,c))return s;if(!zot(t)&&t.getBounds){if(!t.getBounds())return void zog("zim create - fit(): please setBounds() on object");if(zot(o)){if(!t.getStage())return void zog("zim create - fit(): please add boundary dimensions or add obj to stage first");if(!t.getStage().getBounds())return void zog("zim create - fit(): please add boundary dimensions or add obj with bounds to stage first");var l=t.getStage().getBounds().width,d=t.getStage().getBounds().height;o=0,r=0,a=l,i=d}zot(n)&&(n=!0),t.scaleX=t.scaleY=1;var u,g=a,h=i,z=t.getBounds().width,f=t.getBounds().height;u=n?g/h>=z/f?h/f:g/z:g/h>=z/f?g/z:h/f,t.scaleX=t.scaleY=u;var p=z*u,x=f*u;return t.x=o+(g-p)/2,t.y=r+(h-x)/2,{x:t.x,y:t.y,width:p,height:x,scale:u,bX:o,bY:r,bWidth:a,bHeight:i}}},e.outline=function(t,o,r){var a,i="obj, color, size";if(a=zob(e.outline,arguments,i))return a;if(zot(t)||!t.getBounds)return void zog("zim create - outline(): please provide object and shape");if(!t.getBounds())return void zog("zim create - outline(): please setBounds() on object");if(!t.parent)return void zog("zim create - outline(): object should be on stage first");zot(o)&&(o="brown"),zot(r)&&(r=2);var n=t.getBounds(),s=new createjs.Shape,c=t.parent,l=t.localToLocal(n.x,n.y,c),d=t.localToLocal(n.x+n.width,n.y,c),u=t.localToLocal(n.x+n.width,n.y+n.height,c),g=(t.localToLocal(n.x,n.y+n.height,c),s.graphics);g.s(o).ss(r).r(l.x,l.y,d.x-l.x,u.y-d.y),zero={x:l.x-n.x*t.scaleX,y:l.y-n.y*t.scaleY};var h=10,z=h+1;return g.s("white").ss(r+2),g.mt(zero.x-z,zero.y+0).lt(zero.x+z,zero.y+0),g.mt(zero.x+0,zero.y-z).lt(zero.x+0,zero.y+z),g.s(o).ss(r),g.mt(zero.x-h,zero.y+0).lt(zero.x+h,zero.y+0),g.mt(zero.x+0,zero.y-h).lt(zero.x+0,zero.y+h),g.s("white").ss(r+2).dc(t.x,t.y,h+6),g.s(o).ss(r).dc(t.x,t.y,h+6),t.parent.addChild(s),t.getStage()&&t.getStage().update(),t},e.centerReg=function(t,o){var r,a="obj, container";if(r=zob(e.centerReg,arguments,a))return r;if(zot(t)||!t.getBounds)return void zog("zim create - centerReg(): please provide object with bounds set");if(!zot(o)){if(!o.getBounds)return void zog("zim create - centerReg(): please provide container with bounds set");t.x=o.getBounds().width/2,t.y=o.getBounds().height/2}var i=t.getBounds();return t.regX=i.x+i.width/2,t.regY=i.y+i.height/2,t},e.expand=function(e,t,o){if(zot(e)||!e.getBounds)return void zog("zim create - expand(): please provide object with bounds set");zot(t)&&(t=20),zot(o)&&(o=t);var r=e.getBounds(),a=new createjs.Shape;return a.graphics.f("0").r(r.x-t,r.y-o,r.width+2*t,r.height+2*o),e.hitArea=a,e},e}(zim||{});