// --------------------------------------------------
// code copyright Lightmaker 2007
// --------------------------------------------------
// tagcloud.js
// version: 1.3
// created: 14 February 2007
// updated: 10 May 2007
// creator: Julian Wheaton
// lightmaker.com
// --------------------------------------------------

function tagcloud(name)
{
	this.name=name;
	this.max=5;
	this.container=null;
	this.stage=1;
	this.totalstages=1;
	this.maxstage=5;
	this.morestages=true;
	this.word_array=new Array();
	this.stage_array=new Array();
}

tagcloud.prototype.clear=function(value)
{
	this.word_array=new Array();
	this.stage_array=new Array();
}

tagcloud.prototype.clearstage=function()
{
	this.stage_array[this.stage]=new Array();
}

tagcloud.prototype.add=function(value,selected,size)
{
	value=this.replace(value.toLowerCase(),"\"","");
	value=this.replace(value,"<","");
	value=this.replace(value,">","");	
	value=this.replace(value,";","");		
	var value_array=value.split(",");
	// trim...
	for (var i=0; i<value_array.length; i++)
	{
		while (value_array[i].substr(0,1)==" ")
		{
			value_array[i]=value_array[i].substr(1,value_array[i].length-1);
		}
		while (value_array[i].substr(value_array[i].length-1,1)==" ")
		{
			value_array[i]=value_array[i].substr(0,value_array[i].length-1);
		}
	}
	// remove duplicate...
	for (var a=value_array.length-1; a>=0; a--)
	{
		for (var i=0; i<value_array.length; i++)
		{
			if (value_array[i]==value_array[a] && a!=i)
			{
				value_array.splice(a,1);
				break;
			}
		}
	}
	if (selected)
	{
		for (var a=value_array.length-1; a>=0; a--)
		{
			for (var i=0; i<this.word_array.length; i++)
			{
				if (this.word_array[i].value==value_array[a] || value_array[a].length==0)
				{
					this.select(value_array[a],true);
					value_array.splice(a,1);
					break;
				}
			}
		}
	}
	if (value_array.length>0)
	{
		// enable all words in word_array...
		for (var a=0; a<value_array.length; a++)
		{
			var exist=false;
			for (var i=0; i<this.word_array.length; i++)
			{
				if (this.word_array[i].value==value_array[a])
				{
					exist=true;
					break;
				}
			}
			if (!exist)
			{
				var o=new Object();
				o.value=value_array[a];
				o.size=(typeof(size)=="number") ? size : 1;
				o.selected=(typeof(selected)=="boolean") ? selected : false;
				this.word_array.push(o);
			}
		}

		if (!this.stage_array[this.stage])
		{
			this.stage_array[this.stage]=new Array();
		}
		
		// store in stage...
		for (var a=0; a<value_array.length; a++)
		{
			for (var i=0; i<this.word_array.length; i++)
			{
				if (this.word_array[i].value==value_array[a])
				{
					var exist=false;
					for (var x=0; x<this.stage_array[this.stage].length; x++)
					{
						if (this.stage_array[this.stage][x]==i)
						{
							exist=true;
						}
					}
					if (!exist)
					{
						this.stage_array[this.stage].push(i);
					}
				}
			}
		}
		
		
		this.onchange();
	}
}


tagcloud.prototype.select=function(value,state)
{
	for (var i=0; i<this.word_array.length; i++)
	{
		if (this.word_array[i].value==value)
		{
			this.word_array[i].selected=(typeof(state)=="boolean") ? state : !this.word_array[i].selected;
			var o=document.getElementById(this.container+"_"+i);
			if (o)
			{
				o.className=(this.word_array[i].selected) ? o.className+" selected" : o.className.replace(" selected","");
			}
			if (typeof(state)!="boolean")
			{
				this.onchange();
			}
			break;
		}
	}
}

tagcloud.prototype.length=function()
{
	var counter=0;
	for (var i=0; i<this.word_array.length; i++)
	{
		if (this.word_array[i].selected)
		{
			counter++;
		}
	}
	return counter;
}


tagcloud.prototype.get=function()
{
	var str='';
	for (var i=0; i<this.word_array.length; i++)
	{
		if (this.word_array[i].selected)
		{
			str+=((str.length==0) ? "" : ",") + this.word_array[i].value;
		}
	}
	return str;
}


tagcloud.prototype.set=function(value_str)
{
	this.add(value_str,true);
}

tagcloud.prototype.rewrite=function(value_str)
{
	for (var i=0; i<this.word_array.length; i++)
	{
		this.word_array[i].selected = false;
	}
	
	this.add(value_str,true);
	
	
}

tagcloud.prototype.onchange=function()
{
}


tagcloud.prototype.stagenext=function()
{
	this.stage++;
	if (this.stage>this.maxstage)
	{
		this.morestages=false;
	}
	if (!this.morestages)
	{
		this.stage=Math.min(this.totalstages,this.stage);
	}
}


tagcloud.prototype.stageback=function()
{
	this.stage--;
	this.stage=Math.max(1,this.stage);
}


tagcloud.prototype.write=function()
{
	var str='';
	var wordlist_array=this.getwordlist();
	for (var i=0; i<wordlist_array.length; i++)
	{
		str+='&nbsp;';
		str+='<a id="'+this.container+'_'+wordlist_array[i].index+'" class="';
		str+='tag'+wordlist_array[i].size;
		if (wordlist_array[i].selected)
		{
			str+=' selected';
		}
		str+='" href="#" onclick="'+this.name+'.select(\''+this.jsvalue(wordlist_array[i].value)+'\');return false;">';
		str+=wordlist_array[i].value;
		str+='</a>';
		str+='&nbsp; ';
	}

	var o=document.getElementById(this.container);
	if (o)
	{
		o.innerHTML=str;
	}
}

tagcloud.prototype.write_noselect=function()
{
	var str='';
	var wordlist_array=this.getwordlist();
	for (var i=0; i<wordlist_array.length; i++)
	{
		if (wordlist_array[i].selected)
		{
			str+='&nbsp;';
			str+='<a id="'+this.container+'_'+wordlist_array[i].index+'" href="../profilesearch/?kw='+wordlist_array[i].value+'">';
			str+='<span class="';
			str+='tag'+wordlist_array[i].size;
			str+=' selected">';
			str+=wordlist_array[i].value;
			str+='</span>';
			str+='</a>';
			str+='&nbsp; ';
		}
	}

	var o=document.getElementById(this.container);
	if (o)
	{
		o.innerHTML=str;
	}
}

tagcloud.prototype.view=function()
{
	var str='';
	var wordlist_array=this.getwordlist();
	for (var i=0; i<wordlist_array.length; i++)
	{
		if (wordlist_array[i].selected)
		{
			str+='&nbsp;';
			str+='<span class="itag">';
			str+=wordlist_array[i].value;
			str+='</span>';
			str+='&nbsp; ';
		}
	}
	return str;
}


tagcloud.prototype.getwordlist=function()
{
	var ret_array=new Array();
	for (var i=0; i<this.stage_array[this.stage].length; i++)
	{
		ret_array.push(this.word_array[this.stage_array[this.stage][i]]);
		ret_array[ret_array.length-1].index=this.stage_array[this.stage][i];
	}
	for (var i=0; i<this.word_array.length; i++)
	{
		if (this.word_array[i].selected)
		{
			var exist=false;
			for (var x=0; x<this.stage_array[this.stage].length; x++)
			{
				if (this.stage_array[this.stage][x]==i)
				{
					exist=true;
				}
			}
			if (!exist)
			{
				ret_array.push(this.word_array[i]);
				ret_array[ret_array.length-1].index=i;
			}
		}
	}
	ret_array.sort(this.sortcompare);
	return ret_array;
}

		
tagcloud.prototype.sortcompare=function(a,b)
{
	if (a.value<b.value)
	{
		return -1;
	}
	else if (a.value>b.value)
	{
		return 1;
	}
	return 0;
}


tagcloud.prototype.jsvalue=function(text)
{
	var ret=text;
	ret=this.replace(ret,"\\","\\\\");
	ret=this.replace(ret,"\'","\\\'");
	return ret;
}

tagcloud.prototype.replace=function(expression, find, replacewith)
{
	var index = expression.indexOf(find, index);
	while (index>-1)
	{
		if (index == 0)
		{
			expression = replacewith + expression.substr(find.length);
		}
		else
		{
			expression = expression.substring(0, index) + replacewith + expression.substr(index+find.length);
		}
		index = expression.indexOf(find, index+replacewith.length);
	}
	return expression;
}

