/**
 * Copyright (C) 2010-2011 Graham Breach
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * jQuery.tagcanvas 1.10
 * For more information, please contact <graham@goat1000.com>
 */
(function(B){var J,I,A=Math.abs,p=Math.sin,g=Math.cos,w={},x={},y={0:"0,",1:"17,",2:"34,",3:"51,",4:"68,",5:"85,",6:"102,",7:"119,",8:"136,",9:"153,",a:"170,",A:"170,",b:"187,",B:"187,",c:"204,",C:"204,",d:"221,",D:"221,",e:"238,",E:"238,",f:"255,",F:"255,"},d,C,c,h=document;for(J=0;J<256;++J){I=J.toString(16);if(J<16){I="0"+I}x[I]=x[I.toUpperCase()]=J.toString()+","}function F(i){return typeof(i)!="undefined"}function l(S){var j,R,N,M,Q=[],O=Math.PI*(3-Math.sqrt(5)),P=2/S;for(j=0;j<S;++j){R=j*P-1+(P/2);N=Math.sqrt(1-R*R);M=j*O;Q.push([g(M)*N,R,p(M)*N])}return Q}function m(P,i){var O=P,N,M,j=(i*1).toPrecision(3)+")";if(P[0]==="#"){if(!w[P]){if(P.length===4){w[P]="rgba("+y[P[1]]+y[P[2]]+y[P[3]]}else{w[P]="rgba("+x[P.substr(1,2)]+x[P.substr(3,2)]+x[P.substr(5,2)]}}O=w[P]+j}else{if(P.substr(0,4)==="rgb("||P.substr(0,4)==="hsl("){O=(P.replace("(","a(").replace(")",","+j))}else{if(P.substr(0,5)==="rgba("||P.substr(0,5)==="hsla("){N=P.lastIndexOf(",")+1,M=P.indexOf(")");i*=parseFloat(P.substring(N,M));O=P.substr(0,N)+i.toPrecision(3)+")"}}}return O}function f(i,j){if(window.G_vmlCanvasManager){return null}var M=h.createElement("canvas");M.width=i;M.height=j;return M}function t(){var j=f(3,3),N,M;if(!j){return false}N=j.getContext("2d");N.strokeStyle="#000";N.shadowColor="#fff";N.shadowBlur="3";N.globalAlpha=0;N.strokeRect(2,2,2,2);N.globalAlpha=1;M=N.getImageData(2,2,1,1);j=null;return(M.data[0]>0)}function L(T,j){var M=1024,P=T.weightGradient,O,R,N,S,Q;if(T.gCanvas){R=T.gCanvas.getContext("2d")}else{T.gCanvas=O=f(M,1);if(!O){return null}R=O.getContext("2d");S=R.createLinearGradient(0,0,M,0);for(N in P){S.addColorStop(1-N,P[N])}R.fillStyle=S;R.fillRect(0,0,M,1)}Q=R.getImageData(~~((M-1)*j),0,1,1).data;return"rgba("+Q[0]+","+Q[1]+","+Q[2]+","+(Q[3]/255)+")"}function s(P,O,M,S,Q,R,j){var N=(R||0)+(j&&j[0]<0?A(j[0]):0),i=(R||0)+(j&&j[1]<0?A(j[1]):0);P.font=O;P.textBaseline="top";P.fillStyle=M;Q&&(P.shadowColor=Q);R&&(P.shadowBlur=R);j&&(P.shadowOffsetX=j[0],P.shadowOffsetY=j[1]);P.fillText(S,N,i)}function k(W,Q,U,V,P,M,S,T,j){var N=V+A(j[0])+T+T,i=P+A(j[1])+T+T,O,R;O=f(N,i);if(!O){return null}R=O.getContext("2d");s(R,Q,M,W,S,T,j);return O}function H(Q,T,U,N){var O=Q.width+A(N[0])+U+U,j=Q.height+A(N[1])+U+U,R,S,P=(U||0)+(N&&N[0]<0?A(N[0]):0),M=(U||0)+(N&&N[1]<0?A(N[1]):0);R=f(O,j);if(!R){return null}S=R.getContext("2d");T&&(S.shadowColor=T);U&&(S.shadowBlur=U);N&&(S.shadowOffsetX=N[0],S.shadowOffsetY=N[1]);S.drawImage(Q,P,M);return R}function D(Y,Q,W){var X=parseInt(Y.length*W),P=parseInt(W*2),N=f(X,P),T,j,O,S,V,U,M,R;if(!N){return null}T=N.getContext("2d");T.fillStyle="#000";T.fillRect(0,0,X,P);s(T,W+"px "+Q,"#fff",Y);j=T.getImageData(0,0,X,P);O=j.width;S=j.height;R={min:{x:O,y:S},max:{x:-1,y:-1}};for(U=0;U<S;++U){for(V=0;V<O;++V){M=(U*O+V)*4;if(j.data[M+1]>0){if(V<R.min.x){R.min.x=V}if(V>R.max.x){R.max.x=V}if(U<R.min.y){R.min.y=U}if(U>R.max.y){R.max.y=U}}}}if(O!=X){R.min.x*=(X/O);R.max.x*=(X/O)}if(S!=P){R.min.y*=(X/S);R.max.y*=(X/S)}N=null;return R}function r(i){return"'"+i.replace(/(\'|\")/g,"").replace(/\s*,\s*/g,"', '")+"'"}function v(i,j,M){M=M||h;if(M.addEventListener){M.addEventListener(i,j,false)}else{M.attachEvent("on"+i,j)}}function G(N,M,j){if(N.complete){M.w=N.width;M.h=N.height;j.push(M)}else{jQuery(N).bind("load",function(){M.w=this.width;M.h=this.height;j.push(M)})}}function u(M,j){var i=1,N;if(M.weightFrom){i=1*(j.getAttribute(M.weightFrom)||M.textHeight)}else{if(h.defaultView&&h.defaultView.getComputedStyle){N=h.defaultView.getComputedStyle(j,null).getPropertyValue("font-size");i=N.replace("px","")*1}else{M.weight=false}}return i}function E(O){var N,M,j=h.documentElement,P;for(N in q.tc){M=q.tc[N];P=B(M.canvas).offset();if(O.pageX){M.mx=O.pageX-P.left;M.my=O.pageY-P.top}else{M.mx=O.clientX+(j.scrollLeft||h.body.scrollLeft)-P.left;M.my=O.clientY+(j.scrollTop||h.body.scrollTop)-P.top}}}function o(N){var j=q,i=h.addEventListener?0:1,M=N.target&&F(N.target.id)?N.target.id:N.srcElement.parentNode.id;if(M&&N.button==i&&j.tc[M]){E(N);j.tc[M].Clicked(N)}}function n(){var M=q.tc,j;for(j in M){M[j].Draw()}}function b(M,i){var j=p(i),N=g(i);return{x:M.x,y:(M.y*N)+(M.z*j),z:(M.y*-j)+(M.z*N)}}function a(M,i){var j=p(i),N=g(i);return{x:(M.x*N)+(M.z*-j),y:M.y,z:(M.x*j)+(M.z*N)}}function K(N,T,S,Q,O,j){var i,P,R,M=N.z1/(N.z1+N.z2+T.z);i=T.y*M;P=T.x*M;R=N.z2+T.z;return{x:P,y:i,z:R}}function e(i){this.ts=new Date().valueOf();this.tc=i;this.x=this.y=this.w=this.h=this.sc=1;this.z=0}d=e.prototype;d.Update=function(i,Q,j,M,P,N){var O=this.tc.outlineOffset;this.x=P*(i-O);this.y=P*(Q-O);this.w=P*(j+O*2);this.h=P*(M+O*2);this.sc=P;this.z=N.z};d.Draw=function(M){var j=new Date().valueOf()-this.ts,i=this.tc;M.setTransform(1,0,0,1,0,0);M.strokeStyle=i.outlineColour;M.lineWidth=i.outlineThickness;M.shadowBlur=M.shadowOffsetX=M.shadowOffsetY=0;if(i.pulsateTo<1){M.globalAlpha=i.pulsateTo+((1-i.pulsateTo)*(0.5+(g(2*Math.PI*j/(1000*i.pulsateTime))/2)))}else{M.globalAlpha=1}M.strokeRect(this.x,this.y,this.w,this.h)};d.Active=function(M,i,j){return(i>=this.x&&j>=this.y&&i<=this.x+this.w&&j<=this.y+this.h)};function z(j,P,N,O,M,R){var S=j.ctxt,Q;this.tc=j;this.image=P.src?P:null;this.name=P.src?"":P;this.a=N;this.p3d={x:O[0]*j.radius*1.1,y:O[1]*j.radius*1.1,z:O[2]*j.radius*1.1};this.x=this.y=0;this.w=M;this.h=R;this.colour=j.textColour;this.weight=this.sc=this.alpha=1;this.weighted=!j.weight;this.outline=new e(j);if(this.image){if(j.txtOpt&&j.shadow){Q=H(this.image,j.shadow,j.shadowBlur,j.shadowOffset);if(Q){this.image=Q;this.w=Q.width;this.h=Q.height}}}else{this.textHeight=j.textHeight;this.extents=D(this.name,j.textFont,this.textHeight);this.Measure(S,j)}this.SetShadowColour=j.shadowAlpha?this.SetShadowColourAlpha:this.SetShadowColourFixed;this.SetDraw(j)}C=z.prototype;C.SetDraw=function(i){this.Draw=this.image?(i.ie>7?this.DrawImageIE:this.DrawImage):this.DrawText};C.Measure=function(Q,j){this.h=this.extents?this.extents.max.y+this.extents.min.y:this.textHeight;Q.font=this.font=this.textHeight+"px "+j.textFont;this.w1=Q.measureText(this.name).width;if(j.txtOpt){var N=j.txtScale,O=N*this.textHeight,P=O+"px "+j.textFont,M=[N*j.shadowOffset[0],N*j.shadowOffset[1]],i;Q.font=P;i=Q.measureText(this.name).width;this.image=k(this.name,P,O,i,N*this.h,this.colour,j.shadow,N*j.shadowBlur,M);if(this.image){this.w=this.image.width/N;this.h=this.image.height/N}this.SetDraw(j);j.txtOpt=this.image}};C.SetWeight=function(i){this.weight=i;this.Weight(this.tc.ctxt,this.tc);this.Measure(this.tc.ctxt,this.tc)};C.Weight=function(N,M){var j=this.weight,i=M.weightMode;this.weighted=true;if(i=="colour"||i=="both"){this.colour=L(M,(j-M.min_weight)/(M.max_weight-M.min_weight))}if(i=="size"||i=="both"){this.textHeight=j*M.weightSize}this.extents=D(this.name,M.textFont,this.textHeight)};C.SetShadowColourFixed=function(M,j,i){M.shadowColor=j};C.SetShadowColourAlpha=function(M,j,i){M.shadowColor=m(j,i)};C.DrawText=function(N,R,M){var S=this.tc,P=this.x,O=this.y,Q,j,T=this.sc,i=this.outline;N.globalAlpha=this.alpha;N.setTransform(T,0,0,T,0,0);N.fillStyle=this.colour;S.shadow&&this.SetShadowColour(N,S.shadow,this.alpha);N.font=this.font;Q=this.w1*T;j=this.h*T;P+=1+(R/T)-(Q/2);O+=1+(M/T)-(j/2);N.fillText(this.name,P,O);i.Update(P,O,this.w1,this.h,T,this.p3d);return i.Active(N,S.mx,S.my)?i:null};C.DrawImage=function(P,T,O){var U=this.tc,R=this.x,Q=this.y,V=this.sc,j=this.outline,M=this.image,S=this.w,N=this.h;P.globalAlpha=this.alpha;P.setTransform(V,0,0,V,0,0);P.fillStyle=this.colour;U.shadow&&this.SetShadowColour(P,U.shadow,this.alpha);R+=(T/V)-(S/2);Q+=(O/V)-(N/2);P.drawImage(M,R,Q,S,N);j.Update(R,Q,S,N,V,this.p3d);return j.Active(P,U.mx,U.my)?j:null};C.DrawImageIE=function(P,T,O){var U=this.tc,M=this.image,V=this.sc,j=this.outline,S=M.width=this.w*V,N=M.height=this.h*V,R=(this.x*V)+T-(S/2),Q=(this.y*V)+O-(N/2);P.globalAlpha=this.alpha;P.drawImage(M,R,Q);j.Update(R,Q,S,N,1,this.p3d);return j.Active(P,U.mx,U.my)?j:null};C.Calc=function(O,N){var i=a(this.p3d,O),j=this.tc,P=j.minBrightness,M=j.radius;this.p3d=b(i,N);i=K(j,this.p3d,this.w,this.h,Math.PI/4,20);this.x=i.x;this.y=i.y;this.sc=(j.z1+j.z2-i.z)/j.z2;this.alpha=Math.max(P,Math.min(1,P+1-((i.z-j.z2+M)/(2*M))))};C.Clicked=function(O){var j=this.a,M=j.target,N=j.href,i;if(M!=""&&M!="_self"){if(self.frames[M]){self.frames[M]=N}else{if(top.frames[M]){top.frames[M]=N}else{window.open(N,M)}}return}if(j.fireEvent){if(!j.fireEvent("onclick")){return}}else{i=h.createEvent("MouseEvents");i.initMouseEvent("click",1,1,window,0,0,0,0,0,0,0,0,0,0,null);if(!j.dispatchEvent(i)){return}}h.location=N};function q(){var j,M={mx:-1,my:-1,z1:20000,z2:20000,z0:0.0002,freezeActive:false,pulsateTo:1,pulsateTime:3,reverse:false,depth:0.5,maxSpeed:0.05,minSpeed:0,decel:0.95,interval:20,initial:null,hideTags:true,minBrightness:0.1,outlineColour:"#ffff99",outlineThickness:2,outlineOffset:5,textColour:"#ff99ff",textHeight:15,textFont:"Helvetica, Arial, sans-serif",shadow:"#000",shadowBlur:0,shadowOffset:[0,0],zoom:1,weight:false,weightMode:"size",weightFrom:null,weightSize:1,weightGradient:{0:"#f00",0.33:"#ff0",0.66:"#0f0",1:"#00f"},txtOpt:true,txtScale:2,frontSelect:false};for(j in M){this[j]=M[j]}this.max_weight=0;this.min_weight=200}c=q.prototype;c.Draw=function(){var U=this.canvas,S=U.width,M=U.height,j=0,R=this.yaw,N=this.pitch,O=S/2,X=M/2,V=this.ctxt,Q,W,T,Y=this.taglist,P=Y.length;V.setTransform(1,0,0,1,0,0);this.active=null;for(T=0;T<P;++T){Y[T].Calc(R,N)}Y=Y.sort(function(Z,i){return Z.sc-i.sc});if(!this.txtOpt&&this.shadow){V.shadowBlur=this.shadowBlur;V.shadowOffsetX=this.shadowOffset[0];V.shadowOffsetY=this.shadowOffset[1]}V.clearRect(0,0,U.width,U.height);for(T=0;T<P;++T){W=Y[T].Draw(V,O,X);if(W&&W.sc>j&&(!this.frontSelect||W.z<=0)){Q=W;Q.index=T;j=W.sc}}if(this.freezeActive&&Q){this.yaw=this.pitch=0}else{this.Animate(S,M)}Q&&(this.active=Q).Draw(V)};c.Animate=function(Q,N){var j=this,P=j.mx,O=j.my,S,R,M,i;if(P>=0&&O>=0&&P<Q&&O<N){S=j.maxSpeed,i=j.reverse?-1:1;this.yaw=i*((S*2*P/Q)-S);this.pitch=i*-((S*2*O/N)-S);this.initial=null}else{if(!j.initial){S=j.minSpeed,R=A(this.yaw),M=A(this.pitch);if(R>S){this.yaw=R>j.z0?j.yaw*j.decel:0}if(M>S){this.pitch=M>j.z0?j.pitch*j.decel:0}}}};c.Clicked=function(N){var i=this.active,M=this.taglist;try{if(i&&M[i.index]){M[i.index].Clicked(N)}}catch(j){}};q.tc={};jQuery.fn.tagcanvas=function(M,j){var i,N=j?jQuery("#"+j):this;if(h.all&&!j){return false}i=N.find("a");if(F(window.G_vmlCanvasManager)){this.each(function(){B(this)[0]=window.G_vmlCanvasManager.initElement(B(this)[0])});M.ie=parseFloat(navigator.appVersion.split("MSIE")[1])}if(!i.length||!this[0].getContext||!this[0].getContext("2d").fillText){return false}this.each(function(){var S,V,Q,U,O,R,P,T=[];j||(i=B(this).find("a"));R=new q;for(S in M){R[S]=M[S]}R.z1=(19800/(Math.exp(R.depth)*(1-1/Math.E)))+20000-19800/(1-(1/Math.E));R.z2=R.z1*(1/R.zoom);R.radius=(this.height>this.width?this.width:this.height)*0.33*(R.z2+R.z1)/(R.z1);R.yaw=R.initial?R.initial[0]*R.maxSpeed:0;R.pitch=R.initial?R.initial[1]*R.maxSpeed:0;R.canvas=B(this)[0];R.ctxt=R.canvas.getContext("2d");R.textFont=r(R.textFont);R.ctxt.textBaseline="top";if(R.shadowBlur||R.shadowOffset[0]||R.shadowOffset[1]){R.ctxt.shadowColor=R.shadow;R.shadow=R.ctxt.shadowColor;R.shadowAlpha=t()}else{delete R.shadow}R.taglist=[];V=l(i.length);for(S=0;S<i.length;++S){Q=i[S].getElementsByTagName("img");if(Q.length){U=new Image;U.src=Q[0].src;O=new z(R,U,i[S],V[S],1,1);G(U,O,R.taglist)}else{R.taglist.push(new z(R,i[S].innerText||i[S].textContent,i[S],V[S],2,R.textHeight+2))}if(R.weight){P=u(R,i[S]);if(P>R.max_weight){R.max_weight=P}if(P<R.min_weight){R.min_weight=P}T.push(P)}}if(R.weight=(R.max_weight>R.min_weight)){for(S=0;S<R.taglist.length;++S){R.taglist[S].SetWeight(T[S])}}q.tc[B(this)[0].id]=R;v("mousemove",E,this);v("mouseout",E,this);v("mouseup",o,this);j&&R.hideTags&&N.hide()});return !!(q.started||(q.started=setInterval(n,M.interval)))}})(jQuery);

