/*
 * Pixastic - JavaScript Image Processing Library
 * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
 * MIT License [http://www.pixastic.com/lib/license.txt]
 */
var Pixastic=function(){function a(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,c)}function b(b){var c=!1,d=function(){c||(c=!0,b())};document.write('<script defer src="//:" id="__onload_ie_pixastic__"></script>');var e=document.getElementById("__onload_ie_pixastic__");e.onreadystatechange=function(){e.readyState=="complete"&&(e.parentNode.removeChild(e),d())},document.addEventListener&&document.addEventListener("DOMContentLoaded",d,!1),a(window,"load",d)}function c(){var a=d("pixastic",null,"img"),b=d("pixastic",null,"canvas"),c=a.concat(b);for(var e=0;e<c.length;e++)(function(){var a=c[e],b=[],d=a.className.split(" ");for(var f=0;f<d.length;f++){var g=d[f];if(g.substring(0,9)=="pixastic-"){var h=g.substring(9);h!=""&&b.push(h)}}if(b.length)if(a.tagName.toLowerCase()=="img"){var j=new Image;j.src=a.src;if(j.complete)for(var k=0;k<b.length;k++){var l=Pixastic.applyAction(a,a,b[k],null);l&&(a=l)}else j.onload=function(){for(var c=0;c<b.length;c++){var d=Pixastic.applyAction(a,a,b[c],null);d&&(a=d)}}}else setTimeout(function(){for(var c=0;c<b.length;c++){var d=Pixastic.applyAction(a,a,b[c],null);d&&(a=d)}},1)})()}function d(a,b,c){var d=new Array;b==null&&(b=document),c==null&&(c="*");var e=b.getElementsByTagName(c),f=e.length,g=new RegExp("(^|\\s)"+a+"(\\s|$)");for(i=0,j=0;i<f;i++)g.test(e[i].className)&&(d[j]=e[i],j++);return d}function f(a,b){if(!Pixastic.debug)return;try{switch(b){case"warn":console.warn("Pixastic:",a);break;case"error":console.error("Pixastic:",a);break;default:console.log("Pixastic:",a)}}catch(c){}!!e}typeof pixastic_parseonload!="undefined"&&pixastic_parseonload&&b(c);var e,g=function(){var a=document.createElement("canvas"),b=!1;try{b=typeof a.getContext=="function"&&!!a.getContext("2d")}catch(c){}return function(){return b}}(),h=function(){var a=document.createElement("canvas"),b=!1,c;try{typeof a.getContext=="function"&&(c=a.getContext("2d"))&&(b=typeof c.getImageData=="function")}catch(d){}return function(){return b}}(),k=function(){var a=!1,b=document.createElement("canvas");if(g()&&h()){b.width=b.height=1;var c=b.getContext("2d");c.fillStyle="rgb(255,0,0)",c.fillRect(0,0,1,1);var d=document.createElement("canvas");d.width=d.height=1;var e=d.getContext("2d");e.fillStyle="rgb(0,0,255)",e.fillRect(0,0,1,1),c.globalAlpha=.5,c.drawImage(d,0,0);var f=c.getImageData(0,0,1,1).data;a=f[2]!=255}return function(){return a}}();return{parseOnLoad:!1,debug:!1,applyAction:function(a,b,c,d){d=d||{};var e=a.tagName.toLowerCase()=="canvas";if(e&&Pixastic.Client.isIE())return Pixastic.debug&&f("Tried to process a canvas element but browser is IE."),!1;var g,h,i=!1;Pixastic.Client.hasCanvas()&&(i=!!d.resultCanvas,g=d.resultCanvas||document.createElement("canvas"),h=g.getContext("2d"));var j=a.offsetWidth,k=a.offsetHeight;e&&(j=a.width,k=a.height);if(j==0||k==0){if(a.parentNode!=null){Pixastic.debug&&f("Image has 0 width and/or height.");return}var l=a.style.position,m=a.style.left;a.style.position="absolute",a.style.left="-9999px",document.body.appendChild(a),j=a.offsetWidth,k=a.offsetHeight,document.body.removeChild(a),a.style.position=l,a.style.left=m}if(c.indexOf("(")>-1){var n=c;c=n.substr(0,n.indexOf("("));var o=n.match(/\((.*?)\)/);if(o[1]){o=o[1].split(";");for(var p=0;p<o.length;p++){thisArg=o[p].split("=");if(thisArg.length==2)if(thisArg[0]=="rect"){var q=thisArg[1].split(",");d[thisArg[0]]={left:parseInt(q[0],10)||0,top:parseInt(q[1],10)||0,width:parseInt(q[2],10)||0,height:parseInt(q[3],10)||0}}else d[thisArg[0]]=thisArg[1]}}}d.rect?(d.rect.left=Math.round(d.rect.left),d.rect.top=Math.round(d.rect.top),d.rect.width=Math.round(d.rect.width),d.rect.height=Math.round(d.rect.height)):d.rect={left:0,top:0,width:j,height:k};var r=!1;Pixastic.Actions[c]&&typeof Pixastic.Actions[c].process=="function"&&(r=!0);if(!r)return Pixastic.debug&&f('Invalid action "'+c+'". Maybe file not included?'),!1;if(!Pixastic.Actions[c].checkSupport())return Pixastic.debug&&f('Action "'+c+'" not supported by this browser.'),!1;Pixastic.Client.hasCanvas()?(g!==a&&(g.width=j,g.height=k),i||(g.style.width=j+"px",g.style.height=k+"px"),h.drawImage(b,0,0,j,k),a.__pixastic_org_image?(g.__pixastic_org_image=a.__pixastic_org_image,g.__pixastic_org_width=a.__pixastic_org_width,g.__pixastic_org_height=a.__pixastic_org_height):(g.__pixastic_org_image=a,g.__pixastic_org_width=j,g.__pixastic_org_height=k)):Pixastic.Client.isIE()&&typeof a.__pixastic_org_style=="undefined"&&(a.__pixastic_org_style=a.style.cssText);var s={image:a,canvas:g,width:j,height:k,useData:!0,options:d},t=Pixastic.Actions[c].process(s);return t?Pixastic.Client.hasCanvas()?(s.useData&&Pixastic.Client.hasCanvasImageData()&&(g.getContext("2d").putImageData(s.canvasData,d.rect.left,d.rect.top),g.getContext("2d").fillRect(0,0,0,0)),d.leaveDOM||(g.title=a.title,g.imgsrc=a.imgsrc,e||(g.alt=a.alt),e||(g.imgsrc=a.src),g.className=a.className,g.style.cssText=a.style.cssText,g.name=a.name,g.tabIndex=a.tabIndex,g.id=a.id,a.parentNode&&a.parentNode.replaceChild&&a.parentNode.replaceChild(g,a)),d.resultCanvas=g,g):a:!1},prepareData:function(a,b){var c=a.canvas.getContext("2d"),d=a.options.rect,e=c.getImageData(d.left,d.top,d.width,d.height),f=e.data;return b||(a.canvasData=e),f},process:function(a,b,c,d){if(a.tagName.toLowerCase()=="img"){var e=new Image;e.src=a.src;if(e.complete){var f=Pixastic.applyAction(a,e,b,c);return d&&d(f),f}e.onload=function(){var f=Pixastic.applyAction(a,e,b,c);d&&d(f)}}if(a.tagName.toLowerCase()=="canvas"){var f=Pixastic.applyAction(a,a,b,c);return d&&d(f),f}},revert:function(a){if(Pixastic.Client.hasCanvas()){if(a.tagName.toLowerCase()=="canvas"&&a.__pixastic_org_image)return a.width=a.__pixastic_org_width,a.height=a.__pixastic_org_height,a.getContext("2d").drawImage(a.__pixastic_org_image,0,0),a.parentNode&&a.parentNode.replaceChild&&a.parentNode.replaceChild(a.__pixastic_org_image,a),a}else Pixastic.Client.isIE()&&typeof a.__pixastic_org_style!="undefined"&&(a.style.cssText=a.__pixastic_org_style)},Client:{hasCanvas:g,hasCanvasImageData:h,hasGlobalAlpha:k,isIE:function(){return!!document.all&&!!window.attachEvent&&!window.opera}},Actions:{}}}();typeof jQuery!="undefined"&&jQuery&&jQuery.fn&&(jQuery.fn.pixastic=function(a,b){var c=[];return this.each(function(){if(this.tagName.toLowerCase()=="img"&&!this.complete)return;var d=Pixastic.process(this,a,b);d&&c.push(d)}),c.length>0?jQuery(c):this}),Pixastic.Actions.desaturate={process:function(a){var b=!!a.options.average&&a.options.average!="false";if(Pixastic.Client.hasCanvasImageData()){var c=Pixastic.prepareData(a),d=a.options.rect,e=d.width,f=d.height,g=e*f,h=g*4,i,j;if(b)while(g--)c[h-=4]=c[i=h+1]=c[j=h+2]=(c[h]+c[i]+c[j])/3;else while(g--)c[h-=4]=c[i=h+1]=c[j=h+2]=c[h]*.3+c[i]*.59+c[j]*.11;return!0}if(Pixastic.Client.isIE())return a.image.style.filter+=" gray",!0},checkSupport:function(){return Pixastic.Client.hasCanvasImageData()||Pixastic.Client.isIE()}};
