var showCanvas,isPngCompressed=false;
$(document).ready(function(){function ia(a){var c=a.menuType,e=a.type,h=a.data;Z=a.taburl;Y=a.tabtitle;E();K=ja();var f=a.w,d=a.h;switch(e){case "visible":$("#save-image").attr({src:h[0]}).load(function(){if(c=="selected"){l=a.centerW;m=a.centerH;N();O();E();V();E()}else{l=f-K;m=d-K;N();O()}f=l;d=m;z.drawImage(this,0,0,f,d,0,0,f,d);$(this).unbind("load")});break;case "entire":e=a.counter;var k=a.ratio,t=a.scrollBar,x=j=n=0,g=h.length,b=e,w=Math.round(g/b),C=function(q,A,B,F,I,Q,R,S,T){R=x*d;if(x==
w-1){B=d-lastH;I=T=lastH}$("#save-image").attr({src:q}).load(function(){$(this).unbind("load");z.drawImage(this,A,B,F,I,Q,R,S,T);if(++x>w-1){if(!(++j>b-1)){if(j==b-1){D=f-lastW;i=dw=l-j*f}else{D=0;i=dw=f}o=j*f;v=0;s=dh=d;x=u=0;n=x+j*w;C(h[n],D,v,i,s,o,u,dw,dh)}}else C(h[++n],A,B,F,I,Q,R,S,T);console.log(x+"+"+j+"+"+n)})},G=function(q,A,B,F,I,Q,R,S,T){Q=j*f;if(j==b-1){A=f-lastW;F=S=lastW}$("#save-image").attr({src:q}).load(function(){$(this).unbind("load");z.drawImage(this,A,B,F,I,Q,R,S,T);j<b-1&&
G(h[++j],A,B,F,I,Q,R,S,T)})};if(!t.x&&t.y){f-=K;w=g;lastH=d*k.y;if(c=="selected"){if(t.realX)d-=K;l=a.centerW}else l=f;m=lastH?d*(w-1)+lastH:d*w;N();O();E();V();E();var D=0,i=dw=f,o=0,v=0,s=dh=d,u=0;C(h[n],D,v,i,s,o,u,dw,dh)}if(t.x&&!t.y){d-=K;b=g;lastW=f*k.x;if(c=="selected"){if(t.realY)f-=K;m=a.centerH}else m=d;l=lastW?f*(b-1)+lastW:f*b;N();O();L.addClass("add-margin");E();D=0;i=dw=f;v=o=0;s=dh=d;u=0;G(h[n],D,v,i,s,o,u,dw,dh)}if(t.x&&t.y){lastW=f*k.x;lastH=d*k.y;f-=K;d-=K;if(c=="selected"){l=a.centerW;
m=a.centerH;console.log(l+"+"+m)}else{l=lastW?f*(b-1)+lastW:f*b;m=lastH?d*(w-1)+lastH:d*w}N();O();D=0;i=dw=f;v=o=0;s=dh=d;u=0;C(h[n],D,v,i,s,o,u,dw,dh)}break}}function ka(){$("#exit").click(function(){chrome.extension.sendRequest({action:"exit"})});$("#tool-panel>div").click(function(a){function c(e){var h=e.nodeName;if(h!="A"&&h!="DIV"){e=e.parentNode;c(e)}return e}a=c(a.target);a.nodeName!="DIV"&&la(a.id)})}function ma(){$("#promote").click(function(a){$(this).disableSelection();a.target.tagName!=
"A"&&$(this).toggleClass("expanded").find("#content").toggle()})}function la(a){if(y.width*y.height!=0&&a!="color"&&a!="done"&&a!="cancel"){if(a=="undo"){$("body").hasClass("draw_free_line")?ca():$(y).attr({width:0,height:0}).unbind();M.length==0&&da();return}if(!$("body").hasClass("draw_free_line")){P({type:"draw"});z.drawImage(y,parseInt($(y).css("left")),parseInt($(y).css("top")))}$(y).attr({width:0,height:0})}if(a!="color"){ea();if(a!="undo"&&a!="resize"){$("#temp-canvas").remove();$("body").removeClass("justCropped draw draw-text draw-blur")}}a!=
"undo"&&a!="color"&&a!="cancel"&&a!="done"&&$($("#"+a)).siblings().removeClass("active").end().addClass("active");switch(a){case "save":na();break;case "crop":oa();break;case "color":pa();break;case "done":qa();break;case "cancel":$("#crop_size").hide();$("body").removeClass("crop selected");$("#crop").removeClass("active");$("#center").css({width:0,height:0});break;case "resize":$("#resize select").unbind().change(function(){var c=parseInt(this.value),e=c/100,h=z.getImageData(0,0,l,m);$("body").removeClass("draw draw-text draw-blur");
P({type:"resize",data:h,relFactor:c});c=M.length;if(c>1)for(c=c-1;c>=0;c--){var f=M[c];if(f.type=="resize"&&(c==0||M[c-1].type!="resize")){h=f.data;l=f.w;m=f.h;break}}$(y).attr({width:l,height:m}).hide();r.putImageData(h,0,0);l*=e;m*=e;N();O();z.drawImage(y,0,0,l,m);$(y).attr({width:0,height:0}).show();E();V();E();$("body").addClass("resized");$("#undo span").css({"background-position-y":"0"})});break;case "undo":ca();break;default:ra(a);break}}function sa(){$("#logo").text(chrome.i18n.getMessage("logo"));
$("title").text(chrome.i18n.getMessage("editTitle"));document.getElementById("save").lastChild.data=chrome.i18n.getMessage("saveBtn");document.getElementById("done").lastChild.data=chrome.i18n.getMessage("doneBtn");document.getElementById("cancel").lastChild.data=chrome.i18n.getMessage("cancelBtn");document.getElementById("save_button").lastChild.data=chrome.i18n.getMessage("save_button");$(".title").each(function(){$(this).attr({title:chrome.i18n.getMessage(this.id.replace(/-/,""))})});$(".i18n").each(function(){$(this).html(chrome.i18n.getMessage(this.id.replace(/-/,
"")))})}function na(){function a(){var d=z.getImageData(0,0,l,m);(new JPEGEncoderThreaded("javascripts/jpeg_encoder_threaded_worker.js")).encode(d,aa,c,true);aa=80}function c(d){if($("#save-image")[0].src!=d)$("#save-image").attr({src:d}).load(function(){$(this).css({width:"auto"});this.width>parseInt($("#save_image_wrapper").css("width"))&&$(this).css({width:"100%"});e();h();$(this).unbind();$("#save-tip").show()});else{e();h();$("#save-tip").show()}}function e(){$("#upload").removeClass(".disable_upload");
$("#image_loader").hide();$("#save-image, #re-edit").css({visibility:"visible"})}function h(){$("#upload").removeClass("disable_upload").siblings().hide().end().show().click(function(){f()})}function f(){function d(){if(localStorage.format&&!isPngCompressed)i.image_type=localStorage.format;v=$.ajax({url:o+"cmd="+w+"&pv="+C+"&ct="+G+"&cv="+D,type:"POST",data:JSON.stringify(i),timeout:3E5,dataType:"json",contentType:"text/plain; charset=UTF-8",beforeSend:function(){$("#upload").parent().hide("fast");
$("#loader").fadeIn("slow").find("a").unbind("click").click(x)},error:function(){t()},success:function(s,u,q){$("#loader").hide();if(q.status==200&&s.code==1){k(s.result.url);ta&&_gaq.push(["_trackEvent","SavePageActions","upload_success","time"])}else t()},complete:function(){}})}function k(s){$("#share-button, #email-link").show("slow").click(function(u){$(u.target).addClass("visited")}).find("a").each(function(){if(this.id=="buzz")this.href+="message="+encodeURI(Y)+"&url="+encodeURI(Z)+"&imageurl="+
s;if(this.id=="twitter")this.href="http://twitter.com/share?url="+encodeURIComponent(s)+"&via=awe_screenshot&text="+Y;else $(this).attr({href:this.href+s})});$("#share-link").show("slow").find('input[type="text"]').attr({value:s}).bind("click focus",function(){$(this).select();if(BrowserDetect.OS!="Linux"){document.execCommand("Copy");$(this).next().show()}})}function t(){$("#loader").hide("fast");b||$("#error").show().find("#retry").unbind("click").click(function(){$("#error").hide();$("#loader").show().find("a").unbind("click").click(x);
d()})}function x(){b=1;v.abort();$("#upload").parent().siblings().hide("fast").end().fadeIn("slow");b=0}var g=$("#save-image").attr("src").replace(/^data:image\/(png|jpeg);base64,/,""),b=0,w="imageUpload",C="1.0",G="chrome",D=ua(),i={src_url:Z,src_title:Y,image_md5:$.md5(g),image_type:"jpg",image_content:g},o="http://awesomescreenshot.com/client?",v;d()}document.body.scrollTop=0;$("#save-tip").hide();$("#image_loader").css({display:"inline-block"});$("#save-image, #re-edit").css({visibility:"hidden"});
$("body").removeClass("crop draw-text").addClass("save");$("#save").removeClass("active");$("#show-canvas").toggle();$("#draw-canvas").attr({width:0,height:0});(function(){$("#re-edit").text(chrome.i18n.getMessage("reEdit")).click(function(){$("body").removeClass("save");$("#show-canvas").toggle();$(L).disableSelection();$("#share+dd div").hide();$("#save_local+dd>p").hide();$(this).unbind()})})();$("#share+dd").html(chrome.i18n.getMessage("savedShareDesc"));$(L).enableSelection();$("#upload").addClass("disable_upload");
$("#save_button").hide().parent().html('Right click on this image, then select "Save Image As..."<p style="display:none; background:url(../images/warning.png) 0 2px no-repeat; margin-top: 6px; padding-left:20px; color: #DDD; font-style:italic;">Saving an image larger than 2M will cause the browser to crash (due to a <a id="browser_bug" style="text-decoration:underline; float:none; margin-right:0;" href="http://code.google.com/p/chromium/issues/detail?id=45395" target="_black">browser bug</a>), so it is necessary to compress the image to <b>JPG</b> format.</p>');
switch(localStorage.format){case "png":if(l*m<217E4)setTimeout(c,300,showCanvas.toDataURL());else{setTimeout(a,300);$("p",$("#save_local").next("dd")).show();isPngCompressed=true}break;case "jpg":default:aa=l*m<217E4?100:80;setTimeout(a,300);break}}function oa(){function a(){$.Draggable("#center",{beforeDrag:function(i){if(i.target.id=="crop_size")i.undraggable=true},onDrag:function(i,o,v,s){d=i+v;k=o+s;e();c()},afterDrag:function(){d<0&&(d=0);k<0&&(k=0);d+g>l&&(d=l-g);k+b>m&&(k=m-b);d*k*(d+g-l)*
(k+b-m)==0&&c()}});$("#center").disableSelection().resizable({resize:function(i,o){d=o.position.left-document.body.scrollLeft;k=o.position.top;g=o.size.width;b=o.size.height;f(i);c();h();e()},stop:function(i,o){d=o.position.left;k=o.position.top;g=o.size.width;b=o.size.height;if(d<0){g+=d;d=0}if(k<0){b+=k;k=0}if(d+g>l)g=l-d;if(k+b>m)b=m-k;c();h()},handles:"all"})}function c(){$("#top").width(g>=0?d+g:d).height(b>=0?k:k+b);$("#right").width(g>=0?l-d-g:l-d).height(b>=0?k+b:k);$("#bottom").width(g>=
0?l-d:l-d-g).height(b>=0?m-k-b:m-k);$("#left").width(g>=0?d:d+g).height(b>=0?m-k:m-k-b);$("#center").width(Math.abs(g)).height(Math.abs(b)).css({left:(g>=0?d:d+g)+"px",top:(b>=0?k:k+b)+"px"})}function e(){k<30?G.css({top:"10px"}):G.css({top:"-30px"})}function h(){G.html(Math.abs(g)+" X "+Math.abs(b))}function f(i){i=i.clientY;var o=D-i;if(i<80)document.body.scrollTop-=25;if(o<40)document.body.scrollTop+=60-o}$("body").removeClass("draw").addClass("crop");$("#center").css({outline:"none"});E();$(showCanvas).unbind("mousedown click");
var d,k,t,x,g,b,w=mflag=0,C=$("#crop-tip"),G=$("#crop_size").hide(),D;$("#helper").hover(function(){C.text(chrome.i18n.getMessage("cropTip")).show()},function(){C.hide()}).mousedown(function(i){C.hide();G.fadeIn("slow");$("#center").css({outline:"1px dashed #777"});d=i.pageX-J;k=i.pageY-H;e();D=window.innerHeight;w=1}).mousemove(function(i){t=i.pageX-J;x=i.pageY-H;f(i);if(w){g=t-d;b=x-k;mflag=1;c();h()}else C.css({top:x+5+"px",left:t+5+"px"})}).mouseup(function(){if(mflag){$("body").addClass("selected");
$(this).unbind();w=mflag=0;g=Math.abs(g);b=Math.abs(b);a()}})}function pa(){$("#color").find("ul").show().hover(function(a){$(this).show();a.stopPropagation()},function(){$(this).hide()}).click(function(a){var c=$(a.target).css("background-color");$(this).prev("span").css({"background-color":c});U=c;$("#text").hasClass("active")&&$("div[contenteditable]").css({color:U});a.stopPropagation()})}function qa(){var a=$("#center"),c=parseInt(a.css("left")),e=parseInt(a.css("top")),h=a.width();a=a.height();
P({type:"crop"});c=z.getImageData(c,e,h,a);z.clearRect(0,0,l,m);$(showCanvas).attr({width:h,height:a});z.putImageData(c,0,0);$("body").removeClass().addClass("cropped justCropped");$("#crop").removeClass("active");W();l=h;m=a;N();$("#center").css({width:0,height:0,outline:"none"});E()}function ca(){function a(){l=e.w;m=e.h;N();E();V();E();O();z.putImageData(e.data,0,0);e=null}var c=M.length,e=M.pop();if(c!=0){c==1&&da();if(e.f){$("body").removeClass("cropped");ba=1}switch(e.type){case "draw":z.putImageData(e.data,
0,0);break;case "crop":a();break;case "resize":fa=e.factor;$("#resize select option").each(function(){$(this).text()==fa+"%"&&$(this).siblings().removeAttr("selected").end().attr({selected:"selected"})});a();break}}}function W(){$("#undo").css({visibility:"visible"}).removeClass("disable").find("span").css({"background-position":"-200px 0"})}function da(){$("#undo").addClass("disable").find("span").css({"background-position":"-200px -20px"})}function ra(a){$("body").removeClass("crop draw_free_line").addClass("draw");
X=1;if(a=="free-line"){$("body").addClass("draw_free_line");$(showCanvas).unbind();$("#temp-canvas").length||ga();va()}else{$(y).unbind("mousedown");if(a=="blur"){$("body").addClass("draw-blur");wa()}else{a=="text"&&$("body").addClass("draw-text");$(showCanvas).unbind().click(function(c){a=="text"&&xa({x:c.pageX,y:c.pageY})}).mousedown(function(c){if(y.width*y.height!=0){P({type:"draw"});z.drawImage(y,parseInt($(y).css("left")),parseInt($(y).css("top")))}$(y).attr({width:0,height:0});switch(a){case "text":break;
default:ya(a,{x:c.pageX,y:c.pageY});break}})}}}function ya(a,c){function e(d,k){function t(){r.clearRect(0,0,i,o);r.beginPath();(function(v,s,u,q){var A=u/2*0.5522848,B=q/2*0.5522848,F=v+u,I=s+q;u=v+u/2;q=s+q/2;r.moveTo(v,q);r.bezierCurveTo(v,q-B,u-A,s,u,s);r.bezierCurveTo(u+A,s,F,q-B,F,q);r.bezierCurveTo(F,q+B,u+A,I,u,I);r.bezierCurveTo(u-A,I,v,q+B,v,q);r.closePath()})(b,b,i-2*b,o-2*b);r.stroke()}function x(){r.clearRect(0,0,i,o);r.beginPath();var v=h<w?b:i-b,s=f<C?b:o-b,u=i-v;my1=o-s;r.moveTo(v,
s);r.lineTo(u,my1);r.stroke();(function(q){r.beginPath();r.moveTo(q[0][0],q[0][1]);for(p in q)p>0&&r.lineTo(q[p][0],q[p][1]);r.lineTo(q[0][0],q[0][1]);r.fill()})(function(q,A,B){var F=[];for(p in q)F.push([q[p][0]+A,q[p][1]+B]);return F}(function(q,A){var B=[];for(p in q)B.push([q[p][0]*Math.cos(A)-q[p][1]*Math.sin(A),q[p][0]*Math.sin(A)+q[p][1]*Math.cos(A)]);return B}([[4,0],[-10,-5.5],[-10,5.5]],Math.atan2(my1-s,u-v)),u,my1))}function g(){r.clearRect(0,0,i,o);r.beginPath();var v=h<w?b:i-b,s=f<C?
b:o-b,u=i-v;my1=o-s;r.moveTo(v,s);r.lineTo(u,my1);r.stroke();r.closePath()}var b=4,w=d-J,C=k-H,G=Math.min(w,h)-b,D=Math.min(C,f)-b,i=Math.abs(w-h)+2*b,o=Math.abs(C-f)+2*b;$(y).attr({width:i,height:o}).css({left:G+"px",top:D+"px",cursor:"crosshair"}).disableSelection();r.strokeStyle=U;r.fillStyle=U;r.lineWidth=b;switch(a){case "rectangle":r.clearRect(0,0,i,o);r.strokeRect(b,b,i-2*b,o-2*b);break;case "ellipse":t();break;case "arrow":x();break;case "line":g();break}}var h=c.x-J,f=c.y-H;$(this).mousemove(function(d){e(d.pageX,
d.pageY)}).mouseup(function(){$(this).unbind("mousemove mouseup");$(y).unbind("mousedown");W();$.Draggable(y)})}function va(){$(y).attr({width:l,height:m}).css({left:0,top:0,cursor:"crosshair"}).disableSelection().mousedown(function(a){P({type:"draw"});var c=document.getElementById("temp-canvas"),e=c.getContext("2d");e.moveTo(a.pageX-J,a.pageY-H);$(this).mousemove(function(h){e.lineTo(h.pageX-J,h.pageY-H);e.strokeStyle=U;e.lineWidth=3;e.stroke()}).mouseup(function(){$(this).unbind("mousemove mouseup");
W();z.drawImage(c,0,0);$(c).remove();c=null;ga()})})}function ga(){$(document.createElement("canvas")).attr({width:l,height:m,id:"temp-canvas"}).insertBefore($(y))}function wa(){$(showCanvas).css({cursor:"url(images/cursor-blur.png)"}).unbind().mousedown(function(){P({type:"draw"});$(this).mousemove(function(a){var c=a.pageX-J;a=a.pageY-H;var e=z.getImageData(c,a,20,20);e=e;var h,f,d,k,t=e.width,x=e.height,g=e.data,b=step=jump=inner=outer=arr=0;for(k=0;k<1;k++)for(var w=0;w<2;w++){if(w){outer=t;inner=
x;step=t*4}else{outer=x;inner=t;step=4}for(h=0;h<outer;h++){jump=w===0?h*t*4:4*h;for(d=0;d<3;d++){b=jump+d;arr=0;arr=g[b]+g[b+step]+g[b+step*2];g[b]=Math.floor(arr/3);arr+=g[b+step*3];g[b+step]=Math.floor(arr/4);arr+=g[b+step*4];g[b+step*2]=Math.floor(arr/5);for(f=3;f<inner-2;f++){arr=Math.max(0,arr-g[b+(f-2)*step]+g[b+(f+2)*step]);g[b+f*step]=Math.floor(arr/5)}arr-=g[b+(f-2)*step];g[b+f*step]=Math.floor(arr/4);arr-=g[b+(f-1)*step];g[b+(f+1)*step]=Math.floor(arr/3)}}}e=e;z.putImageData(e,c,a)})}).mouseup(function(){$(this).unbind("mousemove");
W()})}function xa(a){function c(){$("<input></input>").appendTo(L).css({top:e+"px",left:h+"px",width:f+"px",color:U}).focus().autoGrowInput({comfortZone:20,minWidth:20,maxWidth:d}).keydown(function(t){if($(this).width()+10>d&&t.keyCode>=48||parseInt($(this).css("top"))-startT+38>k&&t.keyCode==13)return false;var x=t.target,g=t.keyCode;if(g==13){e+=18;c()}if(g==8)if(!x.value){$(x).prev().prev().focus().end().end().next().remove().end().remove();e-=18}g==38&&$(x).prev().prev().focus();g==40&&$(x).next().next().focus();
t.stopPropagation()})}ea();$("body").addClass("draw-text");var e=startT=a.y-H-10,h=a.x-J;h>l-f&&(h=l-f);var f=20,d=l-h,k=m-e;X==1&&c();if(X==2)X=1}function ea(){var a=$(L).find("input:not(#share-link input)");if(a.length){var c="";a.each(function(){c+=this.value});if(c){W();P({type:"draw"});X=2;a.each(function(){var e=this.value;if(e){var h=parseInt($(this).css("left")),f=parseInt($(this).css("top"));z.font="bold 14px/18px Arial,Helvetica,sans-serif";z.fillStyle=$(this).css("color");z.fillText(e,
h+1,f+14)}$(this).next().remove().end().remove()})}}}function P(a){switch(a.type){case "draw":M.push({type:"draw",data:z.getImageData(0,0,l,m)});break;case "crop":M.push({type:"crop",data:z.getImageData(0,0,l,m),w:l,h:m,f:ba});ba=0;break;case "resize":M.push({type:"resize",data:a.data,w:l,h:m,absFactor:a.absFactor});break}}function N(){L.css({width:l+"px",height:m+"px"})}function O(){$(showCanvas).attr({width:l,height:m})}function E(){var a=L.offset();J=a.left;H=a.top}function ja(){var a=document.createElement("p");
a.style.width="100%";a.style.height="200px";var c=document.createElement("div");c.style.position="absolute";c.style.top="0px";c.style.left="0px";c.style.visibility="hidden";c.style.width="200px";c.style.height="150px";c.style.overflow="hidden";c.appendChild(a);document.body.appendChild(c);var e=a.offsetWidth;c.style.overflow="scroll";a=a.offsetWidth;if(e==a)a=c.clientWidth;document.body.removeChild(c);return e-a}function ua(){var a=new XMLHttpRequest;a.open("GET","./manifest.json",false);a.send(null);
return JSON.parse(a.responseText).version}function V(){J||H!=48&&H!=88?L.addClass("add-margin"):L.removeClass("add-margin")}var J,H,l,m,K=17,L=$("#edit-area").disableSelection(),M=[],ba=1,ha=1,X=1;showCanvas=document.getElementById("show-canvas");var z=showCanvas.getContext("2d"),y=document.getElementById("draw-canvas"),r=y.getContext("2d"),U="red",Z,Y,aa=80,fa=100,ta=BrowserDetect.OS=="Windows"||BrowserDetect.OS=="Linux";chrome.extension.onRequest.addListener(function(a){if(ha){sa();ia(a);ka();ma();
ha=0}});chrome.extension.sendRequest({action:"ready"});$(window).unbind("resize").resize(function(){E();V()})});
