var isSearchFocus = false;
var searchFocusTimer=null;
var curTr = null;
function searchFocus(_input) {
	isSearchFocus = true;
	if (_input.value == "Enter Device ID/Name/SIM card") {
		_input.value = "";
		_input.setAttribute("presearvalue", "?");
	}
	var search = function() {
		if (_input.getAttribute("presearvalue") != _input.value) {
			_input.setAttribute("presearvalue", _input.value);
			searchListOut(_input.value);
		}
		searchFocusTimer = setTimeout(search, 300);
	};
	search();
}
function viewSearchListAll() {
	isSearchFocus = true;
	searchListOut("");
}
function timeHiddenSearch() {
		var hidden = function() {
			if (!isSearchFocus) {
				document.getElementById("searchListOutCanvas").style.display = "none";
			}
	    };
	setTimeout(hidden, 200);
}
function viewSearchListAllBlur(){alert(isSearchFocus+",1");
isSearchFocus = false;alert(isSearchFocus+",2");
}
function searchBlur(_input) {
	isSearchFocus = false;
	window.clearTimeout(searchFocusTimer);
	if (_input.value == "") {
		_input.value = "Enter Device ID/Name/SIM card";
	}
}
function searchListOut(v) {
	overindex = 0;
	var _canvas = document.getElementById("searchListOutCanvas");
	_canvas.innerHTML = "";
	_canvas.style.display = "block";
	_canvas.scrollTop = 0;
	var p = 0;
	var pvalue;
	if(null!=Vechieljson){
		for(var i=0;i<Vechieljson.data.length;i++){	
  		        pvalue = Vechieljson.data[i][1];
				if ((pvalue.toLowerCase().indexOf(v.toLowerCase()) > -1) || (Vechieljson.data[i][2].toLowerCase().indexOf(v.toLowerCase()) > -1) || Vechieljson.data[i][3].toLowerCase().indexOf(v.toLowerCase()) > -1) {
					p++;
					var _div = document.createElement("div");
					if (p == 1) {
						_div.className = "search_item_over";
					} else {
						_div.className = "search_item";
					}
					_div.innerHTML = pvalue;
					_div.title = pvalue;
					_div.setAttribute("username", $.trim(Vechieljson.data[i][1]));
					_div.setAttribute("index", i);
					_div.setAttribute("userid",Vechieljson.data[i][0]);
					_div.onclick = function() {
						selectSearchItem(this.getAttribute("username"), this.getAttribute("userid"));///////////
					};
					_div.onmouseover = function() {
						this.className = "search_item_over";
						overindex = this.getAttribute("index");
					};
					_div.onmouseout = function() {
						this.className = "search_item";
						overindex = this.getAttribute("index");
					};
					_canvas.appendChild(_div);
				}  	   
			
		}
	}
	if (p == 0) {
		_canvas.innerHTML = "<span style=\"bordercolor:white;\">" + "No queries to the data" + "</span>";
	}
}
function selectSearchItem(username, userid) {
	document.getElementById("searchinput").value = username;
	//document.getElementById("userid").value = userid;
	document.getElementById("searchListOutCanvas").style.display = "none";
	document.getElementById("searchinput").setAttribute("presearvalue", username);
	
	searchshow(userid);
}
function rootItemStyle() {
	var _canvas = document.getElementById("searchListOutCanvas");
	for ( var i = _canvas.childNodes.length - 1; i >= 0; i--) {
		_canvas.childNodes[i].className = "search_item";
	}
}


function searchshow(id){
  if(curTr != null){
		curTr.css("background","");
		curTr.css("color","#676767");
		curTr = null;
	}
  $("#vehicle_table_history_content").find("tr").each(function(){	  
  		if($(this).attr("name") == id){
		     var container = $('#vehicle_table_history_title'),
				scrollTo = $(this);
				//scrollTo.css("background","#A0B0C7");
				//scrollTo.css("color","#fffff");
				container.scrollTop(
				 	 scrollTo.offset().top - container.offset().top-20 + container.scrollTop()
				);				
				curTr = scrollTo;
				scrollTo.css("background","#A0B0C7");
				//scrollTo.css("color","#fffff");
  		}
  });

}
var overindex = 0;
function selectNextItem() {
	var _canvas = document.getElementById("searchListOutCanvas");
	if (_canvas.firstChild.nodeName == "SPAN") {
		return
	}
	if (overindex < _canvas.childNodes.length - 1) {
		_canvas.childNodes[overindex].className = "search_item";
		overindex++;
		_canvas.childNodes[overindex].className = "search_item_over";
	}
	if (overindex > 10) {
		_canvas.scrollTop = (overindex - 10) * 18;
	}
	_canvas.childNodes[overindex].className = "search_item_over";
}
function selectPrevItem() {
	var _canvas = document.getElementById("searchListOutCanvas");
	if (_canvas.firstChild.nodeName == "SPAN") {
		return
	}
	if (overindex > 0) {
		_canvas.childNodes[overindex].className = "search_item";
		overindex--;
		_canvas.childNodes[overindex].className = "search_item_over";
	}
	if (overindex > 10) {
		_canvas.scrollTop = (overindex - 10) * 18;
	}
	_canvas.childNodes[overindex].className = "search_item_over";
}
function selectOverItem() {
	var _canvas = document.getElementById("searchListOutCanvas");
	if (_canvas.firstChild.nodeName == "SPAN") {
		return;
	}
	selectSearchItem(_canvas.childNodes[overindex].getAttribute("username"),
			_canvas.childNodes[overindex].getAttribute("userid"));
	
	_canvas.style.display = "none";
}
document.onkeydown = function(e) {
	if (isSearchFocus) {
		var theEvent = window.event || e;
		var k = theEvent.keyCode ? theEvent.keyCode
				: theEvent.which ? theEvent.which : theEvent.charCode;
		if (k == 40) {
			selectNextItem();
			return false; //解决点击箭头下拉，再用方向键问题。
		} else if (k == 38) {
			selectPrevItem();
			return false;
		} else if (k == 13) {
			selectOverItem();
			return false;
		}
	}
};
addListener(window.document, "click", function(){timeHiddenSearch();});

(function(doc){
	var ele = doc.getElementById("searchinput");
	if(ele && ele.value === ""){
		ele.value = "Enter Device ID/Name/SIM card";
	}
})(document);

function addListener(element,e,fn){ element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn)};

