// Global Var
var maximum = 0;
var tagCloud = new Array();
var c = 0;

minWords--;
maxWords++;


// set our shortcut
var JQ = jQuery.noConflict();

function makeTag(count,word,href) {
	this.count = count;
	this.word = word;
	this.href = href;
	// alert(this.count + "\n" + this.word + "\n" + this.href);
}

function sortNumber(a,b) {return a - b;}

function makeTagCloud(max) {
			JQ("#tagcloud").html('');

			
			//JQ("#typeString").prepend('<link href="" media="all" />');
			for ( var tag in tagCloud )// start looping through the tags 
			{	if (max < 0) {max = 0;}
				if (max < minWords) {max = minWords;}
				if (tag <= max)
				{
					// alert( tagCloud[tag].word );
					percent = Math.floor((parseFloat(tagCloud[tag].count) /parseFloat(maximum)) * 100);
					
					if (percent <20) {
						size = 'smallest';//'x-small';
					} else if (percent>= 20 && percent <40) {
						size = 'small';
					} else if (percent>= 40 && percent <60) {
						size = 'medium';
					} else if (percent>= 60 && percent <80) {
						size = 'large';
					} else {
						size = 'largest';//'x-large';
					}

					if (tagCloud[tag].href == undefined)
						text = tagCloud[tag].word;
					else
						text = '<a href="' + tagCloud[tag].href +'">' + tagCloud[tag].word + '</a>';
					
					JQ('<span></span>')
						.html(text)
						.addClass(size)
						//.css('font-size', size)
						.appendTo("#tagcloud");
				}
			}
}

JQ(function() {

	JQ.ajax({
		url: dataPath,
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		error: function(){
			alert('Error loading tags XML');
		},
		success: function(xml){
			JQ(xml).find('tag').each(function() {
				// alert((this).text());
				// here gets the values in the XML, 
				tagCloud[c] = new makeTag(JQ(this).attr('count'),JQ(this).text(),JQ(this).attr('href'))
				c++;
				// here collect then in a array
				if (maximum < JQ(this).attr('count')){maximum = JQ(this).attr('count');}
			});
			if (maxWords > c)
				maxWords = c;
			if (useSlider == true)
			{
				makeTagCloud(minWords);
			} else {
				makeTagCloud(maxWords);
			}
		}
	});

	if (useSlider == true)
	{
		JQ('<div id="slider"></div>')//<div id="slider2"></div>')
			.appendTo("#" + containerType);
	}

	JQ('<div id="tagcloud"></div>')
		.appendTo("#" + containerType);

	if (useSlider == true)
	{
		makeTagCloud(minWords);
	} else {
		makeTagCloud(maxWords);
	}

	JQ("head").append('<link href="' + stylePath + '" rel="stylesheet" />');

	if (useSlider == true)
	{
		JQ("head").append('<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" /><style type="text/css">#slider { margin: 10px; }</style>');
		JQ.getScript("http://jqueryui.com/latest/ui/ui.core.js");
		JQ.getScript("http://jqueryui.com/latest/ui/ui.slider.js", function(){

			JQ("#" + containerType + " #slider").slider({
				value:0,
				min: minWords,
				max: maxWords,
				step: 1,
				slide: function(event, ui) {
					//JQ("#slider2")
					//	.html(JQ('#slider').slider('option', 'value'))
					if (minWords > JQ('#slider').slider('option', 'value'))
						makeTagCloud(minWords);
					else
						makeTagCloud(JQ('#slider').slider('option', 'value'));
				}
			});

			JQ("#" + containerType + " .ui-widget").css( 'font-size', '0.5em' );
		});
	}
});