/**
 * Copyright (C) 2010 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.2
 * For more information, please contact <graham@goat1000.com>
 */
(function(b){var a={Point3D:function(c,e,d){this.x=c;this.y=e;this.z=d},RotX:function(f,d){var e=Math.sin(d),g=Math.cos(d);return new a.Point3D(f.x,(f.y*g)+(f.z*e),(f.y*-e)+(f.z*g))},RotY:function(f,d){var e=Math.sin(d),g=Math.cos(d);return new a.Point3D((f.x*g)+(f.z*-e),f.y,(f.x*e)+(f.z*g))},RotZ:function(f,d){var e=Math.sin(d),g=Math.cos(d);return new a.Point3D((f.x*g)+(f.y*e),(f.x*-e)+(f.y*g),f.z)},Project:function(j,d,g,e,f){var k,i,c;k=(j.y*a.z1)/(a.z1+a.z2+j.z);i=(j.x*a.z1)/(a.z1+a.z2+j.z);c=a.z2+j.z;return new a.Point3D(i,k,c)},Draw:function(e){var j=0,f,h,k,d,g;k=e.getContext("2d");k.clearRect(0,0,e.width,e.height);k.strokeStyle="white";k.fillStyle="white";f=e.width/2;h=e.height/2;this.active=null;for(g=0;g<this.taglist.length;++g){d=this.taglist[g].Draw(k,f,h,this.yaw,this.pitch);if(d&&d.sc>j){this.active=d;this.active.index=g;j=d.sc}}if(a.freezeActive&&this.active){this.yaw=this.pitch=0}else{this.Animate(e.width,e.height)}if(this.active){this.active.Draw(k)}},Animate:function(c,d){if(a.mx>=0&&a.my>=0&&a.mx<c&&a.my<d){this.yaw=(a.maxSpeed*2*a.mx/c)-a.maxSpeed;this.pitch=-((a.maxSpeed*2*a.my/d)-a.maxSpeed);if(a.reverse){this.yaw=-this.yaw;this.pitch=-this.pitch}a.initial=null}else{if(!a.initial){this.yaw=this.yaw*a.decel;this.pitch=this.pitch*a.decel}}},Clicked:function(d){try{if(this.active&&this.taglist[this.active.index]){this.taglist[this.active.index].Clicked(d)}}catch(c){}},Outline:function(c,g,d,e,f){this.ts=new Date();this.Update=function(i,m,j,k,l){this.x=i;this.y=m;this.w=j;this.h=k;this.sc=l};this.Draw=function(i){var h=new Date()-this.ts;i.save();i.scale(this.sc,this.sc);i.strokeStyle=a.outlineColour;i.lineWidth=a.outlineThickness;if(a.pulsateTo<1){i.globalAlpha=a.pulsateTo+((1-a.pulsateTo)*(0.5+(Math.cos(2*Math.PI*h/(1000*a.pulsateTime))/2)))}i.beginPath();i.rect(this.x-a.outlineOffset,this.y-a.outlineOffset,this.w+a.outlineOffset*2,this.h+a.outlineOffset*2);i.closePath();i.stroke();i.restore()};this.Update(c,g,d,e,f)},Tag:function(f,e,i,d,c,g){this.name=f;this.href=e;this.target=i;this.p3d=new a.Point3D;this.p3d.x=d[0]*a.radius*1.1;this.p3d.y=d[1]*a.radius*1.1;this.p3d.z=d[2]*a.radius*1.1;this.x=0;this.y=0;this.w=c;this.h=g;this.sc=1;this.alpha=1;this.Draw=function(o,j,n,l,k){var h;this.Calc(l,k);o.save();o.globalAlpha=this.alpha;o.scale(this.sc,this.sc);o.textBaseline="top";o.fillStyle=a.textColour;o.font=a.textHeight+"px "+a.textFont;h=o.measureText(this.name);this.w=((h.width+2)*this.sc);this.h1=this.h*this.sc;j=(j-this.w/2)/this.sc;n=(n-this.h1/2)/this.sc;this.w=h.width+2;this.h1=this.h+2;o.fillText(this.name,j+this.x+1,n+this.y+1,this.w-2);o.beginPath();o.rect(j+this.x,n+this.y,this.w,this.h1);o.closePath();o.restore();if(o.isPointInPath(a.mx,a.my)){if(this.active){this.active.Update(j+this.x,n+this.y,this.w,this.h1,this.sc)}else{this.active=new a.Outline(j+this.x,n+this.y,this.w,this.h1,this.sc)}}else{this.active=null}return this.active};this.Calc=function(k,j){var h=a.RotY(this.p3d,k);this.p3d=a.RotX(h,j);h=a.Project(this.p3d,this.w,this.h,Math.PI/4,20);this.x=h.x;this.y=h.y;this.sc=(a.z1+a.z2-h.z)/a.z2;this.alpha=a.minBrightness+1-((h.z-a.z2+a.radius)/(2*a.radius))};this.Clicked=function(h){if(this.target==""||this.target=="_self"){document.location=this.href}else{if(self.frames[this.target]){self.frames[this.target]=this.href}else{if(top.frames[this.target]){top.frames[this.target]=this.href}else{window.open(this.href,this.target)}}}}},MouseMove:function(c){a.mx=c.pageX-a.cx;a.my=c.pageY-a.cy},MouseClick:function(c){a.MouseMove(c);a.Clicked(c)},PointsOnSphere:function(k){var c,j,e,d,h=[],f=Math.PI*(3-Math.sqrt(5)),g=2/k;for(c=0;c<k;++c){j=c*g-1+(g/2);e=Math.sqrt(1-j*j);d=c*f;h.push([Math.cos(d)*e,j,Math.sin(d)*e])}return h},mx:-1,my:-1,cx:0,cy:0,z1:20000,z2:20000,freezeActive:false,pulsateTo:1,pulsateTime:3,reverse:false,depth:0.5,maxSpeed:0.05,decel:0.95,initial:null,minBrightness:0.1,outlineColour:"#ffff99",outlineThickness:2,outlineOffset:5,textColour:"#ff99ff",textHeight:15,textFont:"Helvetica, Arial, sans-serif"};jQuery.fn.tagcanvas=function(e){var c=this.find("a"),g=this.offset(),d=this.eq(0),f;if(!c.length||!d[0].getContext||!d[0].getContext("2d").fillText){return false}for(f in e){a[f]=e[f]}a.z1=(19800/(Math.exp(a.depth)*(1-1/Math.E)))+20000-19800/(1-(1/Math.E));a.z2=a.z1;d.each(function(){var h,j;a.radius=(this.height>this.width?this.width:this.height)*0.33*(a.z2+a.z1)/(a.z1);a.taglist=[];j=a.PointsOnSphere(c.length);for(h=0;h<c.length;++h){a.taglist.push(new a.Tag(c[h].textContent,c[h].href,c[h].target,j[h],10,a.textHeight))}a.yaw=a.initial?a.initial[0]*a.maxSpeed:0;a.pitch=a.initial?a.initial[1]*a.maxSpeed:0;a.cx=g.left;a.cy=g.top;setInterval(function(){a.Draw(d[0])},10);document.addEventListener("mousemove",a.MouseMove,false);document.addEventListener("mouseout",a.MouseMove,false);document.addEventListener("mousedown",a.MouseClick,false)});return true}})(jQuery);

$(document).ready(function() {
  if( ! $('#tagCloud').tagcanvas({
    textColour : '#0eaa18',
    outlineThickness : 1,
    outlineColour: '#4fc958',
    maxSpeed : 0.05,
    depth : 0.75,
    reverse: true,
    minBrightness: 0.2,
    textHeight: 17,
    textFont: '"Trebuchet MS", Arial, Helvetica, sans-serif',
    depth: 0.9
  })) {
    $('#tagCloud').hide();
  }
});
