var Searchbox = new Object();
//methods
Searchbox.Init = function() {

	document.getElementById("searchbox_radio_1").checked = true;
	this.radio_1 = $("#searchbox_radio_1");
	this.radio_1.bind("click", function() { Searchbox.selectSearcher(document.getElementById("searchbox_radio_1").value,1); });
	this.radio_1.checked = true;
	
	this.radio_2 = $("#searchbox_radio_2");
	this.radio_2.bind("click", function() { Searchbox.selectSearcher(document.getElementById("searchbox_radio_2").value,2); });
	this.select_2 = $("#searchbox_select_2");
	this.select_2.bind("click", function() { document.getElementById('searchbox_radio_2').checked = true; Searchbox.showSearchersDlg(2); });

	this.select_3 = $("#searchbox_select_3");
	this.select_3.bind("click", function() { document.getElementById('searchbox_radio_3').checked = true; Searchbox.showSearchersDlg(3); });
	this.radio_3 = $("#searchbox_radio_3");
	this.radio_3.bind("click", function() { Searchbox.selectSearcher(document.getElementById("searchbox_radio_3").value,3); });	
	
	this.searchers_dlg = $("#searchbox_searchers_dlg"); 
	this.searchers_dlg.bind("mouseout", function() { Searchbox.hideSearchersDlg(1) });	
	this.searchers_dlg.bind("mouseover", function() {Searchbox.hideSearchersDlg(0) });
//	$("#searchbox_filtr").bind("keypress", function () { Searchbox.showSuggestion(); });
	$("#searchbox_closeicon").bind("click", function () { this.parentNode.style.display = 'none'; });
}

Searchbox.loadSearchers = function() {
	var filtr = document.getElementById("searchbox_filtr").value;
	this.suggestSearchers(filtr);
	Searchbox.hideSearchersDlg(0);
}

Searchbox.showSuggestion = function(key) {
	clearTimeout(this.suggestTime);
	this.suggestTime = setTimeout("Searchbox.loadSearchers()",200);
}

Searchbox.suggestSearchers = function(filtr) {
	if(typeof(filtr) != 'undefined')
		filtr = '&v='+filtr;
	else
		filtr = '';
	ajaxLoading(true);
	$.get("?p=plugin&a=searchbox&f=xmllist"+filtr, function(data) {
		$("#searchers_list").empty();
		eval(data);
		var domFragment = document.createDocumentFragment();
		for(i=0; i < AjaxObj.length; i++)
		{
			//name
			var pNameElement = document.createElement("p");
			pNameElement.appendChild(document.createTextNode(AjaxObj[i].name));
			$(pNameElement).bind('click', function () { Searchbox.selectSearcher()}, false);

			//icon
			var iconElement = document.createElement("img");
			iconElement.setAttribute("src","./plugins/searchbox/searchers/"+AjaxObj[i].icon+'.ico')
			iconElement.setAttribute("type",'image/icon')
			$(iconElement).addClass("icon");

			//suggest
			var divElement = document.createElement("div");
			$(divElement).addClass("suggestText");
			
			if(i==0)
				divElement.style.backgroundColor = '#eee';
			
			$(divElement).bind('mouseover', function() { this.style.backgroundColor = '#eee' }, false);	
			$(divElement).bind('mouseout', function() { this.style.backgroundColor = '#fff' }, false);
			divElement.setAttribute('sid',AjaxObj[i].id);
			divElement.appendChild(iconElement);
			divElement.appendChild(pNameElement);
			domFragment.appendChild(divElement);
		}
		$("#searchers_list").append(domFragment); 
		ajaxLoading(false);
	});
}

Searchbox.showSearchersDlg = function(buttonid) { 
	this.currentRadioButtonId=buttonid;
	this.suggestSearchers();
	
	this.searchers_dlg.css("zIndex","1002");
	this.searchers_dlg.css("display","block"); 
	this.searchers_dlg.css("top",(pageY+10)+"px"); 
	this.searchers_dlg.css("left",(pageX-50)+"px");
	document.getElementById("searchbox_filtr").focus();
	$("#searchbox_filtr").attr('autocomplete','off');
	
	if ($.browser.mozilla)
		$("#searchbox_filtr").keypress(Searchbox.processKey);
	else
		$("#searchbox_filtr").keydown(Searchbox.processKey); 
}

Searchbox.processKey = function(e) {
	
	// handling up/down/escape requires results to be visible
	// handling enter/tab requires that AND a result to be selected
	var list = $("#searchers_list div");
	
		 	if ((/27$|38$|40$/.test(e.keyCode) || (/^13$|^9$/.test(e.keyCode)))) 
	{
        if (e.preventDefault)
            e.preventDefault();
		if (e.stopPropagation)
            e.stopPropagation();

		e.cancelBubble = true;
		e.returnValue = false;
		switch(e.keyCode) {
			
			case 38: // up
					for(i = 0; i < list.length; i++)
					{
						if(list[i].style.backgroundColor == 'rgb(238, 238, 238)' || list[i].style.backgroundColor == '#eee') {
							list[i].style.backgroundColor = "#fff";
							if(i == 0) i = list.length;
							list[--i].style.backgroundColor = "#eee";
							break;
						}
					}
				break;
	
			case 40: // down
					for(i = 0; i < list.length; i++)
					{
						if(list[i].style.backgroundColor == 'rgb(238, 238, 238)' || list[i].style.backgroundColor == '#eee') {
							list[i].style.backgroundColor = "#fff";
							if(i == list.length-1) i = -1;
							list[++i].style.backgroundColor = "#eee";
							break;
						}
					}
				break;

			case 9:  // tab
			case 13: // enter
				Searchbox.selectSearcher();
				break;
				
			case 27: //	escape
				Searchbox.closeSearchersDlg();
				break;
		}
		

		return false;
	}
	Searchbox.showSuggestion();
}

Searchbox.closeSearchersDlg = function() {
	$("#searchbox_searchers_dlg").css("display","none");
}

Searchbox.hideSearchersDlg = function (param) {
	if(param)
		this.time2close = setTimeout("Searchbox.closeSearchersDlg",3000)
	else
		clearTimeout(this.time2close); 
}

Searchbox.selectSearcher = function() {
	var buttonid = 1; 
	if(document.getElementById('searchbox_radio_2').checked == true)
		buttonid = 2;
	else if(document.getElementById('searchbox_radio_3').checked == true)
		buttonid = 3;
	
	var list = $("#searchers_list div");
	var sid = false;
	
	if(arguments.length > 0)
		var sid = arguments[0];
	else
	{
		for(i = 0; i < list.length; i++)
		{
			if(list[i].style.backgroundColor == 'rgb(238, 238, 238)' || list[i].style.backgroundColor == '#eee') {
				sid = list[i].getAttribute('sid');
				break;
			}
		}
	}
	
	ajaxLoading(true);
	$.get("?p=plugin&a=searchbox&f=pickupSearcher&sid="+sid+"&index="+buttonid, function(jsCode) {
		ajaxLoading(false);
		eval(jsCode);
		$("#searchboxLogo").attr("src","./plugins/searchbox/searchers/"+AjaxObj.logo+".png");
		$("#searchboxLogo").click(function() {location.href = AjaxObj.href; });
		$("#searchbox_radio_"+buttonid+"_label").empty();
		$("#searchbox_radio_"+buttonid+"_label").append(AjaxObj.name);		
		var selectedRadioButton = document.getElementById("searchbox_radio_"+buttonid);
		selectedRadioButton.value = sid;
		
	});
	
	this.closeSearchersDlg();
}


//initialization
$(document).ready(function() {	
	Searchbox.Init();
	$("#searchbox_input_1").suggest('?p=plugin&a=searchbox&f=suggest');
})