/*
Plugin Name: CSS Tag Cloud
Plugin URI: http://vincentvanuffelen.info/xxx/csstagcloud/
Description: Generates a css formated tag cloud of the posting's tags    
Version: 1.0
Author: Vincent Van Uffelen
Author URI: http://www.vincentvanuffelen.info/
*/
/*	
  	Copyright 2008 Vincent Van Uffelen 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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.
	
*/

var csstagcloud = {
	"url" : null,
	"timer" : null,
	"tag_id" : null,
	"active_tag_id" : null,
	"ajax" : null,
	"prefix" : "", 
	"tagcloud" : [],
	"num_classes" : null,
	"locked" : false,
	"disabled" : false,
	"requested_id" : 0,
	"inititialized" : false,
	
	//g == get, to keep tag lists code short
	"g" : function (tag_id) {
		if (!csstagcloud.inititialized) {
		  csstagcloud.init();
		}
		// copy original tag cloud formating
		if (csstagcloud.tagcloud.length == 0) {
			var tags = $$('a[id^="' + csstagcloud.prefix +'"]');
			for (var i=0;i<tags.length;i++) {
				csstagcloud.tagcloud[csstagcloud.tagcloud.length] = Array(tags[i].id+"", tags[i].classNames()+"");
			}
		}
		
		// clear active timer
		if (csstagcloud.timer != null) {
			window.clearInterval(csstagcloud.timer);
		}
		csstagcloud.timer = window.setInterval("csstagcloud.csstagcloud_invoke()", 1000);
		csstagcloud.tag_id = tag_id;
		return true;
	},
	
	//r == reset, to keep tag lists code short
	"r" : function (tag_id) {
		if (csstagcloud.timer != null) {
			window.clearInterval(csstagcloud.timer);
		}
		if (csstagcloud.active_tag_id != null) {
			for (var i=0;i<csstagcloud.tagcloud.length;i++) {
				var tag = $(csstagcloud.tagcloud[i][0]);
				if (tag) {
					tag.className = csstagcloud.tagcloud[i][1];
				}
			}
			csstagcloud.active_tag_id = null;
		}
		return true;
	},
	
	//d == disable, to avoid ajax tag cloud update while new page gets loaded
	"d" : function () {
		if (csstagcloud.timer != null) {
			window.clearInterval(csstagcloud.timer);
		}
		csstagcloud.disabled = true;
		return true;
	},
	
	"csstagcloud_invoke" : function () {
		if (!csstagcloud.locked && !csstagcloud.disabled) { 
			csstagcloud.locked = true; // prevents sending more than one request
			if (csstagcloud.timer != null) {
				window.clearInterval(csstagcloud.timer);
			}
			if (csstagcloud.url != null && csstagcloud.tag_id != null && csstagcloud.tag_id != csstagcloud.active_tag_id) {
				// set ajax class to every node
				var tags = $$('a[id^="' + csstagcloud.prefix +'"]');
				for (var i=0;i<tags.length;i++) {
					if (tags[i].id != csstagcloud.prefix + csstagcloud.tag_id) {
						tags[i].removeClassName(csstagcloud.prefix + "none");
						tags[i].addClassName(csstagcloud.prefix + "ajax");
					}
				}
				
				//initialize sack with global variable
				csstagcloud.ajax = new sack(csstagcloud.url);
				csstagcloud.ajax.method = 'GET';
				csstagcloud.ajax.setVar("tag_id", csstagcloud.tag_id);
				csstagcloud.ajax.onError = function() {alert('AJAX error in CSS Tag Cloud')};
				csstagcloud.ajax.onCompletion = csstagcloud.csstagcloud_callback;
				csstagcloud.ajax.runAJAX();
				
				csstagcloud.requested_id = csstagcloud.tag_id;
			}
		}
		return true;
	},

	"csstagcloud_callback" : function() {
		if (!csstagcloud.disabled) {
			csstagcloud.locked = false;

			if (csstagcloud.ajax.response != null) { // && !csstagcloud.outevent) {
				var tags = $$('a[id^="' + csstagcloud.prefix +'"]');
				var rel_tags = eval(csstagcloud.ajax.response);
				if (rel_tags && rel_tags.length > 0) {
					// prepare variables to calculate ranges for tag classes
					var min_count = 1;
					var max_count = 1;
					for (var i=0;i<rel_tags.length;i++) {
						if (rel_tags[i][1] < min_count) {
							min_count = rel_tags[i][1];
						} else if (rel_tags[i][1] > max_count) {
							max_count = rel_tags[i][1];
						}
					}
					if(csstagcloud.num_classes < 2 ) {
	    				csstagcloud.num_classes = 2;
					}
					
					// clear tag cloud of unwanted css classes
					for (var i=0;i<tags.length;i++) {
						tags[i].removeClassName(csstagcloud.prefix + "ajax");
						tags[i].addClassName(csstagcloud.prefix + "none");	
					}
					
					// format active tag
					var active_tag = $(csstagcloud.prefix + csstagcloud.requested_id);
					if (active_tag) {
						active_tag.removeClassName(csstagcloud.prefix + "none");
						active_tag.className = active_tag.className.replace(new RegExp(csstagcloud.prefix + "[0-9]{1,2}"), csstagcloud.prefix + csstagcloud.num_classes);
					}
					
					// update tag format according to relation to actual tag
					for (var i=0;i<rel_tags.length;i++) {
						for (var j=0;j<tags.length;j++) {
							if (tags[j].id == csstagcloud.prefix + rel_tags[i][0] 
								&& tags[j].id != csstagcloud.prefix + csstagcloud.tag_id) {
								var tag = $(tags[j].id);
								if (tag) {
									var css_class = Math.round(1 + Math.round((csstagcloud.num_classes - 1) * Math.log(rel_tags[i][1] - min_count + 2)/Math.log(max_count - min_count+2)-1));
									tag.className = tag.className.replace(new RegExp(csstagcloud.prefix + "[0-9]{1,2}"), csstagcloud.prefix + css_class);
									tag.removeClassName(csstagcloud.prefix + "none");
								}
								break;
							} 
						}
					}
				} else {
					csstagcloud.r(0);	
				}
				csstagcloud.requested_id = 0;
				csstagcloud.active_tag_id = csstagcloud.tag_id;
			}
		}
	},
	
	"init" : function() {
	  if (csstagcloud_vars != undefined) {
  	  csstagcloud.url = csstagcloud_vars.url;
      csstagcloud.prefix = csstagcloud_vars.prefix;
      csstagcloud.num_classes = csstagcloud_vars.num_classes;
      csstagcloud.timeout = csstagcloud_vars.timeout;
    }
    csstagcloud.inititialized = true;
	}
}


