/**
 * 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.8
 * For more information, please contact <graham@goat1000.com>
 */
(function(h){var C,B,x=Math.abs,a=Math.sin,b=Math.cos,o={},q={},r={"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,"};for(C=0;C<256;++C){B=C.toString(16);if(C<16){B="0"+B}q[B]=q[B.toUpperCase()]=C.toString()+","}function F(M){var j,L,H,G,K=[],I=Math.PI*(3-Math.sqrt(5)),J=2/M;for(j=0;j<M;++j){L=j*J-1+(J/2);H=Math.sqrt(1-L*L);G=j*I;K.push([b(G)*H,L,a(G)*H])}return K}function z(J,i){var I=J,H,G,j=(i*1).toPrecision(3)+")";if(J[0]==="#"){if(!o[J]){if(J.length===4){o[J]="rgba("+r[J[1]]+r[J[2]]+r[J[3]]}else{o[J]="rgba("+q[J.substr(1,2)]+q[J.substr(3,2)]+q[J.substr(5,2)]}}I=o[J]+j}else{if(J.substr(0,4)==="rgb("||J.substr(0,4)==="hsl("){I=(J.replace("(","a(").replace(")",","+j))}else{if(J.substr(0,5)==="rgba("||J.substr(0,5)==="hsla("){H=J.lastIndexOf(",")+1,G=J.indexOf(")");i*=parseFloat(J.substring(H,G));I=J.substr(0,H)+i.toPrecision(3)+")"}else{I=J}}}return I}function p(i,j){if(window.G_vmlCanvasManager){return null}var G=document.createElement("canvas");G.width=i;G.height=j;return G}function k(){var j=p(3,3),H,G;if(!j){return false}H=j.getContext("2d");H.strokeStyle="#000";H.shadowColor="#fff";H.shadowBlur="3";H.globalAlpha=0;H.strokeRect(2,2,2,2);H.globalAlpha=1;G=H.getImageData(2,2,1,1);j=null;return(G.data[0]>0)}function s(N,j){var G=1024,J=N.weightGradient,I,L,H,M,K;if(N.gCanvas){L=N.gCanvas.getContext("2d")}else{N.gCanvas=I=p(G,1);if(!I){return null}L=I.getContext("2d");M=L.createLinearGradient(0,0,G,0);for(H in J){M.addColorStop(1-H,J[H])}L.fillStyle=M;L.fillRect(0,0,G,1)}K=L.getImageData(~~((G-1)*j),0,1,1).data;return"rgba("+K[0]+","+K[1]+","+K[2]+","+(K[3]/255)+")"}function v(J,I,G,M,K,L,j){var H=(L||0)+(j&&j[0]<0?x(j[0]):0),i=(L||0)+(j&&j[1]<0?x(j[1]):0);J.font=I;J.textBaseline="top";J.fillStyle=G;K&&(J.shadowColor=K);L&&(J.shadowBlur=L);j&&(J.shadowOffsetX=j[0],J.shadowOffsetY=j[1]);J.fillText(M,H,i)}function m(Q,K,O,P,J,G,M,N,j){var H=P+x(j[0])+N+N,i=J+x(j[1])+N+N,I,L;I=p(H,i);if(!I){return null}L=I.getContext("2d");v(L,K,G,Q,M,N,j);return I}function n(K,N,O,H){var I=K.width+x(H[0])+O+O,j=K.height+x(H[1])+O+O,L,M,J=(O||0)+(H&&H[0]<0?x(H[0]):0),G=(O||0)+(H&&H[1]<0?x(H[1]):0);L=p(I,j);if(!L){return null}M=L.getContext("2d");N&&(M.shadowColor=N);O&&(M.shadowBlur=O);H&&(M.shadowOffsetX=H[0],M.shadowOffsetY=H[1]);M.drawImage(K,J,G);return L}function d(S,K,Q){var R=parseInt(S.length*Q),J=parseInt(Q*2),H=p(R,J),N,j,I,M,P,O,G,L;if(!H){return null}N=H.getContext("2d");N.fillStyle="#000";N.fillRect(0,0,R,J);v(N,Q+"px "+K,"#fff",S);j=N.getImageData(0,0,R,J);I=j.width;M=j.height;L={min:{x:I,y:M},max:{x:-1,y:-1}};for(O=0;O<M;++O){for(P=0;P<I;++P){G=(O*I+P)*4;if(j.data[G+1]>0){if(P<L.min.x){L.min.x=P}if(P>L.max.x){L.max.x=P}if(O<L.min.y){L.min.y=O}if(O>L.max.y){L.max.y=O}}}}if(I!=R){L.min.x*=(R/I);L.max.x*=(R/I)}if(M!=J){L.min.y*=(R/M);L.max.y*=(R/M)}H=null;return L}function y(i){return"'"+i.replace(/(\'|\")/g,"").replace(/\s*,\s*/g,"', '")+"'"}function w(H,G,j){if(H.complete){G.w=H.width;G.h=H.height;j.push(G)}else{jQuery(H).bind("load",function(){G.w=this.width;G.h=this.height;j.push(G)})}}function u(G,j){var i=1,H;if(G.weightFrom){i=1*(j.getAttribute(G.weightFrom)||G.textHeight)}else{if(document.defaultView&&document.defaultView.getComputedStyle){H=document.defaultView.getComputedStyle(j,null).getPropertyValue("font-size");i=H.replace("px","")*1}else{G.weight=false}}return i}function g(I){var H,G,j=document.documentElement;for(H in D.tc){G=D.tc[H];if(I.pageX){G.mx=I.pageX-G.cx;G.my=I.pageY-G.cy}else{G.mx=I.clientX+(j.scrollLeft||document.body.scrollLeft)-G.cx;G.my=I.clientY+(j.scrollTop||document.body.scrollTop)-G.cy}}}function c(H){var j=D,i=document.addEventListener?0:1,G=H.target&&H.target.id!=undefined?H.target.id:H.srcElement.parentNode.id;if(G&&H.button==i&&j.tc[G]){g(H);j.tc[G].Clicked(H)}}function t(){var G=D.tc,j;for(j in G){G[j].Draw()}}function f(G,i){var j=a(i),H=b(i);return{x:G.x,y:(G.y*H)+(G.z*j),z:(G.y*-j)+(G.z*H)}}function e(G,i){var j=a(i),H=b(i);return{x:(G.x*H)+(G.z*-j),y:G.y,z:(G.x*j)+(G.z*H)}}function A(H,N,M,K,I,j){var i,J,L,G=H.z1/(H.z1+H.z2+N.z);i=N.y*G;J=N.x*G;L=H.z2+N.z;return{x:J,y:i,z:L}}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}E.prototype.Update=function(i,K,j,G,J,H){var I=this.tc.outlineOffset;this.x=J*(i-I);this.y=J*(K-I);this.w=J*(j+I*2);this.h=J*(G+I*2);this.sc=J;this.z=H.z};E.prototype.Draw=function(G){var j=new Date().valueOf()-this.ts,i=this.tc;G.setTransform(1,0,0,1,0,0);G.strokeStyle=i.outlineColour;G.lineWidth=i.outlineThickness;G.shadowBlur=G.shadowOffsetX=G.shadowOffsetY=0;if(i.pulsateTo<1){G.globalAlpha=i.pulsateTo+((1-i.pulsateTo)*(0.5+(b(2*Math.PI*j/(1000*i.pulsateTime))/2)))}else{G.globalAlpha=1}G.strokeRect(this.x,this.y,this.w,this.h)};E.prototype.Active=function(G,i,j){return(i>=this.x&&j>=this.y&&i<=this.x+this.w&&j<=this.y+this.h)};function l(j,J,H,I,G,L){var M=j.ctxt,K;this.tc=j;this.image=J.src?J:null;this.name=J.src?"":J;this.a=H;this.p3d={x:I[0]*j.radius*1.1,y:I[1]*j.radius*1.1,z:I[2]*j.radius*1.1};this.x=this.y=0;this.w=G;this.h=L;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.shadowBlur||j.shadowOffset[0]||j.shadowOffset[1])){K=n(this.image,j.shadow,j.shadowBlur,j.shadowOffset);if(K){this.image=K;this.w=K.width;this.h=K.height}}}else{this.textHeight=j.textHeight;this.extents=d(this.name,j.textFont,this.textHeight);this.Measure(M,j)}this.SetShadowColour=j.shadowAlpha?this.SetShadowColourAlpha:this.SetShadowColourFixed;this.Draw=this.image?this.DrawImage:this.DrawText}l.prototype.Measure=function(j,i){this.h=this.extents?this.extents.max.y+this.extents.min.y:this.textHeight;j.font=this.font=this.textHeight+"px "+i.textFont;this.w1=j.measureText(this.name).width;if(i.txtOpt){this.image=m(this.name,this.font,this.textHeight,this.w1,this.h,this.colour,i.shadow,i.shadowBlur,i.shadowOffset);if(this.image){this.Draw=this.DrawImage}else{i.txtOpt=false}}};l.prototype.SetWeight=function(i){this.weight=i;this.Weight(this.tc.ctxt,this.tc);this.Measure(this.tc.ctxt,this.tc)};l.prototype.Weight=function(H,G){var j=this.weight,i=G.weightMode;this.weighted=true;if(i==="colour"||i==="both"){this.colour=s(G,(j-G.min_weight)/(G.max_weight-G.min_weight))}if(i=="size"||i=="both"){this.textHeight=j*G.weightSize}this.extents=d(this.name,G.textFont,this.textHeight)};l.prototype.SetShadowColourFixed=function(G,j,i){G.shadowColor=j};l.prototype.SetShadowColourAlpha=function(G,j,i){G.shadowColor=z(j,i)};l.prototype.DrawText=function(H,L,G){var M=this.tc,J=this.x,I=this.y,K,j,N=this.sc,i=this.outline;H.globalAlpha=this.alpha;H.setTransform(N,0,0,N,0,0);H.fillStyle=this.colour;this.SetShadowColour(H,M.shadow,this.alpha);H.font=this.font;K=this.w1*N;j=this.h*N;J+=1+(L/N)-(K/2);I+=1+(G/N)-(j/2);H.fillText(this.name,J,I);i.Update(J,I,this.w1,this.h,N,this.p3d);return i.Active(H,M.mx,M.my)?i:null};l.prototype.DrawImage=function(J,N,I){var O=this.tc,L=this.x,K=this.y,M,H,P=this.sc,j=this.outline,G=this.image;J.globalAlpha=this.alpha;J.setTransform(P,0,0,P,0,0);J.fillStyle=this.colour;O.shadow&&this.SetShadowColour(J,O.shadow,this.alpha);M=G.width;H=G.height;L+=(N/P)-(M/2);K+=(I/P)-(H/2);J.drawImage(G,L,K,M,H);j.Update(L,K,M,H,P,this.p3d);return j.Active(J,O.mx,O.my)?j:null};l.prototype.Calc=function(I,H){var i=e(this.p3d,I),j=this.tc,J=j.minBrightness,G=j.radius;this.p3d=f(i,H);i=A(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(J,Math.min(1,J+1-((i.z-j.z2+G)/(2*G))))};l.prototype.Clicked=function(I){var j=this.a,G=j.target,H=j.href,i;if(G!=""&&G!="_self"){if(self.frames[G]){self.frames[G]=H}else{if(top.frames[G]){top.frames[G]=H}else{window.open(H,G)}}return}if(j.fireEvent){if(!j.fireEvent("onclick")){return}}else{i=document.createEvent("MouseEvents");i.initMouseEvent("click",true,true,window,0,0,0,0,0,false,false,false,false,0,null);if(!j.dispatchEvent(i)){return}}document.location=H};function D(){var j,G={mx:-1,my:-1,cx:0,cy:0,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,frontSelect:false};for(j in G){this[j]=G[j]}this.max_weight=0;this.min_weight=200}D.prototype.Draw=function(){var H=this.canvas,L=0,I,K,M=this.ctxt,G,J,j=this.taglist.length;M.setTransform(1,0,0,1,0,0);I=H.width/2;K=H.height/2;this.active=null;for(J=0;J<j;++J){this.taglist[J].Calc(this.yaw,this.pitch)}this.taglist=this.taglist.sort(function(N,i){return N.sc-i.sc});M.textBaseline="top";if(!this.txtOpt&&(this.shadowBlur||this.shadowOffset[0]||this.shadowOffset[1])){M.shadowBlur=this.shadowBlur;M.shadowOffsetX=this.shadowOffset[0];M.shadowOffsetY=this.shadowOffset[1]}M.clearRect(0,0,H.width,H.height);for(J=0;J<j;++J){G=this.taglist[J].Draw(M,I,K);if(G&&G.sc>L&&(!this.frontSelect||G.z<=0)){this.active=G;this.active.index=J;L=G.sc}}if(this.freezeActive&&this.active){this.yaw=this.pitch=0}else{this.Animate(H.width,H.height)}if(this.active){this.active.Draw(M)}};D.prototype.Animate=function(K,H){var j=this,J=j.mx,I=j.my,M,L,G,i;if(J>=0&&I>=0&&J<K&&I<H){M=j.maxSpeed,i=j.reverse?-1:1;this.yaw=i*((M*2*J/K)-M);this.pitch=i*-((M*2*I/H)-M);this.initial=null}else{if(!j.initial){M=j.minSpeed,L=x(this.yaw),G=x(this.pitch);if(L>M){this.yaw=L>j.z0?j.yaw*j.decel:0}if(G>M){this.pitch=G>j.z0?j.pitch*j.decel:0}}}};D.prototype.Clicked=function(H){var i=this.active,G=this.taglist;try{if(i&&G[i.index]){G[i.index].Clicked(H)}}catch(j){}};D.tc={};jQuery.fn.tagcanvas=function(G,j){var i,H=j?jQuery("#"+j):this;if(document.all&&!j){return false}i=H.find("a");if(typeof(window.G_vmlCanvasManager)!="undefined"){this.each(function(){h(this)[0]=window.G_vmlCanvasManager.initElement(h(this)[0])})}if(!i.length||!this[0].getContext||!this[0].getContext("2d").fillText){return false}this.each(function(){var K,J,M,P,Q,L,I=h(this).offset(),O,N=[];if(!j){i=h(this).find("a")}L=new D;for(K in G){L[K]=G[K]}L.z1=(19800/(Math.exp(L.depth)*(1-1/Math.E)))+20000-19800/(1-(1/Math.E));L.z2=L.z1*(1/L.zoom);L.radius=(this.height>this.width?this.width:this.height)*0.33*(L.z2+L.z1)/(L.z1);L.yaw=L.initial?L.initial[0]*L.maxSpeed:0;L.pitch=L.initial?L.initial[1]*L.maxSpeed:0;L.cx=I.left;L.cy=I.top;L.canvas=h(this)[0];L.ctxt=L.canvas.getContext("2d");L.textFont=y(L.textFont);L.ctxt.shadowColor=L.shadow;L.shadow=L.ctxt.shadowColor;L.shadowAlpha=(L.shadowBlur||L.shadowOffset[0]||L.shadowOffset[1])&&k();L.taglist=[];J=F(i.length);for(K=0;K<i.length;++K){M=i[K].getElementsByTagName("img");if(M.length){P=new Image;P.src=M[0].src;Q=new l(L,P,i[K],J[K],1,1);w(P,Q,L.taglist)}else{L.taglist.push(new l(L,i[K].innerText||i[K].textContent,i[K],J[K],2,L.textHeight+2))}if(L.weight){O=u(L,i[K]);if(O>L.max_weight){L.max_weight=O}if(O<L.min_weight){L.min_weight=O}N.push(O)}}if(L.weight=(L.max_weight>L.min_weight)){for(K=0;K<L.taglist.length;++K){L.taglist[K].SetWeight(N[K])}}D.tc[h(this)[0].id]=L;if(!D.started){jQuery(document).bind("mousemove",g);jQuery(document).bind("mouseout",g);jQuery(document).bind("mouseup",c);D.started=setInterval(t,L.interval)}if(j&&L.hideTags){H.hide()}});return true}})(jQuery);

