var maxRowsToShow = 50;

function ISearchRow(id, row, state, translationStatus, translationProblems, categoryId, autoPublish) {
	this.id = id;
	this.row = row;
	if (state) {
		this.state = state;
	}

	if (translationStatus) {
		this.beanTranslated = true;
		this.translationStatus = translationStatus;
		this.translationProblems = translationProblems;
		this.categoryId = categoryId;
		this.autoPublish = autoPublish;
	}
}

ISearchRow.prototype.stringToSearch = function() {
	var string = '';

	for (j = 0; j < this.row.length; j++) {
		string += this.row[j] + "|";
	}

	return string;
}

ISearchRow.prototype.getHTML = function(search, lang) {
	var tr = document.createElement("tr");
	var td;
	var img;
	
	if (allowMassPublish || allowMassDelete) {
		td = document.createElement("td");
		var input = document.createElement("input");
		input.setAttribute('type', 'checkbox');
		input.setAttribute('name', 'id_' + this.id);
		td.appendChild(input);
		tr.appendChild(td);
	}
	
	var a = document.createElement("a");
	a.innerHTML = highlight(this.row[0], search);
	if (deleteMode) {
		a.setAttribute('href', 'undelete?id=' + this.id);
		
	} else {
		a.setAttribute('href', 'edit?id=' + this.id + (lang == '' ? '' : '&lang=' + lang));
	}
	td = document.createElement("td");
	td.appendChild(a);

	if (this.autoPublish) {
		td.appendChild(document.createTextNode(' '));
		img = document.createElement("img");
		img.setAttribute('src', resourcePath + 'images/clock-icon.png');
		td.appendChild(img);
	}

	tr.appendChild(td);

	for (i = 1; i < this.row.length; i++) {
		td = document.createElement("td");
		td.innerHTML = highlight(this.row[i], search);
		tr.appendChild(td);
	}

	var rowClass = 'core';

	if (this.state) {
		rowClass = this.state;
	}

	if (this.beanTranslated && languages) {
		td = document.createElement("td");
		td.className = 'language';

		if (this.translationProblems != '') {
			img = document.createElement("img");

			if (this.translationStatus == 'translated') {
				img.setAttribute('src', resourcePath + 'images/language-some-missing.png');
			} else {
				img.setAttribute('src', resourcePath + 'images/language-' + this.translationStatus + '.png');
			}

			img.setAttribute('title', this.translationProblems);
			td.appendChild(img);
		}

		tr.appendChild(td);
	}

	if (rankField && search == '' && !menuTree && !deleteMode) {
		var td = document.createElement("td");
		td.appendChild(getRankLink(this.id));
		tr.appendChild(td);
	}

	tr.className = rowClass;

	return tr;






	function getRankLink(id) {
		var anchor = document.createElement("a");
		anchor.setAttribute('href', rankAction + '?id=' + id + (lang == '' ? '' : '&lang=' + lang));

		var img = document.createElement("img");
		img.setAttribute('src', resourcePath + 'images/rank.png');

		anchor.appendChild(img);

		return anchor;
	}

	function highlight(text, search) {
		var index = text.toLowerCase().indexOf(search);
		if (index == -1) return breakLines(text);
		var first = text.substring(0, index);
		var middle = text.substring(index, index + search.length);
		var last = text.substring(index + search.length);

		return breakLines(first) + "<span class=\"highlight\">" + breakLines(middle) + "</span>" + breakLines(last);
	}

	function breakLines(text) {
		return text.replace(/\n/g, "<br/>");
	}
}


ISearchRow.prototype.getColumns = function() {
	return this.row.length;
}














function showOverflow(rows, show) {
	var tr = document.getElementById("results-overflow");
	if (!tr) return;

	if (tr.childNodes[0]) {
		tr.removeChild(tr.childNodes[0]);
	}

	if (show) {
		var td = document.createElement("td");
		td.className = 'overflow';
		td.setAttribute('colspan', 3);

		td.innerHTML = rows + ' rows match - only the first ' + maxRowsToShow + ' matching rows are shown. [<a href="javascript:showAllRows();" class="system">Show All</a>]';
		tr.appendChild(td);
	}
}

function getTableHeader(list, beanTranslated) {
	var thead = document.createElement("thead");
	var tr = document.createElement("tr");
	
	if (allowMassPublish || allowMassDelete) {
		tr.appendChild(document.createElement("td"));
	}

	for (i = 0; i < list.length; i+=2) {
		var td = document.createElement("td");
		var a = document.createElement("a");
		var gear = document.createElement("div");
		gear.className = 'gear';

		a.appendChild(document.createTextNode(list[i]));
		a.appendChild(gear);

		a.setAttribute('href', 'list?orderBy=' + list[i+1]);
		a.className = 'system';

		td.appendChild(a);
		tr.appendChild(td);
	}

	if (beanTranslated && languages) {
		td = document.createElement("td");
		td.appendChild(document.createTextNode("language"));

		var gear = document.createElement("div");
		gear.className = 'gear';

		td.appendChild(gear);
		tr.appendChild(td);
	}

	thead.appendChild(tr);
	return thead;
}

function getExtraRow(state, length, cssStyle) {
	var tr = document.createElement('tr');
	var td = document.createElement('td');
	tr.className = cssStyle;
	td.appendChild(document.createTextNode(state));
	td.colSpan = length;
	tr.appendChild(td);
	return tr;
}

function rebuildList() {
	var theDiv = document.getElementById("results");
	var theInput = document.getElementById("search");

	var newTable = document.createElement("table");
    var newTableHeader = getTableHeader(header, beanTranslated);
    var newTableBody = document.createElement("tbody");
	newTable.className = "isearch-table";

	var search;
	if (theInput) {
		search = theInput.value.toLowerCase();
	} else {
		search = '';
	}

	var categoryId = getSelect(document.getElementById('categoryIdSelect'));

	var resultRow = 0;
	var showingRows = true;

	for (var i = 0; i < list.length; i++) {
		var row = list[i];
		if ( (row.stringToSearch().toLowerCase().indexOf(search) != -1) &&
			 (categoryId == '' || row.categoryId == categoryId) ) {
			resultRow++;

			if (showingRows) {
				newTableBody.appendChild(row.getHTML(search, lang));
			}

			if (resultRow > maxRowsToShow && maxRowsToShow != -1) {
				showingRows = false;
			}
		}
	}

	if (!showingRows) {
		newTableBody.appendChild(getExtraRow(resultRow + ' rows match - only the first ' + maxRowsToShow + ' matching rows are shown.', list[0].getColumns(), 'isearch-warning'));
	}

	showOverflow(resultRow, !showingRows);

	newTable.appendChild(newTableHeader);
	newTable.appendChild(newTableBody);
	theDiv.replaceChild(newTable, theDiv.childNodes[0]);
}

function showAllRows() {
	maxRowsToShow = -1;
	rebuildList();
}

function loadCsvTemplate() {
	var theInput = document.getElementById("search");

	var search;
	if (theInput) {
		search = theInput.value.toLowerCase();
	} else {
		search = '';
	}

	var idList = new Array();

	for (var i = 0; i < list.length; i++) {
	    var row = list[i];

		if (row.stringToSearch().toLowerCase().indexOf(search) != -1) {
			idList.push(row.id);
		}
	}

	var inputs = document.getElementById("csv-input-holder");

	while (inputs.childNodes.length > 0) {
		inputs.removeChild(inputs.childNodes[0]);
	}

	for (i = 0; i < idList.length; i++) {
		var input = document.createElement("input");
		input.setAttribute("name", "id");
		input.setAttribute("value", idList[i]);
		input.setAttribute("type", "hidden");
		inputs.appendChild(input);
	}

	document.getElementById("csv-form").submit();
}

