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

