/**
 * 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.12
 * For more information, please contact <graham@goat1000.com>
 */
(function(F){var O,N,E=Math.abs,p=Math.sin,g=Math.cos,x=Math.max,S=Math.min,z={},A={},B={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,G,c,h=document,w;for(O=0;O<256;++O){N=O.toString(16);if(O<16){N="0"+N}A[N]=A[N.toUpperCase()]=O.toString()+","}function K(i){return typeof(i)!="undefined"}function l(aa){var j,Z,V,U,Y=[],W=Math.PI*(3-Math.sqrt(5)),X=2/aa;for(j=0;j<aa;++j){Z=j*X-1+(X/2);V=Math.sqrt(1-Z*Z);U=j*W;Y.push([g(U)*V,Z,p(U)*V])}return Y}function R(W,U,ab,aa,Z,Y){var ac,ad=[],X=Math.PI*(3-Math.sqrt(5)),V=2/W;for(ab=0;ab<W;++ab){aa=ab*V-1+(V/2);ac=ab*X;Z=g(ac);Y=p(ac);ad.push(U?[aa,Z,Y]:[Z,aa,Y])}return ad}function u(i){return R(i)}function D(i){return R(i,1)}function n(X,i){var W=X,V,U,j=(i*1).toPrecision(3)+")";if(X[0]==="#"){if(!z[X]){if(X.length===4){z[X]="rgba("+B[X[1]]+B[X[2]]+B[X[3]]}else{z[X]="rgba("+A[X.substr(1,2)]+A[X.substr(3,2)]+A[X.substr(5,2)]}}W=z[X]+j}else{if(X.substr(0,4)==="rgb("||X.substr(0,4)==="hsl("){W=(X.replace("(","a(").replace(")",","+j))}else{if(X.substr(0,5)==="rgba("||X.substr(0,5)==="hsla("){V=X.lastIndexOf(",")+1,U=X.indexOf(")");i*=parseFloat(X.substring(V,U));W=X.substr(0,V)+i.toPrecision(3)+")"}}}return W}function f(i,j){if(window.G_vmlCanvasManager){return null}var U=h.createElement("canvas");U.width=i;U.height=j;return U}function t(){var j=f(3,3),V,U;if(!j){return false}V=j.getContext("2d");V.strokeStyle="#000";V.shadowColor="#fff";V.shadowBlur=3;V.globalAlpha=0;V.strokeRect(2,2,2,2);V.globalAlpha=1;U=V.getImageData(2,2,1,1);j=null;return(U.data[0]>0)}function T(ab,j){var U=1024,X=ab.weightGradient,W,Z,V,aa,Y;if(ab.gCanvas){Z=ab.gCanvas.getContext("2d")}else{ab.gCanvas=W=f(U,1);if(!W){return null}Z=W.getContext("2d");aa=Z.createLinearGradient(0,0,U,0);for(V in X){aa.addColorStop(1-V,X[V])}Z.fillStyle=aa;Z.fillRect(0,0,U,1)}Y=Z.getImageData(~~((U-1)*j),0,1,1).data;return"rgba("+Y[0]+","+Y[1]+","+Y[2]+","+(Y[3]/255)+")"}function s(X,W,U,aa,Y,Z,j){var V=(Z||0)+(j&&j[0]<0?E(j[0]):0),i=(Z||0)+(j&&j[1]<0?E(j[1]):0);X.font=W;X.textBaseline="top";X.fillStyle=U;Y&&(X.shadowColor=Y);Z&&(X.shadowBlur=Z);j&&(X.shadowOffsetX=j[0],X.shadowOffsetY=j[1]);X.fillText(aa,V,i)}function k(ag,Y,ac,ae,X,U,aa,ab,j,af,ad){var V=ae+E(j[0])+ab+ab,i=X+E(j[1])+ab+ab,W,Z;W=f(V+af,i+ad);if(!W){return null}Z=W.getContext("2d");s(Z,Y,U,ag,aa,ab,j);return W}function M(Y,ab,ac,V){var W=Y.width+E(V[0])+ac+ac,j=Y.height+E(V[1])+ac+ac,Z,aa,X=(ac||0)+(V&&V[0]<0?E(V[0]):0),U=(ac||0)+(V&&V[1]<0?E(V[1]):0);Z=f(W,j);if(!Z){return null}aa=Z.getContext("2d");ab&&(aa.shadowColor=ab);ac&&(aa.shadowBlur=ac);V&&(aa.shadowOffsetX=V[0],aa.shadowOffsetY=V[1]);aa.drawImage(Y,X,U);return Z}function H(ag,Y,ae){var af=parseInt(ag.length*ae),X=parseInt(ae*2),V=f(af,X),ab,j,W,aa,ad,ac,U,Z;if(!V){return null}ab=V.getContext("2d");ab.fillStyle="#000";ab.fillRect(0,0,af,X);s(ab,ae+"px "+Y,"#fff",ag);j=ab.getImageData(0,0,af,X);W=j.width;aa=j.height;Z={min:{x:W,y:aa},max:{x:-1,y:-1}};for(ac=0;ac<aa;++ac){for(ad=0;ad<W;++ad){U=(ac*W+ad)*4;if(j.data[U+1]>0){if(ad<Z.min.x){Z.min.x=ad}if(ad>Z.max.x){Z.max.x=ad}if(ac<Z.min.y){Z.min.y=ac}if(ac>Z.max.y){Z.max.y=ac}}}}if(W!=af){Z.min.x*=(af/W);Z.max.x*=(af/W)}if(aa!=X){Z.min.y*=(af/aa);Z.max.y*=(af/aa)}V=null;return Z}function r(i){return"'"+i.replace(/(\'|\")/g,"").replace(/\s*,\s*/g,"', '")+"'"}function y(i,j,U){U=U||h;if(U.addEventListener){U.addEventListener(i,j,false)}else{U.attachEvent("on"+i,j)}}function L(V,U,j){if(V.complete){U.w=V.width;U.h=V.height;j.push(U)}else{jQuery(V).bind("load",function(){U.w=this.width;U.h=this.height;j.push(U)})}}function J(V,U){var j=h.defaultView,i=U.replace(/\-([a-z])/g,function(W){return W[1].toUpperCase()});return(j&&j.getComputedStyle&&j.getComputedStyle(V,null).getPropertyValue(U))||(V.currentStyle&&V.currentStyle[i])}function v(U,j){var i=1,V;if(U.weightFrom){i=1*(j.getAttribute(U.weightFrom)||U.textHeight)}else{if(V=J(j,"font-size")){i=(V.indexOf("px")>-1&&V.replace("px","")*1)||(V.indexOf("pt")>-1&&V.replace("pt","")*1.25)||V*3.3}else{U.weight=false}}return i}function I(W){var V,U,j=h.documentElement,X;for(V in q.tc){U=q.tc[V];X=F(U.canvas).offset();if(W.pageX){U.mx=W.pageX-X.left;U.my=W.pageY-X.top}else{U.mx=W.clientX+(j.scrollLeft||h.body.scrollLeft)-X.left;U.my=W.clientY+(j.scrollTop||h.body.scrollTop)-X.top}}}function o(V){var j=q,i=h.addEventListener?0:1,U=V.target&&K(V.target.id)?V.target.id:V.srcElement.parentNode.id;if(U&&V.button==i&&j.tc[U]){I(V);j.tc[U].Clicked(V)}}function Q(U){var i=q,j=U.target&&K(U.target.id)?U.target.id:U.srcElement.parentNode.id;if(j&&i.tc[j]){U.cancelBubble=true;U.returnValue=false;U.preventDefault&&U.preventDefault();i.tc[j].Wheel((U.wheelDelta||U.detail)>0)}}function m(){var U=q.tc,j;for(j in U){U[j].Draw()}}function b(U,i){var j=p(i),V=g(i);return{x:U.x,y:(U.y*V)+(U.z*j),z:(U.y*-j)+(U.z*V)}}function a(U,i){var j=p(i),V=g(i);return{x:(U.x*V)+(U.z*-j),y:U.y,z:(U.x*j)+(U.z*V)}}function P(V,ab,aa,Y,W,j){var i,X,Z,U=V.z1/(V.z1+V.z2+ab.z);i=ab.y*U;X=ab.x*U;Z=V.z2+ab.z;return{x:X,y:i,z:Z}}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;this.SetMethod(i.outlineMethod)}d=e.prototype;d.SetMethod=function(U){var j={block:["PreDraw","DrawBlock"],colour:["PreDraw","DrawColour"],outline:["PostDraw","DrawOutline"],classic:["LastDraw","DrawOutline"]},i=j[U]||j.outline;this[i[0]]=this.Draw;this.drawFunc=this[i[1]]};d.Update=function(aa,Z,ab,X,Y,j,W,i){var U=this.tc.outlineOffset,V=2*U;this.x=Y*aa+W-U;this.y=Y*Z+i-U;this.w=Y*ab+V;this.h=Y*X+V;this.sc=Y;this.z=j.z};d.DrawOutline=function(X,i,W,j,U,V){X.strokeStyle=V;X.strokeRect(i,W,j,U)};d.DrawColour=function(V,Y,W,Z,U,i,aa,j,X){return this[aa.image?"DrawColourImage":"DrawColourText"](V,Y,W,Z,U,i,aa,j,X)};d.DrawColourText=function(W,Z,X,aa,U,i,ab,j,Y){var V=ab.colour;ab.colour=i;ab.Draw(W,j,Y);ab.colour=V;return 1};d.DrawColourImage=function(Z,ac,aa,ad,Y,i,ag,j,ab){var ae=Z.canvas,W=~~x(ac,0),V=~~x(aa,0),X=S(ae.width-W,ad)+0.5|0,af=S(ae.height-V,Y)+0.5|0,U;if(w){w.width=X,w.height=af}else{w=f(X,af)}if(!w){return this.SetMethod("outline")}U=w.getContext("2d");U.drawImage(ae,W,V,X,af,0,0,X,af);Z.clearRect(W,V,X,af);ag.Draw(Z,j,ab);Z.setTransform(1,0,0,1,0,0);Z.save();Z.beginPath();Z.rect(W,V,X,af);Z.clip();Z.globalCompositeOperation="source-in";Z.fillStyle=i;Z.fillRect(W,V,X,af);Z.restore();Z.globalCompositeOperation="destination-over";Z.drawImage(w,0,0,X,af,W,V,X,af);Z.globalCompositeOperation="source-over";return 1};d.DrawBlock=function(X,i,W,j,U,V){X.fillStyle=V;X.fillRect(i,W,j,U)};d.Draw=function(X,i,j,V){var W=new Date().valueOf()-this.ts,U=this.tc;X.setTransform(1,0,0,1,0,0);X.strokeStyle=U.outlineColour;X.lineWidth=U.outlineThickness;X.shadowBlur=X.shadowOffsetX=X.shadowOffsetY=0;if(U.pulsateTo<1){X.globalAlpha=U.pulsateTo+((1-U.pulsateTo)*(0.5+(g(2*Math.PI*W/(1000*U.pulsateTime))/2)))}else{X.globalAlpha=1}return this.drawFunc(X,this.x,this.y,this.w,this.h,U.outlineColour,i,j,V)};d.Active=function(U,i,j){return(i>=this.x&&j>=this.y&&i<=this.x+this.w&&j<=this.y+this.h)};d.PreDraw=d.PostDraw=d.LastDraw=function(){};function C(W,j,aa,ac,ab,Y,U,V){var Z=W.ctxt,X;this.tc=W;this.image=j.src?j:null;this.name=j.src?"":j;this.a=aa;this.p3d={x:ac[0]*W.radius*1.1,y:ac[1]*W.radius*1.1,z:ac[2]*W.radius*1.1};this.x=this.y=0;this.w=ab;this.h=Y;this.colour=U||W.textColour;this.textFont=V||W.textFont;this.weight=this.sc=this.alpha=1;this.weighted=!W.weight;this.outline=new e(W);if(this.image){if(W.txtOpt&&W.shadow){X=M(this.image,W.shadow,W.shadowBlur,W.shadowOffset);if(X){this.image=X;this.w=X.width;this.h=X.height}}}else{this.textHeight=W.textHeight;this.extents=H(this.name,this.textFont,this.textHeight);this.Measure(Z,W)}this.SetShadowColour=W.shadowAlpha?this.SetShadowColourAlpha:this.SetShadowColourFixed;this.SetDraw(W)}G=C.prototype;G.SetDraw=function(i){this.Draw=this.image?(i.ie>7?this.DrawImageIE:this.DrawImage):this.DrawText};G.Measure=function(Y,j){this.h=this.extents?this.extents.max.y+this.extents.min.y:this.textHeight;Y.font=this.font=this.textHeight+"px "+this.textFont;this.w=Y.measureText(this.name).width;if(j.txtOpt){var V=j.txtScale,W=V*this.textHeight,X=W+"px "+this.textFont,U=[V*j.shadowOffset[0],V*j.shadowOffset[1]],i;Y.font=X;i=Y.measureText(this.name).width;this.image=k(this.name,X,W,i,V*this.h,this.colour,j.shadow,V*j.shadowBlur,U,V,V);if(this.image){this.w=this.image.width/V;this.h=this.image.height/V}this.SetDraw(j);j.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(V,U){var j=this.weight,i=U.weightMode;this.weighted=true;if(i=="colour"||i=="both"){this.colour=T(U,(j-U.min_weight)/(U.max_weight-U.min_weight))}if(i=="size"||i=="both"){this.textHeight=j*U.weightSize}this.extents=H(this.name,this.textFont,this.textHeight)};G.SetShadowColourFixed=function(U,j,i){U.shadowColor=j};G.SetShadowColourAlpha=function(U,j,i){U.shadowColor=n(j,i)};G.DrawText=function(U,Y,j){var Z=this.tc,W=this.x,V=this.y,X,i,aa=this.sc;U.globalAlpha=this.alpha;U.setTransform(aa,0,0,aa,0,0);U.fillStyle=this.colour;Z.shadow&&this.SetShadowColour(U,Z.shadow,this.alpha);U.font=this.font;X=this.w;i=this.h;W+=(Y/aa)-(X/2);V+=(j/aa)-(i/2);U.fillText(this.name,W,V)};G.DrawImage=function(W,aa,V){var ab=this.tc,Y=this.x,X=this.y,ac=this.sc,j=this.image,Z=this.w,U=this.h;W.globalAlpha=this.alpha;W.setTransform(ac,0,0,ac,0,0);W.fillStyle=this.colour;ab.shadow&&this.SetShadowColour(W,ab.shadow,this.alpha);Y+=(aa/ac)-(Z/2);X+=(V/ac)-(U/2);W.drawImage(j,Y,X,Z,U)};G.DrawImageIE=function(W,aa,V){var j=this.image,ab=this.sc,Z=j.width=this.w*ab,U=j.height=this.h*ab,Y=(this.x*ab)+aa-(Z/2),X=(this.y*ab)+V-(U/2);W.globalAlpha=this.alpha;W.drawImage(j,Y,X)};G.Calc=function(W,V){var i=a(this.p3d,W),j=this.tc,X=j.minBrightness,U=j.radius;this.p3d=b(i,V);i=P(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=x(X,S(1,X+1-((i.z-j.z2+U)/(2*U))))};G.CheckActive=function(V,Z,U){var aa=this.tc,ab=this.sc,i=this.outline,Y=this.w,j=this.h,X=this.x-Y/2,W=this.y-j/2;i.Update(X,W,Y,j,ab,this.p3d,Z,U);return i.Active(V,aa.mx,aa.my)?i:null};G.Clicked=function(W){var j=this.a,U=j.target,V=j.href,i;if(U!=""&&U!="_self"){if(self.frames[U]){self.frames[U]=V}else{if(top.frames[U]){top.frames[U]=V}else{window.open(V,U)}}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=V};function q(){var j,U={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,outlineMethod:"outline",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,wheelZoom:true,zoomMin:0.3,zoomMax:3,zoomStep:0.05,shape:"sphere",lock:null};for(j in U){this[j]=U[j]}this.max_weight=0;this.min_weight=200}c=q.prototype;c.Draw=function(){var ad=this.canvas,ab=ad.width,U=ad.height,j=0,aa=this.yaw,V=this.pitch,W=ab/2,ag=U/2,ae=this.ctxt,Y,af,ac,Z=-1,ah=this.taglist,X=ah.length;if(aa==0&&V==0&&this.drawn){return this.Animate(ab,U)}ae.setTransform(1,0,0,1,0,0);this.active=null;for(ac=0;ac<X;++ac){ah[ac].Calc(aa,V)}ah=ah.sort(function(ai,i){return ai.sc-i.sc});for(ac=0;ac<X;++ac){af=ah[ac].CheckActive(ae,W,ag);if(af&&af.sc>j&&(!this.frontSelect||af.z<=0)){Y=af;Y.index=Z=ac;j=af.sc}}this.active=Y;if(!this.txtOpt&&this.shadow){ae.shadowBlur=this.shadowBlur;ae.shadowOffsetX=this.shadowOffset[0];ae.shadowOffsetY=this.shadowOffset[1]}ae.clearRect(0,0,ab,U);for(ac=0;ac<X;++ac){if(!(Z==ac&&Y.PreDraw(ae,ah[ac],W,ag))){ah[ac].Draw(ae,W,ag)}Z==ac&&Y.PostDraw(ae)}if(this.freezeActive&&Y){this.yaw=this.pitch=this.drawn=0}else{this.Animate(ab,U);this.drawn=1}Y&&Y.LastDraw(ae)};c.Animate=function(Z,W){var U=this,Y=U.mx,X=U.my,j=U.lock,ab,aa,V,i;if(Y>=0&&X>=0&&Y<Z&&X<W){ab=U.maxSpeed,i=U.reverse?-1:1;if(j!="x"){this.yaw=i*((ab*2*Y/Z)-ab)}if(j!="y"){this.pitch=i*-((ab*2*X/W)-ab)}this.initial=null}else{if(!U.initial){ab=U.minSpeed,aa=E(U.yaw),V=E(U.pitch);if(j!="x"&&aa>ab){this.yaw=aa>U.z0?U.yaw*U.decel:0}if(j!="y"&&V>ab){this.pitch=V>U.z0?U.pitch*U.decel:0}}}};c.Zoom=function(i){this.z2=this.z1*(1/i);this.drawn=0};c.Clicked=function(V){var U=this.taglist,i=this.active;try{if(i&&U[i.index]){U[i.index].Clicked(V)}}catch(j){}};c.Wheel=function(j){var U=this.zoom+this.zoomStep*(j?1:-1);this.zoom=S(this.zoomMax,x(this.zoomMin,U));this.Zoom(this.zoom)};q.tc={};jQuery.fn.tagcanvas=function(U,j){var i,V=j?jQuery("#"+j):this;if(h.all&&!j){return false}i=V.find("a");if(K(window.G_vmlCanvasManager)){this.each(function(){F(this)[0]=window.G_vmlCanvasManager.initElement(F(this)[0])});U.ie=parseFloat(navigator.appVersion.split("MSIE")[1])}if(!i.length||!this[0].getContext||!this[0].getContext("2d").fillText){return false}this.each(function(){var Y,W,aa,ad,ae,Z,ac,ab=[],X={sphere:l,vcylinder:u,hcylinder:D};j||(i=F(this).find("a"));Z=new q;for(Y in U){Z[Y]=U[Y]}Z.z1=(19800/(Math.exp(Z.depth)*(1-1/Math.E)))+20000-19800/(1-(1/Math.E));Z.z2=Z.z1*(1/Z.zoom);Z.radius=(this.height>this.width?this.width:this.height)*0.33*(Z.z2+Z.z1)/(Z.z1);Z.yaw=Z.initial?Z.initial[0]*Z.maxSpeed:0;Z.pitch=Z.initial?Z.initial[1]*Z.maxSpeed:0;Z.canvas=F(this)[0];Z.ctxt=Z.canvas.getContext("2d");Z.textFont=Z.textFont&&r(Z.textFont);Z.pulsateTo*=1;Z.textHeight*=1;Z.minBrightness*=1;Z.ctxt.textBaseline="top";if(Z.shadowBlur||Z.shadowOffset[0]||Z.shadowOffset[1]){Z.ctxt.shadowColor=Z.shadow;Z.shadow=Z.ctxt.shadowColor;Z.shadowAlpha=t()}else{delete Z.shadow}Z.taglist=[];Z.shape=X[Z.shape]||X.sphere;W=Z.shape(i.length);for(Y=0;Y<i.length;++Y){aa=i[Y].getElementsByTagName("img");if(aa.length){ad=new Image;ad.src=aa[0].src;ae=new C(Z,ad,i[Y],W[Y],1,1);L(ad,ae,Z.taglist)}else{Z.taglist.push(new C(Z,i[Y].innerText||i[Y].textContent,i[Y],W[Y],2,Z.textHeight+2,Z.textColour||J(i[Y],"color"),Z.textFont||r(J(i[Y],"font-family"))))}if(Z.weight){ac=v(Z,i[Y]);if(ac>Z.max_weight){Z.max_weight=ac}if(ac<Z.min_weight){Z.min_weight=ac}ab.push(ac)}}if(Z.weight=(Z.max_weight>Z.min_weight)){for(Y=0;Y<Z.taglist.length;++Y){Z.taglist[Y].SetWeight(ab[Y])}}q.tc[F(this)[0].id]=Z;y("mousemove",I,this);y("mouseout",I,this);y("mouseup",o,this);if(Z.wheelZoom){y("mousewheel",Q,this);y("DOMMouseScroll",Q,this)}j&&Z.hideTags&&V.hide();U.interval=U.interval||Z.interval});return !!(q.started||(q.started=setInterval(m,U.interval)))}})(jQuery);

