<html>
	<head>
		<title>Tidx Test</title>
		<style type="text/css">
			td { vertical-align: top; }
			th { text-align: left; }
		</style>
	</head>
	<body>
		<table>
			<tr>
				<td><label for="or_search">Logical OR Search:</label></td>
				<td><input type="text" id="or_search" name="search" size="50" /></td>
				<td><input type="button" id="btn_or_search" value="Search"/></td>
			</tr>
			<tr>
				<td><label for="and_search">Logical AND Search:</label></td>
				<td><input type="text" id="and_search" name="search" size="50" /></td>
				<td><input type="button" id="btn_and_search" value="Search" /></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td colspan="2">
					<textarea name="output" id="output" rows="10" cols="45"></textarea>
				</td>
			</tr>
		</table>

		<br/>

		<table>
			<thead>
				<tr>
					<th>ID</th>
					<th>Name</th>
					<th>Email</th>
					<th>City</th>
					<th>State</th>
					<th>Country</th>
					<th>Rank</th>
					<th>Description</th>
				</tr>
			</thead>
			<tbody id="result_table"></tbody>
		</table>
		<script type="text/javascript" src="tidx.js"></script>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
		<script type="text/javascript">
			var t = new Tidx();
			var json = null;

			$(function(){
				var output = $('#output');

				// Load test JSON
				output.val('Loading test.json .. ');
				$.getJSON('test.json', function(data){
					json = data;
					output.val(output.val() + "loaded\nIndexing test.json .. ");

					var s = new Date().getTime();
					for(var i = 0; i < json.length; i++){
						for(var f in json[i]){
							switch(f){
								case 'id': break;
								case 'email': t.index(false, i, f, json[i][f]); break;
								default: t.index(true, i, f, json[i][f]); break;
							}
						}
					}

					output.val(output.val() + 'indexed in ' + (new Date().getTime() - s) + "ms\n");
				});

				// Setup the search buttons
				$('#btn_or_search').click(function(){
					dosearch(false, $('#or_search').val());
				});

				$('#btn_and_search').click(function(){
					dosearch(true, $('#and_search').val());
				});

				// Enter should work to search, too
				$('#or_search').keydown(function(e){
					if(e.which == 13){ $('#btn_or_search').click(); }
				});

				$('#and_search').keydown(function(e){
					if(e.which == 13){ $('#btn_and_search').click(); }
				}).focus();
			});


			function dosearch(and_search, value){
				var output = $('#output');
				output.val(output.val() + 'Searching .. ');

				var s = new Date().getTime();
				var results = t.search(and_search, value);
				output.val(output.val() + 'completed in ' + (new Date().getTime() - s) + "ms\n");

				var rtbl = $('#result_table').empty();
				for(var idx in results){
					var i = results[idx];
						
					rtbl.append(
						'<tr>' + 
							'<td>' + json[i]['id'] + '</td>' +
							'<td>' + json[i]['name'] + '</td>' +
							'<td>' + json[i]['email'] + '</td>' +
							'<td>' + json[i]['city'] + '</td>' +
							'<td>' + json[i]['state'] + '</td>' +
							'<td>' + json[i]['country'] + '</td>' +
							'<td>' + json[i]['rank'] + '</td>' +
							'<td>' + json[i]['desc'] + '</td>' +
						'</tr>'
					);
				}
			}
		</script>
	</body>
</html>