function ajax_request(callback, url, parameters)
{
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, IE7 ,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE6
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	function response()
	{
		callback(http_request);
	}

	http_request.onreadystatechange = response;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}

function smartSearch(textbox) {
	//initialize
	this.textbox = textbox;
	this.showContent = false;
	this.obForm = document.getElementById('search_mini_form');
	this.obSearch = document.getElementById('smart-search-extended');
	this.moreCheck = document.getElementById('smart_search_more');
	this.dialog = document.getElementById('smart_search_quick_results');
	
	this.obResults		= document.getElementById('smart_search_quick_results');
	this.obCorrections	= document.getElementById('smart_search_corrections');
	this.obMore			= document.getElementById('smart_search_header_more');
	this.obTags 		= document.getElementById('smart_search_tags');

	// show/hide triggers
	this.showResults = false;
	this.showCorrections = false;
	this.showMore = false;
	this.showTags = false;

	this.previous = null;
	this.textbox.hasfocus = true;
	var oThis = this;

	// assign onStuffs
	this.textbox.onblur = function () { this.hasfocus = false; setTimeout('quickResultsPreview(); smartSearchClose();', 1000); };

	this.textbox.onclick = function (oEvent) { this.onkeyup(oEvent); }

	this.textbox.onfocus = this.obSearch.onkeyup = this.textbox.onkeyup = function (oEvent) {
		this.hasfocus = true;
		oThis.HandleKeyUp(oEvent);
		return false;
	}

	this.showHideDialog = function()
	{
		// hide/show children
		this.obResults.style.display = this.showResults ? 'block' : 'none';
		this.obCorrections.style.display = this.showCorrections ? 'block' : 'none';
		this.obMore.style.display = this.showMore ? 'block' : 'none';
		this.obTags.style.display = this.showTags ? 'block' : 'none';

		// hide/show main
		this.obSearch.style.display = (this.showCorrections || this.showResults || this.showTags) ? 'block' : 'none';
		return true;
	}

	this.HandleKeyUp = function(oEvent)
	{
		if (!oEvent)
			oEvent = window.event;
		switch(oEvent.keyCode) 	{
			case 13:	//enter
				return this.chooseSelected();
			case 27:	//escape
				this.textbox.hasfocus = false;
				return smartSearchClose();
			case 37:	//left
			case 39:	//right
			case 32:	// anything else
				return;
			case 38:	//up
			case 40:	//down
				this.showHideDialog();
				return this.moveSelected(oEvent.keyCode==38 ? -1 : 1);
			default:	// everything else
				this.update();
		}
	}

	this.chooseSelected = function()
	{
		lis = this.obSearch.getElementsByTagName('li')
			if(lis.length == 0) return false;		// no li elements

		to_select = null;
		for(x=0;x<lis.length;x++)
			if(lis[x].className == 'selected')
				to_select = x;			// this one was selected

		if(to_select == null)			// none selecetd, return true to trigger normal enter
			return true;

		hrefs = lis[to_select].getElementsByTagName('a');

		// this seems to be the only way to change the location before the enter key submits the form.
		if(lis[to_select].parentNode.parentNode.id == 'smart_search_corrections_ul') {		// this is a 'did you mean'
			textbox.value = hrefs[0].innerHTML;
			window.location.href = '/index.php/catalogsearch/result/?q='+hrefs[0].innerHTML;
		} else
			window.location.href = hrefs[0].href;	// go straight to link
	}

	this.moveSelected = function(direction)
	{
		var list = this.obSearch.getElementsByTagName('ul');
		if(list.length == 0) return false;		// no ul

		lis = list[0].getElementsByTagName('li')

			to_select = 0;
		for(x=0;x<lis.length;x++) {
			if(lis[x].className == 'selected') {
				to_select = x;			// this one was selected
				to_select += direction;				// move selected up/down
			}
			lis[x].className = '';
		}

		if(to_select >= lis.length)
			to_select = 0;				// select firist
		else if(to_select < 0)
			to_select = lis.length-1;		// select last


		lis[to_select].className = 'selected';
		lis[to_select].onmouseover();				// simulate mouse over
	}

	this.update = function()	{
		if(this.textbox.value.length < 3) {
			this.textbox.hasfocus = false;
			smartSearchClose();
			return false;		// exit if too short
		}

		if(this.previous == this.textbox.value) return false;	// exit if same request
		this.previous = this.textbox.value;

		var _get = '?q='+this.textbox.value;
		if(this.moreCheck.checked) _get += '&more';

		ajax_request(this._smartSearchUpdate, '/bolt_extensions/quicksearch.php', _get);
	}

	this._smartSearchUpdate = function(http_request)	{
		if (http_request.readyState == 4 && http_request.status == 200) {
			var xmldoc = http_request.responseXML;

			// results
			var shown_results = 0;

			var results = xmldoc.getElementsByTagName('results');

			if(results.length < 1)	return;

			results = results[0];
			objDiv = document.getElementById('smart_search_quick_results_ul');
			objDiv.innerHTML = null;	// clear
			objList = document.createElement('ul');
			for (var iNode = 0; iNode < results.childNodes.length; iNode++) {
				var node = results.childNodes.item(iNode);
				if (node.childNodes.length > 0) {
					this.showResults = true;

					var node_item = node.childNodes.item(0);
					objListElement = document.createElement('li');

					var price = Math.floor(node.getAttribute('price')*100)/100;
					var special_price = Math.floor(node.getAttribute('special_price')*100)/100;

					objLinkNode = document.createElement('a');
					objLinkNode.href = '/index.php/catalog/product/view/id/'+node.getAttribute('id')+'/'+node.getAttribute('url');

					

					
					objLinkNode.innerHTML = '';

					if(price > special_price && special_price > 0) {	// special price
						objLinkNode.innerHTML = "<span class=\"smart_search_price special_price\"><span class=\"slashed_price\">$"+price+"</span>$"+special_price+"</span>"
					} else {			// normal price
						objLinkNode.innerHTML = "<span class=\"smart_search_price\">$"+price+"</span>"
					}

					objLinkNode.innerHTML +=
						"<img class=\"smart_search_img\" src=\""+node.getAttribute('img')+"\" />"+
						"<span class=\"smart_search_name\">"+node_item.data+"</span>"+
						"<span class=\"smart_search_desc\">"+node.getAttribute('desc')+"</span>";

					objListElement.appendChild(objLinkNode);
					objList.appendChild(objListElement);
					objDiv.appendChild(objList);

					shown_results++;
				}
			}

			if(	results.getAttribute("count") > results.getAttribute("limit"))
				this.showMore = true;


			// tags
			var show_tags = false;
			var results = xmldoc.getElementsByTagName('tags')[0];
			objDiv = document.getElementById('smart_search_tags');
			objUL = document.getElementById('smart_search_tags_ul');
			objUL.innerHTML = null;	// clear
			objList = document.createElement('ul');
			for (var iNode = 0; iNode < results.childNodes.length; iNode++) {
				var node = results.childNodes.item(iNode);
				if (node.childNodes.length > 0) {
					this.showTags = true;
					var node_item = node.childNodes.item(0);

					objListElement = document.createElement('li');
					objLinkNode = document.createElement('a');
					objLinkNode.href = "/tag/product/list/tagId/"+node.getAttribute('id');
					objLinkNode.innerHTML = this.trimName(node_item.data);

					objListElement.appendChild(objLinkNode);
					objList.appendChild(objListElement);
					objUL.appendChild(objList);
				}
			}

			// did you mean
			var show_corrections = false;
			var results = xmldoc.getElementsByTagName('corrections')[0];
			objDiv = document.getElementById('smart_search_corrections_ul');
			objDiv.innerHTML = null;	// clear
			objList = document.createElement('ul');
			for (var iNode = 0; iNode < results.childNodes.length; iNode++) {
				var node = results.childNodes.item(iNode);
				if (node.childNodes.length > 0) {
					this.showCorrections = true;
					var node_item = node.childNodes.item(0);

					objListElement = document.createElement('li');
					objLinkNode = document.createElement('a');
					objLinkNode.href = "#";
					objLinkNode.onclick = new Function("var obSearch = document.getElementById('search').value = this.innerHTML; document.getElementById('search_mini_form').submit();");
					objLinkNode.innerHTML = this.trimName(node_item.data);

					objListElement.appendChild(objLinkNode);
					objList.appendChild(objListElement);
					objDiv.appendChild(objList);
				}
			}

			this.showHideDialog();

		}
		smartSearchNotWorking();
	}




	this.trimName = function(name)
	{
		var max_len = 30;
		if(name.length > max_len)
			name = name.substr(0, max_len)+'...';
		return name;
	}

	this.smartSearchToggleMore = function()
	{
		var moreText = this.obMore.getElementsByTagName('a');
		moreText=moreText[0];	// get the first

		// toggle check
		this.moreCheck.checked = !this.moreCheck.checked;

		if(this.moreCheck.checked)
			moreText.innerHTML = 'less';
		else
			moreText.innerHTML = 'more';

		// get back onto the textbox and update
		this.previous = null;
		this.textbox.focus();
	}
}

function quickResultsPreview(text)
{
	var obPreview = document.getElementById('smart-search-extended-preview');
	
	if(text == undefined || text == '') {
		obPreview.innerHTML = '';
		obPreview.style.display = 'none';
		return false;
	}

	obPreview.innerHTML = text;
	obPreview.style.display = 'block';

}

function smartSearchWorking(search)
{
	return false;	// lags too much
	var loading_img = '/bolt_extensions/quicksearch/images/opc-ajax-loader.gif';
	if(search == undefined)
		var search = document.getElementById('search');
		
	search.style.backgroundImage = "url('"+loading_img+"')";
	search.style.backgroundRepeat = 'no-repeat';
	search.style.backgroundPosition = 'right';
}

function smartSearchNotWorking(search)
{
	return false;	// lags too much
	if(search == undefined)
		var search = document.getElementById('search');
		
	search.style.backgroundImage = null;
}


function smartSearchOpen()
{
	ob = document.getElementById('smart-search-extended');
	ob.style.display = 'block';
}

function smartSearchClose()
{	
	search = document.getElementById('search');

	if(search.hasfocus == false)
		obSearch.style.display = 'none';
}


function smartSearchPerhapsReplace(val)
{
	ob = document.getElementById('search');
	ob.value = val;
}

function dump(element) {
var string = '';
 
//Loop through all the child objects in element
for(property in element)
{
    //Add the name and value of the child object
    //string += property + ': '+ element[property] + ''+ '\n';           
    string += property + ': ';
	if(property[element])
		string += property[element] + '';
	string += '\n';
}
//Ouput the result

alert(string);


} 



