//	"JavaScript de Sugoi TagCloud" v.0.90
//			Programmed by Piroli YUKARINOMIYA (Open MagicVox)
//			@see http://www.magicvox.net/archive/2005/09241727.php
//			Original concept by ogawa (Ogawa::Memoranda)
//			@see http://as-is.net/blog/archives/001027.html

// Division ticks of slider controller
var division = 10;

// min count of tag appearance (cf. to retrieve with MTTags)
var count_min = 1;
// max count of tag appearance (cf. to retrieve with MTTags)
var count_max = 39;

////////////////////////////////////////////////////////////////////////
function E (id) { return document.getElementById (id); }

////////////////////////////////////////////////////////////////////////
// Retrieve the font size by linear compensation
// mapping count in [count_min, count_max] -> [size_min, size_max]
function calcFontSize (count) {
	var size_min = 12;			// min font size on count_max
	var size_max = 28;			// max font size on count_min
	return (size_max - size_min) * (count - count_min) / (count_max - count_min) + size_min;
}

// Retrieve the normalized count of tag appearance
// mapping count in [count_min, count_max] -> [1, division]
function calcNormalizeCount (count) {
	return division * (count - count_min) / (count_max - count_min) + 1;
}

// Retrieve style by freshness
function calcRefreshTime (diff) {
	if (diff <=  90) return 'diff1';
	if (diff <= 180) return 'diff2';
	if (diff <= 360) return 'diff3';
	if (360 <= diff) return 'diff4';
	return null;
}

// Initialize the tag cloud named 'name'
var tags = null;
function initCloud (name)
{
	tags = new Array();
	var tagsNode = E(name);
	var childNodes = tagsNode.childNodes;
	var now = (new Date()).getTime();
	for (var i = 0; i < childNodes.length; i++) {
		var e = childNodes.item (i);
		if (e.nodeName.match (/li/i)) {
			var s = e.title.split (':');
			e.style.fontSize = calcFontSize (s[1]) + 'px';
			var d = s[2].split ('-');
			var diff = (now - (new Date(d[0], d[1] - 1, d[2])).getTime()) / 86400000;
			var style = calcRefreshTime (diff);
			if (style)
				e.className = style;
			tags.push ([ e, calcNormalizeCount (s[1]) ]);
		}
	}
}

// Filter tags by coff value
var coff = 0;					// cut-off value (memo. set his initial value here)
function refreshCoff () {
	if (tags)
		for (var i = 0; i < tags.length; i++) {
			var tag = tags[i];
			tag[0].style.visibility = tag[1] <= coff ? 'hidden' : 'visible';
		}
}

////////////////////////////////////////////////////////////////////////
// Slider control like Gooooooogle
function getSliderPos (tick) {
	return tick * 8 + 21;
}

function alignSlider (pos_y) {
	return parseInt ((pos_y - 21) / 8 + 0.5);
}

function limitSlider (pos_y) {
	var limit_min = getSliderPos (0);
	var limit_max = getSliderPos (division);
	return pos_y < limit_min ? limit_min : (limit_max < pos_y ? limit_max : pos_y);
}

function moveSlider (tick) {
	coff = tick < 0 ? 0 : (division < tick ? division : tick);
	E('ctrl-minus').style.marginTop = (division * 8 + 36) + 'px';
	E('ctrl-slider').style.marginTop = getSliderPos (coff) + 'px';
	refreshCoff ();
}

function initSlider () {
	// Show slider controller
	var parts = '/image/ctrl/';	// directory path of slider images
	document.write ('<div id="controller">');
	document.write ('<div id="shadow"><img src="'+parts+'dslidertopshadow.png"><br>');
	for (var i = 1; i < division; i++)
		document.write ('<img src="'+parts+'dsliderbarshadow.png"><br>');
	document.write ('<img src="'+parts+'dsliderbottomshadow.png"></div>');
	document.write ('<div id="base"><img src="'+parts+'dslidertop.png"><br>');
	for (var i = 1; i < division; i++)
		document.write ('<img src="'+parts+'dsliderbar.png"><br>');
	document.write ('<img src="'+parts+'dsliderbottom.png"></div>');
	document.write ('<img id="ctrl-plus" src="'+parts+'zoom-plus.png">');
	document.write ('<img id="ctrl-minus" src="'+parts+'zoom-minus.png">');
	document.write ('<img id="ctrl-slider" src="'+parts+'slider.png">');
	document.write ('</div>');
	moveSlider (coff);

	// Event handling of inc/dec buttons
	E("ctrl-plus").onclick = function () { moveSlider (coff - 1); }
	E("ctrl-minus").onclick = function () { moveSlider (coff + 1); }

	// Event handling of clicking the slider base
	E("base").onclick = function (e) { moveSlider (alignSlider (e.layerY - 6)); }

	// Event handling of D&D of slider
	var grabObj = null;
	var slider = E("ctrl-slider");
	slider.onmousedown = function (e) {
	}
	slider.onmousemove = function (e) {
	}
	slider.onmouseup = function (e) {
	}
}

////////////////////////////////////////////////////////////////////////
function selectCloud (index) {
	// see <ul id="...">s below
	var clouds = ['tags_name', 'tags_date', 'tags_count'];
	for (var i = 0; i < clouds.length; i++)
		E(clouds [i]).style.display = 'none';
	initCloud (clouds [index]);
	E(clouds [index]).style.display = 'block';
	refreshCoff ();

}
