function search(array, keywords)
{
        var matches = find(array, keywords.toLowerCase());
        
        return matches;
}

function trim(string) {

	// remove off front

	while ((string.length > 0) && (string.indexOf(" ") == 0)) {
		string = string.substring(1, string.length);
	}

	// and end

	while ((string.length > 0) && (string.lastIndexOf(" ") == string.length-1)) {
			string = string.substring(0, string.length-1);
	}

	return string;
}

function clean(string) {

	var stringout = new String();
	
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != '\'') {
			stringout += string.charAt(i);
		}
	}
	
	return stringout;
}
	

function parse(keywords) {

	var parsed = new Array();

	// trim on any leading and terminating white space

	keywords = clean(trim(keywords));

	while (keywords.length > 0) {

		if (keywords.indexOf(' ') > 0) {
			parsed[parsed.length] = keywords.substring(0, keywords.indexOf(' '));
			keywords = trim(keywords.substring(keywords.indexOf(' ')+1, keywords.length));
		} else {
			parsed[parsed.length] = trim(keywords);
			keywords = '';
		}

	}

	return handleKeywords(parsed);

}

function handleKeywords(array) {
	
	for (idx in array) {
		if (array[idx] == 'length') {
			array[idx] = '_length';
		}
	}
	
	return array;
	
}

function find(array, keywords) {

	var matches = new Array();

	// try to match exact first
		
	matches[0] = array[keywords];

	// match each individually

	var keywordarray = parse(keywords);

	for (word in keywordarray) {
		matches[matches.length] = array[keywordarray[word]];
	}

	// compile list of all matches

	var allmatches = new Array();

	for (matchlist in matches) {
		for (match in matches[matchlist]) {
		    var alreadypresent = 0;
			for (alreadyfound in allmatches) {
				if (allmatches[alreadyfound] == matches[matchlist][match]) {
					alreadypresent = 1;
				}
			}
			if (alreadypresent == 0) {
				allmatches[allmatches.length] = matches[matchlist][match];
			}
		}
	}

	// count number of times a given match was found

	var matchcount = new Array();

	for (found in allmatches) {
		matchcount[found] = 0;
	}

	for (found in allmatches) {
		for (matchlist in matches) {
			for (match in matches[matchlist]) {
				if (matches[matchlist][match] == allmatches[found]) {
					matchcount[found]++;
				}
			}
		}
	}

	//bubblesort into order

	var changed = 1;

	while (changed) {
		changed = 0;

		for (var i = 0; i < matchcount.length-1; i++) {
			if (matchcount[i] < matchcount[i+1]) {
				var lowercount = matchcount[i];
				var loweridx = allmatches[i];
				matchcount[i] = matchcount[i+1];
				allmatches[i] = allmatches[i+1];
				matchcount[i+1] = lowercount;
				allmatches[i+1] = loweridx;
				changed = 1;
			}
		}

	}

	return allmatches;

}


function findContentEntryByValue(value, array) {

	var foundentries = array[value];
	if (foundentries.length > 0) {
		return foundentries[0];
	}
}

function findContentEntriesBySection(section, array) {

	return sortByDate(array[section]);
}

function indexArray(array) {

	var sorted = new Array();
	
	var n = 0;
	
	for (idx in array) {
		sorted[n++] = array[idx];
	}
	
	return sorted

}

function sortByDate(array) {
	var changed = 1;
	
	var sorted = indexArray(array);
	

	while (changed) {
		changed = 0;

		for (var i = 0; i < sorted.length-1; i++) {
			if (sorted[i].date < sorted[i+1].date) {
				var loweridx = sorted[i];
				sorted[i] = sorted[i+1];
				sorted[i+1] = loweridx;
				changed = 1;
			}
		}

	}
	
	return sorted;
}

function sortByTitle(array) {

	var changed = 1;
	
	var sorted = indexArray(array);

	while (changed) {
		changed = 0;

		for (var i = 0; i < sorted.length-1; i++) {
				if (sorted[i].title > sorted[i+1].title) {
				var loweridx = sorted[i];
				sorted[i] = sorted[i+1];
				sorted[i+1] = loweridx;
				changed = 1;
			}
		}

	}
	
	return sorted;
}

function sortByAuthor(array) {

	var changed = 1;
	
	var sorted = indexArray(array);

	while (changed) {
		changed = 0;

		for (var i = 0; i < sorted.length-1; i++) {
			if (sorted[i].author > sorted[i+1].author) {
				var loweridx = sorted[i];
				sorted[i] = sorted[i+1];
				sorted[i+1] = loweridx;
				changed = 1;
			}
		}

	}
	
	return sorted;
}

function sortBy(array, sortby) {
	if (sortby == "date") {
		return sortByDate(array);
	} else if (sortby == "author") {
		return sortByAuthor(array);
	} else if (sortby == "title") {
		return sortByTitle(array);
	}
	
	return array;
}



