var __slice=Array.prototype.slice;!function(t){var e;t.fn.sketch=function(){var o,i,n;if(i=arguments[0],o=2<=arguments.length?__slice.call(arguments,1):[],this.length>1&&t.error("Sketch.js can only be called on one element at a time."),n=this.data("sketch"),"string"==typeof i&&n){if(!n[i])return t.error("Sketch.js did not recognize the given command.");if("function"==typeof n[i])return n[i].apply(n,o);if(0===o.length)return n[i];if(1===o.length)return n[i]=o[0]}else if(n)return n;else return this.data("sketch",new e(this.get(0),i)),this},e=function(){function e(e,o){this.el=e,this.canvas=t(e),this.context=e.getContext("2d"),this.options=t.extend({toolLinks:!0,defaultTool:"marker",defaultColor:"#00f",defaultSize:2},o),this.painting=!1,this.color=this.options.defaultColor,this.size=this.options.defaultSize,this.tool=this.options.defaultTool,this.actions=[],this.action=[],this.canvas.bind("click mousedown mouseup mousemove mouseleave mouseout touchstart touchmove touchend touchcancel",this.onEvent),this.options.toolLinks&&t("body").delegate('a[href="#'+this.canvas.attr("id")+'"]',"click",function(e){var o,i,n,s,a,r,h;for(a=0,i=t(this),s=(o=t(i.attr("href"))).data("sketch"),r=(h=["color","size","tool"]).length;a<r;a++)n=h[a],i.attr("data-"+n)&&s.set(n,t(this).attr("data-"+n));return t(this).attr("data-download")&&s.download(t(this).attr("data-download")),t(this).attr("data-save")&&s.save(t(this).attr("data-save")),t(this).attr("data-eraser")&&s.stopPainting(),!1})}return e.prototype.download=function(t){var e;return t||(t="png"),"jpg"===t&&(t="jpeg"),e="image/"+t,window.open(this.el.toDataURL(e))},e.prototype.save=function(e){jQuery("#"+e).val(this.el.toDataURL("image/png")),t(".btn-save-sign").trigger("click")},e.prototype.set=function(t,e){return this[t]=e,this.canvas.trigger("sketch.change"+t,e)},e.prototype.startPainting=function(){return this.painting=!0,this.action={tool:this.tool,color:this.color,size:parseFloat(this.size),events:[]}},e.prototype.stopPainting=function(){return this.action&&this.actions.push(this.action),this.painting=!1,this.action=null,this.redraw()},e.prototype.onEvent=function(e){return e.originalEvent&&e.originalEvent.targetTouches&&e.originalEvent.targetTouches.length>0&&(e.pageX=e.originalEvent.targetTouches[0].pageX,e.pageY=e.originalEvent.targetTouches[0].pageY),t.sketch.tools[t(this).data("sketch").tool].onEvent.call(t(this).data("sketch"),e),e.preventDefault(),!1},e.prototype.redraw=function(){var e;if(this.el.width=this.canvas.width(),this.context=this.el.getContext("2d"),e=this,t.each(this.actions,function(){if(this.tool)return t.sketch.tools[this.tool].draw.call(e,this)}),this.painting&&this.action)return t.sketch.tools[this.action.tool].draw.call(e,this.action)},e}(),t.sketch={tools:{}},t.sketch.tools.marker={onEvent:function(t){switch(t.type){case"mousedown":case"touchstart":this.startPainting();break;case"mouseup":case"mouseout":case"mouseleave":case"touchend":case"touchcancel":this.stopPainting()}if(this.painting)return this.action.events.push({x:t.pageX-this.canvas.offset().left,y:t.pageY-this.canvas.offset().top,event:t.type}),this.redraw()},draw:function(t){var e,o,i,n,s;for(this.context.lineJoin="round",this.context.lineCap="round",this.context.beginPath(),this.context.moveTo(t.events[0].x,t.events[0].y),s=t.events,i=0,n=s.length;i<n;i++)e=s[i],this.context.lineTo(e.x,e.y),o=e;return this.context.strokeStyle=t.color,this.context.lineWidth=t.size,this.context.stroke()}},t.sketch.tools.eraser={onEvent:function(e){return t.sketch.tools.marker.onEvent.call(this,e)},draw:function(e){var o;return o=this.context.globalCompositeOperation,this.context.globalCompositeOperation="copy",e.color="rgba(0,0,0,0)",t.sketch.tools.marker.draw.call(this,e),this.context.globalCompositeOperation=o}}}(jQuery);