/*search.js
 * runs through search results and creates drop-down results
 */

var SearchTypes = $H({
	webpage: {
		template: new Template('<div class="#{type} searchtitle"><a href="#{relative}#{location}">#{title}</a></div>'),
		runTemplate: function(item) {
			if (relative) {
				item.relative = relative;
			}
			return this.template.evaluate(item);
		},
		match: function(item, term) {
			var reg = new RegExp(term, 'i');
			if (item.title.match(reg)) {
				return true;
			}
			var myMatch = item.matches.find(function(s) { return (s.match(reg) != null)});
			if (typeof(myMatch) != 'undefined') {
				return true;
			} else {
				return false;
			}
		}
	}, 
	contact: {
		templatePhone: new Template('<div class="#{type} searchtitle">#{firstName} #{lastName}, #{title}<br/>#{phone}</div>'),
		templateEmail: new Template('<div class="#{type} searchtitle">#{firstName} #{lastName}, #{title}<br/><a href="mailto:#{email}" class="email">#{email}</a></div>'),
		runTemplate: function(item) {
			if (item.email) {
				return this.templateEmail.evaluate(item);
			} else {
				return this.templatePhone.evaluate(item);
			}
		},
		match: function(item, term) {
			var reg = new RegExp(term, 'i');
			var fn = false;
			var ln = false;
			var tl = false;
			if (item.firstName) {
				fn = item.firstName.match(reg);
			}
			if (item.lastName) {
				ln = item.lastName.match(reg);
			}
			if (item.title) {
				tl = item.title.match(reg);
			}
			return (fn || ln || tl);
		}
	}
});

var PDSearch = Class.create({
	
	initialize: function(argObject) {
		var args = $H({items: [], handlers:SearchTypes, searchbox: 'search', resultsbox: $('search_results'), "def": 'Search'}).merge(argObject);
		this.args = args;
		this.myItems = args.get('items');
		this.handlers = args.get('handlers');
		this.searchBox = args.get('searchbox');
		this.resultsBox = args.get('resultsbox');
		this.def = args.get('def');
	},
	
	addHandler: function(aType, anObject) {
		this.handlers.set(aType, anObject);
	},
	
	setHandlers: function (handlerHash) {
		this.handlers = handlerHash;
	},
	
	search: function(forTerm) {
		return this.myItems.select(function(item) {
			return this.handlers.get(item.type).match(item, forTerm);
		}.bind(this));
	},
	
	update: function() {
		var results = this.search($F(this.searchBox));
		this.resultsBox.update(results.collect(function(item) {
			return this.handlers.get(item.type).runTemplate(item);
		}.bind(this)).join("\n"));
		$$('#search_results div.searchtitle').each(function(el, n) {
			if (n % 2 == 1) {
				el.addClassName('odd');
			}
		});
		if (results.length > 0) {
			this.resultsBox.show();
		}
	}, 
	
	activate: function() {
		new Form.Element.Observer(
			$(this.searchBox), .2, function(el) {
				var value = $F(this.searchBox);
				if (value.length > 2) {
					this.update();
				} else {
					this.resultsBox.hide();
				}
			}.bindAsEventListener(this)
		);
		$(this.searchBox).observe('focus', function() {
			var value = $F(this.searchBox);
			if (value == this.def) {
				$(this.searchBox).setValue('');
			} else if (value.length > 2) {
				this.update();
			}
		}.bindAsEventListener(this));
		$(this.searchBox).observe('blur', function() {
			setTimeout(function() {
				this.resultsBox.hide();
			}.bind(this), 200);
		}.bindAsEventListener(this));
		this.resultsBox.hide();
	}
	
});