/**
 *	Search functionality
 */
var nxlog = function(msg) {
	if(window.console) {
		console.log(msg);
	}
}

var $ = function(id) {
	try {
		document.getElementById(id);
	} catch(e) {
	}
}

var _ = function() {
	arguments.join = Array.prototype.join;
	return arguments.join("");
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];


var createHttpXmlRequest = function() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

var searchbox_init = function() {
	var formbox = document.forms['quick_find'];
	
	if(formbox) {
		var input = formbox.elements[0];
		var hrefs = formbox.getElementsByTagName('a');
		var href = hrefs[0];
		
		input.autocomplete = false;
		
		if(input.addEventListener) {
			
			href.addEventListener('click', function(e) {
				formbox.submit();
			}, false);
			
			input.addEventListener('focus', function(e) {
				if(window.searchHideTimer) {
					clearTimeout(window.searchHideTimer);
					window.searchHideTimer = null;
				}
			}, false);
			
			input.addEventListener('blur', function(e) {
				if(!window.searchHideTimer) {
					window.searchHideTimer = setTimeout(function(v) {
						if(window.resultText) {
							window.resultText.style.display='none';
						}
					}, 1000);
				}
			}, false);
			
			input.addEventListener('keyup', function(e) {
				if(window.searchTimer) {
					clearTimeout(window.searchTimer);
				}
				
				if(input.value.length > 1) {
					window.searchTimer = setTimeout(function() {
						var xmlReq = createHttpXmlRequest();
						if (!xmlReq) return;
						
						var method = "GET";
						var url = "/quick_find.php?keywords="+input.value;
						var callback = function(req) {							
							var service = new Function("return "+req.responseText);
							var products = service();
							
							if(!window.resultText) {
								window.resultText = document.createElement('div');
								var searchResult = window.resultText;
								searchResult.className = 'showsearch';
								searchResult.style.position = 'absolute';
								searchResult.style.left = _(input.parentNode.offsetLeft+1, 'px');
								searchResult.style.top = _(input.parentNode.offsetTop+input.parentNode.offsetHeight-1, 'px');
								searchResult.style.width = _(input.parentNode.offsetWidth-2, 'px');
								
								searchResult.addEventListener('mouseover', function(e) {
									if(window.searchHideTimer) {
										clearTimeout(window.searchHideTimer);
										window.searchHideTimer = null;
									}
								}, false);
								
								searchResult.addEventListener('mouseout', function(e) {
									if(!window.searchHideTimer) {
										window.searchHideTimer = setTimeout(function(v) {
											if(window.resultText) {
												window.resultText.style.display='none';
											}
										}, 1000);
									}
								}, false);
								
								document.body.appendChild(searchResult);
							}
							
							/*var nodes = window.resultText.childNodes;
							if(nodes.length > 0) {
								for(var j=0; j < nodes.length; ++j) {
									var node = nodes[j];
									node.parentNode.removeChild(node);
								}
							}*/
							window.resultText.innerHTML = ''; // bad hack
							
							if(products.length > 0) {
								for(var i=0; i<products.length; ++i) {
									var product = products[i];
									var node = document.createElement('a');
									node.href = product.url;
									node.appendChild(document.createTextNode(product.product));
									window.resultText.appendChild(node);
								}
							} else {
								window.resultText.appendChild(document.createTextNode('No search results'));
							}
							
							window.resultText.style.display = 'block';
						}
						
						xmlReq.open(method, url, true);
						xmlReq.setRequestHeader('User-Agent','XMLHTTP/1.0');

						xmlReq.onreadystatechange = function () {
							if (xmlReq.readyState != 4) return;
							if (xmlReq.status != 200 && xmlReq.status != 304) {
//								alert('HTTP error ' + req.status);
								return;
							}
							callback(xmlReq);
						}
						xmlReq.send(null);

					}, 400);
				} else {
					if(window.resultText) {
						window.resultText.style.display = 'none';
					}
				}
				
			}, false);
		} else if(input.attachEvent) {
			href.attachEvent('onclick', function(e) {
				formbox.submit();
			});
			
			input.attachEvent('onfocus', function(e) {
				if(window.searchHideTimer) {
					clearTimeout(window.searchHideTimer);
					window.searchHideTimer = null;
				}
			});
			
			input.attachEvent('onblur', function(e) {
				if(!window.searchHideTimer) {
					window.searchHideTimer = setTimeout(function(v) {
						if(window.resultText) {
							window.resultText.style.display='none';
						}
					}, 1000);
				}
			});
			
			input.attachEvent('onkeyup', function(e) {
				if(window.searchTimer) {
					clearTimeout(window.searchTimer);
				}
				
				if(input.value.length > 1) {
					window.searchTimer = setTimeout(function() {
						var xmlReq = createHttpXmlRequest();
						if (!xmlReq) return;
						
						var method = "GET";
						var url = "/quick_find.php?keywords="+input.value;
						var callback = function(req) {							
							var service = new Function("return "+req.responseText);
							var products = service();
							
							if(!window.resultText) {
								window.resultText = document.createElement('div');
								var searchResult = window.resultText;
								searchResult.className = 'showsearch';
								searchResult.style.position = 'absolute';
								
								var curleft = curtop = 0;
								var obj = input.parentNode;
								
								if (obj.offsetParent) {
									do {
										curleft += obj.offsetLeft;
										curtop += obj.offsetTop;
									} while(obj = obj.offsetParent);
								}
								
								searchResult.style.left = _(curleft+1, 'px');
								searchResult.style.top = _(curtop+input.parentNode.offsetHeight-3, 'px');
								searchResult.style.width = _(input.parentNode.offsetWidth, 'px');
								
								searchResult.attachEvent('onmouseover', function(e) {
									if(window.searchHideTimer) {
										clearTimeout(window.searchHideTimer);
										window.searchHideTimer = null;
									}
								}, false);
								
								searchResult.attachEvent('onmouseout', function(e) {
									if(!window.searchHideTimer) {
										window.searchHideTimer = setTimeout(function(v) {
											if(window.resultText) {
												window.resultText.style.display='none';
											}
										}, 1000);
									}
								}, false);
								
								document.body.appendChild(searchResult);
							}
							
							/*var nodes = window.resultText.childNodes;
							if(nodes.length > 0) {
								for(var j=0; j < nodes.length; ++j) {
									var node = nodes[j];
									node.parentNode.removeChild(node);
								}
							}*/
							window.resultText.innerHTML = ''; // bad hack
							
							if(products.length > 0) {
								for(var i=0; i<products.length; ++i) {
									var product = products[i];
									var node = document.createElement('a');
									node.href = product.url;
									node.appendChild(document.createTextNode(product.product));
									window.resultText.appendChild(node);
								}
							} else {
								window.resultText.appendChild(document.createTextNode('No search results'));
							}
							
							window.resultText.style.display = 'block';
						}
						
						xmlReq.open(method, url, true);
						xmlReq.setRequestHeader('User-Agent','XMLHTTP/1.0');

						xmlReq.onreadystatechange = function () {
							if (xmlReq.readyState != 4) return;
							if (xmlReq.status != 200 && xmlReq.status != 304) {
//								alert('HTTP error ' + req.status);
								return;
							}
							callback(xmlReq);
						}
						xmlReq.send(null);

					}, 400);
				} else {
					if(window.resultText) {
						window.resultText.style.display = 'none';
					}
				}
				
			});
		}
	}
}

if(window.addEventListener) {
	window.addEventListener('load', searchbox_init, false);
} else if(window.attachEvent) {
	window.attachEvent('onload', searchbox_init);
}
