
// add method for cloud object
function addWord(sWord, dTime) 
{
  if (!(sWord.match(/^(\@|http:\/\/|#)/)) && (sWord.length >= this.MinLength))
  {
	    sWord = sWord.replace(/[:.\-\(\)]+/,"");
			sWord = sWord.replace(/^([\'\"])/,"");
			sWord = sWord.replace(/([\'\"])$/,"");
			
			if (this.Stopwords.indexOf(sWord) == -1)
			{
        var i = this.find(sWord);
  	    if (i < 0)
    		{
				  this.cleanup();
  			  var oWord = new cloudElement(sWord,dTime);
  				this.Elements.push(oWord);
    		}
    		else
    		{
          this.Elements[i].Freq++;
  				this.Elements[i].Timestamp = dTime;
    		}
			}
  }
}

// find method for cloud object; finds a word
function findWord(sWord)
{
  for (var i=0;i<this.Elements.length; i++)
	{
	  if (this.Elements[i].Word == sWord)
		{
		  return (i);
		}
	}
	return(-1);
}

// html method for cloud object
function cloudHTML() 
{
	
  var sHTML = "";
	
  var arrSorted = this.Elements.sort( function (a,b) { return b.Freq-a.Freq }).slice(0,this.MaxWords);
  var min = 1;
	var max = arrSorted[0].Freq;
	var gradient = 3;
	var offset = 0;
	
	if (max != min)
	{
  	gradient = 15 / (max - min);
  	offset = (max - 16*min) / (max - min);
	}
	var arrAlpha = arrSorted.sort( function (a,b) { return ((a.Word > b.Word) ? 1:(a.Word < b.Word)? -1 :0)}); 
	for (var i in arrAlpha)
	{
	  num = gradient * arrAlpha[i].Freq + offset; 
	  sHTML += "<span class='cloud" + num.toFixed(0) + "'><a target='_blank' href='http://search.twitter.com/search?ands="+this.SearchTag+"+" + arrAlpha[i].Word +"' title='" + arrAlpha[i].Freq + "'>" + arrAlpha[i].Word + "</a> </span>";
	}
	return sHTML;
}

// cleanup cloud elements so that it doesn't grow too big
function cleanCloud()
{
  if (this.Elements.length > this.MaxElements)
	{
	  var arrSorted = this.Elements.sort(compare);
		var i = this.find(arrSorted[0].Word);
		this.Elements.splice(i,1);
	} 
		
  function compare(a,b)
	{
    if (a.Freq == b.Freq) 
		{
		  if (a.Timestamp == b.Timestamp)
			{
			  return 0;
			}
			else
			{
			  return (a.Timestamp-b.Timestamp);
			}
		}
		else
		{
		  return (a.Freq - b.Freq);
		}	
	}
}

// my cloud object
function cloud (sTag, iMaxWords, iMinLength, fStopWords) {
  // cloud properties
  this.index = 0;
  this.Elements = [];
  this.MaxWords = iMaxWords;
	this.SearchTag = sTag;
	this.MaxElements = iMaxWords * 10;
  this.MinLength = iMinLength;
	this.WordCount = 0;
	
	// cloud methods
  this.add = addWord;
	this.html = cloudHTML;
	this.find = findWord;
	this.cleanup = cleanCloud;
	
	this.Stopwords = [
"->",
"2010",
"3500",
"able",
"about",
"above",
"add",
"after",
"again",
"all",
"already",
"also",
"another",
"any",
"app",
"application",
"available",
"back",
"because",
"been",
"before",
"being",
"below",
"best",
"better",
"between",
"both",
"box",
"buch",
"but",
"called",
"can",
"can't",
"certain",
"change",
"check",
"click",
"come",
"could",
"create",
"created",
"creating",
"current",
"currently",
"did",
"different",
"does",
"doesn",
"doesn't",
"doing",
"don",
"done",
"don't",
"down",
"each",
"easy",
"edit",
"end",
"etc",
"even",
"ever",
"every",
"first",
"fixme",
"following",
"found",
"from",
"full",
"full",
"general",
"generate",
"get",
"getting",
"give",
"going",
"gonna",
"good",
"got",
"great",
"had",
"has",
"have",
"help",
"here",
"highlight",
"home",
"hope",
"hoping",
"instead",
"into",
"ipad",
"iphone",
"its",
"it's",
"just",
"key",
"knew",
"know",
"last",
"let's",
"like",
"line",
"link",
"links",
"list",
"look",
"looking",
"make",
"making",
"many",
"maybe",
"mean",
"meant",
"menu",
"might",
"more",
"most",
"much",
"name",
"need",
"new",
"next",
"not",
"note",
"notes",
"nothing",
"now",
"number",
"off",
"one",
"only",
"open",
"options",
"org",
"other",
"out",
"output",
"over",
"own",
"page",
"pages",
"path",
"place",
"plugin",
"pretty",
"put",
"read",
"ready",
"really",
"right",
"root",
"run",
"running",
"same",
"says",
"search",
"section",
"see",
"set",
"setup",
"should",
"show",
"shows",
"some",
"something",
"specify",
"start",
"still",
"started",
"string",
"stuff",
"such",
"sure",
"take",
"tech",
"tell",
"temp",
"test",
"text",
"than",
"that",
"thats",
"that's",
"their",
"the",
"them",
"then",
"there",
"these",
"they",
"thing",
"think",
"this",
"those",
"through",
"time",
"top",
"try",
"two",
"type",
"under",
"use",
"used",
"uses",
"using",
"usr",
"very",
"view",
"want",
"watch",
"watching",
"way",
"well",
"were",
"we're",
"what",
"whatever",
"whenever",
"which",
"why",
"when",
"where",
"will",
"with",
"without",
"work",
"would",
"yeah",
"yet",
"you",
"your",
"yours"];
	
}



