/**
 * PhotoJShop
 * A JavaScript library to perform image editing using filters similar to those found
 * in most photo editing software.
 * jQuery Plugin package
 *
 * Requirements:
 *	- jQuery
 * 
 * Licensed under the MIT License
 *
 * @author Alejandro U. Alvarez <alejandro@urbanoalvarez.es>
 * @version 1.0.4
 */
(function($, window, document, undefined) { $.fn.PhotoJShop=function(e){function c(e){var r=u,i=a,c=r.data.length,p=t.matrix,d=null,v=0,m=0,g,y,b;if(p==null&&t.effect!==null)p=f[t.effect];if(p==null&&t.color==null){console.error("PhotoJShop error: Please specify an effect or custom matrix");return n}else if(p!==null){var w=Math.floor(p.length/2),E=Math.floor(p[0].length/2),S=p.length*p[0].length,x,T,N,C}if(t.color!==null)d=l[t.color];for(g=0;g<r.height;g++){for(y=0;y<r.width*4;y++){if(m==3){i.data[v]=r.data[v];v++;m=0;continue}b=0;C=S;if(d!==null){b=r.data[v-m]*d[m][0];b+=r.data[v-m+1]*d[m][1];b+=r.data[v-m+2]*d[m][2];b=Math.min(Math.max(b/3,0),255);i.data[v]=b;b=0}if(p!==null){for(x=0;x<p.length;x++){for(T=0;T<p[0].length;T++){if(p[x][T]==0)continue;N=0;if(x<w){N-=(w-x)*r.width*4}else if(x>w){N+=(x-w)*r.width*4}N+=(E-T)*4;if(v+N<0||N>r.data.length){C--}else{b+=p[x][T]*r.data[v+N]}}}}if(p!==null){b/=C;b=Math.min(Math.max(b,0),255);if(p!==null)i.data[v]=b}v++;m++}}o.putImageData(i,0,0);if(t.replace){if(n.is("img"))$("img").attr("src",h());else s.putImageData(i,0,0);return n}else{return h()}}function h(){return i.toDataURL()}var t=$.extend({effect:null,color:null,replace:true,matrix:null},e);if(typeof e=="string"){t.effect=e}var n=this,r=this,i=document.createElement("canvas"),s=null,o=i.getContext("2d"),u=null,a=null,f={blur:[[1,1,1],[1,1,1],[1,1,1]],blur2:[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],sharpen:[[0,-1,0],[-1,13,-1],[0,-1,0]],emboss:[[-10,-7,0],[10,8,-10],[0,7,10]],lighten:[[1.1]],darken:[[.9]],"edge-enhance":[[0,0,0],[-20,20,0],[0,0,0]],"edge-detect":[[0,9,0],[9,-40,9],[0,9,0]],"hard-edge":[[2,22,1],[22,1,-22],[1,-22,-2]],laplace:[[-2,-2,-2],[-2,15,-2],[-2,-2,-2]]},l={"b&w":[[1,1,1],[1,1,1],[1,1,1]],sepia:[[1.2,1.1,1],[.8,.7,.6],[.3,.2,.1]],vintage:[[4,.5,-.6],[-.2,3.1,-.1],[.6,.4,2]],recolor:[[.1,1.5,1.5],[1.5,.1,1.5],[1.5,1.5,.1]],red:[[3,0,0],[3,0,0],[3,0,0]],blue:[[0,0,3],[0,0,3],[0,0,3]],green:[[0,3,0],[0,3,0],[0,3,0]]};i.width=this.width(),i.height=this.height();o.beginPath();o.rect(0,0,i.width,i.height);o.fillStyle="rgba(0,0,0,0)";o.fill();a=o.getImageData(0,0,i.width,i.height);if(this.is("img")){r=document.createElement("canvas");s=r.getContext("2d");r.width=this.width();r.height=this.height();s.drawImage(this.get(0),0,0);u=s.getImageData(0,0,r.width,r.height);c();return}else if(!this.is("canvas")){alert("Unsupported element");return}s=r.getContext("2d");u=s.getImageData(0,0,this.width(),this.height());c()} })(jQuery, window, document);