/*!
jCanvas v5.0
Copyright 2011, Caleb Evans
Licensed under the MIT license
*/
(function(j,u,n,s,A,b,r,l){var o,i,m=j.fn,k=j.extend,B=n.PI,C=n.round,a=n.sin,e=n.cos,c,d;function y(E){if(!E){i=k({},o)}else{i=k({},i,E)}return this}o={width:0,height:0,cornerRadius:0,fillStyle:"transparent",strokeStyle:"transparent",strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",rounded:false,shadowX:0,shadowY:0,shadowBlur:3,shadowColor:"transparent",opacity:1,compositing:"source-over",x:0,y:0,x1:0,y1:0,scaleX:1,scaleY:1,start:0,end:360,ccw:false,inDegrees:true,fromCenter:true,closed:false,sides:3,angle:0,text:"",font:"normal 12pt sans-serif",align:"center",baseline:"middle",source:"",repeat:"repeat"};i=k({},o);function w(E,F){E.fillStyle=F.fillStyle;E.strokeStyle=F.strokeStyle;E.lineWidth=F.strokeWidth;if(F.rounded){E.lineCap="round";E.lineJoin="round"}else{E.lineCap=F.strokeCap;E.lineJoin=F.strokeJoin}E.shadowOffsetX=F.shadowX;E.shadowOffsetY=F.shadowY;E.shadowBlur=F.shadowBlur;E.shadowColor=F.shadowColor;E.globalAlpha=F.opacity;E.globalCompositeOperation=F.compositing}function g(E,F){if(F.mask){E.save();E.clip()}if(F.closed){E.closePath();E.fill();E.stroke()}else{E.fill();E.stroke();E.closePath()}}function f(E){return E.inDegrees?B/180:1}function x(F,H,G,E){H.toRad=f(H);F.save();if(!H.fromCenter){H.x+=G/2;H.y+=E/2}if(H.angle){F.translate(H.x,H.y);F.rotate(H.angle*H.toRad);F.translate(-H.x,-H.y)}}function z(E){E=E||{};o=k(o,E.props||{});i=k({},o);if(E.name){j.fn[E.name]=function(G){var K=this,H,F,I,J=k({},i,G);for(I=0;I<K.length;I+=1){H=K[I];if(!H.getContext){continue}F=H.getContext("2d");w(F,J);J.toRad=f(J);E.fn.call(H,F,J)}return K}}return j.fn[E.name]}j.fn.jCanvas=y;j.fn.loadCanvas=function(E){if(!this[0].getContext){return null}return this[0].getContext(E||"2d")};j.fn.getCanvasImage=function(E){if(!this[0].toDataURL){return null}if(E===l){E="image/png"}else{E=E.replace(/^([a-z]+)$/gi,"image/$1").replace(/jpg/gi,"jpeg")}return this[0].toDataURL(E)};j.fn.draw=function(G){var F=this,E;for(E=0;E<F.length;E+=1){if(!F[E].getContext){continue}G.call(F[E],F[E].getContext("2d"))}return this};j.fn.gradient=function(F){if(!this[0].getContext){return null}var E=this[0].getContext("2d"),K=k({},i,F),J,I,H=0,G=1;if(K.r1!==l||K.r2!==l){J=E.createRadialGradient(K.x1,K.y1,K.r1,K.x2,K.y2,K.r2)}else{J=E.createLinearGradient(K.x1,K.y1,K.x2,K.y2)}while(K["c"+G]!==l){H+=1;G+=1}for(G=1;G<=H;G+=1){I=C(100/(H-1)*(G-1))/100;if(K["s"+G]===l){K["s"+G]=I}J.addColorStop(K["s"+G],K["c"+G])}return J};j.fn.pattern=function(G){if(!this[0].getContext){return null}var E=this[0].getContext("2d"),K=k({},i,G),F=new A(),I;F.src=K.source;function H(){if(F.complete){I=E.createPattern(F,K.repeat);return true}else{return false}}function J(){H();if(K.load){K.load.call(this[0],I)}}if(K.load){F.onload=J}else{if(!H()){F.onload=J}}return I};j.fn.clearCanvas=function(F){var E,G,H=k({},i,F);for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");x(E,H,H.width,H.height);if(!H.width&&!H.height){E.clearRect(0,0,this[G].width,this[G].height)}else{E.clearRect(H.x-H.width/2,H.y-H.height/2,H.width,H.height)}}return this};j.fn.saveCanvas=function(){var E;for(E=0;E<this.length;E+=1){if(!this[E].getContext){continue}this[E].getContext("2d").save()}return this};j.fn.restoreCanvas=function(){var E;for(E=0;E<this.length;E+=1){if(!this[E].getContext){continue}this[E].getContext("2d").restore()}return this};j.fn.scaleCanvas=function(F){var E,G,H=k({},i,F);H.width=H.width||1;H.height=H.height||1;for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");E.save();E.translate(H.x,H.y);E.scale(H.scaleX,H.scaleY);E.translate(-H.x,-H.y)}return this};j.fn.translateCanvas=function(F){var E,G,H=k({},i,F);for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");E.save();E.translate(H.x,H.y)}return this};j.fn.rotateCanvas=function(F){var E,G,H=k({},i,F);for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");x(E,H,0,0)}return this};j.fn.drawRect=function(J){var M,I,H=k({},i,J),G,L,F,K,E;for(I=0;I<this.length;I+=1){if(!this[I].getContext){continue}M=this[I].getContext("2d");w(M,H);x(M,H,H.width,H.height);M.beginPath();if(H.cornerRadius){H.closed=true;G=H.x-H.width/2;L=H.y-H.height/2;F=H.x+H.width/2;K=H.y+H.height/2;E=H.cornerRadius;if((F-G)-(2*E)<0){E=(F-G)/2}if((K-L)-(2*E)<0){E=(K-L)/2}M.moveTo(G+E,L);M.lineTo(F-E,L);M.arc(F-E,L+E,E,3*B/2,B*2,false);M.lineTo(F,K-E);M.arc(F-E,K-E,E,0,B/2,false);M.lineTo(G+E,K);M.arc(G+E,K-E,E,B/2,B,false);M.lineTo(G,L+E);M.arc(G+E,L+E,E,B,3*B/2,false)}else{M.rect(H.x-H.width/2,H.y-H.height/2,H.width,H.height)}M.restore();g(M,H)}return this};j.fn.drawArc=function(F){var E,G,H=k({},i,F);if(!H.inDegrees&&H.end===360){H.end=B*2}for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");w(E,H);x(E,H,H.radius*2,H.radius*2);E.beginPath();E.arc(H.x,H.y,H.radius,(H.start*H.toRad)-(B/2),(H.end*H.toRad)-(B/2),H.ccw);E.restore();g(E,H)}return this};j.fn.drawEllipse=function(F){var E,H,I=k({},i,F),G=I.width*4/3;for(H=0;H<this.length;H+=1){if(!this[H].getContext){continue}E=this[H].getContext("2d");w(E,I);x(E,I,I.width,I.height);E.beginPath();E.moveTo(I.x,I.y-I.height/2);E.bezierCurveTo(I.x-G/2,I.y-I.height/2,I.x-G/2,I.y+I.height/2,I.x,I.y+I.height/2);E.bezierCurveTo(I.x+G/2,I.y+I.height/2,I.x+G/2,I.y-I.height/2,I.x,I.y-I.height/2);E.restore();g(E,I)}return this};j.fn.drawLine=function(G){var F,J,K=k({},i,G),E=2,I=0,H=0;for(J=0;J<this.length;J+=1){if(!this[J].getContext){continue}F=this[J].getContext("2d");w(F,K);F.beginPath();F.moveTo(K.x1,K.y1);while(I!==l&&H!==l){I=K["x"+E];H=K["y"+E];F.lineTo(I,H);E+=1}g(F,K)}return this};j.fn.drawQuad=function(J){var M,I,H=k({},i,J),G=2,F=0,E=0,L=0,K=0;for(I=0;I<this.length;I+=1){if(!this[I].getContext){continue}M=this[I].getContext("2d");w(M,H);M.beginPath();M.moveTo(H.x1,H.y1);while(F!==l&&E!==l&&L!==l&&K!==l){F=H["x"+G];E=H["y"+G];L=H["cx"+(G-1)];K=H["cy"+(G-1)];M.quadraticCurveTo(L,K,F,E);G+=1}g(M,H)}return this};j.fn.drawBezier=function(O){var P,N,K=k({},i,O),J=2,G=1,I=0,H=0,M=0,F=0,L=0,E=0;for(N=0;N<this.length;N+=1){if(!this[N].getContext){continue}P=this[N].getContext("2d");w(P,K);P.beginPath();P.moveTo(K.x1,K.y1);while(I!==l&&H!==l&&M!==l&&F!==l&&L!==l&&E!==l){I=K["x"+J];H=K["y"+J];M=K["cx"+G];F=K["cy"+G];L=K["cx"+(G+1)];E=K["cy"+(G+1)];P.bezierCurveTo(M,F,L,E,I,H);J+=1;G+=2}g(P,K)}return this};j.fn.drawText=function(F){var E,G,H=k({},i,F);for(G=0;G<this.length;G+=1){if(!this[G].getContext){continue}E=this[G].getContext("2d");w(E,H);E.textBaseline=H.baseline;E.textAlign=H.align;E.font=H.font;E.strokeText(H.text,H.x,H.y);E.fillText(H.text,H.x,H.y)}return this};j.fn.drawImage=function(K){var M,F,J,H=k({},i,K),I=new A(),E;I.src=H.source;function L(N){if(I.complete){E=(I.width/I.height);if(H.sx===l){H.sx=I.width/2}if(H.sy===l){H.sy=I.height/2}H.sWidth=H.sWidth||I.width;H.sHeight=H.sHeight||I.height;if(H.width&&!H.height){H.height=H.width/E}else{if(!H.width&&H.height){H.width=H.height*E}else{if(!H.width&&!H.height){H.width=I.width;H.height=I.height}}}x(N,H,H.width,H.height);N.drawImage(I,H.sx-H.sWidth/2,H.sy-H.sHeight/2,H.sWidth,H.sHeight,H.x-H.width/2,H.y-H.height/2,H.width,H.height);N.restore();return true}else{return false}}function G(){L(M);if(H.load){H.load.call(F)}}for(J=0;J<this.length;J+=1){F=this[J];if(!F.getContext){continue}M=F.getContext("2d");w(M,H);if(H.load){I.onload=G}else{if(!L(M)){I.onload=G}}}return this};j.fn.drawPolygon=function(K){var P,J,H=k({},i,K),Q=B/H.sides,G=(B/2)+Q,O=(B*2)/H.sides,N=e(O/2)*H.radius,F,M,E,L,I;H.closed=true;if(H.sides>2){for(J=0;J<this.length;J+=1){if(!this[J].getContext){continue}P=this[J].getContext("2d");w(P,H);x(P,H,H.radius,H.radius);P.beginPath();for(I=0;I<H.sides;I+=1){F=H.x+C(H.radius*e(G));M=H.y+C(H.radius*a(G));if(I===0){P.moveTo(F,M)}else{P.lineTo(F,M)}if(H.projection){E=H.x+C((N+N*H.projection)*e(G+Q));L=H.y+C((N+N*H.projection)*a(G+Q));P.lineTo(E,L)}G+=O}P.restore();g(P,H)}}return this};j.fn.setPixels=function(L){var N,F,K,I,G=k({},i,L),E,H,J,M;for(K=0;K<this.length;K+=1){F=this[K];if(!F.getContext){continue}N=F.getContext("2d");if(!G.x&&!G.y&&!G.width&&!G.height){G.width=F.width;G.height=F.height;G.x=G.width/2;G.y=G.height/2}x(N,G,G.width,G.height);E=N.getImageData(G.x-G.width/2,G.y-G.height/2,G.width,G.height);H=E.data;J=H.length;M=[];if(G.each!==l){for(I=0;I<J;I+=4){M=G.each.call(F,H[I],H[I+1],H[I+2],H[I+3]);H[I]=M[0];H[I+1]=M[1];H[I+2]=M[2];H[I+3]=M[3]}}N.putImageData(E,G.x-G.width/2,G.y-G.height/2);N.restore()}return this};function t(F,G){var E;for(E=0;E<F.length;E+=1){G[F[E]]=G["_"+F[E]]}}function v(F,G){var E;for(E=0;E<F.length;E+=1){G["_"+F[E]]=G[F[E]]}}c=["width","height","opacity"];d=["backgroundColor","color","borderColor","borderTopColor","borderRightColor","borderBottomColor","borderLeftColor","fillStyle","strokeStyle","shadowColor"];function h(F){var H,I,G=[],E=1;if(typeof F==="object"){G=F}else{if(F.match(/^[a-z]+$/gi)){if(F==="transparent"){F="rgba(255, 255, 255, 0)"}I=u.documentElement;H=I.style.color;I.style.color=F;F=j.css(I,"color");I.style.color=H}if(F.match(/^\#/gi)){if(F.length===4){F=F.replace(/(\w)/gi,"$1$1")}G=F.match(/[0-9a-f]{2}/gi);G[0]=r(G[0],16);G[1]=r(G[1],16);G[2]=r(G[2],16)}else{if(F.match(/^rgb/gi)){G=F.match(/[0-9\.]+/gi);if(F.match(/\%/gi)){E=2.55}G[0]=b(G[0])*E;G[1]=b(G[1])*E;G[2]=b(G[2])*E}}if(F.indexOf("rgba")!==-1){G[3]=b(G[3])}else{G[3]=1}}return G}function D(F,E){F.now[E]=F.start[E]+(F.end[E]-F.start[E])*F.pos;if(E<3){F.now[E]=C(F.now[E])}}function p(E){if(E.pos===0){E.start=h(E.start),E.end=h(E.end)}E.now=[];D(E,0);D(E,1);D(E,2);D(E,3);E.now="rgba("+E.now.join(",")+")";if(E.elem.style){E.elem.style[E.prop]=E.now}else{E.elem[E.prop]=E.now}}function q(E){var F;for(F=0;F<E.length;F+=1){if(!j.fx.step[E[F]]){j.fx.step[E[F]]=p}}}j.fn.getLayers=function(){var E=this[0],F;if(!E||!E.getContext){return[]}F=j.data(E,"layers");if(F===l){F=[];j.data(E,"layers",F)}return F};j.fn.getLayer=function(E){E=E||0;return this.getLayers()[E]};j.fn.addLayer=function(G){var J=this,F,I,E,H;G=k(G,i,j.extend({},G));G.method=G.fn||G.method;for(H=0;H<J.length;H+=1){F=j(J[H]);if(!J[H].getContext){continue}I=F.getLayers();if(typeof G==="function"){G.method="draw"}I.push(G)}return J};j.fn.removeLayer=function(E){E=E||0;this.getLayers().splice(E,1);return this};j.fn.drawLayers=function(){var K=this,F,E,J,I,H,G;for(H=0;H<K.length;H+=1){F=j(K[H]);if(!F[0].getContext){continue}E=F[0].getContext("2d");I=F.getLayers();E.clearRect(0,0,F[0].width,F[0].height);for(G=0;G<I.length;G+=1){J=I[G];if(J.method==="draw"){J.call(F[0],E)}else{if(m[J.method]){m[J.method].call(F,J)}}}}return K};j.fn.animateLayer=function(){var J=this,F=s.prototype.slice.call(arguments,0),E,I,G,H;if(typeof F[0]==="object"){F.unshift(0)}if(F[2]===l){F.splice(2,0,null);F.splice(3,0,null);F.splice(4,0,function(){})}else{if(typeof F[2]==="function"){F.splice(2,0,null);F.splice(3,0,null)}}if(F[3]===l){F[3]=null;F.splice(4,0,function(){})}else{if(typeof F[3]==="function"){F.splice(3,0,null)}}if(F[4]===l){F[4]=function(){}}for(H=0;H<J.length;H+=1){E=j(J[H]);I=E.getLayers();G=I[F[0]];if(!G||G.method==="draw"){continue}G=k(G,i,j.extend({},G));v(c,G);v(c,F[1]);j(G).animate(F[1],{duration:F[2],easing:(j.easing[F[3]]?F[3]:null),complete:(function(K){return function(){t(c,G);K.drawLayers(true);F[4].call(K[0])}}(E)),step:(function(K,L){return function(){t(c,L);K.drawLayers(true)}}(E,G))})}return J};q(d);y.defaults=o;y.prefs=i;y.extend=z;j.jCanvas=y}(jQuery,document,Math,Array,Image,parseFloat,parseInt));
