Autocompleter.IndexedSearch = new Class({

	Extends: Autocompleter.Base,

	options: {
		minLength: 2,
		delay: 200
	},

	initialize: function(element, list,index, options) {
		this.parent(element, options);
		this.list = list;
		this.index = index;
	},

	query:function() {
		var terms = this.queryToTerms(this.queryValue);
		items = this.search(terms);

		var tokens = [];
		items.each(function(item) {
			if(item && item.uid) {
				tokens.push(window.list[item.uid].text);
			}
		});
		
		/* if (!tokens) {tokens.push(window.list[item.uid].text);}; */
		
		this.update(tokens);
	},

	queryToTerms:function(query) {
		var query = query.trim().toLowerCase();
		query = query.replace(/(\d+)-([\d-]+)/,'$1');
		query = query.replace(/[^\d\w,-äöüß]/i,' ');
		//console.log(query);		
		return query.split(' ');
	},

	markQueryValue: function(str) {
		if (this.options.markQuery && this.queryValue) {
			var searchTerms = this.queryToTerms(this.queryValue);
			searchTerms.each(function(term) {
				if (term.length > 1) {
					var regExp = new RegExp('(' + term.escapeRegExp() + ')', 'ig');
					str = str.replace(regExp, '<span class="term">$1</span>');
				}
			},this);
		}
		return str;
	},

	search: function (searchQuery) {
		var items = [];
		var pos = 0;
		var weight = 0;
		searchQuery.each(function (searchWord) {
			for (indexWord in this.index) {
				if ((pos = indexWord.indexOf(searchWord)) >= 0) {
					weight = (pos == 0 ? 15 : 10) * (searchWord.length / indexWord.length)
					this.index[indexWord].each(function (uid) {
						if (items[uid]) {
							items[uid].cnt += weight;
						} else {
							items[uid] = {cnt:weight,uid:uid};
						}
					});
				}
			}
		});

		items = items.sort(function(a,b) {return -parseInt(a.cnt)+parseInt(b.cnt);});
		return items;
	},

	search2: function (searchQuery) {
		var items = [];
		var pos = 0;
		var weight = 0;
		searchQuery.each(function (searchWord) {
			if (this.index[searchWord]) {
				weight =  2;
				this.index[searchWord].each(function (uid) {
					if (items[uid]) {
						items[uid].cnt += weight;
					} else {
						items[uid] = {cnt:weight,uid:uid};
					}
				});
			}
		});

		items = items.sort(function(a,b) {return -parseInt(a.cnt)+parseInt(b.cnt);});
		return items;
		
	}
});

function submitSearch() {
	var value = window.autocomp.selectedValue; 
	value = value ? value : $('productsearch').value;
	$('productsearch').value = value;
	
	for (productId in window.list) {
		if (window.list[productId].text == value) {
			var matches = window.location.href.match(/(.+)\/(en|de)\/(.+)/);
			
			if (matches != null) {
			window.location.href = matches[1]+'/'+window.list[productId].link;
			}
			else {
			/* 
			 * For searching from a different host enter location of catalogue base here:
			 */
			window.location.href = 'http://10.0.0.124/wp'+'/'+window.list[productId].link;
			}
			
		}
	}
}

window.addEvent('domready',function() {
	window.autocomp = new  Autocompleter.IndexedSearch(
		'productsearch',
		window.list,
		window.index,
		{
			maxChoices:20,
			overflow:true,
			width:'600px',
			selectMode:false,
			onSelection:submitSearch
		}
	);
	$('searchform').addEvent('submit', function(ev) {
		ev = new Event(ev);
		
		submitSearch();
		ev.stopPropagation();
		ev.preventDefault();
		return false;
	});
});

