(function($){var l=function(a,b){b=$.extend({defaultValue:0},b);if(typeof a=="number"){return[String(a),String(a),String(a),String(a)]}if(typeof a=="string"){var c=a.split(/[, ]/g);if(c.length==1){return[c[0],c[0],c[0],c[0]]}if(c.length==2){return[c[0],c[1],c[0],c[1]]}if(c.length==3){return[c[0],c[1],c[2],c[1]]}return c.slice(0,4)}return[b.defaultValue,b.defaultValue,b.defaultValue,b.defaultValue]};$.registerLiquidCanvasPlugin({name:"rect",paint:function(a){a.ctx.beginPath();a.ctx.rect(0,0,a.width,a.height);a.ctx.closePath();if(this.action){this.action.paint(a)}}});$.registerLiquidCanvasPlugin({name:"roundedRect",defaultOpts:{radius:20},paint:function(a){var b=a.ctx;var c=this.opts;b.beginPath();b.moveTo(0,c.radius);b.lineTo(0,a.height-c.radius);b.quadraticCurveTo(0,a.height,c.radius,a.height);b.lineTo(a.width-c.radius,a.height);b.quadraticCurveTo(a.width,a.height,a.width,a.height-c.radius);b.lineTo(a.width,c.radius);b.quadraticCurveTo(a.width,0,a.width-c.radius,0);b.lineTo(c.radius,0);b.quadraticCurveTo(0,0,0,c.radius);b.closePath();if(this.action){this.action.paint(a)}},shrink:function(a,b){this.defaultShrink(a,b);this.opts.radius-=b}});$.registerLiquidCanvasPlugin({name:"ellipse",defaultOpts:{},paint:function(a){var b=a.ctx,rx=a.width/2,ry=a.height/2,k=4*(Math.sqrt(2)-1)/3;b.beginPath();b.moveTo(rx,0);b.bezierCurveTo(rx+k*rx,0,rx*2,ry-k*ry,rx*2,ry);b.bezierCurveTo(rx*2,ry+k*ry,rx+k*rx,ry*2,rx,ry*2);b.bezierCurveTo(rx-k*rx,ry*2,0,ry+k*ry,0,ry);b.bezierCurveTo(0,ry-k*ry,rx-k*rx,0,rx,0);b.closePath();if(this.action){this.action.paint(a)}},shrink:function(a,b){this.defaultShrink(a,b);this.opts.radius-=b}});$.registerLiquidCanvasPlugin({name:"effectRect",defaultOpts:{radius:20,effect:"round",arrow:0},paint:function(g){var i,ctx=g.ctx,arrowmargin,arrow={},radius=l(this.opts.radius),effect=l(String(this.opts.effect).toLowerCase(),{defaultValue:"round"}),arrowprops=String(this.opts.arrow).toLowerCase().split(/[, ]/g);for(i=0;i<arrowprops.length;i++){if($.inArray(arrowprops[i],["start","mid","end"])>=0){arrow.position=arrowprops[i]}else if($.inArray(arrowprops[i],["top","right","bottom","left"])>=0){arrow.side=arrowprops[i]}else if(!isNaN(Number(arrowprops[i]))){if(typeof arrow.width=="undefined"){arrow.width=Number(arrowprops[i])}else if(typeof arrow.height=="undefined"){arrow.height=Number(arrowprops[i])}else if(typeof arrow.offset=="undefined"){arrow.offset=Number(arrowprops[i])}}}if(typeof arrow.width=="undefined"||arrow.width===0||arrow.height===0){arrow=null;arrowmargin=[0,0,0,0]}else{if(typeof arrow.side=="undefined"){arrow.side="bottom"}if(typeof arrow.position=="undefined"){arrow.position="start"}if(typeof arrow.height=="undefined"){arrow.height=arrow.width}if(typeof arrow.offset=="undefined"){arrow.offset=0}arrowmargin=[arrow.side=="top"?arrow.height:0,arrow.side=="right"?arrow.width:0,arrow.side=="bottom"?arrow.height:0,arrow.side=="left"?arrow.width:0]}var h=function(b,c,d){var e=function(a){switch(d){case"top":ctx.lineTo(a.x+arrow.width/2,a.y-arrow.height);ctx.lineTo(a.x+arrow.width,a.y);break;case"right":ctx.lineTo(a.x+arrow.width,a.y+arrow.height/2);ctx.lineTo(a.x,a.y+arrow.height);break;case"bottom":ctx.lineTo(a.x-arrow.width/2,a.y+arrow.height);ctx.lineTo(a.x-arrow.width,a.y);break;case"left":ctx.lineTo(a.x-arrow.width,a.y-arrow.height/2);ctx.lineTo(a.x,a.y-arrow.height);break}};if(arrow===null||arrow.side!=d){ctx.lineTo(c.x,c.y)}else{var f;switch(arrow.position){case"start":switch(d){case"top":f={x:b.x+(c.x-b.x-arrow.width)/2+arrow.offset,y:b.y};break;case"right":f={x:b.x,y:b.y+(c.y-b.y-arrow.height)/2+arrow.offset};break;case"bottom":f={x:b.x-(b.x-c.x-arrow.width)/2-arrow.offset,y:b.y};break;case"left":f={x:b.x,y:b.y-(b.y-c.y-arrow.height)/2-arrow.offset};break}break;case"mid":switch(d){case"top":f={x:b.x+(c.x-b.x-arrow.width)/2,y:b.y};break;case"right":f={x:b.x,y:b.y+(c.y-b.y-arrow.height)/2};break;case"bottom":f={x:b.x-(b.x-c.x-arrow.width)/2,y:b.y};break;case"left":f={x:b.x,y:b.y-(b.y-c.y-arrow.height)/2};break}break;case"end":switch(d){case"top":f={x:c.x-arrow.width-arrow.offset,y:b.y};break;case"right":f={x:b.x,y:c.y-arrow.height-arrow.offset};break;case"bottom":f={x:c.x+arrow.width+arrow.offset,y:b.y};break;case"left":f={x:b.x,y:c.y+arrow.height+arrow.offset};break}break}ctx.lineTo(f.x,f.y);e(f);ctx.lineTo(c.x,c.y)}};var j=g.width<g.height?g.width:g.height;for(i=0;i<radius.length;i++){if(radius[i].substr(radius[i].length-1)=="%"){radius[i]=j*Number(radius[i].replace(/\s*%$/,""))/100}else{radius[i]=Number(radius[i])}}ctx.beginPath();ctx.moveTo(arrowmargin[3],radius[0]+arrowmargin[0]);if(radius[0]>0){switch(effect[0]){case"round":ctx.quadraticCurveTo(arrowmargin[3],arrowmargin[0],radius[0]+arrowmargin[3],arrowmargin[0]);break;case"neground":ctx.quadraticCurveTo(radius[0],radius[0],radius[0]+arrowmargin[3],arrowmargin[0]);break;case"straight":ctx.lineTo(radius[0]+arrowmargin[3],arrowmargin[0]);break}}h({x:radius[0]+arrowmargin[3],y:arrowmargin[0]},{x:g.width-radius[1]-arrowmargin[1],y:arrowmargin[0]},"top");if(radius[1]>0){switch(effect[1]){case"round":ctx.quadraticCurveTo(g.width-arrowmargin[1],arrowmargin[0],g.width-arrowmargin[1],radius[1]+arrowmargin[0]);break;case"neground":ctx.quadraticCurveTo(g.width-radius[1]-arrowmargin[1],radius[1]+arrowmargin[0],g.width-arrowmargin[1],radius[1]+arrowmargin[0]);break;case"straight":ctx.lineTo(g.width-arrowmargin[1],radius[1]+arrowmargin[0]);break}}h({x:g.width-arrowmargin[1],y:radius[1]+arrowmargin[0]},{x:g.width-arrowmargin[1],y:g.height-radius[2]-arrowmargin[2]},"right");if(radius[2]>0){switch(effect[2]){case"round":ctx.quadraticCurveTo(g.width-arrowmargin[1],g.height-arrowmargin[2],g.width-radius[2]-arrowmargin[1],g.height-arrowmargin[2]);break;case"neground":ctx.quadraticCurveTo(g.width-radius[2]-arrowmargin[1],g.height-radius[2]-arrowmargin[2],g.width-radius[2]-arrowmargin[1],g.height-arrowmargin[2]);break;case"straight":ctx.lineTo(g.width-radius[2]-arrowmargin[1],g.height-arrowmargin[2]);break}}h({x:g.width-radius[2]-arrowmargin[1],y:g.height-arrowmargin[2]},{x:radius[3]+arrowmargin[3],y:g.height-arrowmargin[2]},"bottom");if(radius[3]>0){switch(effect[3]){case"round":ctx.quadraticCurveTo(arrowmargin[3],g.height-arrowmargin[2],arrowmargin[3],g.height-radius[3]-arrowmargin[2]);break;case"neground":ctx.quadraticCurveTo(radius[3]+arrowmargin[3],g.height-radius[3]-arrowmargin[2],arrowmargin[3],g.height-radius[3]-arrowmargin[2]);break;case"straight":ctx.lineTo(arrowmargin[3],g.height-radius[3]-arrowmargin[2]);break}}h({x:arrowmargin[3],y:g.height-radius[3]-arrowmargin[2]},{x:arrowmargin[3],y:radius[0]+arrowmargin[0]},"left");ctx.closePath();if(this.action){this.action.paint(g)}},shrink:function(a,b){this.defaultShrink(a,b);if(this.opts.radius>0){this.opts.radius-=b}}});$.registerLiquidCanvasPlugin({name:"arrowRoundRect",defaultOpts:{radius:20},paint:function(a){var b=a.ctx;var c=this.opts;b.beginPath();b.moveTo(0,c.radius);b.lineTo(0,a.height-c.radius);b.quadraticCurveTo(0,a.height,c.radius,a.height);b.lineTo(a.width-c.radius,a.height);b.lineTo(a.width,a.height-c.radius);b.lineTo(a.width,c.radius);b.lineTo(a.width-c.radius,0);b.lineTo(c.radius,0);b.quadraticCurveTo(0,0,0,c.radius);b.closePath();if(this.action){this.action.paint(a)}},shrink:function(a,b){this.defaultShrink(a,b);this.opts.radius-=b}});$.registerLiquidCanvasPlugin({name:"dualcolor",defaultOpts:{mode:"horizontal",from:"#fff",to:"#666"},paint:function(a){var b=a.ctx.createLinearGradient(0,0,0,a.height);b.addColorStop(0,this.opts.from);b.addColorStop(0.499,this.opts.from);b.addColorStop(0.5,this.opts.to);b.addColorStop(1,this.opts.to);a.ctx.fillStyle=b;this.action.paint(a);a.ctx.fill()}});$.registerLiquidCanvasPlugin({name:"fill",defaultOpts:{color:"#aaa"},paint:function(a){a.ctx.fillStyle=this.opts.color;this.action.paint(a);a.ctx.fill()}});$.registerLiquidCanvasPlugin({name:"image",defaultOpts:{url:"http://www.ruzee.com/files/liquid-canvas-image.png"},paint:function(a){var b=new Image();b.src=this.opts.url;b.onload=function(){a.ctx.drawImage(this,0,0)}}});$.registerLiquidCanvasPlugin({name:"gradient",defaultOpts:{from:"#fff",to:"#666"},paint:function(a){var b=a.ctx.createLinearGradient(0,0,0,a.height);b.addColorStop(0,this.opts.from);b.addColorStop(1,this.opts.to);a.ctx.fillStyle=b;this.action.paint(a);a.ctx.fill()}});$.registerLiquidCanvasPlugin({name:"shadow",defaultOpts:{width:3,color:'#000',shift:2},paint:function(a){var b=this.opts.width;a.ctx.fillStyle=this.opts.color;a.ctx.globalAlpha=1.0/b;for(var s=0;s<b;++s){this.action.paint(a);a.ctx.fill();this.action.shrink(a,1)}a.ctx.globalAlpha=1;a.ctx.translate(0,-this.opts.shift)}});$.registerLiquidCanvasPlugin({name:"border",defaultOpts:{color:'#8f4',width:3},paint:function(a){var b=this.opts.width;a.ctx.strokeStyle=this.opts.color;a.ctx.lineWidth=b;this.action.shrink(a,b/2);this.action.paint(a);a.ctx.stroke();this.action.shrink(a,b/2)}});$.registerLiquidCanvasPlugin({name:"movesize",defaultOpts:{x:0,y:0,w:0,h:0},paint:function(a){a.ctx.translate(Number(this.opts.x),Number(this.opts.y));a.width+=Number(this.opts.w);a.height+=Number(this.opts.h)}})})(jQuery);
