function Desenho(e,l){var j=this;var p=false;var I=false;var o=false;var t="";var h=[];var i=e.getContext("2d");var F,C,a;var E=[i];var d=e.offsetWidth;var v=e.offsetHeight;var g=1;var G=4;var D=0;var f=["rgba(0,0,0,0)","#000","#666","#8b0000","#008b00","#00008b","#008b8b","#8b8b00","#8b4500","#8b0a50","#551a8b","#548b54","#8b6969","#8b7b8b","#fff","#ccc","#ff0000","#00ff00","#0000ff","#00ffff","#ffff00","#ff7f00","#ff1493","#9b30ff","#9aff9a","#ffc1c1","#ffe1ff"];var H;var r;var b;var w;var x=new Array();var J=new Array();this.onSelCor=null;this.onCodigo=null;this.onStartDraw=null;function y(K){return document.getElementById(K)}this.borracha=function(N,M,O,P){N-=G*3;M-=G*3;E[O].clearRect(N,M,G*6+1,G*6+1);for(var K=N;K<=N+G*6;K++){for(var L=M;L<=M+G*6;L++){if(K>=0&&L>=0&&K<d&&L<v){x[K][L]=14}}}};this.linha=function(L,N,K,M,O){E[O].beginPath();E[O].lineWidth=G;E[O].moveTo(L,N);E[O].lineTo(K,M);E[O].stroke();E[O].closePath()};this.gLinha=function(L,S,K,Q){var T;var O=Math.abs(Q-S)>Math.abs(K-L);if(O){T=L;L=S;S=T;T=K;K=Q;Q=T}if(L>K){T=L;L=K;K=T;T=S;S=Q;Q=T}var V=K-L;var U=Math.abs(Q-S);var N=-V/2;var M;var P=S;if(S<Q){M=1}else{M=-1}for(var R=L;R<=K;R++){if(O){j.gElipseF(P,R,Math.floor(G/2),Math.floor(G/2))}else{j.gElipseF(R,P,Math.floor(G/2),Math.floor(G/2))}N=N+U;if(N>=0){P=P+M;N=N-V}}};this.retanguloF=function(K,M,L,O,N){E[N].fillRect(K,M,L,O)};this.gRetanguloF=function(N,Q,P,R){var M=N+P;var O=Q+R;t+="#"+N+"#"+Q+"#"+P+"#"+R;for(var K=N;K<M;K++){for(var L=Q;L<O;L++){x[K][L]=g}}};this.retanguloB=function(K,M,L,O,N){E[N].strokeRect(K,M,L,O)};this.gRetanguloB=function(L,P,S,N){var M=Math.ceil(G/2);var K=L+S+M-1;var O=P+N+M-1;L-=M;P-=M;for(var T=L;T<=K;T++){for(var R=0;R<G;R++){x[T][P+R]=g;x[T][O-R]=g}}for(var Q=P;Q<=O;Q++){for(var R=0;R<G;R++){x[L+R][Q]=g;x[K-R][Q]=g}}};this.elipseF=function(K,P,O,M,N){var L=4*((Math.sqrt(2)-1)/3);E[N].beginPath();E[N].moveTo(K,P-M);E[N].bezierCurveTo(K+(L*O),P-M,K+O,P-(L*M),K+O,P);E[N].bezierCurveTo(K+O,P+(L*M),K+(L*O),P+M,K,P+M);E[N].bezierCurveTo(K-(L*O),P+M,K-O,P+(L*M),K-O,P);E[N].bezierCurveTo(K-O,P-(L*M),K-(L*O),P-M,K,P-M);E[N].fill()};this.gElipseF=function(P,O,S,V){if(S>0&&V>0){var U;var T;var W,R;var N;var Q,L;var M,K;Q=2*S*S;L=2*V*V;U=S;T=0;W=V*V*(1-2*S);R=S*S;N=0;M=L*S;K=0;while(M>=K){for(var X=U;X>=1;X--){x[P+X-1][O+T-1]=g;x[P-X][O+T-1]=g;x[P-X][O-T]=g;x[P+X-1][O-T]=g}T++;K+=Q;N+=R;R+=Q;if((2*N+W)>0){U--;M-=L;N+=W;W+=L}}U=0;T=V;W=V*V;R=S*S*(1-2*V);N=0;M=0;K=Q*V;while(M<=K){for(var X=U;X>=1;X--){x[P+X-1][O+T-1]=g;x[P-X][O+T-1]=g;x[P-X][O-T]=g;x[P+X-1][O-T]=g}U++;M+=L;N+=W;W+=L;if((2*N+R)>0){T--;K-=Q;N+=R;R+=Q}}}};this.elipseB=function(K,P,O,M,N){var L=4*((Math.sqrt(2)-1)/3);E[N].beginPath();E[N].moveTo(K,P-M);E[N].bezierCurveTo(K+(L*O),P-M,K+O,P-(L*M),K+O,P);E[N].bezierCurveTo(K+O,P+(L*M),K+(L*O),P+M,K,P+M);E[N].bezierCurveTo(K-(L*O),P+M,K-O,P+(L*M),K-O,P);E[N].bezierCurveTo(K-O,P-(L*M),K-(L*O),P-M,K,P-M);E[N].stroke()};this.gElipseB=function(P,O,S,V){if(S>0&&V>0){var U;var T;var W,R;var N;var Q,L;var M,K;Q=2*S*S;L=2*V*V;U=S;T=0;W=V*V*(1-2*S);R=S*S;N=0;M=L*S;K=0;while(M>K){j.gElipseF(P+U,O+T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P-U,O+T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P-U,O-T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P+U,O-T,Math.floor(G/2),Math.floor(G/2));T++;K+=Q;N+=R;R+=Q;if((2*N+W)>0){U--;M-=L;N+=W;W+=L}}U=0;T=V;W=V*V;R=S*S*(1-2*V);N=0;M=0;K=Q*V;while(M<=K){j.gElipseF(P+U,O+T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P-U,O+T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P-U,O-T,Math.floor(G/2),Math.floor(G/2));j.gElipseF(P+U,O-T,Math.floor(G/2),Math.floor(G/2));U++;M+=L;N+=W;W+=L;if((2*N+R)>0){T--;K-=Q;N+=R;R+=Q}}}};function B(M,N,O){var P,R;P=N;R=O;if(x[N][O]==M){while(x[P+1][R]==M){P++}var L=P;do{P=N-1;R++;while(x[P+1][R]==M&&P+1<=L){P++}}while(P==L);R--;var K=L-N;var Q=R-O;return{x:N,y:O,w:K,h:Q}}else{return{w:-1,h:-1}}}function m(M,N,O){var P,R;P=N;R=O;if(x[N][O]==M){while(x[P-1][R]==M){P--}var L=P;do{P=N+1;R--;while(x[P-1][R]==M&&P-1>=L){P--}}while(P==L);R++;var K=N-L;var Q=O-R;return{x:L,y:R,w:K,h:Q}}else{return{w:-1,h:-1}}}function z(L,M,N){var O,R;O=M;R=N;if(x[M][N]==L){while(x[O][R+1]==L){R++}var Q=R;do{R=N-1;O--;while(x[O][R+1]==L&&R+1<=Q){R++}}while(R==Q);O++;var K=M-O;var P=Q-N;return{x:O,y:N,w:K,h:P}}else{return{w:-1,h:-1}}}function n(L,M,N){var O,R;O=M;R=N;if(x[M][N]==L){while(x[O][R-1]==L){R--}var Q=R;do{R=N+1;O++;while(x[O][R-1]==L&&R-1>=Q){R--}}while(R==Q);O--;var K=O-M;var P=N-Q;return{x:M,y:Q,w:K,h:P}}else{return{w:-1,h:-1}}}function u(K,P,M,N,O,L){this.x=K;this.y=P;this.w=M;this.h=N;this.ref=O;this.andada=L}this.balde=function(P,O){var K=x[P][O];if(g!=K){while(x[P-1][O]==K){P--}while(x[P][O-1]==K){O--}var S=B(K,P,O);var N=new u(P,O,S.w,S.h,0,0);var M;var R=S.w;var L;t="";i.fillRect(S.x,S.y,S.w+1,S.h+1);j.gRetanguloF(S.x,S.y,S.w+1,S.h+1);t=t.substr(1,t.length);do{M=0;if(N.ref==2){M+=N.andada}while(M<=N.h){S=n(K,N.x+N.w+1,N.y+N.h+1-M);L=S.h;if(L!=-1){J[L].push(new u(S.x,S.y,S.w,S.h,1,N.h+1-M));i.fillRect(S.x,S.y,S.w+1,S.h+1);j.gRetanguloF(S.x,S.y,S.w+1,S.h+1);if(L>R){R=L}M+=L}else{M++}}M=0;if(N.ref==1){M+=N.andada}while(M<=N.h){S=z(K,N.x-1,N.y+M);L=S.h;if(L!=-1){J[L].push(new u(S.x,S.y,S.w,S.h,2,N.h+1-M));i.fillRect(S.x,S.y,S.w+1,S.h+1);j.gRetanguloF(S.x,S.y,S.w+1,S.h+1);if(L>R){R=L}M+=L}else{M++}}M=0;if(N.ref==4){M+=N.andada}while(M<=N.w){S=B(K,N.x+M,N.y+N.h+1);L=S.w;if(L!=-1){J[L].push(new u(S.x,S.y,S.w,S.h,3,N.w+1-M));i.fillRect(S.x,S.y,S.w+1,S.h+1);j.gRetanguloF(S.x,S.y,S.w+1,S.h+1);if(L>R){R=L}M+=L}else{M++}}M=0;if(N.ref==3){M+=N.andada}while(M<=N.w){S=m(K,N.x+N.w+1-M,N.y-1);L=S.w;if(L!=-1){J[L].push(new u(S.x,S.y,S.w,S.h,4,N.w+1-M));i.fillRect(S.x,S.y,S.w+1,S.h+1);j.gRetanguloF(S.x,S.y,S.w+1,S.h+1);if(L>R){R=L}M+=L}else{M++}}N=J[R].pop();while(N==null&&R>0){N=J[--R].pop()}}while(N!=null);var Q="3@"+t;h.push(Q);if(j.onCodigo instanceof Function){j.onCodigo(Q)}}};function k(K){if(!I&&p){document.onmousemove=c;document.addEventListener("touchmove",c,false);document.onmouseup=s;document.addEventListener("touchend",s,false);c(K);I=true;H=b;r=w;switch(D){case 0:t=H+"#"+r;break;case 7:j.balde(H,r);I=false;break;case 8:j.mudaCor(x[H][r],true);if(j.onSelCor instanceof Function){j.onSelCor()}I=false;break;case 1:t=H+"#"+r;j.borracha(b,w,0,true);break}if(j.onStartDraw instanceof Function){j.onStartDraw()}}}function q(N){var M,L,O,K;L=O=0;if(N.getBoundingClientRect){M=N.getBoundingClientRect();L=Math.round(M.left);O=Math.round(M.top);L+=document.documentElement.scrollLeft;O+=document.documentElement.scrollTop}else{if(document.getBoxObjectFor){M=document.getBoxObjectFor(N);L=M.x;O=M.y}else{while(N&&N.nodeName!="BODY"){L+=N.offsetLeft;O+=N.offsetTop;if(window.opera){L-=1;O-=1}else{L+=1;O+=1}K=parseInt(document.defaultView.getComputedStyle(N,null).getPropertyValue("border-width"));if(K>0){L+=K;O+=K}N=N.offsetParent}}}return{x:L,y:O}}function c(L){if(L.touches){b=L.touches[0].pageX;w=L.touches[0].pageY}else{b=L.clientX;w=L.clientY}b=b+document.documentElement.scrollLeft-q(l).x;w=w+document.documentElement.scrollTop-q(l).y;var K=Math.floor(G/2);if(b>=d-K){b=d-K}else{if(b<=K){b=K}}if(w>=v-K){w=v-K}else{if(w<=K){w=K}}if(I&&p){if(D==0){if(t.length>230){var M="2@"+t;h.push(M);if(j.onCodigo instanceof Function){j.onCodigo(M)}t=H+"#"+r}j.linha(H,r,b,w,0);j.gLinha(H,r,b,w);H=b;r=w;t+="#"+H+"#"+r}else{if(D==1){if(t.length>230){var M="21@"+t;h.push(M);if(j.onCodigo instanceof Function){j.onCodigo(M)}t=b+"#"+w}j.borracha(b,w,0,true);t+="#"+b+"#"+w}else{F.clearRect(0,0,d,v);j.desenhar(H,r,b,w,D,1,false)}}}L.preventDefault();L.stopPropagation()}this.desenhar=function(N,Y,L,V,S,X,Q){if(N==L&&Y==V){return}var W,U,T,R;if(N>=L){W=L;U=N}else{W=N;U=L}if(Y>=V){T=V;R=Y}else{T=Y;R=V}switch(S){case 6:j.linha(N,Y,L,V,X);if(Q){j.gLinha(N,Y,L,V)}break;case 2:if(W==U||T==R){Q=false;break}j.retanguloF(W,T,U-W,R-T,X);if(Q){j.gRetanguloF(W,T,U-W,R-T)}break;case 3:j.retanguloB(W,T,U-W,R-T,X);if(Q){j.gRetanguloB(W,T,U-W,R-T)}break;case 4:if(W==U||T==R){Q=false;break}var M=Math.floor((U-W)/2);var K=Math.floor((R-T)/2);var P=Math.round(W+M);var O=Math.round(T+K);j.elipseF(P,O,M,K,X);if(Q){j.gElipseF(P,O,M,K)}break;case 5:var M=Math.floor((U-W)/2);var K=Math.floor((R-T)/2);var P=Math.round(W+M);var O=Math.round(T+K);j.elipseB(P,O,M,K,X);if(Q){j.gElipseB(P,O,M,K)}break}if(Q){var Z="1@"+S+"#"+N+"#"+Y+"#"+L+"#"+V;h.push(Z);if(j.onCodigo instanceof Function){j.onCodigo(Z)}}};function s(K){if(I&&p){var L;if(D==0){L="2@"+t;h.push(L);if(j.onCodigo instanceof Function){j.onCodigo(L)}}else{if(D==1){L="21@"+t;h.push(L);if(j.onCodigo instanceof Function){j.onCodigo(L)}}else{j.desenhar(H,r,b,w,D,0,true);F.clearRect(0,0,d,v)}}I=false}}this.mudaOpcao=function(K){if(K==1){i.fillStyle="#FFFFFF"}else{if(D==1){i.fillStyle=f[g]}}D=K};this.mudaBorda=function(K,M){i.lineWidth=K;if(l!=null){F.lineWidth=K}G=K;if(M){var L="6@"+K;h.push(L);if(j.onCodigo instanceof Function){j.onCodigo(L)}}};this.mudaCor=function(K,M){K=parseInt(K);i.strokeStyle=f[K];if(l!=null){F.strokeStyle=f[K];F.fillStyle=f[K]}if(D!=1){i.fillStyle=f[K]}g=K;if(M){var L="5@"+K;h.push(L);if(j.onCodigo instanceof Function){j.onCodigo(L)}}};this.limparTela=function(M,N){if(M){x=new Array(d);for(var K=0;K<d;K++){x[K]=new Array(v);for(var L=0;L<v;L++){x[K][L]=14}}x[-1]=new Array();x[d]=new Array();for(var K=-1;K<=v;K++){x[-1][K]=0;x[d][K]=0}for(K=0;K<d;K++){x[K][-1]=0;x[K][v]=0}h=["5@"+g,"6@"+G];if(!N){if(j.onCodigo instanceof Function){j.onCodigo("4@")}}}i.clearRect(0,0,d,v);i.fillStyle="#FFF";j.retanguloF(0,0,d,v,0);i.fillStyle=f[g];if(l!=null){F.clearRect(0,0,d,v)}I=false};this.getSequencia=function(){return h.join("|")};this.setSequencia=function(K){h=K.split("|")};this.getCor=function(){return g};this.getCorReal=function(){return f[g]};this.getLargura=function(){return d};this.getAltura=function(){return v};this.getUltimaAcao=function(){if(h.length>0){return h[h.length-1]}return null};this.liberar=function(K){p=K;if(K){e.style.cursor="crosshair";if(l!=null){l.style.cursor="crosshair"}}else{e.style.cursor="default";if(l!=null){l.style.cursor="default"}}};if(l!=null){F=l.getContext("2d");E.push(F);l.onmousedown=k;l.addEventListener("touchstart",k,false);F.lineCap="round";F.fillStyle="#000000";F.strokeStyle="#000000";F.lineWidth=G}i.lineCap="round";i.fillStyle="#000000";i.strokeStyle="#000000";i.lineWidth=G;for(var A=0;A<=d;A++){J[A]=new Array()}j.limparTela(true)};