/*
* jQuery TagCloud 0.5.0
* Copyright (c) 2008 Ron Valstar
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*/
/*
(function(F){var C;var A={};var G={};var L=2.399963;F.tagcloud={id:"TagCloud",version:"0.5.0",defaults:{height:null,type:"list",sizemax:13,sizemin:13,colormax:"535454",colormin:"cecccc",seed:null,power:0.5}};F.fn.extend({tagcloud:function(M){C=F.extend({},F.tagcloud.defaults,M);if(C.seed===null){C.seed=Math.ceil(Math.random()*45309714203)}switch(C.type){case"sphere":case"cloud":A={position:"relative"};G={position:"absolute",display:"block"};break;case"list":A={height:"auto"};G={position:"static",display:"inline"};break}B.setSeed(C.seed+123456);return this.each(function(f,a){var q=F(a);var R=q.find(">li");var S=R.length;var Z=q.width();var l=C.height===null?(0.004*Z*S):C.height;q.css({width:Z,height:l,listStyle:"none",margin:0,padding:0});q.css(A);var e=-2147483647;var r=2147483648;var T=-1;for(var d=0;d<S;d++){var p=F(R[d]);var n=p.attr("value")==-1?T++:p.attr("value");if(n>e){e=n}if(n<r){r=n}T=n}var b=e-r;var X=new Array();for(var d=0;d<S;d++){X[d]=d}for(var d,V,c=X.length;c;d=parseInt(B.rand(0,1000)/1000*c),V=X[--c],X[c]=X[d],X[d]=V){}T=-1;for(var d=0;d<S;d++){var p=F(R[d]);var n=p.attr("value")==-1?T++:p.attr("value");T=n;var g=((S-d-1)/(S-1));var g=(n-r)/b;var m=C.sizemin+g*(C.sizemax-C.sizemin);var N;if(m == C.sizemin){N = D(C.colormin,C.colormin,g);}else{N = D(C.colormax,C.colormax,g);}p.css({fontSize:m,position:"absolute",color:"#"+N,margin:0,padding:0}).children().css({color:"#"+N});var Y=p.width();var h=p.height();var Q={};if(C.type!="list"){if(C.type=="cloud"){var s=B.rand(0,Z-Y);var W=X[d]*(l/S)-h/2}else{var P=Math.pow(d/S,C.power);var U=(d+Math.PI/2)*L;var s=Z/2-Y/2+0.5*Z*P*Math.sin(U);var W=l/2-h/2+0.5*l*P*Math.cos(U)}Q.left=s;Q.top=W}for(var O in G){Q[O]=G[O]}p.css(Q)}})}});var B=new function(){this.seed=23145678901;this.A=48271;this.M=2147483647;this.Q=this.M/this.A;this.R=this.M%this.A;this.oneOverM=1/this.M};B.setSeed=function(M){this.seed=M};B.next=function(){var M=this.seed/this.Q;var N=this.seed%this.Q;var O=this.A*N-this.R*M;this.seed=O+(O>0?0:this.M);return(this.seed*this.oneOverM)};B.rand=function(N,M){return Math.floor((M-N+1)*this.next()+N)};function I(M){return M.toString(16)}function K(M){return parseInt(M,16)}function H(Q){var M=Q.length==3;var O=[];for(var N=0;N<3;N++){var P=Q.substring(N*(M?1:2),(N+1)*(M?1:2));O.push(K(M?P+P:P))}return O}function J(M){var O="";for(var N=0;N<3;N++){var P=I(M[N]);if(P.length==1){P="0"+P}O+=P}return O}function D(R,S,P){var Q=H(R);var M=H(S);var O=[];for(var N=0;N<3;N++){O.push(Q[N]+Math.floor(P*(M[N]-Q[N])))}return J(O)}function E(M){if(window.console&&window.console.log){if(typeof (M)=="string"){window.console.log(M)}else{for(var N in M){window.console.log(N+": "+M[N])}}}}F.fn.TagCloud=F.fn.Tagcloud=F.fn.tagcloud})(jQuery);
*/

/*
* jQuery TagCloud 0.5.0
* Copyright (c) 2008 Ron Valstar
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*/
(function($) {

	// private variables
	var oSettings;
	var oUlCss = {};
	var oLiCss = {};
	var fGAng = 2.399963; // sphere angle in radians*pi
	// default settings
	$.tagcloud = {
		 id: "TagCloud"
		,version: "0.5.0"
		,defaults: {
			 height: null
			,type: "cloud"		// "cloud", "list" or "sphere"
			,sizemax: 20
			,sizemin: 10
			,colormax: "00F"
			,colormin: "B4D2FF"
			,seed: null			// only for type=="cloud"
			,power: .5			// only for type=="sphere"
		}
	};
	$.fn.extend({
		tagcloud: function(_settings) {
			oSettings = $.extend({}, $.tagcloud.defaults, _settings);
			if (oSettings.seed===null) oSettings.seed = Math.ceil(Math.random()*45309714203);

			switch (oSettings.type) {
				// treemap ::	http://www.jquery.info/The-TreeMap-plugin
								
				case "sphere":
				case "cloud":
					oUlCss = {position:"relative"};
					oLiCss = {position:"absolute",display:"block"};
				break;
				case "list":
					oUlCss = {height:"auto"};
					oLiCss = {position:"static",display:"inline"};
				break;
			}
			Rng.setSeed(oSettings.seed+123456);			
			return this.each(function(i,o) {
				var mUl = $(o);
				var aLi = mUl.find(">li");
				var iNumLi = aLi.length;


				var iUlW = mUl.width();
//				var iUlH = oSettings.height===null?mUl.height():oSettings.height;
				var iUlH = oSettings.height===null?(.004*iUlW*iNumLi):oSettings.height;
				mUl.css({width:iUlW,height:iUlH,listStyle:"none",margin:0,padding:0});
				mUl.css(oUlCss);
				

				var iValMx = -2147483647;
				var iValMn = 2147483648;
				var iLastVal = -1;
				for (var j=0;j<iNumLi;j++) {
					var mLi = $(aLi[j]);
					var normalValue;
					var iVal = mLi.attr("value")==-1?iLastVal++:mLi.attr("value");
					normalValue = mLi.attr("value");
					if (iVal>iValMx) iValMx = iVal;
					if (iVal<iValMn) iValMn = iVal;
					iLastVal = iVal;
				}
				var iValRn = iValMx-iValMn;

				// place on line to create minimal overlays
				var aLine = new Array();
				for (var j=0;j<iNumLi;j++) aLine[j] = j;
				for (var j, x, k = aLine.length; k; j = parseInt(Rng.rand(0,1000)/1000 * k), x = aLine[--k], aLine[k] = aLine[j], aLine[j] = x);

				iLastVal = -1;
				for (var j=0;j<iNumLi;j++) {
					var mLi = $(aLi[j]);;
					var iVal = mLi.attr("value")==-1?iLastVal++:mLi.attr("value");
					iLastVal = iVal;
					//
					var fPrt = ((iNumLi-j-1)/(iNumLi-1));
					var fPrt = (iVal-iValMn)/iValRn;
					//
					var iSzFnt = oSettings.sizemin + fPrt*(oSettings.sizemax-oSettings.sizemin);
					
					//EDIT
					//This will decide which color to assign the link.
					//If it is the minimum fontsize then give it the color value = colormin				
					var sColor;
					if(mLi.attr("value") == minValue){
						sColor = colorRng(oSettings.colormin,oSettings.colormin,fPrt);
					}
					else{//otherwise give it the value that is equal to colormax
						sColor = colorRng(oSettings.colormax,oSettings.colormax,fPrt);
					}
					
					mLi.css({"fontSize":iSzFnt,position:"absolute",color:"#"+sColor,margin:0,padding:0}).children().css({color:"#"+sColor});
					var iLiW = mLi.width();
					var iLiH = mLi.height()
					//
					var oCss = {};
					if (oSettings.type!="list") {
						if (oSettings.type=="cloud") {
							var iXps = Rng.rand(0,iUlW-iLiW);
							var iYps = aLine[j]*(iUlH/iNumLi) - iLiH/2;
						} else {
							var fRds = Math.pow(j/iNumLi,oSettings.power);
							var fRad = (j+Math.PI/2)*fGAng;
							var iXps = iUlW/2 - iLiW/2 + .5*iUlW*fRds*Math.sin(fRad);
							var iYps = iUlH/2 - iLiH/2 + .5*iUlH*fRds*Math.cos(fRad);
						}
						oCss.left = iXps;
						oCss.top  = iYps;
					}
					for (var prop in oLiCss) oCss[prop] = oLiCss[prop];
					mLi.css(oCss);
				}
			});
		}
	});
	// Park-Miller RNG
	var Rng = new function() {
		this.seed = 23145678901;
		this.A = 48271;
		this.M = 2147483647;
		this.Q = this.M/this.A;
		this.R = this.M%this.A;
		this.oneOverM = 1.0/this.M;
	}
	Rng.setSeed = function(seed) {
		this.seed = seed;
	}
	Rng.next = function() {
		var hi   = this.seed/this.Q;
		var lo   = this.seed%this.Q;
		var test = this.A*lo - this.R*hi;
		this.seed = test + (test>0?0:this.M);
		return (this.seed*this.oneOverM);
	}
	Rng.rand = function(lrn, urn) {
		return Math.floor((urn - lrn + 1) * this.next() + lrn);
	}
	// hex dec
	function d2h(d) {return d.toString(16);}
	function h2d(h) {return parseInt(h,16);}
//	function getC(s,rgb) {
//		var aRng = [[[0,1],[1,2],[2,3]],[[0,2],[2,4],[4,6]]][s.length==3?0:1][rgb];
//		return s.substring(aRng[0],aRng[1]);
//	}
	function getRGB(s) {
		var b3 = s.length==3;
		var aClr = [];
		for (var i=0;i<3;i++) {
			var sClr = s.substring( i*(b3?1:2), (i+1)*(b3?1:2) );
			aClr.push(h2d(b3?sClr+sClr:sClr));
		}
		return aClr;
	}
	function getHex(a) {
		var s = "";
		for (var i=0;i<3;i++) {
			var c = d2h(a[i]);
			if (c.length==1) c = "0"+c; // todo: this can be better
			s += c;
		}
		return s;
	}
	function colorRng(mn,mx,prt) {
		var aMin = getRGB(mn);
		var aMax = getRGB(mx);		
		var aRtr = [];
		for (var i=0;i<3;i++) aRtr.push( aMin[i] + Math.floor(prt*(aMax[i]-aMin[i])) );
		return getHex(aRtr);
	}
	// trace
	function trace(o) {
		if (window.console&&window.console.log) {
			if (typeof(o)=="string")	window.console.log(o);
			else						for (var prop in o) window.console.log(prop+": "+o[prop]);
		}
	};
	// set functions
	$.fn.TagCloud = $.fn.Tagcloud = $.fn.tagcloud;
})(jQuery);
