eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('w 1P(){7 m=["#1H","#1I","#1J","#1L","#1R","#1K","#1D","#1o","#1y","#1A","#1C"],o=[],x=[],B=0,z=[],u=0,8=1Q,S=15;5.G=0;5.B=0;5.r=w(){o=[];x=[];z=[];9(P 5.r.v[0]=="1b"&&5.r.v[0].p){o=5.r.v[0];9(P 5.r.v[1]=="1b"&&5.r.v[1].p){x=5.r.v[1]}9(P 5.r.v[1]=="1w"){x=1r(\'["\'+5.r.v[1].1p(/,/g,\'","\')+\'"]\')}}M{J(7 i=0;i<5.r.v.p;i++){o[i]=5.r.v[i]}}9(!x.p&&u>1){u=0}5.R()};5.1m=w(a){8=A.1k(a);9(!8){1h 1x}n(5.G)};5.R=w(){9(8){7 a=8.1z("8");1d(a.p){8.1B(a[0])}n(5.G)}};5.1F=w(a){a=5.G+a;a=a%18;9(a<0){a=18+a}5.G=a;5.R()};5.17=w(a){9(a>2){a=0}u=a;9(u>1&&!x.p){u=0}7 b=B/11;J(7 i=0,j=o.p;i<j;i++){1i(u){C 0:z[i].T=o[i];D;C 1:z[i].T=6.Y(o[i]/b*10)/10+"%";D;C 2:z[i].T=x[i];D}}};5.u=w(){5.17(u+1)};7 n=w(a){9(!8){1h}B=0;J(7 i=0;i<o.p;i++){B+=o[i]}7 b=B/11;7 c=2*6.t/B,Q=1,q=1,y=0,F=a*6.t/1G,O=0.1E,Z=10;J(7 i=0;i<o.p;i++){7 d=A.I("8");7 e=A.I("1t");1i(u){C 0:7 f=o[i];D;C 1:7 f=6.Y(o[i]/b*10)/10+"%";D;C 2:7 f=x[i];D}z[i]=A.1q(f);e.E(z[i]);d.E(e);d.K="V-1l";7 g=(F+c*o[i]/2)%(6.t*2);d.s.1n=(g>6.t?1:-1)*(S/3*2*6.L(6.1g(g)))+"H";d.s.1s=(g<6.t/2||g>6.t/2*3?1:-1)*(S/3*2*6.L(6.1f(g)))+"H";8.E(d);9(o[i]==0){d.s.1u="1v"}7 h=A.I("8");F+=c*o[i];y=F-6.t/2*(Q-1);1d(y>6.t/2+O){7 j=A.I("8");j.K="1e U-"+q;j.s.1c=m[i%m.p];j.s.N=Z--;8.E(j);Q++;Z=10*Q;9(Q>4){q=Q-4}M{q=Q}y=F-6.t/2*(Q-1)}9(6.L(6.t/2-y)<=O){7 j=A.I("8");j.K="1e U-"+q;j.s.1c=m[i%m.p];j.s.N=Z--;8.E(j);Q++;Z=10*Q;9(Q>4){q=Q-4}M{q=Q}}M{h.K="V U-"+q;7 k=6.L(S*6.1j(y));7 l=S;9(k>0){9(k>S){l=S*6.1f(y)/6.1g(y);k=S;h.s[[0,"1a","19","1a","19"][q]]=S-l+"H"}h.s["16"+[0,"14","13","12","X"][q]]="W "+l+"H 1M";h.s["16"+[0,"13","12","X","14"][q]]="W "+k+"H "+m[i%m.p];h.1N=k;h.1O=m[i%m.p];h.s.N=Z--;8.E(h)}}}}}',62,116,'|||||this|Math|var|div|if|||||||||||||||items|length||setValues|style|PI|title|arguments|function|titles|angle|labels|document|sum|case|break|appendChild|whole|start|em|createElement|for|className|abs|else|zIndex|sigma|typeof||redraw||nodeValue|sector|pie|solid|Top|round|||100|Right|Bottom|Left||border|setTitle|360|height|width|object|backgroundColor|while|bg|cos|sin|return|switch|tan|getElementById|label|printTo|top|92278f|replace|createTextNode|eval|left|span|display|none|string|false|f7941d|getElementsByTagName|0054a6|removeChild|ccc|8dc63f|01|rotate|180|00a651|f66|00aeef|662d91|a67c52|transparent|size|colour|PieChart|null|ffcc00'.split('|'),0,{}))

// Note: The file system has been prefixed as of Google Chrome 12:
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  console.log('Error: ' + msg);
}
function dataURItoBlob(dataURI, callback) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var bb = new window.WebKitBlobBuilder();
    bb.append(ab);
    return bb.getBlob(mimeString);
}
if (extension.online) 
	{
	background=chrome.extension.getBackgroundPage().background;
	var je = new JPEGEncoderThreaded();
	}
var shadowDistance=5;
var imageURL;
if (!localStorage['lastTool']) localStorage['lastTool']='line';
function editor_obj()
	{
	var theColor='#ff0000',lineWidth=5;
	var canvas,canvasOffset,$canvas,c,canvasData,$canvasData,czindex=5,startX,startY,points=[];
	var scrollBarWidth,scrollBarHeight;
	var scrollLeft,scrollTop;
	var tool={current:'line',status:'ready'};
	var clip={rx1:0,rx2:0,ry1:0,ry2:0};
	var cancel=false;
	var lastX,lastY;
	tool.free={};
	tool.spray={};
	tool.line={};
	tool.rectangle={};
	tool.arrow={};
	tool.ellipsis={};
	tool.crop={};
	tool.text={};
	tool.undo={};
	tool.redo={};

	var levels=[];
	var currentLevel,currentIndex=-1,maxIndex=-1;
	var imageChanged=true;

	function newLevel(inObj)
		{
		imageChanged=true;
		currentIndex++;
		maxIndex++;
		if (levels.length>currentIndex)
			{
			levels.slice(currentIndex);
			maxIndex=currentIndex;
			}
		if (!inObj) inObj={};
	    var newObject = jQuery.extend(true, {}, inObj);
		newObject.canvas=canvas;
		newObject.c=c;
		newObject=$.extend(true,newObject,clip);
		levels[currentIndex]=newObject;
		currentLevel=newObject;
		currentLevel.active=true;
		currentLevel.color=theColor;
		currentLevel.lineWidth=lineWidth;
//		c.strokeStyle=currentLevel.color;
//		c.fillStyle=currentLevel.color;
		resetButtons();
		return(newObject);
		}

	tool.undo.click=function ()
		{
		imageChanged=true;
		if (currentLevel.type=='crop')
			{
			currentIndex--;
			if (currentIndex<0 || true)
				{
				clip.rx1=clip.ry1=0;
				clip.ry2=$('#canvasId').height();
				clip.rx2=$('#canvasId').width();
				}
			else
				{
				currentLevel=levels[currentIndex];
				clip.rx1= currentLevel.rx1;
				clip.rx2= currentLevel.rx2;
				clip.ry1= currentLevel.ry1;
				clip.ry2= currentLevel.ry2;				
				}
			onResize();
			}
		else
			{
			currentLevel.canvas.style.display='none';
			currentIndex--;
			currentLevel=levels[currentIndex];
			}
		changeTool(localStorage['lastTool']);
		//$('canvas.tool:visible:last').hide();
		resetButtons();
		};
	tool.redo.click=function ()
		{
		imageChanged=true;
		currentIndex++;
		currentLevel=levels[currentIndex];
		if (currentLevel.type=='crop')
			{
			clip.rx1=currentLevel.rx1;
			clip.rx2=currentLevel.rx2;
			clip.ry1=currentLevel.ry1;
			clip.ry2=currentLevel.ry2;
			}
		else
			currentLevel.canvas.style.display='';
		changeTool(localStorage['lastTool']);
		onResize();
		resetButtons();
		};


	function createCanvas()
		{
		$canvas=$('<canvas class=tool></canvas>').appendTo($('#divCanvasData')); //.data('clip-x1', clip.rx1).data('clip-y1', clip.ry1);
		canvas=$canvas[0];
		c=canvas.getContext('2d');
		canvas.width=$canvasData.width();
		canvas.height=$canvasData.height();
		$canvas.css({'margin-left':-clip.rx1,'margin-top':-clip.ry1});
		$canvas.css( {position:'absolute','z-index':czindex++,left:0 ,'co':$canvasData.position().left,'top':0 ,'color':$canvasData.offset().top-scrollTop} );
		}
	function clipReload()
		{
		clip.rx1=0;
		clip.rx2=canvasData.width;
		clip.ry1=0;
		clip.ry2=canvasData.height;
		}
	this.reloadCanvas=function()
		{
		if(background.url) $('title').html(background.url);
		//document.getElementById('divCanvasData').appendChild(canvasData);
		clipReload();
		onResize();
		//editor.reloadCanvas();
		resetButtons();
		$('#toolbar .button[tag=' + localStorage['lastTool'] + ']').trigger('click');
		}	
	
	this.init=function()
		{
		canvasData=document.getElementById('canvasId');
		$canvasData=$(canvasData);
		binds();
		resetButtons();
//		if (extension.mac) $('#save').hide();
		}	
//**********Text
	tool.text.begin=function (e)
		{
		tool.status='started';
		startX=e.x;
		startY=e.y;
		$('#text_helper').remove();
		$('<div id=text_helper style="position:absolute;border:2px dashed gray">adf</div>').appendTo('#divCanvasData');
		x1=startX;y1=startY;
		x2=e.x;y2=e.y;
		if (x1<x2) {rx1=x1;rx2=x2;} else {rx1=x2;rx2=x1;}
		if (y1<y2) {ry1=y1;ry2=y2;} else {ry1=y2;ry2=y1;}

		$('#text_helper')
		.css({'z-index':10000,
			left:-clip.rx1+rx1, //+$canvasData.offset().left,
			top:-clip.ry1+ry1, //+$canvasData.offset().top,
			width:rx2-rx1<70 ? 70 : rx2-rx1,
			height:ry2-ry1<70 ? 70 : ry2-ry1});	
		b=['left','top','z-index','width','height','position','font-weight'];
		var ta=$('<textarea></textarea>');
		for (i in b)
			{
			ta.css(b[i], $('#text_helper').css(b[i])  );
			}
		ta.css(
			{'background-color':'transparent',
			padding:0,
			margin:0,
			border:'none',
			color:theColor,
			'font-size':'20pt',
			'font-weight':'bolder'
			});
		ta.mousedown(function  (e) {e.stopImmediatePropagation(); })
		ta.appendTo('#divCanvasData').autoResize().focus();
		$('#text_helper').remove();
		ta.blur(tool.text.finish);
		};
	tool.text.finish=function ()
		{
		createCanvas();
		newLevel();
		currentLevel.type='text';
		a={
			text:$('textarea').val(),
			font:$('textarea').css('font-size')+ ' ' + $('textarea').css('font-family'),
			'font-size':$('textarea').css('font-size'),
			'font-weight':$('textarea').css('font-weight'),
			lineHeight:$('textarea').css('line-height'),
			x: startX, y:startY,
			direction:$('textarea').css('direction')
		};
		currentLevel=$.extend(currentLevel,a);
		$('textarea').remove();
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});	
		tool.status='ready'; $('textarea').unbind('blur');
		};
	tool.text.draw=function(inX)
		{
		inX.ctx.textBaseline='top';
		inX.ctx.strokeStyle=inX.data.color;
		inX.ctx.fillStyle=inX.data.color;
		inX.ctx.font=inX.data.font;
		lineHeight=		inX.ctx.font=parseInt(inX.data['font-size'])*1.5;
		var lines = inX.data.text.split("\n");
		for (a=0; a<lines.length; a++) {
				inX.ctx.fillStyle='000';
				inX.ctx.globalAlpha='0.2'
				inX.ctx.fillText(lines[a], inX.data.x+2, inX.data.y +2+ parseInt(lineHeight)*a);
				inX.ctx.globalAlpha='1'
				inX.ctx.strokeStyle=inX.data.color;
				inX.ctx.fillStyle=inX.data.color;
				inX.ctx.fillText(lines[a], inX.data.x, inX.data.y + parseInt(lineHeight)*a);
				}

		};
//********crop
	tool.crop.begin=function  (e)
		{
		tool.status='started';
		startX=e.x;
		startY=e.y;
		};
	tool.crop.click=function()
		{
		//clipReload();
		onResize();
		};
	tool.crop.up=function ()
		{
		clip.rx1=rx1;
		clip.rx2=rx2;
		clip.ry1=ry1;
		clip.ry2=ry2;
		newLevel();
		currentLevel.type='crop';
		tool.crop.draw()
		}
	tool.crop.draw=function (inX)
		{
//		clip.rx1=rx1;
//		clip.rx2=rx2;
//		clip.ry1=ry1;
//		clip.ry2=ry2;
		$('#crop_helper').remove();
//		$('#divCanvasData').css({'width':x2-x1,height:y2-y1})
		onResize();
		$('#divCanvasData').scrollTop(0).scrollLeft(0);
		tool.status='ready';
		$('#toolbar .button[tag=line]').trigger('click');
		//tool.current='line';
		resetButtons();
		};
	tool.crop.move=function (e)
		{
		x1=startX; y1=startY;
		x2=e.x; y2=e.y;
		if (x1<x2) {rx1=x1;rx2=x2;} else {rx1=x2;rx2=x1;}
		if (y1<y2) {ry1=y1;ry2=y2;} else {ry1=y2;ry2=y1;}
		$('#crop_helper').remove();
		$('<div id=crop_helper><div id=crop_helper_bottom></div><div id=crop_helper_left></div><div id=crop_helper_top></div><div id=crop_helper_right></div></div>').appendTo('#divCanvasData');
		$('#crop_helper *').css({
				'background-color':'blue',
				'opacity':'0.6',
				'position':'absolute',
				'z-index':10000
				});
		$('#crop_helper_left').css({'ackground-color':'blue',
				left: 0,top:$canvasData.position().top,top:0,
				width:rx1-clip.rx1,height:$canvasData.height()+scrollTop
				});
		$('#crop_helper_top').css({'ackground-color':'red',
				left: rx1-clip.rx1,top:0,
				width:clip.rx2-(rx1-clip.rx1),height:ry1-clip.ry1
				});
		$('#crop_helper_bottom').css({'ackground-color':'yellow',
				left: rx1-clip.rx1,top:ry2-clip.ry1,
				width:clip.rx2-(rx1-clip.rx1),height:clip.ry2-ry2
				});
		$('#crop_helper_right').css({'ackground-color':'green',
				left: rx2-clip.rx1,top:ry1-clip.ry1,
				width:clip.rx2-(rx2-clip.rx1),height:ry2-ry1
				});
		};

	function pos(e) {
		ans ={x:clip.rx1+e.pageX-$('#divCanvasData').offset().left+$('#divCanvasData').scrollLeft(), y:clip.ry1+e.pageY-$('#divCanvasData').offset().top+$('#divCanvasData').scrollTop()};
		if (ans.x<0  || ans.y<0) return (false);
		if (e.pageX> $('#divCanvasData').offset().left+$('#divCanvasData').width()-scrollBarWidth ) return(false);
		if (e.pageY> $('#divCanvasData').offset().top+$('#divCanvasData').height()-scrollBarHeight ) return(false);
		if (e.pageX< $('#divCanvasData').offset().left ) return(false);
		if (e.pageY< $('#divCanvasData').offset().top ) return(false);
		return (ans);
		}

	function canvasAutoResize(e,dif)
		{
		ans=getMinMax();
		$canvas.css({left:ans.x1-dif,top:ans.y1-dif});
		canvas.width=ans.x2-ans.x1+dif*2;
		canvas.height=ans.y2-ans.y1+dif*2;
		c.moveTo(points[0].x-ans.x1,points[0].y-ans.y1);
		return({x1:ans.x1-dif,x2:ans.x2+dif,y1:ans.y1-dif,y2:ans.y2+dif,w:x2-x1,h:y2-y1});
		}
	function getMinMax(points)
		{
		x1=y1=65535;
		x2=y2=0;
		$.each(points,function (a,b)
			{
			if (b.x<x1) x1=b.x;
			if (b.x>x2) x2=b.x;
			if (b.y<y1) y1=b.y;
			if (b.y>y2) y2=b.y;
			});
		return ({x1:x1,x2:x2,y1:y1,y2:y2});
		}
//**********Spray
	tool.spray.begin=function (e)		
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='spray';
		currentLevel.points=[];
		tool.spray.move(e);
		};
	tool.spray.move=function(e)
		{
		for(var j=c.lineWidth*25; j>0; j--) {
			var rndx = e.x + Math.round(Math.random()*(c.lineWidth*15)-(c.lineWidth*2));
			var rndy = e.y+ Math.round(Math.random()*(c.lineWidth*15)-(c.lineWidth*2));
			currentLevel.points.push({x:rndx, y:rndy});
			}
		ans=getMinMax(currentLevel.points);
		enlargeCanvas(canvas,ans.x1,ans.y1,ans.x2,ans.y2);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.spray.draw=function (inX)
		{

//		id=inX.ctx.createImageData(inX.canvas.width,inX.canvas.height);
		id=inX.ctx.getImageData(0,0,inX.canvas.width,inX.canvas.height);
		pix=id.data;
		for(var i=0;i<inX.data.points.length;i++)
			{
			thisPoint= (inX.data.points[i].y-inX.data.canvasOffset.y-1)* inX.canvas.width*4; 
			thisPoint+=(inX.data.points[i].x-inX.data.canvasOffset.x)*4;
			pix[thisPoint]=0;
			pix[thisPoint+1]=0;
			pix[thisPoint+2]=0;
			pix[thisPoint+3]=255;
			};
		inX.ctx.putImageData	(id,0,0);
		};	
	tool.spray.up=function (){tool.status='ready';};
////    Free
	tool.free.up=function (){tool.status='ready';};
	tool.free.begin=function (e)
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='free';
		currentLevel.points=[];
		tool.free.move(e);
		};
	tool.free.move=function(e)
		{
		currentLevel.points.push({x:e.x,y:e.y});
//		ans=canvasAutoResize(e,2);
		canvas.height=canvas.height+1;
		canvas.height=canvas.height-1;
		ans=getMinMax(currentLevel.points);
		enlargeCanvas(canvas,ans.x1,ans.y1,ans.x2,ans.y2);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.free.draw=function(inX)
		{
		
		inX.ctx.beginPath();
		inX.ctx.lineWidth=inX.data.lineWidth;
		
		inX.ctx.strokeStyle='00';
		inX.ctx.fillStyle='000';
		inX.ctx.globalAlpha=0.3;
		for(var i=1;i<inX.data.points.length;i++)
		{
			inX.ctx.lineTo(inX.data.points[i].x -inX.data.canvasOffset.x+shadowDistance  ,inX.data.points[i].y -inX.data.canvasOffset.y+shadowDistance);
		};					
		inX.ctx.stroke();
		inX.ctx.closePath();
		inX.ctx.beginPath();
		inX.ctx.strokeStyle=inX.data.color;
		inX.ctx.globalAlpha=1;
		inX.ctx.fillStyle=inX.data.color;
		for(var i=1;i<inX.data.points.length;i++)
			{
			inX.ctx.lineTo(inX.data.points[i].x -inX.data.canvasOffset.x  ,inX.data.points[i].y -inX.data.canvasOffset.y);
			};
		
		inX.ctx.stroke();
		inX.ctx.closePath();
		};	
	//line
	tool.line.begin=function (e)	
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='line';
		currentLevel.start={x:e.x,	y:e.y};
		};
	tool.line.move=function(e)
		{
		currentLevel.end={x:e.x,y:e.y};
		canvas.height=canvas.height+1;
		canvas.height=canvas.height-1;
		enlargeCanvas(canvas,currentLevel.start.x,currentLevel.start.y,currentLevel.end.x,currentLevel.end.y);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.line.draw=function(inX)
		{
		if (! (inX.data.start && inX.data.end)) return;
		inX.ctx.beginPath();
		inX.ctx.strokeStyle=inX.data.color;
		inX.ctx.fillStyle=inX.data.color;
		inX.ctx.lineWidth=inX.data.lineWidth;
		 drawLine=function(x1,y1,x2,y2,shadow)
			{
			inX.ctx.lineWidth=inX.data.lineWidth;
			if(shadow)
				{
				inX.ctx.strokeStyle='000';
				inX.ctx.globalAlpha=0.2;
				}
			else
				{
				drawLine(x1+shadowDistance,y1+shadowDistance,x2+shadowDistance,y2+shadowDistance,true)
				inX.ctx.strokeStyle=inX.data.color;
				inX.ctx.fillStyle=inX.data.color;
				inX.ctx.globalAlpha=1;
				}
			inX.ctx.beginPath();
			inX.ctx.moveTo(x1,y1);
			inX.ctx.lineTo(x2,y2);
			inX.ctx.stroke();
			inX.ctx.closePath();
			}
		drawLine(inX.data.start.x  -inX.data.canvasOffset.x  ,inX.data.start.y -inX.data.canvasOffset.y,
				inX.data.end.x    -inX.data.canvasOffset.x,inX.data.end.y     -inX.data.canvasOffset.y);
		inX.ctx.lineCap='round';
		inX.ctx.stroke();
		inX.ctx.closePath();
		};
	tool.line.up=function () {tool.status='ready';};
	//ellipsis
	tool.ellipsis.begin=function (e)	
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='ellipsis';
		currentLevel.start={x:e.x,	y:e.y};
		};
	tool.ellipsis.move=function(e)
		{
		currentLevel.end={x:e.x,y:e.y};
		canvas.height=canvas.height+1;
		canvas.height=canvas.height-1;
		enlargeCanvas(canvas,currentLevel.start.x,currentLevel.start.y,currentLevel.end.x,currentLevel.end.y);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.ellipsis.draw=function(inX)
		{
		if (! (inX.data.start && inX.data.end)) return;
		inX.ctx.beginPath();
		inX.ctx.strokeStyle=inX.data.color;
		inX.ctx.fillStyle=inX.data.color;
		inX.ctx.ellipsisWidth=inX.data.ellipsisWidth;
		 drawEllipsis=function(x1,y1,x2,y2,shadow)
			{
			inX.ctx.lineWidth=inX.data.lineWidth;
			if(shadow)
				{
				inX.ctx.strokeStyle='000';
				inX.ctx.globalAlpha=0.2;
				}
			else
				{
				drawEllipsis(x1+shadowDistance,y1+shadowDistance,x2+shadowDistance,y2+shadowDistance,true)
				inX.ctx.strokeStyle=inX.data.color;
				inX.ctx.fillStyle=inX.data.color;
				inX.ctx.globalAlpha=1;
				}
			inX.ctx.beginPath();

				var KAPPA = 4 * ((Math.sqrt(2) -1) / 3);

				var rx = (x2-x1)/2;
				var ry = (y2-y1)/2;

				var cx = x1+rx;
				var cy = y1+ry;
				var trg=inX.ctx;

				trg.beginPath();
				trg.moveTo(cx, cy - ry);
				trg.bezierCurveTo(cx + (KAPPA * rx), cy - ry,  cx + rx, cy - (KAPPA * ry), cx + rx, cy);
				trg.bezierCurveTo(cx + rx, cy + (KAPPA * ry), cx + (KAPPA * rx), cy + ry, cx, cy + ry);
				trg.bezierCurveTo(cx - (KAPPA * rx), cy + ry, cx - rx, cy + (KAPPA * ry), cx - rx, cy);
				trg.bezierCurveTo(cx - rx, cy - (KAPPA * ry), cx - (KAPPA * rx), cy - ry, cx, cy - ry);

			inX.ctx.stroke();
			inX.ctx.closePath();
			}
		drawEllipsis(inX.data.start.x  -inX.data.canvasOffset.x  ,inX.data.start.y -inX.data.canvasOffset.y,
				inX.data.end.x    -inX.data.canvasOffset.x,inX.data.end.y     -inX.data.canvasOffset.y);
		inX.ctx.ellipsisCap='round';
		inX.ctx.stroke();
		inX.ctx.closePath();
		};
	tool.ellipsis.up=function () {tool.status='ready';};
	//Rectangle
	tool.rectangle.begin=function (e)	
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='rectangle';
		currentLevel.start={x:e.x,	y:e.y};
		};
	tool.rectangle.move=function(e)
		{
		currentLevel.end={x:e.x,y:e.y};
		canvas.height=canvas.height+1;
		canvas.height=canvas.height-1;
		enlargeCanvas(canvas,currentLevel.start.x,currentLevel.start.y,currentLevel.end.x,currentLevel.end.y);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.rectangle.draw=function(inX)
		{
		if (! (inX.data.start && inX.data.end)) return;

		function drawRect(x1,y1,x2,y2,shadow)
			{
			inX.ctx.lineWidth=inX.data.lineWidth;
			if(shadow)
				{
				inX.ctx.strokeStyle='000';
				inX.ctx.globalAlpha=0.2;
				}
			else
				{
				drawRect(x1+shadowDistance,y1+shadowDistance,x2+shadowDistance,y2+shadowDistance,true)
				inX.ctx.strokeStyle=inX.data.color;
				inX.ctx.fillStyle=inX.data.color;
				inX.ctx.globalAlpha=1;
				}
			inX.ctx.beginPath();
			inX.ctx.moveTo(x1,y1);
			inX.ctx.lineTo(x1,y2);
			inX.ctx.lineTo(x2,y2);
			inX.ctx.lineTo(x2,y1);
			inX.ctx.lineTo(x1,y1);
			inX.ctx.stroke();
			inX.ctx.closePath();
			}
		
		drawRect(inX.data.start.x  -inX.data.canvasOffset.x  ,inX.data.start.y -inX.data.canvasOffset.y,
				inX.data.end.x    -inX.data.canvasOffset.x,inX.data.end.y     -inX.data.canvasOffset.y);
		inX.ctx.lineCap='round';

		};
	tool.rectangle.up=function () {tool.status='ready';};	
	//Arrow
	tool.arrow.begin=function (e)	
		{
		tool.status="started";
		createCanvas();
		newLevel();
		currentLevel.type='arrow';
		currentLevel.start={x:e.x,	y:e.y};
		};
	tool.arrow.move=function(e)
		{
		currentLevel.end={x:e.x,y:e.y};
		canvas.height=canvas.height+1;
		canvas.height=canvas.height-1;
		enlargeCanvas(canvas,currentLevel.start.x,currentLevel.start.y,currentLevel.end.x,currentLevel.end.y);
		tool[tool.current].draw({canvas:canvas,ctx:c,data:currentLevel});
		};
	tool.arrow.draw=function(inX)
		{
		if (! (inX.data.start && inX.data.end)) return;

		function drawArrow(x1,y1,x2,y2,shadow)
			{
			inX.ctx.lineWidth=inX.data.lineWidth;
			if(shadow)
				{
				inX.ctx.strokeStyle='000';
				inX.ctx.globalAlpha=0.2;
				}
			else
				{
				drawArrow(x1+shadowDistance,y1+shadowDistance,x2+shadowDistance,y2+shadowDistance,true)
				inX.ctx.strokeStyle=inX.data.color;
				inX.ctx.fillStyle=inX.data.color;
				inX.ctx.globalAlpha=1;
				}
			inX.ctx.beginPath();
			inX.ctx.moveTo(x1,y1);
			inX.ctx.lineTo(x2,y2);

			var dy=20
			var dx1=15
			var dx2=15
			var DeltaX = x2 - x1;
			var DeltaY = y2 - y1;
			var L = Math.sqrt(DeltaX*DeltaX + DeltaY*DeltaY);


			inX.ctx.lineTo(x2 - DeltaY * dy / L  - DeltaX * dx1 / L,
			               y2 + DeltaX * dy / L -  DeltaY * dx1 / L);
			
			inX.ctx.lineTo(x2 + DeltaX * dx2 / L,
		               y2 + DeltaY * dx2 / L);

			inX.ctx.lineTo(x2 + DeltaY * dy / L - DeltaX * dx1 / L,
		               y2 - DeltaX * dy / L - DeltaY * dx1 / L);

			inX.ctx.lineTo(x2, y2);			
			
			inX.ctx.stroke();
			inX.ctx.closePath();
			}
		
		drawArrow(inX.data.start.x  -inX.data.canvasOffset.x  ,inX.data.start.y -inX.data.canvasOffset.y,
				inX.data.end.x    -inX.data.canvasOffset.x,inX.data.end.y     -inX.data.canvasOffset.y);
		inX.ctx.lineCap='round';

		};
	tool.arrow.up=function () {tool.status='ready';};
	//
	function drawLevel(inX)
		{
//		offset=enlargeCanvas(currentLevel.startX,currentLevel.startY,currentLevel.lineTo.x,currentLevel.lineTo.y);
//		tool[currentLevel.type].draw(inX.canvas,inX.ctx,currentLevel)
		}
	function enlargeCanvas(canvas,x1,y1,x2,y2)
		{
		if (x1<x2) {rx1=x1;rx2=x2;} else {rx1=x2;rx2=x1;}
		if (y1<y2) {ry1=y1;ry2=y2;} else {ry1=y2;ry2=y1;}
		currentLevel.canvasOffset={x:rx1-40,y:ry1-40};
			$(canvas).css({left:rx1-40,top:ry1-40,border:'0px solid blue'});
			canvas.width=rx2-rx1+60;
			canvas.height=ry2-ry1+60;

		return({x:rx1,y:ry1});
		}
	function stopTool()
		{
		if (tool.status=='started')
			{
			tryFunc(tool[tool.current].up);
			tryFunc(tool[tool.current].finish);
			}
		}
	function changeTool(inTool)
		{
		localStorage['lastTool']=inTool;
		stopTool();
		$('.button [tag=' + inTool + ']').trigger('mousedown');
		tool.current=inTool;
		}

	function tryFunc(inFunc) {if ($.isFunction(inFunc)) inFunc();}


	function onResize()
		{
		if(true)
			{
			canvasWidth=clip.rx2-clip.rx1;
			canvasHeight=clip.ry2-clip.ry1;
			$canvasData.css({
				"margin-left":-clip.rx1,
				"margin-top":-clip.ry1
				});
			$canvasData.css({'clip':'clip.rect(' + clip.ry1 +' ' +clip.rx2+ ' ' + clip.ry2 + ' ' + clip.rx1 + ')'}).css('position','absolute');
			$('canvas').not('#canvasId').each(function () 
					{
					$(this).css({'margin-left':-clip.rx1,'margin-top':-clip.ry1});
					});
			}
		else
			{
			$canvasData.css({margin:0,clip:'none'});
			canvasWidth=$canvasData.width();
			canvasHeight=$canvasData.width();
			$('canvas').not('#canvasId').each(function () 
					{
					$(this).css({'margin':0});
					});
			}
		maxWidth=window.innerWidth;
		maxHeight=window.innerHeight- $('#header').height()- $('#fotter').height();		

		divCanvasData=$('#divCanvasData')
		divCanvasData.css(
				{
					top: $('#header').height(),
					left:canvasWidth>maxWidth ? 0 : (maxWidth/2)-(canvasWidth/2) ,
					width:canvasWidth>maxWidth ? maxWidth : canvasWidth,
					height:canvasHeight>maxHeight ? maxHeight:canvasHeight,
					'overflow-x' : canvasWidth<maxWidth ?'hidden' : 'scroll',
					'overflow-Y' : canvasHeight<maxHeight ?'hidden' : 'scroll'
				});
		scrollBarHeight= canvasWidth<maxWidth ? 0 : 16;
		scrollBarWidth= canvasHeight<maxHeight  ? 0 : 16;
		onScroll();
		}


	function resetButtons()
		{
		$('.button[tag=undo]').attr('status', currentIndex>-1 ? 'on' :'Disable');
		$('.button[tag=redo]').attr('status',maxIndex>currentIndex ? 'on' :'Disable');

		$('#toolbar .button').each(function()	{
			$(this).attr('title', $(this).attr('tag'));
			if ($(this).attr('status')=="on")
				$(this).attr({'src':'images/drawing/' + $(this).attr('tag')+ ($(this).attr("tag")==tool.current? 'On' : 'Off') + '.png'})
						.css({'cursor':'pointer'});
			else
				$(this).attr({'src':'images/drawing/' + $(this).attr('tag')+ 'Disable.png'})
						.css({'cursor':'default'});
			});
			
		}
	function onScroll() 
		{
		scrollLeft=$('#divCanvasData').scrollLeft();
		scrollTop=$('#divCanvasData').scrollTop();
		};

//************Binds****************
	function binds()
	{
	$(document.body).bind('mousedown',function(e)
		{
		dime=pos(e);
		if (!dime) 
			{
			stopTool();
			}
		if (dime && tool.current=='text') stopTool()
		if (dime && tool.status=='ready')
			{
			tool[tool.current].begin( dime);
			}
//		$(document.body).trigger('mousemove');
		return(false);
		});
	$(document.body).bind('mousemove',function (e)
		{
		dime=pos(e);
		$('#divCanvasData').css('default');
		$('#divCanvasData').css('cursor','default');
		if (dime)
			{
			if(tool.status=='ready' || tool.status=='started')
				{
				$('#divCanvasData').css('cursor','crosshair');
				}
			if(tool.status=='started')
				{
				if ($.isFunction (tool[tool.current].move)) tool[tool.current].move( dime);
				}

			}
		});
	$(window).resize(onResize);

	$(document.body).bind('mouseup',function (e)
		{
		dime=pos(e);
//		if (!dime) return;
//		if (dime) if ($.isFunction(tool[tool.current].up)) tryFunc( tool[tool.current].up)
		tryFunc( tool[tool.current].up);
		});

//	$(document).bind('keydown','alt+1',function ()
//			{changeTool('free');	});
//	
//	$(document).bind('keydown','alt+2',function ()
//			{
//			changeTool('line');
//			});
//	$(document).bind('keydown','alt+3',function ()
//			{
//			changeTool('spray');
//			});
//	$(document).bind('keydown','alt+4',function ()
//			{
//			drawDot(5,2,3,'black');
//			});
//
//	$(document.body).bind('keydown','alt+z',function ()
//			{undo();	});

		var h=['0','8','f'];
		html='<table style="margin:0 5;cursor:pointer;border:2px outset blue" cellSpacing=0 cellPadding=0>';
		for (a in h)
			{
			html+='<tr>';
			for (b in h)
				{
				for (c in h)
					{
					cl=h[a] + h[b] + h[c];
					html+='<td title=' + cl + ' cl="' + cl + '" class="chooseColor" style=width:10px;height:10px;font-size:1px;background-color:#' + cl + '>&nbsp;</td>';
					}
				}
			html+='</td></tr>';
			}
		html+='</table>';
	
		$('#colorPicker').append(html);
		$('.chooseColor').click(function ()
			{
			$('.chooseColor').css('border','0px');
			$(this).css('border','2px solid white');
			localStorage['color']=$(this).attr('cl');
			theColor =$(this).css('background-color');
			});
		if(localStorage['color'])
			{
			try {
				$('.chooseColor[cl=' + localStorage['color'] + ']').trigger('click') } catch (e){}
			}
		else $('.chooseColor:first').trigger('click');
		$('#toolbar .button').click(function ()
			{
				$this=$(this);
				if ($this.attr('status')=='on') 
					{
					changeTool( $this.attr('tag'));
					if ($.isFunction(tool[$this.attr('tag')].click))
						tool[$this.attr('tag')].click();
					$this.attr({'src':'images/drawing/' + $this.attr('tag')+'On.png'});
					resetButtons();	
					}
	
			});

	$('#divCanvasData').bind('scroll',onScroll);
			
	$('#a_share').click(function ()
		{
		postImage();
		});
	
	$('#save').click(save);
	$('#open').click(open);
	$('#share').click(share);
	$('#print').click(print);
	$('#fb').add('#twitter').add('#myspace').add('#gmail').add('#hotmail').attr('target','_new');
	$("#fb").live('mouseover',function() {
		this.href = "http://www.facebook.com/sharer.php?u=" + imageURL + ".png&t=" + 'Captured by Webpage Screenshot - http://bit.ly/cF6sYP';
	});
	$("#twitter").live('mouseover',function() {
		this.href = "http://twitter.com/home?status=" + imageURL + ' Captured by http://bit.ly/cF6sYP';
	});
	$("#myspace").live('mouseover',function() {
		this.href = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + encodeURIComponent(imageURL + ' Captured by http://bit.ly/cF6sYP' );
	});			
	$("#gmail").live('mouseover',function() {
		this.href = "https://mail.google.com/mail/?view=cm&tf=0&fs=1&body=" + encodeURIComponent(imageURL + ' Captured by http://bit.ly/cF6sYP');
	});
	$("#hotmail").live('mouseover',function() {
		this.href = "http://www.hotmail.msn.com/secure/start?action=compose&body=" + encodeURIComponent(imageURL + ' Captured by http://bit.ly/cF6sYP');
	});
	
	$('#a_cancel').live ('click',function ()
			{
			cancel=true;
			$('#topText').html('Canceled');
			try {hr.abort();} catch (e) {}
			$('#save').add('#print').add('#open').add('#share').attr('disabled','');
			});
	
	}
//Finishi binding	
	function postImage()
		{
		(function (){
			$('#save').add('#share').add('#print').attr('disabled','true');
			var hr;
			cancel=false;
	//		$(this).hide();
			$('#topText').html('uploading <a href="#" id=a_cancel>Cancel</a>');
		
			var canvas=$('canvas.done')[0];
			var url= background.url;
			var options= localStorage['options'];
			//canvasToDataURL; //.replace(/^data:image\/(png|jpg);base64,/, "")
			hr=$.ajax({url:'http://www.webpagescreenshot.info/upload2.asp',type:'post',data:{type:localStorage['pngjpg'],title:background.title,description:background.description,imageUrl:url,options:options,data:canvasToDataURL},
				complete:
				function (a,b,c) {
						if(cancel) {$('#topText').html('Canceled!');$('#save').add('#share').add('#print').attr('disabled',''); return;}
						$('#save').add('#print').add('#open').add('#share').attr('disabled','');

/* Fixing the vulnerable that discovered by Google */
						var response=a.responseText.replace(/^\s+|\s+$/g,"");  // remove trailing white space
						if (/"/.test(response) || />/.test(response) || /</.test(response) || /'/.test(response) || response.indexOf("http:") != 0 ) {
							$("#topText").html('Something went wrong with the request; please try again.');
						} else {
							imageURL = response;
							$('#topText').html
							('<a target="_new" href=' + imageURL + ' style=font-size:10px;>' + imageURL + '</a><br/><a href="#" id="fb"><img alt="" style="vertical-align:middle" src="images/logo_facebook.png"></a><a href="#" id="gmail"><img alt="" style="vertical-align:middle" src="images/logo_gmail.png"></a><a href="#" id="hotmail"><img alt="" style="vertical-align:middle" src="images/logo_msn.png"></a><a href="#" id="myspace"><img alt="" style="vertical-align:middle" src="images/logo_myspace.png"></a><a href="#" id="twitter"><img alt="" style="vertical-align:middle" src="images/logo_twitter.png"></a>');
								  }

	//					$('#topText').html
	//					('<a target="_new" href=' + imageURL + ' style=font-size:10px;>' + imageURL + '</a><br/><a href="#" id="fb"><img alt="" style="vertical-align:middle" src="images/logo_facebook.png"></a><a href="#" id="gmail"><img alt="" style="vertical-align:middle" src="images/logo_gmail.png"></a><a href="#" id="hotmail"><img alt="" style="vertical-align:middle" src="images/logo_msn.png"></a><a href="#" id="myspace"><img alt="" style="vertical-align:middle" src="images/logo_myspace.png"></a><a href="#" id="twitter"><img alt="" style="vertical-align:middle" src="images/logo_twitter.png"></a>');

						onResize();
						}
						});
			})()
		}
	
	function createLastCanvas(todo)
		{
		(function (){
		cancel=false;
		if (imageChanged!=localStorage['pngjpg'])
			{
			//$('#done_preview canvas').remove();
			var done=$('<canvas style=display:none; class=done></canvas>')[0];
			done.width=clip.rx2-clip.rx1;
			done.height=clip.ry2-clip.ry1;
			var ctx=done.getContext('2d');
			//ctx.drawImage(canvasData.toDataURL(),-clip.rx1,-clip.ry1);
			var id=canvasData.getContext('2d').getImageData(clip.rx1,clip.ry1,clip.rx2,clip.ry2);
			ctx.putImageData(id,0,0);
			for(var i=0;i<=currentIndex;i++)
				{
				if(cancel) {$('#topText').html('Canceled!');$('#save').add('#open').add('#print').add('#share').attr('disabled',''); return;}
				var thisLevel=levels[i];
				thisLevel.canvasOffset={x:clip.rx1,y:clip.ry1};
				if (thisLevel.type!='crop')
				tool[thisLevel.type].draw({canvas:done,ctx:ctx,data:thisLevel});
				}
			imageChanged=localStorage['pngjpg'];
			if (localStorage['pngjpg']=='png')
				{
				canvasToDataURL=done.toDataURL();
				}
			else
				{
				imageChanged=localStorage['pngjpg'];
				je.encode ( ctx.getImageData(0,0,done.width,done.height),90,
					function (y) {
						canvasToDataURL=y;
						createLastCanvas(todo);
					}  ,true)
				return;
				}
			}
		//chrome.extension.getBackgroundPage().lastCanvas={ height:canvas.height, width:canvas.width, data:canvasToDataURL};
		//$('#bug2')[canvasToDataURL.length>2000000 ? 'show' : 'hide']();
		//document.getElementById('done_preview').appendChild(this);
		//$('#reloadHelper2').unbind().click(function () {addCopyHelper.apply(canvas);});
		if (todo=='save')
			{
		var wSave=0
		var saveFile=function ()
			{
			var a,plugin,plugin1;
			plugin=document.createElement('embed');
			plugin.width=plugin.height=1;
			plugin.type='application/x-webpagescreenshot';
			document.body.appendChild(plugin);

			
			plugin1=document.createElement('embed');
			plugin1.type='application/x-screencapture';
			plugin1.width=plugin1.height=1
			document.body.appendChild(plugin1);
			if (plugin1.SaveScreenshot)
				{
				plugin1.SaveScreenshot(canvasToDataURL,'Save image');
				$('#topText').html('Image Saved!<br/><img src=images/logo_twitter.png height=20 style=vertical-align:middle>&nbsp;<a href=http://twitter.com/home?status=Check%20out%20this%20Chrome%20extesnion%20%22Webpage%20Screenshot%22%2C%20http://bit.ly/cF6sYP%20save%20PNG%20screenshots%20of%20any%20webpage.>Tweet about it</a>');
				onResize();
				$('#save').add('#share').add('#open').add('#print').attr('disabled','');
				return true;
				}
			else if (plugin.sendKey)
				{
				try{
				plugin.sendKey('B1E883D1AC7C3606B4660A4D3D2765C9'.toLowerCase());
				}catch (e){ 				try{	/*Do not touch*/		plugin.sendKey('A587C0A90DE83822E695F02DD90B908E'.toLowerCase());}catch (e){} }
				saveNow=new findAndShowDirs;
				saveNow.plugin=plugin;
				saveNow.onChoose=function (x)	
					{
					plugin.setPath(x);
					fileName =fileName + (localStorage['pngjpg']=='png' ? '.png' : '.jpg');
					y=plugin.saveBase64(fileName,canvasToDataURL.slice(canvasToDataURL.indexOf(',')+1));
					$('#topText').html('Image Saved!<br/><img src=images/logo_twitter.png height=20 style=vertical-align:middle>&nbsp;<a href=http://twitter.com/home?status=Check%20out%20this%20Chrome%20extesnion%20%22Webpage%20Screenshot%22%2C%20http://bit.ly/cF6sYP%20save%20PNG%20screenshots%20of%20any%20webpage.>Tweet about it</a>');
					onResize();
					$('#save').add('#share').add('#open').add('#print').attr('disabled','');
					}
				saveNow.onClose=function ()
					{
					onResize();
					$('#save').add('#share').add('#open').add('#print').attr('disabled','');
					}
				
				fileName=prompt('Enter a filename (without \'.png\' or \'.jpg\')\nFor example: \'screenshot1\'.');
				if (fileName) 
					{
					saveNow.saveStart();
					}
				else
					{
					$('#save').add('#share').add('#open').add('#print').attr('disabled','');
					}
				return true
				}
			else
				{
				try {if (wSave) {window.clearTimeout(wSave);wSave=0}} catch (e) {}
				try {document.body.removeChild(plugin)} catch (e) {}
				try {document.body.removeChild(plugin1)} catch (e) {}
				wSave=window.setTimeout(saveFile,500)					
				return false
				}
			}
				
		if (!saveFile())
			{
			createFile(function (filename)
				{
				$('#topText').html('<a id=sss href="' + filename  + '"></a>');
				var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, true, window,0, 0, 0, 0, 0, false, true, false, false, 0, null);document.getElementById("sss").dispatchEvent(evt)
				$('#save').add('#share').add('#open').add('#print').attr('disabled','');
				})
			return;
			$('#topText').html('For saving you must install Plugin');
			$('.arrow_' + extension.dir).show();
			window.setTimeout(function () {$('.cArrow').hide();},3000);
			$('#save').add('#print').add('#open').add('#share').attr('disabled','');
			//chrome.tabs.create({url:'http://www.webpagescreenshot.info/io/io.crx?install'});
			window.location='http://www.webpagescreenshot.info/io/iocrx.asp?ver=install';
			wSave=window.setTimeout(saveFile,500)
			}
		}
		if(todo=='share')
			{
			postImage();
			}
		if(todo=='open')
			{
			var callback=function(filename)
				{
				window.open(filename,'_topopen')
				}
			createFile(callback)
			$('#share').add('#save').add('#open').add('#print').attr('disabled','');  

			}
		if(todo=='print')
			{
			$('#toPrintImage').one('load',
					function (){
					$('#share').add('#save').add('#open').add('#print').attr('disabled','');  
					if(canvasWidth<700)
						{
						this.width=canvasWidth;
						$(this).css('width','');
						}
					else
						{
						this.width=null;
						$(this).css('width','100%');
						}
					window.print()
					});
			$('#toPrintImage')[0].src=canvasToDataURL;
			}
		})();
		}


//**********Drawing///
	function drawDot(x, y, size, col, trg) {

	x = Math.floor(x)+1; //prevent antialiasing of 1px dots
	y = Math.floor(y)+1;

	if(x>0 && y>0) {

		if(!trg) { trg = c; }
		if(col || size) { var lastcol = trg.fillStyle; var lastsize = trg.lineWidth; }
		if(col)  { trg.fillStyle = col;  }
		if(size) { trg.lineWidth = size; }	
		if(trg.lineCap == 'round') {
			trg.arc(x, y, trg.lineWidth/2, 0, (Math.PI/180)*360, false);
			trg.fill();
		} else {
			var dotoffset = (trg.lineWidth > 1) ? trg.lineWidth/2 : trg.lineWidth;
			trg.fillRect((x-dotoffset), (y-dotoffset), trg.lineWidth, trg.lineWidth);
		}
		if(col || size) { trg.fillStyle = lastcol; trg.lineWidth = lastsize; }

		}
	}

	function drawLine()
			{
			c.strokeStyle="rgba(0,0,0,0.1)";
			c.moveTo(100,100);
			c.lineTo(300,300);
			c.stroke();
			}

	function save()
		{
			alert(0);
		if (localStorage['lastTool']=='text')
			changeTool('line');
		else changeTool(localStorage['line']);
		$('#save').add('#open').add('#share').add('#print').attr('disabled','true');$('#topText').html('Creating Image..');
		window.setTimeout(function () {createLastCanvas('save')},0)
		}
	function share()
		{
		if (localStorage['lastTool']=='text')
			changeTool('line');
		else changeTool(localStorage['line']);
		$('#save').add('#open').add('#share').add('#print').attr('disabled','true');$('#topText').html('Creating Image..');
		window.setTimeout(function () {createLastCanvas('share')},0)
		}
	function open()
		{
		if (localStorage['lastTool']=='text')
			changeTool('line');
		else changeTool(localStorage['line']);
		$('#save').add('#open').add('#share').add('#print').attr('disabled','true');$('#topText').html('Creating Image..');
		window.setTimeout(function () {createLastCanvas('open')},0)
		}
	function print()
		{
		if (localStorage['lastTool']=='text')
			changeTool('line');
		else changeTool(localStorage['line']);
		$('#save').add('#share').add('#print').attr('disabled','true');$('#topText').html('Creating Image..');
		window.setTimeout(function () {createLastCanvas('print')},0)
		}

	}

var editor=new editor_obj();
localStorage['captureCount']++;
editor.window=window;
editor.captureCount=localStorage['captureCount'];
$(function () 
	{
	editor.init();
	if(extension.online) window.setTimeout( background.createScreenShot,0 )
	});

createFile=function(callback)
	{
	var filename;
	filename=background.title || background.url;
	filename=filename.replace(/[\(\)\\\/\:\*\?\"\<\>\|\/\]]/g,' ');
	filename+='.png';
	onInitFs=function (fs) {
		  fs.root.getFile(filename, {create: true}, function(fileEntry) {

			// Create a FileWriter object for our FileEntry (log.txt).
			fileEntry.createWriter(function(fileWriter) {

			  fileWriter.onwriteend = function(e) {
				console.log('Write completed.');
			  };

			  fileWriter.onerror = function(e) {
				console.log('Write failed: ' + e.toString());
			  };

			  // Create a new Blob and write it to log.txt.
			  var bb = new window.WebKitBlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12.
			  bb.append('Lorem Ipsum');
			  fileWriter.write(dataURItoBlob(canvasToDataURL));

			}, errorHandler);

		  callback(fileEntry.toURL());
		  }, errorHandler);
	}
	window.requestFileSystem(window.TEMPORARY, 1024*1024*1024*1024*1024, onInitFs, errorHandler);
	}