var tagClouds = Array();
var tagCloudStatus = Array();
var tagCloudHorizontalAcceleration = Array();
var tagCloudVerticalAcceleration = Array();

var bodyTag;

var horiziontalChanges = 1;
var verticalChanges = 0.5;
var minimumHorizontalAcceleration = 1;
var shooterCheckbox;

floatingClouds = true;

function initializeTagCloud()
	{
	if(window.location && window.location.toString().indexOf("chair") > 0)
		{
		floatingClouds = false;
		}

	//curry, kari, kare, curries, curried, curry
	bodyTag = getTags("body")[0];
	var tagCloudTerms = document.getElementById("tagCloudTerms");
	shooterCheckbox = document.getElementById("shooter");
	if(tagCloudTerms)
		{
		listItems = tagCloudTerms.getElementsByTagName("li");	
		}
	setInterval("animateClouds();", 50);
	}

function cloudMouseOver(event)
	{
	sender = getSenderByEvent(event);
	cloudMouseOverBySender(sender)
	}

function cloudMouseOverBySender(sender)
	{
	for(var tagCloudIndex=0; tagCloudIndex < tagClouds.length; tagCloudIndex++)
		{
		if(tagClouds[tagCloudIndex] == sender)
			{
			tagCloudStatus[tagCloudIndex] = "pause";
			}
		}
	}

function cloudClick(event)
	{
	if (shooterCheckbox.checked)
		{
		sender = getSenderByEvent(event);
		explodeCloud(sender);
		}
	}

function updatePoints(relativeValue)
	{
	if(shooterCheckbox.checked)
		{
		var previousPoints = 0;
		var shooterScore = getId("shooterScore");
		shooterScore.style.display = "block";
		if(shooterScore.innerHTML != "" && parseInt(shooterScore.innerHTML) != "NaN")
			{
			previousPoints = parseInt(shooterScore.innerHTML);
			}
		shooterScore.innerHTML = (previousPoints + relativeValue);
		}
	}

function explodeCloud(cloud)
	{
	var explosionPositionLeft = cloud.style.left;
	var explosionPositionTop = cloud.style.top;
	cloud.style.left = getBrowserWidth() + "px";
	cloud.style.top = (Math.random() * getBrowserHeight()) + "px";
	var newPoints = parseInt((1 / tagClouds.length) * 1000);
	updatePoints(newPoints);
	var explosion = document.createElement("div");
	classAttribute = document.createAttribute("class")
	classAttribute.nodeValue = "cloudExplosion";
	explosion.setAttributeNode(classAttribute);
	idAttribute = document.createAttribute("id")
	idAttribute.nodeValue = "explosion" + (Math.random() * 9999);
	explosion.setAttributeNode(idAttribute);
	explosion.style.position = "absolute";
	explosion.style.left = explosionPositionLeft;
	explosion.style.top = explosionPositionTop;
	explosion.style.background = 'url("explosion.gif?' + (Math.random() * 9999) + '")';
	bodyTag.appendChild(explosion);
	setTimeout("removeExplosion('" + idAttribute.nodeValue + "');", 2500);

	}

function removeExplosion(byId)
	{
	var explosion = document.getElementById(byId);
	bodyTag.removeChild(explosion);
	}

function cloudMouseOut(event)
	{
	sender = getSenderByEvent(event);
	cloudMouseOutBySender(sender);
	}

function cloudLinkOver(event)
	{
	sender = getSenderByEvent(event);
	divTag = ascendToFindNode(sender, "div")
	divTag = ascendToFindNode(divTag, "div")
	cloudMouseOverBySender(divTag);
	}

function cloudLinkOut(event)
	{
	sender = getSenderByEvent(event);
	divTag = ascendToFindNode(sender, "div")
	divTag = ascendToFindNode(divTag, "div")
	cloudMouseOutBySender(divTag);
	}

function cloudLinkClick(event)
	{
	sender = getSenderByEvent(event);
	if (shooterCheckbox.checked)
		{
		var divTag = ascendToFindNode(sender, "div")
		var divTag = ascendToFindNode(divTag, "div")
		explodeCloud(divTag);
		stopEventPropagation(event);
		}
	else if(sender.hasAttribute("title"))
		{
		alert(sender.getAttribute("title"));
		}
	}

function cloudMouseOutBySender(sender)
	{
	for(var tagCloudIndex=0; tagCloudIndex < tagClouds.length; tagCloudIndex++)
		{
		if(tagClouds[tagCloudIndex] == sender)
			{
			tagCloudStatus[tagCloudIndex] = "animate";
			}
		}
	}


function animateClouds()
	{
	for(var tagCloudIndex=0; tagCloudIndex < tagClouds.length; tagCloudIndex++)
		{
		if(tagCloudStatus[tagCloudIndex] == "animate" || tagCloudStatus[tagCloudIndex] == "pause" && shooterCheckbox.checked)
			{
			var tagCloud = tagClouds[tagCloudIndex];
			var horizontalAcceleration = tagCloudHorizontalAcceleration[tagCloudIndex];
			horizontalAcceleration = horizontalAcceleration + (Math.random() * horiziontalChanges - (horiziontalChanges/2));
			if(horizontalAcceleration < minimumHorizontalAcceleration)
				{
				horizontalAcceleration = minimumHorizontalAcceleration;
				}
			tagCloudHorizontalAcceleration[tagCloudIndex] = horizontalAcceleration;
			var newHorizontalPosition = parseFloat(tagCloud.style.left.replace("px","")) - horizontalAcceleration;

			var verticalAcceleration = tagCloudVerticalAcceleration[tagCloudIndex];
			verticalAcceleration = verticalAcceleration + (Math.random() * verticalChanges - (verticalChanges/2));
			tagCloudVerticalAcceleration[tagCloudIndex] = verticalAcceleration;
			var newVerticalPosition = parseFloat(tagCloud.style.top.replace("px","")) - verticalAcceleration;
	
			if(newHorizontalPosition < -194)
				{
				if(tagCloud.style.display != "none")
					{
					updatePoints(-1000);
					}
				newHorizontalPosition = getBrowserWidth();
				newVerticalPosition = (Math.random() * getBrowserHeight());
				tagCloudHorizontalAcceleration[tagCloudIndex] = 0;
				tagCloudVerticalAcceleration[tagCloudIndex] = 0;
				}
	
			tagCloud.style.left = newHorizontalPosition + "px"
			tagCloud.style.top = newVerticalPosition + "px"
			}	
		}
	}

//var tagCloudTermTemplate = '<a href="#tagcloud-{{term}}" style="font-size:{{font-size}}" onclick="alert(\'{{popup-message}}\')" onmouseover="">{{term}}</a> ';
//tagCloudTemplate = '<div class="tagCloud" id="{{tag-cloud-id}}"><div class="inner">{{terms}}</div></div>';


var listItems = Array();
var fontSizes = Array("x-large", "large", "medium", "small", "x-small");

function addTagCloud(terms)
	{
	var divTag = document.createElement("div");
	classAttribute = document.createAttribute("class")
	classAttribute.nodeValue = "tagCloud";
	divTag.setAttributeNode(classAttribute);
	addEvent(divTag, "click", cloudClick);
	var innerTag = document.createElement("div");
	anotherClassAttribute = document.createAttribute("class")
	anotherClassAttribute.nodeValue = "inner";
	innerTag.setAttributeNode(anotherClassAttribute);
	
	if(floatingClouds == false)
		{
		divTag.style.backgroundImage = 'url("balmer-chair.gif?' + (Math.random() * 9999) + '")';
		}

	innerTag.innerHTML = "<p>Tag Cloud</p>";
	for(var i = 0; i < 4; i++)
		{
		var chosenFontSize = fontSizes[parseInt(Math.random() * fontSizes.length)];
		var chosenListItem = listItems[parseInt(Math.random() * listItems.length)];
		var term = chosenListItem.innerHTML;
		var popupMessage = "";
		if(chosenListItem.hasAttribute("title"))
			{
			popupMessage = chosenListItem.getAttribute("title");
			}
		if(popupMessage == "")
			{
			popupMessage = "You are it!";
			}
		var anchorTag = document.createElement("a");
		hrefAttribute = document.createAttribute("href")
		hrefAttribute.nodeValue = "#tagcloud-" + term;
		anchorTag.setAttributeNode(hrefAttribute);
		titleAttribute = document.createAttribute("title")
		titleAttribute.nodeValue = popupMessage;

		anchorTag.setAttributeNode(titleAttribute);
		addEvent(anchorTag, "mouseover", cloudLinkOver);
		addEvent(anchorTag, "mouseout", cloudLinkOut);
		addEvent(anchorTag, "click", cloudLinkClick);

		styleAttribute = document.createAttribute("style")
		styleAttribute.nodeValue = "font-size:" + chosenFontSize;
		anchorTag.setAttributeNode(styleAttribute);
		anchorTag.appendChild(document.createTextNode(term));
		innerTag.appendChild(anchorTag);
		innerTag.appendChild(document.createTextNode(' '));
		}
	divTag.appendChild(innerTag)

	tagCloudStatus.push("animate");
	bodyTag.appendChild(divTag)
	divTag.style.left = getBrowserWidth() + "px";
	divTag.style.top = (Math.random() * getBrowserHeight()) + "px";
	tagClouds.push(divTag);
	tagCloudHorizontalAcceleration.push(0);
	tagCloudVerticalAcceleration.push(0);
	return divTag;
	}


function tagCloudOff()
	{
	clearTagCloudButtons();
	var tagCloudOn = getId("tagCloudButtonsOn");
	tagCloudOn.className += " tagCloudButtonsDisabled";
	var tagCloudOff = getId("tagCloudButtonsOff");
	tagCloudOff.className += " tagCloudButtonsEnabled";
	for(var tagCloudIndex=0; tagCloudIndex < tagClouds.length; tagCloudIndex++)
		{
		var tagCloud = tagClouds[tagCloudIndex];
		tagCloud.style.display = "none";
		}
	}

function clearTagCloudButtons()
	{
	var tagCloudOn = getId("tagCloudButtonsOn");
	tagCloudOn.className = tagCloudOn.className.replace("tagCloudButtonsDisabled", "");
	tagCloudOn.className = tagCloudOn.className.replace("tagCloudButtonsEnabled", "");
	var tagCloudOff = getId("tagCloudButtonsOff");
	tagCloudOff.className = tagCloudOff.className.replace("tagCloudButtonsDisabled", "");
	tagCloudOff.className = tagCloudOff.className.replace("tagCloudButtonsEnabled", "");
	}

function tagCloudOn()
	{
	clearTagCloudButtons();
	var tagCloudOn = getId("tagCloudButtonsOn");
	tagCloudOn.className += " tagCloudButtonsEnabled";
	var tagCloudOff = getId("tagCloudButtonsOff");
	tagCloudOff.className += " tagCloudButtonsDisabled";
	for(var tagCloudIndex=0; tagCloudIndex < tagClouds.length; tagCloudIndex++)
		{
		var tagCloud = tagClouds[tagCloudIndex];
		tagCloud.style.display = "block";
		}

	}

function addMoreClouds()
	{
	var numberOfTagClouds = getId("numberOfTagClouds")
	return 	addTagCloud([
		["kari", "large", "Mythbusters!"],
		["curry", "small", ""],
		["where", "medium", "88 Manners Mall, Wellington, New Zealand "],
		["kari", "x-small", "What is hari-kari?"],
		["curie", "x-large", "Marie Curie!"],
		["curried", "x-large", ""]
		]);
	}

function removeClouds()
	{
	oldCloud = tagClouds.pop();
	oldCloud.style.display = "none";
	updateCloudCounter();
	}

function addMoreClouds()
	{
	var cloud = addTagCloud([
		["kari", "large", "Mythbusters!"],
		["curry", "small", ""],
		["where", "medium", "88 Manners Mall, Wellington, New Zealand "],
		["kari", "x-small", "What is hari-kari?"],
		["curie", "x-large", "Marie Curie!"],
		["curried", "x-large", ""]
		]);
	addEvent(cloud, "mouseover", cloudMouseOver);
	addEvent(cloud, "mouseout", cloudMouseOut);
	updateCloudCounter();
	}

function updateCloudCounter()
	{
	var numberOfTagClouds = getId("numberOfTagClouds")
	numberOfTagClouds.innerHTML = tagClouds.length;
	}

function startWithAFewClouds(event)
	{
	if(!window.numberOfClouds)
		{
		window.numberOfClouds = 5;
		}
	while(numberOfClouds > 0)
		{
		addMoreClouds();
		numberOfClouds--;
		}
	getId("shooter").checked = true;
	}

