/*!
 * noiseGen Plugin for jQuery
 *
 * Version 0.1.2
 *
 * Copyright 2011, Luca Ongaro
 * Licensed under the MIT license.
 *
 */
(function(a){"use strict",a.fn.noiseGen=function(b){var c={width:50,height:50,opacity:.2,fallbackImage:!1,grainDimension:1,fromColor:"000000",toColor:"606060",independentChannels:!1,distribution:"bell",bias:0,useCache:!1},d=document.createElement("canvas");b=a.extend(c,b);if(typeof b.distribution=="string")switch(b.distribution){case"uniform":b.distribution=1;break;case"triangular":b.distribution=2;break;case"bell":b.distribution=5;break;default:b.distribution=5}else b.distribution=Math.abs(parseInt(b.distribution));if(!d.getContext||!d.getContext("2d"))return b.fallbackImage?this.css("background-image","url("+b.fallbackImage+")"):this;var e=d.getContext("2d"),f=0,g=0,h=null,i={parametricRandom:function(a,b){var c,d=0;for(c=0;c<a;c++)d+=Math.random();return Math.pow(d/c,Math.pow(1.2,b))},hexToRGB:function(a){return a=parseInt(a,16),{red:(a&16711680)>>16,green:(a&65280)>>8,blue:a&255}},mapToRange:function(a,b,c){return Math.floor(a*(c-b))+b},hasLocalStorage:function(){try{return!!localStorage.getItem}catch(a){return!1}}()};d.width=b.width,d.height=b.height,typeof b.grainDimension=="number"&&(b.grainDimension={width:b.grainDimension,height:b.grainDimension}),!!b.useCache&&i.hasLocalStorage&&!!window.JSON&&(h=localStorage.getItem(window.JSON.stringify(b)));if(!h){while(f<d.width){while(g<d.height){var j=i.hexToRGB(b.fromColor),k=i.hexToRGB(b.toColor),l,m,n;if(!b.independentChannels){var o=i.parametricRandom(b.distribution,b.bias);l=i.mapToRange(o,j.red,k.red),m=i.mapToRange(o,j.green,k.green),n=i.mapToRange(o,j.blue,k.blue)}else l=i.mapToRange(i.parametricRandom(b.distribution,b.bias),j.red,k.red),m=i.mapToRange(i.parametricRandom(b.distribution,b.bias),j.green,k.green),n=i.mapToRange(i.parametricRandom(b.distribution,b.bias),j.blue,k.blue);e.fillStyle="rgba("+l+","+m+","+n+","+b.opacity+")",e.fillRect(f,g,b.grainDimension.width,b.grainDimension.height),g+=b.grainDimension.height}g=0,f+=b.grainDimension.width}h=d.toDataURL("image/png");if(!!b.useCache&&i.hasLocalStorage)try{localStorage.setItem(window.JSON.stringify(b),h)}catch(p){}}return this.css("background-image","url("+h+")")}})(jQuery)
