function AreaCode(id,ac,desc,state,country)
{
	this.id = id;
	this.ac = ac + "";
	this.desc = desc;
	this.state = state;
	this.country = country;
}

AreaCode.prototype = {
	match: function(x) {
		x = x.toLowerCase();
		if (this.ac.indexOf(x) >= 0) return true;
		if (this.desc.toLowerCase().indexOf(x) >= 0) return true;
		if (this.state.toLowerCase().indexOf(x) >= 0) return true;
		if (this.country.toLowerCase().indexOf(x) >= 0) return true;
		return false;
	},
	render: function() {
		var cs;
		var q = arguments[0] || null;
		if (this.country.length > 0 && this.state.length > 0) {
			cs = this.state + ", " + this.country;
		} else {
			cs = this.country + this.state;
		}
		var desc = $.trim(this.desc);
		cs = $.trim(cs);
		if (cs == "") { cs = tag("em", null, "Unknown"); } else { cs = highlight(cs, q); }
		if (desc == "") { desc = tag("em", null, "No details"); } else { desc = highlight(desc, q); }
		var html = tag('div','class="areacode"',tag('h1',null,highlight(this.ac,q))+tag('h2',null,cs)+tage('p',null,desc));
		$('#results').append(html);
	}
};

var ac_lookup = new Array();
var ac_visible_count = 0;
var ac_visible_q = -99;

function search_reset()
{
	$('#results').html(tag('p',false,'Search for Area Code or City name'));
}
function search_none_found()
{
	$('#results').html(tag('p',false,'No results found. Try and be less specific.') + tag("p",null,"If this area code is missing from our database <a href=\"#\" onclick=\"return ac_edit_new()\">please add it.</a>"));
}
function search_too_many()
{
	$('#results').append(tag('p','class="hot"','Too many results found. Please be more specific.'));
}
function page_home_icon()
{
	page_go('page-home_icon');
	$('title').text('Area Codes');
	return false;
}
function ac_edit_new()
{
	page_go('page-edit');
}

function update_status(n)
{
	var q = $('#q').val().toLowerCase();
	var message;
	if (q == "") {
		message = "Area Code Search";
	} else {
		if (n == 0) {
			message = "No area codes";
		} else if (n > 0) {
			message = n + " " + plural("area code", n);
		} else {
			message = "Too many area codes";
		}
		message += " found &middot; <a href=\"#\" onclick=\"$('#q').val(''); ac_search()\">Clear</a>";
	}
	$('#search_results').show().html(message);
	ac_visible_count = n;
	return false;
}


var search_timer = null;

function ac_search()
{
	if (search_timer) {
		clearTimeout(search_timer);
		search_timer = null;
	}
	search_timer = setTimeout(ac_search_really, 250);
	return false;
}

function ac_search_really()
{
	process_start("search", ac_search_thread, 1, { offset: 0, results: 0, last_match: null, start_q: "" });
}
function ac_search_thread(state)
{
	var n = 0;
	var q = $.trim($('#q').val().toLowerCase());

	if (ac_visible_q == q) {
		process_stop("search");
		return;
	}
	$('#search-progress').show();
	if (q == "") {
		search_reset();
	} else {
		if (state.offset == 0) {
			$('#results').html("");
			state.start_q = q;
		}
		if (state.start_q == q) {
			for (var i = state.offset; i < ac_lookup.length; i++, n++) {
				if (n == 5) {
					state.offset = i;
					update_status(state.results);
					return;
				}
				if (i >= ac_lookup.length) { alert("ac_lookup=" + i + ">=" + ac_lookup.length); }
				if (typeof ac_lookup[i] != 'object') continue;
				if (ac_lookup[i].match(q)) {
					ac_lookup[i].render(q);
					++state.results;
				}
				if (state.results > 15) {
					search_too_many();
					state.results = -1;
					break;
				}
			}
			if (state.results == 0) {
				search_none_found();
			}
		}
	}
	process_stop("search");
	$('#search-progress').hide();
	update_status(state.results);
	if (state.start_q != q) {
		ac_search_really();
	} else {
		ac_visible_q = q;
	}
}

function ac_save()
{

}
