var isSearchFocus = false;
var searchFocusTimer=null;
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) {
				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 = "请输入设备ID/名称/SIM卡号";
	}
}
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(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;
		  
		  switch(lineStatus){//判断在那个面板中显示（-1全部，1在线，0离线）
	
	   		  case -1:  
	   		           
						if ((pvalue.toLowerCase().indexOf(v.toLowerCase()) > -1) || (curObject.simCode && curObject.simCode.toLowerCase().indexOf(v.toLowerCase()) > -1) || (curObject.gprsCode && 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", pvalue);
							_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);
						}
	   		  break;    
	   		  
	   		  case 1:   if(curObject.online == 1){  	
	   		  	            
	   		            	
							if ((pvalue.toLowerCase().indexOf(v.toLowerCase()) > -1) || (curObject.simCode.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", pvalue);
								_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);
							}
				   	 }
	   		  break;
	   		  
	   		  case 0:   if(curObject.online != 1){

							if ((pvalue.toLowerCase().indexOf(v.toLowerCase()) > -1) || (curObject.simCode.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", pvalue);
								_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);
							}
	   		            	
	   		            }
	   		  break;
						    		  
		  }   	   
			
		}
	}
	if (p == 0) {
		_canvas.innerHTML = "<span style=\"bordercolor:white;\">" + "没有查询到数据" + "</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){
  var flag=false;
  $('dl#group').find("dd").each(function(){
      $(this).children().each(function(){
         var listid=$(this).attr('id');
		 listid=listid.substring(listid.indexOf("_")+1);//获取车辆id
         if(id==listid){           	   
		   switch(lineStatus){//判断在那个面板中显示（-1全部，1在线，0离线）
    		  case -1:  if($(this).parent().is(":hidden")){
							$(this).parent().prev().click();
						}
						showMenu($(this),0);
    		  break;    
    		  
    		  case 1:   if($(this).attr("status")=="1"){
    		            	if($(this).parent().is(":hidden")){
								$(this).parent().prev().click();
							}
							showMenu($(this),0);
    		            }
    		  break;
    		  case 0:   if($(this).attr("status")=="0"){
    		            	if($(this).parent().is(":hidden")){
								$(this).parent().prev().click();
							}
							showMenu($(this),0);
    		            }
    		  break;
    		  
    		 }
    		 flag=true;
    		 return false; 
         } 
     });
     if(flag){
    	 return false; 
     }
  });

}
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 = "请输入设备ID/名称/SIM卡号";
	}
})(document);

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

