function Search(dirty) {
	this.when = (new Date).getTime();
	this.dirty = dirty;

	if ('' == dirty) {
		this.clean = '';
	} else {
		this.clean = Search.scrub(dirty);
	}
}

Search.TLDS = ['com', 'net', 'org'];
Search.QUERY_PATH = '/services/quick/';
Search.cache = {};
Search.saved = {};
Search.last = new Search('');

Search.filters = [];
jQuery.each(Search.TLDS,
	function(tld) {
	    Search.filters.push(new RegExp('\\.' + tld, ''));
	});
Search.filters.push(new RegExp('^.*:\\/\\/', ''));
Search.filters.push(new RegExp('^.*@', ''));
Search.filters.push(new RegExp('www\\.', ''));
Search.filters.push(new RegExp('^\\-+', ''));
Search.filters.push(new RegExp('\\-+$', ''));
Search.filters.push(new RegExp('[^a-zA-Z0-9\\-]', 'g'));
Search.scrub = function(dirty) {
	jQuery.each(
		Search.filters,
		function() {
		  dirty = dirty.replace(this, '');
		});
	return dirty.substring(0,63);
};

Search.prototype = {};

Search.prototype.isEmpty = function() {
	return '' == this.clean;
};

Search.prototype.isSameAsLast = function() {
	return Search.last.clean == this.clean;
};

Search.prototype.isCached = function() {
	return 'object' == typeof(Search.cache[this.clean]);
};

Search.prototype.isSaved = function() {
	if (0 !== arguments.length) {
		Search.saved[this.clean] = true;
	}
	return Search.saved[this.clean] === true;
};

Search.prototype.execute = function(callback) {
	var name = this.clean;
	if (this.isCached()) {
		setTimeout(function() {
		  callback(Search.cache[name].results, false);
		}, 100);
	} else {
		var cache = {
			'stamp': (new Date).getTime(),
			'results': null
		};
		Search.cache[name] = cache;
		jQuery.getJSON(
			Search.QUERY_PATH,
			{name: name},
			function(json) {
				cache.results = json;
				callback(json, true);
			});
	}
};

Search.prototype.fakeit = function(callback) {
	var fake = {'name': this.clean};
	jQuery.each(Search.TLDS, function() {
	  fake[this] = 'u';
	});
	callback(fake, false);
};
