/*
Idea Generator.js
12/20/2005
Developed by David Keay and Joshua Keay
www.MonkeyBusinessLabs.com
*/

var wordLists = new Array(3);
wordLists[0] = new Array();
wordLists[1] = new Array();
wordLists[2] = new Array();
var wordLocations = new Array();
wordLocations[0] = -1;
wordLocations[1] = -1;
wordLocations[2] = -1;
var rowToReplace;
var currentColumn;
var animating = false;

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

var objs = new Object();



var spinCounter = 0;
var spinDelay = 30;

var patternRandomCW = new Array(24);
var patternRandomCCW = new Array(24);
var patternNudgeCW = new Array(6);
var patternNudgeCCW = new Array(6);
patternRandomCW = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,1];
patternRandomCCW = [4,3,2,1,4,3,2,1,4,3,2,1,4,3,2,1,4,3,2,1,4,3,2,4];
patternNudgeCW = [0,1,2,3,1,0];
patternNudgeCCW = [0,3,2,1,3,0];

/*
Returns a random number between 0 and 1
*/
function rnd()
{
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
};

/*
Returns a random number between 0 and number.
*/
function rand(number)
{
	return rnd()*number;
};

/*
this calls getElementById, but is faster for duplicated lookups.
*/
function o(x) {
   if ( !objs[x] )
      objs[x] = document.getElementById(x);
   return objs[x];
}

/*
Prints msg to the screen
*/
function debug(msg) {
	if (!debug.box) {
		debug.box = document.createElement("div");
		debug.box.setAttribute("style", "background-color: white; " +
										"font-family: monospace; " +
										"border: solid black 3px; " +
										"position: absolute;top:200px;" +
										"z-index: 100;" +
										"padding: 10px;");
		document.body.appendChild(debug.box);
	}

	var p = document.createElement("p");
	p.appendChild(document.createTextNode(msg));
	debug.box.appendChild(p);
}

/*
Sets the animation-related variables to their animating or non-animating values.
*/
function startAnimating()
{
	animating = true;
	spinCounter = 0;
}

function stopAnimating()
{
	animating = false;
	spinCounter = 0;
}

/*
Nudge the wheel for the appropriate column.
*/
function nextWordSpin(column)
{
	if (!animating)
	{
		o("text" + (column+1)).innerHTML = " ";
		nudgeWheelLauncher(column, 'CCW');
	}
}

/*
Nudge the wheel for the appropriate column (CCW version);
*/
function previousWordSpin(column)
{
	if (!animating)
	{
		o("text" + (column+1)).innerHTML = " ";
		nudgeWheelLauncher(column, 'CW');
	}
}

/*
Nudges the wheel (0, 1, or 2 - inner, middle, outer), either clockwise or counterclockwise ("CW" or "CCW")
*/
function nudgeWheelLauncher(wheel, direction)
{
	startAnimating();
	window.setTimeout('nudgeWheel' + wheel + direction + '()', 0);
}

/*
Nudges the outer wheel clockwise
*/
function nudgeWheel2CW()
{
	o("od" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	o("n" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("od" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	o("n" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCW.length-1)
	{
		window.setTimeout('nudgeWheel2CW()', spinDelay);
	} else
	{
		stopAnimating();
		o("n0").style.visibility = "hidden";
		previousWord(2);
	}
}

/*
Nudges the middle wheel clockwise
*/
function nudgeWheel1CW()
{
	o("md" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	o("m" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("md" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	o("m" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCW.length-1)
	{
		window.setTimeout('nudgeWheel1CW()', spinDelay);
	} else
	{
		stopAnimating();
		o("m0").style.visibility = "hidden";
		previousWord(1);
	}
}

/*
Nudges the inner wheel clockwise
*/
function nudgeWheel0CW()
{
	o("id" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	o("l" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("id" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	o("l" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCW.length-1)
	{
		window.setTimeout('nudgeWheel0CW()', spinDelay);
	} else
	{
		stopAnimating();
		o("l0").style.visibility = "hidden";
		previousWord(0);
	}
}

/*
Nudges the outer wheel counterclockwise
*/
function nudgeWheel2CCW()
{
	o("od" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	o("n" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("od" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	o("n" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCCW.length-1)
	{
		window.setTimeout('nudgeWheel2CCW()', spinDelay);
	} else
	{
		o("n0").style.visibility = "hidden";
		stopAnimating();
		nextWord(2);
	}
}

/*
Nudges the middle wheel counterclockwise
*/
function nudgeWheel1CCW()
{
	o("md" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	o("m" + patternNudgeCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("md" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	o("m" + patternNudgeCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCCW.length-1)
	{
		window.setTimeout('nudgeWheel1CCW()', spinDelay);
	} else
	{
		stopAnimating();
		o("m0").style.visibility = "hidden";
		nextWord(1);
	}
}

/*
Nudges the inner wheel counterclockwise
*/
function nudgeWheel0CCW()
{
	o("id" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	o("l" + patternNudgeCCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("id" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	o("l" + patternNudgeCCW[spinCounter]).style.visibility = "visible";
	if (spinCounter < patternNudgeCCW.length-1)
	{
		window.setTimeout('nudgeWheel0CCW()', spinDelay);
	} else
	{
		stopAnimating();
		o("l0").style.visibility = "hidden";
		nextWord(0);

	}
}

/*
Rotates the outer and inner wheels CW, and middle CCW.
*/
function spinAllRandom()
{
	o("l" + patternRandomCW[spinCounter]).style.visibility = "hidden";
	o("m" + patternRandomCW[spinCounter]).style.visibility = "hidden";
	o("n" + patternRandomCW[spinCounter]).style.visibility = "hidden";
	o("sd" + patternRandomCW[spinCounter]).style.visibility = "hidden";
	spinCounter = spinCounter + 1;
	o("sd" + patternRandomCW[spinCounter]).style.visibility = "visible";
	o("l" + patternRandomCW[spinCounter]).style.visibility = "visible";
	o("m" + patternRandomCW[spinCounter]).style.visibility = "visible";
	o("n" + patternRandomCW[spinCounter]).style.visibility = "visible";

	if (spinCounter < 23)
	{
		window.setTimeout('spinAllRandom()', spinDelay);
	} else
	{
		o("l" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("m" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("n" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("sd" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("od0").style.visibility = "visible";
		o("md0").style.visibility = "visible";
		o("id0").style.visibility = "visible";
		stopAnimating();
		randomizeAll();
	}
}


/*
Display the help image.
*/
function showHelp()
{
	o("helpWindow").style.display="block";
}

/*
Dismiss the help image.
*/
function hideHelp()
{
	o("helpWindow").style.display="none";
}

/*
If the minimized view checkbox is checked, then enter minimized view.
If not, then enter normal view.
*/
function minimizedViewToggle(){
	if (o("minimizedViewCheckBox").checked == true)
	{
		widget.setPreferenceForKey(1, "minimizedView");
		o("diskLayer").style.display = "none";
		o("backImage").src="Images/hidden_back.png";
	} else
	{
		widget.setPreferenceForKey(0, "minimizedView");
		o("diskLayer").style.display = "block";
		o("backImage").src="Images/default_back.png";
	}
}

/*
Initialize the widget:
1: Load the words from the files
2: Check the minimized view preference.
3: Create the glass buttons
4: Fill the lists on the back with words.
*/
function loaded()
{

	nonRandomWordLists();

//	debug("A");
//	populateList('1');
//	populateList('2');
//	populateList('3');
//	debug("nothing");
	o("front").style.display = "block";
	o("loadingScreen").style.display = "none";

//	debug(o("text1").style.top);
//	o("front").style.top = 200;
}


/*
Brings up the resetWordList dialog
*/
function resetWordList()
{
	o("resetConfirmBack").style.display = "block";
}

/*
Hide the resetWordList dialog
*/
function resetWordListNo()
{
	o("resetConfirmBack").style.display = "none";
}

/*
Restore the lists to the defaults
*/
function resetWordListYes()
{
	widget.system("cat oc1 > column1.txt", null);
	widget.system("cat oc2 > column2.txt", null);
	widget.system("cat oc3 > column3.txt", null);
	nonRandomWordLists();
	populateList(1);
	populateList(2);
	populateList(3);
	resetWordListNo();
}

/*
Event for users submitting the addWord inputs. Prevents the html form from actually submitting.
*/
function inputEnter(listNum)
{
	addWord(listNum);
	return false;
}

/*
Adds a word to the appropriate list, then saves the new list and refreshes the displayed list.
-Will not work if there is no word, or if the word is a duplicate
*/
function addWord(listNum)
{
	if (o("input" + listNum).value != "")
	{
		var word = o("input" + listNum).value;
		var repeat = 0;

		//check to see if the word is in the list.
		var i = 0;
		var s1;
		var s2;

		while (i < wordLists[listNum-1].length)
		{
			s1 = wordLists[listNum-1][i];
			s2 = word;
			if (s1.toUpperCase() == s2.toUpperCase()) {
				repeat = 1;
				break;
			}
			i++;
		}

		if (repeat == 0)
		{
			wordLists[listNum-1].splice(0, 0, word);
			var innerList = o('inner-list' + listNum);
			writeWordList(listNum);
			populateList(listNum);
			o("input" + listNum).value = "";
		}
	}
}

/*
Removes the word at position from the list listNum (1, 2, or 3), then saves the new word list and refreshes the displayed list.
*/
function removeWord(listNum, word)
{
	wordLists[listNum-1].splice(word, 1);
	writeWordList(listNum);
	populateList(listNum);
}

/*
Saves the current displayed list (1, 2, or 3) of words to the data files.
*/
function writeWordList(listNum)
{
	var fullList = 'echo "';
	var i = 0;
	while (i < wordLists[listNum-1].length)
	{
		fullList = fullList + wordLists[listNum-1][i] + "|";
		i++;
	}
	fullList = fullList + '|" > column' + listNum + '.txt';
	widget.system(fullList, null);
}

/*
replaces spaces with non breaking spaces
*/
function nbsp(word)
{
	w = new String(word);
	w = w.replace(/ /g, "&nbsp;");
	return w;
}

/*
Reloads the currently displayed lists (1, 2, or 3) (with data from the wordLists array).
*/
function populateList(listNum)
{
	var innerList = o('inner-list' + listNum);

	while (innerList.hasChildNodes())
		innerList.removeChild(innerList.firstChild);
	var row;
	var span;
	var i = 0;
	while (i < wordLists[listNum-1].length)
	{
		row = document.createElement('div');
		row.setAttribute("class", "row");
		span = document.createElement('span');
		span.setAttribute ("class", "col1");
		span.innerHTML = '<img class="listBackFade" src="images/list-background-right-short.png"/>' + nbsp(wordLists[listNum-1][i]);
		span.setAttribute("ondblClick", "editRow(this, " + listNum + ")");
		span.setAttribute("realWord", wordLists[listNum-1][i]);
		row.appendChild(span);
		span = document.createElement('span');
		span.setAttribute ("class", "col2");
		span.innerHTML = '<img src="images/x-normal.tif" onclick="removeWord(' + listNum + ', ' + i + ')" onmousedown="this.src=\'images/x-down.tif\'" onmouseout="this.src=\'images/x-normal.tif\'" onmouseover="this.src=\'images/x-over.tif\'">';
		row.appendChild(span);
		innerList.appendChild(row);
		i++;
	}
}

/*
Sets the word replacement variables (rowToReplace, currentColumn), and displays a word replacement Input box over row.
*/
function editRow(row, listNum)
{
	rowToReplace = row;
	currentColumn = listNum;

	var inputID = "input" + (currentColumn+3);
	var inputDiv = "input" + (currentColumn+3) + "div";

	o(inputDiv).style.top=rowToReplace.offsetTop-5;
	o(inputDiv).style.left=-1;
	o(inputID).value=row.realWord;
	o(inputDiv).style.display="block";
	o(inputID).focus();
}

/*
If the user hits a key, check to see if it's escape. If so, then hide any word replacement boxes that are open.
*/
function blockEscape()
{
	if (window.event.keyCode==27)
	{
		o("input"+(currentColumn+3)).value=rowToReplace.realWord;
		hideInput();
	}
}

/*
Hides a replacement Input box, if one is visible.
*/
function hideInput()
{
	o("input"+(currentColumn+3)+"div").style.display="none";
}

/*
If what is in the word input box is valid, then finish the word replacement. otherwise, cancel it.
*/
function lostInputFocus()
{
	replaceWord();
	hideInput();
}

/*
Replace the word that is in the list "currentColumn", and row "rowToReplace"
*/
function replaceWord()
{
	var inputID = "input" + (currentColumn+3);
	var inputDiv = "input" + (currentColumn+3) + "div";

	var newWord = o(inputID).value;

	if (newWord == "")
	{
		hideInput();
		return false;
	}

	if (newWord == rowToReplace.realWord)
	{
		hideInput();
		return false;
	}

	var i = 0;

	while (i < wordLists[currentColumn-1].length)
	{
		if (wordLists[currentColumn-1][i].toUpperCase() == newWord.toUpperCase()) {
			return false;
		}
		i++;
	}

	i = 0;

	while (i < wordLists[currentColumn-1].length)
	{
		if (rowToReplace.realWord == wordLists[currentColumn-1][i])
		{
			wordLists[currentColumn-1][i] = o(inputID).value;
			rowToReplace.innerHTML = '<img class="listBackFade" src="images/list-background-right-short.png"/>' + nbsp(o(inputID).value);
			rowToReplace.realWord = o(inputID).value;
			i = wordLists[currentColumn-1].length;
			writeWordList(currentColumn);
		}
		i++;
	}
	hideInput();
	return false;
}

/*
Copies the word lists from the data files to the wordLists array.
*/
function nonRandomWordLists(){

	var file;
	if(navigator.appName == "Microsoft Internet Explorer") {
		file = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		file = new XMLHttpRequest();
	}
	/*

		var file = false;

	try {
		file = new XMLHttpRequest();
		}
		try {
			file = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
	   			file = new ActiveXObject("Microsoft.XMLHTTP");
	  		} catch (E) {
				file = false;
			}
 		}
 	}

 	*/
	var tempWords = new Array();

	file.open("GET",'column1.txt',false);
	file.send(null);
	tempWords = file.responseText.split('||');
	wordLists[0] = tempWords[0].split('|');
	file.close;
	file.open("GET",'column2.txt',false);
	file.send(null);
	tempWords = file.responseText.split('||');
	wordLists[1] = tempWords[0].split('|');
	file.close;
	file.open("GET",'column3.txt',false);
	file.send(null);
	tempWords = file.responseText.split('||');
	wordLists[2] = tempWords[0].split('|');
	file.close;
}

/*
Displays the next word in the list, column is 0, 1, or 2
*/
function nextWord(column)
{
	var current = (wordLocations[column] + 1) % wordLists[column].length;
	wordLocations[column] = current;
	o("text" + (column+1)).innerHTML=(wordLists[column][current]);
	obj = o("text" + (column+1));
//	debug(obj.offsetHeight);
	if (obj.offsetHeight >13 && obj.offsetHeight < 17) obj.style.top=7;
		else obj.style.top = 0;
}

/*
Displays the previous word in the list, column is 0, 1, or 2
*/
function previousWord(column)
{
	var current = (wordLocations[column] - 1 + wordLists[column].length) % wordLists[column].length;
	wordLocations[column] = current;
	o("text" + (column+1)).innerHTML=(wordLists[column][current]);
	obj = o("text" + (column+1));
	if (obj.offsetHeight >13 && obj.offsetHeight < 17) obj.style.top=7;
		else obj.style.top = 0;
}

/*
Sets the 3 displayed word to random words from their lists.
*/
function randomButton()
{
	if (!animating){
		startAnimating();
		o("text1").innerHTML = " ";
		o("text2").innerHTML = " ";
		o("text3").innerHTML = " ";
		o("l" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("m" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("n" + patternRandomCW[spinCounter]).style.visibility = "hidden";
		o("od0").style.visibility = "hidden";
		o("md0").style.visibility = "hidden";
		o("id0").style.visibility = "hidden";
		spinAllRandom();
	}
}

/*
set all three words to a random word.
*/
function randomizeAll()
{
	setRandomWord(1);
	setRandomWord(2);
	setRandomWord(3);
}

/*
Sets the word in list num (1, 2, or 3) to a random word.
*/
function setRandomWord(num)
{
	var word = Math.floor(rand(wordLists[num-1].length));
	o("text" + num).innerHTML=(wordLists[num-1][word]);
	obj = o("text" + num);
//	if (num == 1)  debug(obj.offsetHeight);
//	if (obj.offsetHeight >13 && obj.offsetHeight < 17)

	if (obj.offsetHeight >13 && obj.offsetHeight < 17) obj.style.top=7;
		else obj.style.top = 0;
}

/*
Display the back of the widget.
*/
function showBack()
{
	var front = o("front");
	var back = o("back");

	if (window.widget) widget.prepareForTransition("ToBack");

	front.style.display="none";
	back.style.display="block";

	if (window.widget) setTimeout ('widget.performTransition();', 0);
}

/*
Hide the back of the widget.
*/
function hideBack()
{
	var front = o("front");
	var back = o("back");

	if (window.widget)
		widget.prepareForTransition("ToFront");

	back.style.display="none";
	front.style.display="block";

	if (window.widget)
		setTimeout ('widget.performTransition();', 0);
}