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

var $ = function(id) {
	try {
		return document.getElementById(id);
	} catch(e) {
		nxlog(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);
	window.addEventListener('load', toolbox_init, false);
} else if(window.attachEvent) {
	window.attachEvent('onload', searchbox_init);
	window.attachEvent('onload', toolbox_init);
}

function doSlide(id){
    timeToSlide = 15; // in milliseconds
    //obj = document.getElementById(id);
    obj = id;
    if(obj.style.width == "auto" || obj.style.display == ""){ // si c hidden on fait le slide
        obj.style.visibility = "hidden";
        obj.style.display = "block";
        height = obj.offsetWidth;
        obj.style.width="0px";
        obj.style.visibility = "visible";
        pxPerLoop = height/timeToSlide;
        slide(obj,0,height,pxPerLoop);
    } else {
    	//obj.style.display = "none";
	obj.style.width="auto";
    }
}

function slide(obj,offset,full,px){
    if(offset < full){
        obj.style.width = offset+"px";
        offset=offset+px;
		var inter = full-px;
		if(offset > inter) {
			obj.style.visibility = "visible";
		}
        setTimeout((function(){slide(obj,offset,full,px);}),2);
    } else {
        obj.style.width = "auto";
	//    obj.style.height = "auto"; //Can be usefull in updated divs otherwise
	//just use full+"px"
    }
}

function toolbox_init(e) {
	var f = ['legalinfo', 'support'];
	for(var i=0; i<f.length; ++i) {
		var d = $(f[i]);
		d = d.firstChild;
		while(d && d.className != 'jackal') {d = d.nextSibling}
		if(d && d.addEventListener) {
			d.addEventListener('click', function(e) {
				var el = e.srcElement || e.target;
				while(el.id.length==0) {el=el.parentNode;}
				doSlide(el);
				toolbox_hyde(el);
				e.returnValue = false;
				e.cancel = true;
				return false;
			}, false);
		} else if(d && d.attachEvent) {
			d.attachEvent('onclick', function(e) {
				var el = e.srcElement || e.target;
				while(el.id.length==0) {el=el.parentNode;}
				toolbox_hyde(d);
                                doSlide(el);
                                e.returnValue = false;
                                e.cancel = true;
                                return false;
			});
		}
	}
}

function toolbox_hyde(d) {
	var top = d.parentNode;
	var children = top.childNodes;
	for(var i=0; i<children.length; ++i) {
		var child = children[i];
		var isSameNode = (d == child);
		if(child.nodeType == 1) {
			var morechildren = child.childNodes;
			for(var j=0; j<morechildren.length; ++j) {
				var additionalchild = morechildren[j];
				if(additionalchild.nodeType == 1 && additionalchild.className == 'hyde') {					
					if(isSameNode) {
						additionalchild.style.display='inline';
						var l=additionalchild.firstChild;
						while(l && l.nodeName != 'A') {l=l.nextSibling;}
						l.className = 'bright';
					} else {
						additionalchild.style.display='none';
						var l=additionalchild.firstChild;
						while(l && l.nodeName != 'A') {l=l.nextSibling;}
						l.className = '';
					}
				}
			}
		}
	}
}

