
onloadChain.add(function(e) {
	var d;
	if (d = dg('search_field')) {
		d.sl_search = new sl_searchField(d);
	}
});

function searchClicked() {
	var d;
	if (d = dg('search_field')) {
		window.location.href = "search.html?q="+escape(d.value);		
	}
};

function sl_searchField(d) {
	this.d = d;
	this.timer = null;
	this.sri = new serverRequestInterface(this,"dynamic_page.php");
	this.results = dg(this.d.id+"_results",document.body,"div",{
		className:"search_res_cont",
		style:{
			padding:"8px",
			position:"absolute",
			top:"0px",
			display:"none"
		}
	});
	
	this.d.onkeypress = function(e) {
		if (this.timer) { clearTimeout(this.timer); }
		this.timer = setTimeout("dg('"+this.id+"').sl_search.update();", 500);
	};
	
	this.show = function(yesNo) {
		this.results.style.display = yesNo ? "block" : "none";
		if (yesNo) {
			positionElementRelative({d:this.results,h:"left",v:"top"},{d:this.d,h:"left",v:"bottom"},0,5);
		};
	};
	
	this.update = function() {
		if (this.d.value == '') {
			this.show(false);
		} else {
			this.sri.request("search",{q:this.d.value},function(response,v,ob){
				removeChildNodes(ob.ref.results);
				
				var d = dg("",ob.ref.results,"div",{
					innerHTML:"X",
					fontWeight:"bold",
					style:{cursor:"pointer"},
					onclick:function(e){
						this.d.show(false);
					}});
				d.d = ob.ref;
				
				for (var i = 0; i < v.length; i ++) {
					var d = dg("",ob.ref.results,"div",{
						innerHTML:"<div class='search_res'><div style='padding:4px;'><span class='search_res_title'>"+v[i].n+"</span> <span class='search_res_text'>"+v[i].l+"</span></div></div>",
						onclick:function(e) {
							window.location.href = this.a;
						}
					});				 
					d.a = v[i].a;
				}
				if (!v.length) {
					var d = dg("",ob.ref.results,"div",{innerHTML:"<div class='search_res'><div style='padding:4px;'><span class='search_res_title' style='font-style:italic'>No Results Found</span></div></div>"});
				}
				ob.ref.show(true);
			});
		}
	};
};

//{"32000":{"a":"home.html","n":"Home","l":" The Winning <span class='hl'>Matrix<\/span> Group Home ..."}}

