/*
 Simple <canvas> Word Cloud
 by timdream
 
 cutomized by HitHot.cc

 usage:
  $('#canvas').wordCloud(settings); // draw word cloud on #canvas.
  $.wordCloudSupported // return true if the browser checks out
  $.miniumFontSize // return minium font size enforced by the browser
 
 available settings
	fontFamily: font list for text.
	gridSize: 8,
	ellipticity: ellipticity of the circle formed by word.
	center: [x,y] of the center of the circle. Set false to use center of canvas.
	drawMask: true to debug mask to show area covered by word.
	maskColor: color of the debug mask.
	maskGridWidth: width of the mask grid border.
	wordColor: color for word, could be one of the following:
		[CSS color value],
		'random-dark', (default)
		'random-light',
		[function(word, weight, fontSize, radius, theta)]
	backgroundColor: background to cover entire canvas or the detect against.
	wait: wait N ms before drawing next word.
	abortThreshold: abort and execute about() when the browser took more than N ms to draw a word. 0 to disable.
	abort: abort handler.
	weightFactor: 
	minSize: minium font size in pixel to draw (default: $.miniumFontSize / 2, larger than that is still look good using bilinear sampling in browser)
	wordList: 2d array in for word list like [['w1', 12], ['w2', 6]]
	clearCanvas: clear canvas before drawing. Faster than running detection on what's already on it.
	fillBox: true will mark the entire box containing the word as filled - no subsequent smaller words can be fit in the gap.
	shape: keyword or a function that represents polar equation r = fn(theta), available keywords:
		'circle', (default)
		'cardioid', (apple or heart shape curve, the most known polar equation)
		'diamond', (alias: 'square'),
		'triangle-forward',
		'triangle', (alias: 'triangle-upright')
		'pentagon',
		'star'
 
    trace mousemove and catch keyword under.
    startCallback: must be function
    finishCallback: must be function
    clickCallback: must be function
    zoomToFit: true / false
    zoomToFitWidthPercentage: 0 to 1
    zoomToFitHeightPercentage: 0 to 1
    fasterGridEmptyChecker: true / false
*/
"use strict";Array.prototype.shuffle=function(){for(var b,a,c=this.length;c;b=parseInt(Math.random()*c),a=this[--c],this[c]=this[b],this[b]=a){}return this};if(!window.setImmediate){window.setImmediate=(function(){return window.msSetImmediate||window.webkitSetImmediate||window.mozSetImmediate||window.oSetImmediate||(function(){if(window.postMessage&&window.addEventListener){var b=[],e=-1,c=-1,a="zero-timeout-message",d=function(g){b.push(g);window.postMessage(a,"*");return ++c},f=function(h){if(h.data==a){h.stopPropagation();if(b.length>0){var g=b.shift();g();e++}}};window.addEventListener("message",f,true);window.clearImmediate=function(h){if(typeof h!=="number"||h>c){return}var g=h-e-1;b[g]=(function(){})};return d}})()||function(a){window.setTimeout(a,0)}})()}if(!window.clearImmediate){window.clearImmediate=(function(){return window.msClearImmediate||window.webkitClearImmediate||window.mozClearImmediate||window.oClearImmediate||function(a){window.clearTimeout(a)}})()}(function(a){a.wordCloudSupported=(function(){var c=a("<canvas />"),b;if(!c[0]||!c[0].getContext){return false}b=c[0].getContext("2d");if(!b.getImageData){return false}if(!b.fillText){return false}if(!Array.prototype.some){return false}if(!Array.prototype.push){return false}if(/opera mini/i.test(window.navigator.userAgent.toLowerCase())){return false}if(a.browser.msie&&/msie 7\.0/i.test(window.navigator.userAgent.toLowerCase())){return false}return true}());a.miniumFontSize=(function(){if(!a.wordCloudSupported){return}var e=document.createElement("canvas").getContext("2d"),c=20,d,b;while(c){e.font="Bold "+c.toString(10)+"px sans-serif";if(e.measureText("\uFF37").width===d&&e.measureText("m").width===b){return c+1}d=e.measureText("\uFF37").width;b=e.measureText("m").width;c--}return 0})();a.fn.wordCloud=function(d){if(!a.wordCloudSupported){return this}var x={adjustX:0,adjustY:0,fontFamily:'"Helvetica Neue",Arial,微軟正黑體,"Microsoft JhengHei","Microsoft YaHei","Lucida Grande","Lucida Sans Unicode",sans-serif',gridSize:8,ellipticity:0.65,center:false,drawMask:false,maskColor:"rgba(255,0,0,0.3)",maskGridWidth:0.3,wordColor:"random-dark",shadowType:1,backgroundColor:"rgba(0, 0, 0, 0)",wait:0,abortThreshold:1000,abort:a.noop,weightFactor:1,minSize:a.miniumFontSize/2,wordList:[],rotateRatio:0.1,clearCanvas:true,fasterGridEmptyChecker:false,fillBox:false,shape:"circle",finished:false};if(d){a.extend(x,d)}if(typeof x.weightFactor!=="function"){var o=x.weightFactor;x.weightFactor=function(g){return g*o}}if(typeof x.shape!=="function"){switch(x.shape){case"circle":default:x.shape=function(g){return 1};break;case"cardioid":x.shape=function(g){return 1-Math.sin(g)};break;case"diamond":case"square":x.shape=function(g){var C=g%(2*Math.PI/4);return 1/(Math.cos(C)+Math.sin(C))};break;case"triangle-forward":x.shape=function(g){var C=g%(2*Math.PI/3);return 1/(Math.cos(C)+Math.sqrt(3)*Math.sin(C))};break;case"triangle":case"triangle-upright":x.shape=function(g){var C=(g+Math.PI*3/2)%(2*Math.PI/3);return 1/(Math.cos(C)+Math.sqrt(3)*Math.sin(C))};break;case"pentagon":x.shape=function(g){var C=(g+0.955)%(2*Math.PI/5);return 1/(Math.cos(C)+0.726543*Math.sin(C))};break;case"star":x.shape=function(g){var C=(g+0.955)%(2*Math.PI/10);if((g+0.955)%(2*Math.PI/5)-(2*Math.PI/10)>=0){return 1/(Math.cos((2*Math.PI/10)-C)+3.07768*Math.sin((2*Math.PI/10)-C))}else{return 1/(Math.cos(C)+3.07768*Math.sin(C))}};break}}x.gridSize=Math.max(x.gridSize,4);var w=x.gridSize,r,b,q,k,j,i,t,m,B=function(D,G,L,I,E){switch(x.wordColor){case"random-dark":return"rgb("+Math.floor(Math.random()*128).toString(10)+","+Math.floor(Math.random()*128).toString(10)+","+Math.floor(Math.random()*128).toString(10)+")";break;case"random-light":return"rgb("+Math.floor(Math.random()*128+128).toString(10)+","+Math.floor(Math.random()*128+128).toString(10)+","+Math.floor(Math.random()*128+128).toString(10)+")";break;case"random-normal":return"rgb("+Math.floor(Math.random()*64+128).toString(10)+","+Math.floor(Math.random()*128+32).toString(10)+","+Math.floor(Math.random()*128+32).toString(10)+")";break;case"random-impress":var K=Math.floor(Math.random()*2)+2;var F=1;var C,H,J;if(Math.floor(Math.random()*2)>0&&++F<=K){C=Math.floor(Math.random()*64+128).toString(10)}else{C=Math.floor(Math.random()*32+0).toString(10)}if((K-F>1||Math.floor(Math.random()*2)>0)&&++F<=K){H=Math.floor(Math.random()*64+128).toString(10)}else{H=Math.floor(Math.random()*32+0).toString(10)}if((K-F>0||Math.floor(Math.random()*2)>0)&&++F<=K){J=Math.floor(Math.random()*64+128).toString(10)}else{J=Math.floor(Math.random()*32+0).toString(10)}return"rgb("+C+","+H+","+J+")";break;default:if(typeof x.wordColor!=="function"){return x.wordColor}else{return x.wordColor(D,G,L,I,E)}}},c=function(){return(x.abortThreshold>0&&(new Date()).getTime()-m>x.abortThreshold)},n=function(E,g,G,C,D,F){return E[(G*C+g)*4+F]},p=function(G,F,E,H,g){var D=H,C=g;while(D--){if((F+D)>=k){continue}C=g;while(C--){if((E+C)>=j){continue}if(typeof(b[F+D][E+C])!=undefined&&b[F+D][E+C]){return false}}}return true},l=function(H,g,I,C,G){var F=w,E;if(!isNaN(i)){while(F--){E=w;while(E--){if(n(H.data,g+F,I+E,C,G,i)!==t[i]){return false}}}}else{var D;while(F--){E=w;while(E--){D=4;while(D--){if(H.data[((I+E)*C+g+F)*4+D]!==t[D]){return false}}}}}return true},h=function(E,D,F,C){var g=F,G;if(x.drawMask){r.fillStyle=x.maskColor}while(g--){G=C;while(G--){b[E+g][D+G]=false;if(x.drawMask){r.fillRect((E+g)*w,(D+G)*w,w-x.maskGridWidth,w-x.maskGridWidth)}}}},e=function(C){var g=C.gw,E;if(x.drawMask){r.fillStyle=x.maskColor}var D=r.getImageData(C.gx*w,C.gy*w,C.gw*w,C.gh*w);out:while(g--){E=C.gh;while(E--){if((x.fasterGridEmptyChecker&&!p(D,g,E,C.gw,C.gh))||!l(D,g*w,E*w,C.gw*w,C.gh*w)){b[C.gx+g][C.gy+E]=false;q[C.gx+g][C.gy+E]=C;if(x.drawMask){r.fillRect((C.gx+g)*w,(C.gy+E)*w,w-x.maskGridWidth,w-x.maskGridWidth)}}if(c()){break out}}}},s=function(S,N,L,U,W,V,Y,I,E,M,T,O,H){var P=document.createElement("canvas");P.setAttribute("width",L);P.setAttribute("height",U);var X=P.getContext("2d");X.save();X.textBaseline="top";X.font="Bold "+E.toString(10)+"px "+x.fontFamily;var G=Math.ceil(E/50);if(T){X.translate(0,U);X.rotate(-Math.PI/2);var g=Y;var D=I;V+=(g-Math.max(X.measureText("m").width,X.measureText("\uFF37").width))/2;var K=[[],[{x:0,y:0,blur:G,color:"#fff"},{x:0,y:0,blur:G*2,color:"#fff"},{x:0,y:0,blur:G*5,color:M},{x:0,y:0,blur:G*7,color:M},],[{x:-0.03*g,y:0,blur:0,color:"red"},{x:0.03*g,y:0,blur:0,color:"cyan"},],[{x:0,y:0,blur:G/2,color:M},{x:0,y:0,blur:G,color:M},{x:0,y:0,blur:G*1.5,color:M},],[{x:0,y:0,blur:G,color:"#232323"},{x:0.01*g,y:0.01*g,blur:G/10,color:"#232323"},],[{x:0.01*g,y:0.01*g,blur:0,color:"#000099"},],[{x:0.01*g,y:0.01*g,blur:0,color:"#000099"},{x:0.03*g,y:0.02*g,blur:0,color:"#000099"},{x:0.01*g,y:0.01*g,blur:3,color:"#000000"},{x:0.03*g,y:0.02*g,blur:3,color:"#000000"},]];var C=H?K[3]:K[x.shadowType];var R=C.length;while(R--){var J=C[R];var F=D+J.blur*2;X.save();X.beginPath();X.rect(0,0,D,g);X.clip();if(J.blur){X.shadowColor=J.color;X.shadowOffsetX=J.x;X.shadowOffsetY=J.y;X.shadowBlur=J.blur;X.fillText(N,W,V)}else{X.fillText(N,W-(J.x||0),V-(J.y||0))}X.restore()}var Q=X.createLinearGradient(W,V,W,V+g);Q.addColorStop(0,"#fff");Q.addColorStop(0.3,M);X.fillStyle=M;X.fillStyle=Q;X.fillText(N,W,V)}else{var g=I;var D=Y;V+=(g-Math.max(X.measureText("m").width,X.measureText("\uFF37").width))/2;var K=[[],[{x:0,y:0,blur:G,color:"#fff"},{x:0,y:0,blur:G*2,color:"#fff"},{x:0,y:0,blur:G*5,color:M},{x:0,y:0,blur:G*7,color:M},],[{x:-0.03*g,y:0,blur:0,color:"red"},{x:0.03*g,y:0,blur:0,color:"cyan"},],[{x:0,y:0,blur:G/2,color:M},{x:0,y:0,blur:G,color:M},{x:0,y:0,blur:G*1.5,color:M},],[{x:0,y:0,blur:G,color:"#232323"},{x:0.01*g,y:0.01*g,blur:G/10,color:"#232323"},],[{x:0.01*g,y:0.01*g,blur:0,color:"#000099"},],[{x:0.01*g,y:0.01*g,blur:0,color:"#000099"},{x:0.03*g,y:0.02*g,blur:0,color:"#000099"},{x:0.01*g,y:0.01*g,blur:3,color:"#000000"},{x:0.03*g,y:0.02*g,blur:3,color:"#000000"},]];var C=H?K[6]:K[x.shadowType];var R=C.length;while(R--){var J=C[R];var F=D+J.blur*2;X.save();X.beginPath();X.rect(0,0,D,g);X.clip();if(J.blur){X.shadowColor=J.color;X.shadowOffsetX=J.x;X.shadowOffsetY=J.y;X.shadowBlur=J.blur;X.fillText(N,W,V)}else{X.fillText(N,W-(J.x||0),V-(J.y||0))}X.restore()}var Q=X.createLinearGradient(W,V,W,V+g);Q.addColorStop(0,"#fff");Q.addColorStop(0.3,M);X.fillStyle=M;X.fillStyle=Q;X.fillText(N,W,V)}X.restore();return P},y=function(D,F){var I=(D.gw*w-D.w)/2,H=(D.gh*w-D.h)/2;var C=D.gx*w+I,J=D.gy*w+H;var G=D.gw*w,K=D.gh*w;var g=("color" in D)?D.color:B(D.word,D.weight,D.fontSize,D.r,D.theta);a.extend(D,{color:g});if(D.mu!==1||D.rotate){var E=s(r,D.word,G*D.mu,K*D.mu,I,H,D.w,D.h,D.fontSize,g,D.rotate,D.mu,F);r.drawImage(E,Math.floor(C),Math.floor(J),G,K)}else{r.font="Bold "+D.fontSize.toString(10)+"px "+x.fontFamily;var E=s(r,D.word,G,K,I,H,D.w,D.h,D.fontSize,g,D.rotate,D.mu,F);r.drawImage(E,Math.floor(C),Math.floor(J),G,K)}},v=function(Q,I){var J,S,M=1,O=(Math.random()<x.rotateRatio),g=x.weightFactor(I);if(g<=x.minSize){return false}if(g<a.miniumFontSize){M=(function(){var R=2;while(R*g<a.miniumFontSize){R+=2}return R})()}r.font="Bold "+(g*M).toString(10)+"px "+x.fontFamily;if(O){var P=r.measureText(Q).width/M,H=Math.max(g*M,r.measureText("m").width,r.measureText("\uFF37").width)/M;H*=3/2;H+=Math.floor(g/8);P+=Math.floor(g/8)}else{var H=r.measureText(Q).width/M,P=Math.max(g*M,r.measureText("m").width,r.measureText("\uFF37").width)/M;P*=3/2;P+=Math.floor(g/8);H+=Math.floor(g/8)}H=Math.ceil(H);P=Math.ceil(P);J=Math.ceil(H/w),S=Math.ceil(P/w);var U=(x.center)?[x.center[0]/w,x.center[1]/w]:[k/2,j/2];var E=Math.floor(Math.sqrt(k*k+j*j)),C=k+j,L,K,N,G,F;L=E+1;while(L--){K=C;N=[];while(K--){var D=x.shape(K/C*2*Math.PI);N.push([Math.floor(U[0]+(E-L)*D*Math.cos(-K/C*2*Math.PI)-J/2),Math.floor(U[1]+(E-L)*D*x.ellipticity*Math.sin(-K/C*2*Math.PI)-S/2),K/C*2*Math.PI])}if(N.shuffle().some(function(T){if(u(T[0],T[1],J,S)){var R={word:Q,weight:I,fontSize:g,r:E-L,theta:T[2],gx:T[0],gy:T[1],gw:J,gh:S,w:H,h:P,mu:M,rotate:O};y(R,false);e(R);return true}return false})){return true}}return false},u=function(E,D,F,C){if(E<0||D<0||E+F>k||D+C>j){return false}var g=F,G;while(g--){G=C;while(G--){if(!b[E+g][D+G]){return false}}}return true};var f=false,A=false,z=false;this.mousemove(function(F){if(!x.finished){return}var E,C,g,K;if(document.layers){E=F.pageX;C=F.pageY;g=window.innerWidth+window.pageXOffset;K=window.innerHeight+window.pageYOffset}else{if(document.all){E=window.event.x+document.body.scrollLeft;C=window.event.y+document.body.scrollTop;g=document.body.clientWidth+document.body.scrollLeft;K=document.body.clientHeight+document.body.scrollTop}else{if(document.getElementById){E=F.pageX;C=F.pageY;g=window.innerWidth+window.pageXOffset;K=window.innerHeight+window.pageYOffset}}}var H,G;H=E+x.adjustX();G=C+x.adjustY();var J=Math.ceil(H/w);var I=Math.ceil(G/w);if(f){var D=q[A][z];r.putImageData(f,(D.gx-0)*w,(D.gy-0)*w);f=false}if(q[J][I]){var D=q[J][I];f=r.getImageData((D.gx-0)*w,(D.gy-0)*w,(D.gw+0)*w,(D.gh+0)*w);A=J;z=I;y(D,true)}});this.click(function(E){var D,C,g,J;if(document.layers){D=E.pageX;C=E.pageY;g=window.innerWidth+window.pageXOffset;J=window.innerHeight+window.pageYOffset}else{if(document.all){D=window.event.x+document.body.scrollLeft;C=window.event.y+document.body.scrollTop;g=document.body.clientWidth+document.body.scrollLeft;J=document.body.clientHeight+document.body.scrollTop}else{if(document.getElementById){D=E.pageX;C=E.pageY;g=window.innerWidth+window.pageXOffset;J=window.innerHeight+window.pageYOffset}}}var G,F;G=D+x.adjustX();F=C+x.adjustY();var I=Math.ceil(G/w);var H=Math.ceil(F/w);if(q[I][H]&&typeof x.clickCallback=="function"){x.clickCallback(q[I][H].word)}});return this.each(function(){if(this.nodeName.toLowerCase()!=="canvas"){return}var L=a(this);if(!(x.zoomToFit===undefined)&&x.zoomToFit){var F,g;if(x.zoomToFitWidthPercetage===undefined){L.attr("width",Math.floor(window.innerWidth*1))}else{L.attr("width",Math.floor(window.innerWidth*x.zoomToFitWidthPercetage))}if(x.zoomToFitHeightPercetage===undefined){L.attr("height",Math.floor(window.innerHeight*1))}else{L.attr("height",Math.floor(window.innerHeight*x.zoomToFitHeightPercetage))}}if(typeof x.startCallback=="function"){x.startCallback()}k=Math.floor(L.attr("width")/w);j=Math.floor(L.attr("height")/w);r=this.getContext("2d"),b=[];q=[];var E=document.createElement("canvas").getContext("2d");E.fillStyle=x.backgroundColor;E.clearRect(0,0,1,1);E.fillStyle=x.backgroundColor;E.fillRect(0,0,1,1);t=E.getImageData(0,0,1,1).data;if(typeof x.wordColor!=="function"&&x.wordColor.substr(0,6)!=="random"){E.fillStyle=x.wordColor;E.fillRect(0,0,1,1);var D=E.getImageData(0,0,1,1).data;var G=4;while(G--){if(Math.abs(D[G]-t[G])>10){i=G;break}}}else{i=NaN}var K=k,J;while(K--){b[K]=[];q[K]=[];J=j;while(J--){b[K][J]=true;q[K][J]=false}}if(x.clearCanvas){r.fillStyle=x.backgroundColor;r.clearRect(0,0,k*(w+1),j*(w+1));r.fillRect(0,0,k*(w+1),j*(w+1))}else{e({gx:0,gy:0,gw:k,gh:j})}r.textBaseline="top";L.trigger("wordcloudstart");var G=0;var I=false;if(x.wait!==0){var C=setInterval(function(){if(G>=x.wordList.length){clearTimeout(C);L.trigger("wordcloudstop");if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true;return}if(I){if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true;return}m=(new Date()).getTime();v(x.wordList[G][0],x.wordList[G][1]);if(c()){clearTimeout(C);x.abort();L.trigger("wordcloudabort");L.trigger("wordcloudstop");if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true}G++},x.wait)}else{window.setImmediate(function H(){if(G>=x.wordList.length){L.trigger("wordcloudstop");if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true;return}if(I){if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true;return}m=(new Date()).getTime();v(x.wordList[G][0],x.wordList[G][1]);if(c()){x.abort();L.trigger("wordcloudabort");L.trigger("wordcloudstop");if(typeof x.finishCallback=="function"){x.finishCallback()}x.finished=true;return}G++;window.setImmediate(H)})}L.one("wordcloudstart",function(M){clearTimeout(C)});L.one("wordcloudstop",function(){I=true})})}})(jQuery);
