
/* live search object */

var ls = {
	submitBtn : null,
	input : null,
	timeOut : null,
	imgDestroy : null,
	div : null,
	h2 : null,
	ul : null,
	thinkingImg : null,
	
	initLiveSearch : function () {
		if (!zXmlHttp.isSupported() || !document.getElementById || !document.createElement) {
			return;
		}
		
		ls.submitBtn = $('searchBtn');
		ls.submitBtn.style.visibility = 'hidden';
		
		ls.input = ($('keywords')) ? $('keywords') : $('artistKeywordField');
		addEvent (ls.input,'keyup',ls.makeQuery,false);
		addEvent (ls.input,'change',ls.makeQuery,false);
	},
	
	makeQuery : function (e) {
		if (!e) {
			e = window.event;
		}

		if (e.srcElement) {
			var target = e.srcElement;
		} else {
			var target = e.target;
		}
		
		if (ls.timeOut != null) {
			clearTimeout(ls.timeOut);
			ls.timeOut = null;
		}
		
		if (ls.imgDestroy != null) {
			clearTimeout(ls.imgDestroy);
			ls.imgDestroy = null;
		}
		
		if (!xmlHttp) {
			var xmlHttp = zXmlHttp.createRequest();
		} else if (xmlHttp.readyState != 0) {
			xmlHttp.abort();
		}
		
		if (!isEmpty(target.value)) {

			ls.createThinkingImg();			
			
			var val = target.value.trim();
			
			xmlHttp.open ("get", "http://www.eastsidebookings.nl/inc/php/search.ajax.php?keywords=" + encodeURIComponent(val), true);
			xmlHttp.onreadystatechange = function () {
				if (xmlHttp.readyState == 4) {
					if (xmlHttp.status == 200) {
						ls.createResultsDiv (xmlHttp.responseText);
					} else {
						ls.submitBtn.style.visibility = '';
					}
				}
			}
			
			ls.timeOut = setTimeout (function () {
									xmlHttp.send (null);
									}, 500);
			
			ls.imgDestroy = setTimeout (function() {
										ls.destroyImage();
												 }, 1000);
			
		} else {
			ls.destroyImage();
			
			var hiddenContent = ($('artistPreviews')) ? $ ('artistPreviews') : $ ('artistContent');
			
			if (hiddenContent.style.display == 'none') {
				hiddenContent.style.display = '';
				if (ls.div != null) {
					ls.div.parentNode.removeChild(ls.div);
					ls.div = null;
					ls.ul = null;
					ls.h2 = null;
				}
			}
		}
		
	},
	makePermalink : function (str)
	{
		return str.replace(/([^A-Za-z0-9_-]+)/,'-');
	},
		
	createResultsDiv : function (txt) {
		/* create search results elements */
		if (!ls.div) {
			ls.div = document.createElement ('div');
			ls.div.id = 'searchResults';
		}
		
		if (!ls.h2) {
			ls.h2 = document.createElement('h2');
			ls.h2.appendChild (document.createTextNode('Zoekresultaten:'));
		}		
		
		ls.removeUl();
		
		if (!ls.ul) {
			ls.ul = document.createElement('ul');
		}
		
		/* remove old search results */
		
		/* get array */
		if (txt != 'Er zijn geen artiesten gevonden.' && txt != 'Error: livesearch failed.') {
			var results = ls.getResultsArray(txt);
			
			for (var i = 0; i < results.length; i++) {
				var props = results[i];
	
				var a = document.createElement('a');
							
				a.href = 'http://www.eastsidebookings.nl/artist/' + encodeURIComponent (props[0]) + '/' + ls.makePermalink(props[2]);
				a.title = 'Lees meer over ' + props[1];
				a.appendChild (document.createTextNode(props[1]));
				
				var li = document.createElement('li');
				li.className = 'searchLI';
				li.appendChild(a);
				ls.ul.appendChild (li);
			}
		} else {
			var li = document.createElement('li');
			li.appendChild(document.createTextNode(txt));
			ls.ul.appendChild(li);
		}
		
		ls.div.appendChild(ls.h2);
		ls.div.appendChild(ls.ul);
		
		var ap = ($('artistPreviews')) ? $ ('artistPreviews') : $ ('artistContent');
		var c = $('content');
		
		c.insertBefore(ls.div,ap);
		
		ap.style.display = 'none';
	},
	
	getResultsArray : function (str) {
		var outerArray = str.split(',');
		for (var i = 0; i < outerArray.length; i++) {
			var innerArray = outerArray[i].split('||');
			outerArray[i] = innerArray;
		}
		
		return outerArray;
	},
	
	removeUl : function () {
		if (ls.ul != null) {
			ls.ul.parentNode.removeChild(ls.ul);
			ls.ul = null;
		}
		return true;
	},
	
	createThinkingImg : function () {
		if (ls.thinkingImg == null) {
			ls.thinkingImg = document.createElement ('img');
			
			ls.thinkingImg.src = 'http://www.eastsidemusic.nl/images/layout/thinking.gif';
			ls.thinkingImg.width = 30;
			ls.thinkingImg.height = 15;
			ls.thinkingImg.alt = 'Loading...';
			ls.thinkingImg.style.marginLeft = '5px';
			
			insertAfter (ls.input.parentNode,ls.thinkingImg,ls.input);
		}
		return;
		
	},
	
	destroyImage : function () {
		if (ls.thinkingImg != null) {
			ls.thinkingImg.parentNode.removeChild (ls.thinkingImg);
			ls.thinkingImg = null;
		}
		
		return;
	}		
}

addEvent (window, 'load', ls.initLiveSearch, false);