var definitionRequest;
var displayStart;
var definitions;
var numDefinitionCells;

function definitionSearch(query, nDefinitionCells) {
	var text = document.getElementById('english');
	postEvent('SEARCH_DEFINITION', query);
	
	if (text.value == null || text.value == "") {
	   	alert("You must type a search query into the text field.");
   	}
   	else {
		displayStart = 0;
		numDefinitionCells = nDefinitionCells;
		definitionRequest = Utils.AJAX.createRequest();
		definitionRequest.open('GET', "searchservlet?dquery=" + encodeURIComponent(text.value), true);
		definitionRequest.onreadystatechange = displayDefinitionResults;	
		clearDefinitionTable();		
		var spanE = document.getElementById('definitionCount');
		spanE.innerHTML = "Searching for definition...";
		showDefinitionTool();
		definitionRequest.send(null);
	}
}

function showDefinitionTool() {
	closeTools();
	View.setDisplay('definitionTool', true);
}

function displayDefinitionResults() {
	if (definitionRequest.readyState == 4) {
		if (definitionRequest.status == 200) {
			var xmldoc = definitionRequest.responseXML;
			var results = xmldoc.getElementsByTagName('results')[0];
			definitions = results.getElementsByTagName('definition');
			updateDefinitions();
		}
		else {
			alert("Bad ready status (perhaps an exception fired in the servlet)");
		}
	}
}

function nextDefinitions() {
	if (definitions != null) {
		var newDisplay = displayStart + numDefinitionCells;
	
		if (newDisplay < definitions.length) {
			displayStart = newDisplay;
			updateDefinitions();
		}
	}	
}

function prevDefinitions() {
	if (definitions != null) {
		var newDisplay = displayStart - numDefinitionCells;
	
		if (newDisplay >= 0) {
			displayStart = newDisplay;
			updateDefinitions();
		}
	}
}

function updateDefinitions() {
	clearDefinitionTable();
	placeDefinitionCount(definitions.length);

	for (var i = 0; i < numDefinitionCells; i++) {
		var definitionIndex = i + displayStart;				
		if (definitionIndex < definitions.length) {
			placeDefinition(definitions[definitionIndex], i);
		}
	}
}

function clearDefinitionTable() {
	for (var i = 0; i < numDefinitionCells; i++) {
		var row = document.getElementById('drow' + i);
		Utils.DOM.clearChildren(row);
	}
}

function placeDefinitionCount(count) {
	var par = document.getElementById('definitionCount');
	Utils.DOM.clearChildren(par);
	var node = document.createTextNode(" Found " + count + " definitions.");
	par.appendChild(node);
}

function addDefinition(cell, textareaName) {
	var textarea = document.getElementById(textareaName);
	CurrentCard.showNewCardCover(false);
	var definition = cell.innerHTML;
	
	if (textarea.value != '') {
		definition = '; ' + definition;	
	}
	
	textarea.value += definition;
	CurrentCard.markEditted(true);
}

function changeColor(cell, color) {
	cell.style.backgroundColor = color;
}

function placeDefinition(definition, rowIndex) {
	var row = document.getElementById('drow' + rowIndex);

	var ccell = row.insertCell(0);
	ccell.setAttribute('width', '7%');
	ccell.innerHTML = definition.getAttribute('simplified');
	ccell.onclick = function() { addDefinition(this, "chineseCard"); };
	ccell.onmouseover = function() { changeColor(this, "#cccccc"); };
	ccell.onmouseout = function () { changeColor(this, "#ffffff"); };

	var pcell =  row.insertCell(1);
	pcell.setAttribute('width', '18%');
	pcell.innerHTML = definition.getAttribute('pinyin');
	pcell.onclick = function() { eval('addDefinition(this, "pinyinCard")'); };
	pcell.onmouseover = function() { changeColor(this, "#cccccc"); };
	pcell.onmouseout = function() { changeColor(this, "#ffffff"); };

	var ecell = row.insertCell(2);
	ecell.innerHTML = definition.getAttribute('english');
	ecell.onclick = function() { eval('addDefinition(this, "englishCard")'); };
	ecell.onmouseover = function() { changeColor(this, "#cccccc"); };
	ecell.onmouseout = function() { changeColor(this, "#ffffff"); };
	
	var tcell = row.insertCell(3);
	tcell.innerHTML = definition.getAttribute('traditional');
	tcell.onclick = function() { eval('addDefinition(this, "chineseCard")'); };
	tcell.onmouseover = function() { changeColor(this, "#cccccc"); };
	tcell.onmouseout = function() { changeColor(this, "#ffffff"); };
}
