var isSearchFocus = false;
var searchFocusTimer=null;
var cartIds=window.parent.cartIds;
var equipmentObject=window.parent.equipmentObject;
function searchFocus(_input) {
	isSearchFocus = true;
	if (_input.value == "请输入设备ID/设备名称/SIM卡号") {
		_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) {
			var flag=true;
			if(cartIds.length != 0){
				
				var v=document.getElementById("report_searchinput").value;

				var cartIdsArr = cartIds.split(",");
				var curObject = null;
				
				for(var i=0;i<cartIdsArr.length;i++){
				  
				    curObject=equipmentObject[cartIdsArr[i]];
				  
					if( (curObject.vehicleName.toLowerCase()==v.toLowerCase() || curObject.gprsCode.toLowerCase()==v.toLowerCase()) || curObject.simCode.toLowerCase()==v.toLowerCase()){
						
						flag=false;
						
						break;
					}
				}			
			}
			if(flag){
				document.getElementById("userid").value="";
				document.getElementById("report_searchinput").value="请输入设备ID/设备名称/SIM卡号";
			}
			document.getElementById("report_searchListOutCanvas").style.display = "none";
		}
	};
	setTimeout(hidden, 200);
}
function searchBlur(_input) {
	isSearchFocus = false;
	window.clearTimeout(searchFocusTimer);
	if (_input.value == "") {
		_input.value = "请输入设备ID/设备名称/SIM卡号";
	}
}
function searchListOut(v) {
	overindex = 0;
	var _canvas = document.getElementById("report_searchListOutCanvas");
	_canvas.innerHTML = "";
	_canvas.style.display = "block";
	_canvas.scrollTop = 0;
	var p = 0;
	var pvalue;
	if(cartIds.length != 0){
		
			
			
			var cartIdsArr = cartIds.split(",");
			
			var curObject = null;
			
			for(var i=0;i<cartIdsArr.length;i++){
				  
				    curObject=equipmentObject[cartIdsArr[i]];
				    
					pvalue = curObject.vehicleName+" ("+curObject.simCode+")";
					
					if ((pvalue.toLowerCase().indexOf(v.toLowerCase()) > -1) || curObject.gprsCode.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", curObject.vehicleName);
						_div.setAttribute("index", i);
						_div.setAttribute("userid", curObject.id);
						_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>" + "没有查询到数据" + "</span>";
	}
}
function selectSearchItem(username, userid) {
	document.getElementById("report_searchinput").value = username;
	document.getElementById("userid").value = userid;
	document.getElementById("report_searchListOutCanvas").style.display = "none";
	document.getElementById("report_searchinput").setAttribute("presearvalue", username);
}
function rootItemStyle() {
	var _canvas = document.getElementById("report_searchListOutCanvas");
	for ( var i = _canvas.childNodes.length - 1; i >= 0; i--) {
		_canvas.childNodes[i].className = "search_item";
	}
}
var overindex = 0;
function selectNextItem() {
	var _canvas = document.getElementById("report_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("report_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("report_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", timeHiddenSearch);
(function(doc){
	var ele = doc.getElementById("report_searchinput");
	if(ele && ele.value === ""){
		ele.value = "请输入设备ID/设备名称/SIM卡号";
	}
})(document);

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

