var tws_searchBoxes = [];
function tws_searchInit(box) {
	if (this.src) {
		var lsrc = this.src.split('-').reverse();
		this.box = box;
		this.onkeyup = tws_searchKeyUp;
		this.onfocus = tws_searchFocus;
		this.onblur = tws_searchBlur;
		this.render = tws_searchRender;
		this.hide = tws_searchHide;
		this.startTimer = tws_searchStartTimer;
		this.reset = tws_searchReset;
		this.spin = tws_searchSpin;
		this.commit = tws_searchCommit;
		this.receive = tws_searchReceive;
		this.prepare = tws_searchPrepare;
		this.parse = tws_searchParse;
		this.jump = tws_searchJump;
		this.searchParams = tws_searchSearchParams;
		this.addSearchParams = tws_searchAddSearchParams;
		this.delay = 300;
		this.minimum = 0;
		this.autocomplete = 'off';
		this.timer = null;
		this.KEY_DOWN = 40;
		this.KEY_UP = 38;
		this.KEY_ESC = 27;
		this.REQUEST_AJAX = 0;
		this.REQUEST_JS = 1;
		this.response = [];
		this.templates = {'item':this.prepare(document.getElementById(this.form.attributes.id.value+'_item'),2),'category':this.prepare(document.getElementById(this.form.attributes.id.value+'_category'),2),'pre':this.prepare(document.getElementById(this.form.attributes.id.value+'_pre'),1),'post':this.prepare(document.getElementById(this.form.attributes.id.value+'_post'),1)};
		this.searchresults = document.getElementById(this.form.attributes.id.value+'_results');
		this.selected = false;
		this.html = {'results': false, 'input': false};
		this.params = {'cmd':'EXT:tw_shop/pi/class.tx_twshop_search.php:tx_twshop_search::search','id':lsrc[1],'conf':lsrc[0],'search':'','lang':this.lang};
		this.ajax = AJAX.client('tws_searchBoxes['+this.box+'].receive(ajax)', AJAX.PERSISTENT, AJAX.server);
		this.marker = {'t': /###SEARCHRESULT_TITLE###/g, 'd': /###SEARCHRESULT_DESCRIPTION###/g, 'i': /###SEARCHRESULT_IMAGE###/g, 'h': /###SEARCHRESULT_URL###/g, 'x': /###SEARCHRESULT_INDEX###/g, 'b': /###SEARCHRESULT_BOX###/g};
		this.prepostmarker = {'n': /###SEARCHRESULT_TITLE###/g, 'n': /###SEARCHRESULT_DESCRIPTION###/g, 'n': /###SEARCHRESULT_IMAGE###/g, 'n': /###SEARCHRESULT_URL###/g, 'x': /###SEARCHRESULT_INDEX###/g, 'b': /###SEARCHRESULT_BOX###/g};
		this.strategy = this.REQUEST_JS;
		this.head = document.getElementsByTagName('head')[0];
		this.startsearch = new Function((typeof(this.onstartsearch)!='undefined') ? this.onstartsearch : '');
		this.stopsearch = new Function((typeof(this.onstopsearch)!='undefined') ? this.onstopsearch : '');
		if (lsrc[2].length) {
			eval('this.params.search=String.fromCharCode(' + lsrc[2] + ');');
		}
	}
}
function tws_searchSearchParams() { return '&tx_twshop_search[search]=' + this.params.search; }
function tws_searchAddSearchParams(s) { return s + ((s.indexOf('?') != -1) ? this.searchParams() : ('?' + this.searchParams().substr(1))); }
function tws_searchFocus() { this.value = this.params.search; }
function tws_searchBlur() { if(!this.selected) this.hide(); }
function tws_searchJump(u) { this.selected = false; this.hide(); self.location = u; return false; }
function tws_searchKeyUp(evt) {
	var k = typeof(event) != 'undefined' ? event['keyCode'] : evt.keyCode;
	if (!evt) evt = event;
	if(k == this.KEY_DOWN && this.searchresults) {
		try { evt.preventDefault(); evt.stopPropagation(); } catch(e) {}
		if (this.selected && (this.searchresults.length > this.selected.num + 1)) {
//			this.keyHighlight(document.getElementById('sp-result-'+(SearchShortcut.selected.num+1)));
		}
		if (!this.selected && this.searchresults.length > 0) {
//			SearchShortcut.keyHighlight(document.getElementById('sp-result-0'));
		}
//		SearchShortcut.flashDomRender();
		
	} else if (k == this.KEY_UP && this.searchresults) {
		try { evt.preventDefault(); evt.stopPropagation(); } catch(e) {}
		if (this.selected && this.selected.num > 0){
//			this.keyHighlight(document.getElementById('sp-result-'+(SearchShortcut.selected.num-1)))
		}
//		SearchShortcut.flashDomRender();
	} else if (k == this.KEY_ESC) {
		this.hide();
		this.reset();
	
	} else{
		this.selected = false;
//		.replace(/[^.A-Z0-9\s]/gi, '')
		var s = new String(this.value).replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/\s+/g, ' ');
		if (s.length < 1 && this.html) {
			this.html.results.innerHTML = '';
			this.spin(false);
			this.hide();
		} else if (s.length > this.minimum) {
			this.params.search = s;
			this.startTimer();
		}
	}
}
function tws_searchRender() {
	var res = this.parse({'n':'','x':'\'pre\'','b': this.box}, this.templates.pre), r;
	for(r = 0; r < this.response.r.length; r++) {
		this.response.r[r]['b'] = this.box;
		this.response.r[r]['x'] = r + 1;
		this.response.r[r]['h'] = 'tws_searchBoxes['+this.box+'].jump(\'/'+this.response.r[r]['h']+'\')';
		res += this.parse(this.response.r[r], this.templates[this.response.r[r].c ? 'category' : 'item']);
	}
	res += this.parse({'n':'','x':'\'post\'','b': this.box}, this.templates.post);
	this.searchresults.innerHTML = res;
	this.searchresults.style.display = 'block';
}
function tws_searchParse(r,t) { if (t) { var pt = t, m; for (m in this.marker) pt = pt.replace(this.marker[m], r[m]); return pt; } else return ''; }
function tws_searchHide() {
	this.searchresults.innerHTML = '';
	this.searchresults.style.display = 'none';
}
function tws_searchStartTimer() {
	if (this.timer) window.clearTimeout(this.timer);
	this.timer = window.setTimeout('tws_searchBoxes['+this.box+'].commit(\''+this.params.search+'\')', this.delay);
}
function tws_searchReset() {
	this.value = '';
}
function tws_searchSpin(onOff) {
	
}
function tws_searchCommit(s) { 
	this.startsearch();
	switch(this.strategy) {
		case this.REQUEST_AJAX:
			this.ajax.request(this.params);
			break;
		case this.REQUEST_JS:
			var script = document.createElement('script');
			script.id = 'twsSearchResults';
			script.type = 'text/javascript';
			script.src = AJAX.server + '&inject=' + this.box + AJAX.params(this.params);
			this.head.appendChild(script);
			break;
	}
}
function tws_searchReceive(r) {
		this.stopsearch();
		if (this.strategy == this.REQUEST_AJAX) {
			this.response = r.result;
		} else{
			this.response = r;
		}
		this.render();
}
function tws_searchPrepare(i,h) {
	if (typeof(i) == 'object' && i != null) { 
		switch(i.nodeType) {
			case 1:
				var n = i.tagName.toLowerCase(), html = '<' + n, a, c, ch = '', mr = false, mt = false, at = h ? {'onmouseover':'tws_searchBoxes['+this.box+'].selected=###SEARCHRESULT_INDEX###;', 'onmouseout':'tws_searchBoxes['+this.box+'].selected=false;', 'onclick':(h==2)?'###SEARCHRESULT_URL###':''} : {}, an;
				for (a = 0; a < i.attributes.length; a++) {
					if ((i.attributes[a].name != 'id') && i.attributes[a].nodeValue) at[i.attributes[a].name] = ((typeof(at[i.attributes[a].name]) != 'undefined') ? (at[i.attributes[a].name] + ';') : '') + i.attributes[a].nodeValue;				
				}
				for (a in at) html += ' ' + a + '="' + at[a] + '"';
				for (c in i.childNodes) { 
					if (i.childNodes[c].nodeType != 2) ch += this.prepare(i.childNodes[c],false);
				}
				html += (ch != '') ? ('>' + ch + '</' + n + '>') : '/>';
				return html;
				break;
			case 3: return i.nodeValue; break; 
		}
	}
	return ''; 
}
function tws_initSearch() {
	var f = document.getElementById('tx_twshop_search');
	if (f && (f.tagName.toUpperCase() == 'FORM') && (typeof(f.elements) == 'object') && (typeof(f.elements['tx_twshop_search[search]']) == 'object')) {
		var sb = f.elements['tx_twshop_search[search]'];
		sb.init = tws_searchInit;
		sb.init(tws_searchBoxes.length);
		tws_searchBoxes[tws_searchBoxes.length] = sb;
	}
}