﻿var LS_searchg;
var LS_searchq;
var LS_resultDiv;
var LS_xmlHttp;
var LS_timer;
var LS_g = '';
var LS_q = '';
var LS_disabled = true; 
var LS_hasRecord = false;
var LS_visible = false;
var LS_currRow = 0;
var LS_count = 0;




function initLS(){
	LS_searchg = document.getElementById("category_dropdown");
	LS_searchq = document.getElementById("search_text");
	LS_resultDiv = document.getElementById("livesearch");
	if (LS_searchq){
		$(LS_searchq).addEvents({
			'focus': function(){LS_disabled=false;}, 
			'blur': function(){LS_disabled=true; window.setTimeout("showHideLS('hidden')",250);},
			'keydown': function(e){return preventKeyDefault(e);},
			'keyup': function(e){selectLS(e);},
			'mouseup': doLS,
			'propertychange': doLS, //IE
			'input': doLS //FireFox
		});

		//LS_searchq.focus();
		LS_disabled = false;
	}
}

function doLS(){
	if (!LS_disabled){
		var g = getG();
		var q = getQ();
		if (LS_g==g && LS_q==q) return;

		LS_g = g;
		LS_q = q;
		LS_hasRecord = false;

		if (LS_timer) window.clearTimeout(LS_timer); 
		LS_timer=window.setTimeout("getLS()",350); 
	}
	
}

function getLS(){
	if (LS_q.length==0){ 
		showHideLS('hidden');
	 	return;
 	}

 	LS_xmlHttp=getXmlHttpObject();
 	if (LS_xmlHttp==null) return;

 	var url="livesearch.jsp?g="+LS_g+"&q="+LS_q;
	url = "index.php?a=ajax&action=liveSearch&b[text]="+LS_q+"&b[category_id]="+LS_g;
	
	LS_xmlHttp.onreadystatechange=drawLS;
	LS_xmlHttp.open("GET",encodeURI(url),true);
	LS_xmlHttp.send(null);
} 

function drawLS(){ 
	if (LS_xmlHttp.readyState==4 || LS_xmlHttp.readyState=="complete"){ 
		if (LS_xmlHttp.responseText.trim().length==0){
			LS_resultDiv.innerHTML='';
			LS_hasRecord = false;
			showHideLS('hidden');
		}else{
			LS_resultDiv.innerHTML=LS_xmlHttp.responseText; 
			LS_hasRecord = true;
			showHideLS('visible');
			
			if($('ls_result_table')){
				LS_count = $('ls_result_table').rows.length;
			}
		}
	} 
}

function selectLS(e){
	var key; 

	if (e.keyCode) key = e.keyCode;
	else key = e.which;

	if (LS_hasRecord){
		switch (key){
		case 38: // Up
			if (LS_currRow==0){
				showHideLS('hidden'); 
			}else if (LS_currRow==1){
				highlightLS(LS_currRow,'off','k');
				resetQ();
				LS_currRow = 0;
			}else{
				highlightLS(LS_currRow-1,'on','k');
			}
			break;
		case 40: // Down
			if (LS_visible){
				if(LS_currRow==LS_count){
					highlightLS(LS_currRow,'off','k');
					LS_currRow = 0;
				}
				highlightLS(LS_currRow+1,'on','k');
			}else{
				if (LS_g==getG() && LS_q==getQ()) showHideLS('visible'); 
				else doLS(); 
			}
			break;
		case 13: // Enter
			showHideLS('hidden');
			break;
		case 27: // Escape
			resetQ();
			showHideLS('hidden');			
			break;
		}
	}else{
		if (key==40){ // Down
			if (LS_g!=getG() || LS_q!=getQ()) doLS();
		}
	}
}

function highlightLS(id,status,device){
	if (id>0){
		var obj = document.getElementById('ls_result'+id);
		var parent_row = document.getElementById('ls_result_row'+id);
		if (obj){
			if (status=='on'){
				highlightLS(LS_currRow,'off'); 

				if (device=='k') setQ(getInnerText(obj)); 
				obj.className = 'ls_inner_tr_movr';
				parent_row.className = 'ls_inner_tr_movr';
				LS_currRow = id;
			}else{
				obj.className = 'ls_inner_tr';
				parent_row.className = 'ls_inner_tr';
			}
		}
	}
}

function submitLS(obj){
	setQ(getInnerText(obj));
	showHideLS('hidden');
	document.searchform.submit();
}

function getG(){
	return LS_searchg.value;
}

function getQ(){
	return LS_searchq.value.ltrim();
}

function getInnerText(obj){
	if (obj.innerText) return obj.innerText;
	else return obj.textContent;
}

function setQ(q){
	LS_disabled = true; 
	LS_searchq.value = q; 
	LS_disabled = false;
}

function resetQ(){
	LS_disabled = true; 
	LS_searchq.value = LS_q; 
	LS_disabled = false;
}


function showHideLS(s){
	if (LS_disabled) s='hidden';

	if (s=='visible'){
		LS_visible = true;
	}else{
		highlightLS(LS_currRow,'off','k');
		LS_visible = false;
	}

	if (navigator.appName.indexOf('Microsoft')!=-1) showHideDummyFrame(s);
	LS_currRow = 0;
	LS_resultDiv.style.visibility = s;
}

// Fix IE div vs select box z-index problem
function showHideDummyFrame(s){
	df = document.getElementById('dummyFrame'); 
	if (df){
		df.style.width = LS_resultDiv.offsetWidth;
		df.style.height = LS_resultDiv.offsetHeight;
		df.style.top= LS_resultDiv.offsetTop;
		df.style.left= LS_resultDiv.offsetLeft;
		df.style.visibility = s;
	}
}

function preventKeyDefault(e){
	if (e.keyCode==27||e.keyCode==38||e.keyCode==40){
		if (e.preventDefault) e.preventDefault();
		return false;
	}
}

function getXmlHttpObject(){
	var xmlHttp=null;

	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// IE
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function toAdvanceSearch(){
	category_id = document.getElementById("category_dropdown").value;
	c = "";
	if(category_id > 0){
		c = "&c="+category_id;
	}
	new_location = 'http://'+location.hostname+"/index.php?a=19"+c;
	location.href='http://'+location.hostname+"/index.php?a=19"+c;
	
}

window.addEvent('load',initLS);
