	var contentDiv = null;
	var translations = null;
	var lede = null;
	var imagesDiv = null;
	
	var textToSearch = "";

	function reload() {
	   window.location.reload();
	}

	function generateInitialize() {
			contentDiv = document.getElementById('contentDiv');
			translations = document.getElementById('translations');
			imagesDiv = document.getElementById('imagesDiv');
			searchControl = document.getElementById('searchControl');

			refreshParagraph();
			
			var list = ($("#contentDiv").text()).split(" ");
			
			for (var i = 0; i < list.length && i < 3; ++i) {
				textToSearch += list[i] + " ";
			}

			wombatInitialize();
			// searchInitialize();
	}
	
	
	function refreshParagraph() {
		var p = paragraph();

		contentDiv.innerHTML = p;
		translations.innerHTML = "";
		contentDiv.innerHTML += "<br />"; 
		
		startTranslating();
	}

	
	var sentences = new Array(
		intransitiveSentence,
		transitiveSentence,
		exclamation,
		quotedStatement,
		transitiveCompoundSentence,
		question
		);
		
	var nouns = subjects.concat(objects);
		
	function r(a) {
		var r = Math.random();
		var randomI = Math.floor(r * a.length);
		return a[randomI];
		}
	
	
	function question () {
		return r(questionPhrases) + " " + intransitiveSentence() + "?";
    }

    function noun() {
        return r(nouns);
    }
	
	function statementIntroduction() {
		return r(subjects) + " " + saidWord();
	}
	
	function saidWord() {
		return r(saidWords);
	}

	function quotedStatement() {
		return capitalize(statementIntroduction()) + ", \"" + sentence() + "\"";
	}

	function intransitiveVerb() {
	    return r(intransitiveVerbs);
	}
	
	function intransitiveSentence () {
		return capitalize(r(nouns)) + " " + r(intransitiveVerbs) + ".";
		}
	
	function transitiveSentence() {
		return capitalize(r(nouns)) + " " + r(transitiveVerbs) + " " + r(nouns) + ".";
	}
	
	function transitiveCompoundSentence() {
		return capitalize(r(subjects)) + " and " + r(nouns) + " " + r(transitiveVerbs) + " " + r(nouns) + ".";
	}
	
	function anaphoricTransitiveSentence() {
		return capitalize(r(subjectPronouns)) + " " + r(transitiveVerbs) + " " + r(nouns) + ".";
	}
	
	function exclamation() {
		return capitalize(r(exclamations)) + "!";
	}

	function sentence() {
		var f = r(sentences);		
		return capitalize(f());
}

function emphaticNoun() {
    return capitalize(noun()) + ". ";
}
	
function capitalize(word) {
		var rv = (word.charAt(0)).toUpperCase();
		rv += word.substr(1, word.length - 1); 
		return rv;
}

var nonCapitalizedWords = new Array(
   "in",
   "and",
   "with",
   "about",
   "of",
   "by",
   "at",
   "the",
   "or",
   "to",
   "a",
   "an",
   "for"
);

function capitalizeWithExceptions(word) {
    for (var i = 0; i < nonCapitalizedWords.length; ++i) {
        if (nonCapitalizedWords[i] == word) { return word; }
    }

    return capitalize(word);
}

function capitalizeAll(phrase) {
    var rv = "";
    var words = phrase.split(' ');
    for (var i = 0; i < words.length; ++i) {
        if (i == 0) {
            rv += capitalize(words[i]) + " ";
        } else {
            rv += capitalizeWithExceptions(words[i]) + " ";
        }
    }

    return rv;
}

	var paragraphs = new Array(
	    new Array(emphaticNoun, emphaticNoun, emphaticNoun, exclamation),
		new Array(exclamation, transitiveSentence, transitiveSentence, intransitiveSentence),
		new Array(exclamation, transitiveCompoundSentence),
		new Array(intransitiveSentence, anaphoricTransitiveSentence, anaphoricTransitiveSentence),
		new Array(intransitiveSentence, anaphoricTransitiveSentence),
		new Array(exclamation, intransitiveSentence),
		new Array(intransitiveSentence, intransitiveSentence, transitiveSentence),
		new Array(transitiveSentence, anaphoricTransitiveSentence, transitiveSentence, exclamation),
		new Array(transitiveSentence, anaphoricTransitiveSentence, transitiveSentence, exclamation),
		new Array(intransitiveSentence, quotedStatement, transitiveSentence, anaphoricTransitiveSentence)
	);
	
	function iterateOn(a) {
		var constructed = "";
				
		for (var i = 0; i < a.length; i++) {
			var f = a[i];
			
			var toAdd = f();			
			constructed += toAdd + " ";
			updateLede(toAdd); 
			}
			
		return constructed;
	}
	
	var req = null;
	
	function getFile(url)
		{ 
		try { 
			req = new XMLHttpRequest();
		}
		catch(e) {		
			try { 
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) { 
				return null;
			}
		}
		
		if(! req) {
			return null;
		}
		
		req.onreadystatechange = function () {
			if (req.readyState == 4) {
				if (req.status == 200) {
					imagesDiv.innerHTML = req.responseText;
				} else {
					alert("Request error " + req.status);
				}
			}
		}
		
		req.open("GET", url, true);
		req.send(null);
	}

	function updateLede(t) { 
		var lede = document.getElementById('lede');
		if ($("#lede").html() == "" && Math.random() < 0.5 ) {	
			$("#lede").html(t); 
		}
		
	}

	function paragraph() {
		return iterateOn(r(paragraphs));
	}
	
	function addASentence() {
		contentDiv.innerHTML += sentence();
		contentDiv.innerHTML += "<br />"; 
	}
	
	function addAParagraph() {
		contentDiv.innerHTML += paragraph();
		contentDiv.innerHTML += "<br />"; 
		
		startTranslating();
	}
		
	function addAQuestion() {
		contentDiv.innerHTML += question();
		contentDiv.innerHTML += "<br />"; 
	}

