var isSearchFocus = false;
var searchFocusTimer=null;

var treeSearchTitle = "设备ID/名称/SIM/分组/客户";
function searchFocus(_input) {
	isSearchFocus = true;
	if (_input.value == treeSearchTitle) {
		_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 = treeSearchTitle;
	}
}
function searchListOut(v) {
	
	v = v.toLowerCase();
	
	overindex = 0;
	var _canvas = document.getElementById("searchListOutCanvas");
	_canvas.innerHTML = "";
	_canvas.style.display = "block";
	_canvas.scrollTop = 0;
	var p = 0;
	
	/*********************插入客户选项****************************/
	if(isIncludeClientNode() &&　clientsJSON　!= null){
		
		var clientItems = [];

		for(var i = 0, len = clientsJSON.data.length; i < len; i++){ 
		    if (clientsJSON.data[i].name.toLowerCase().indexOf(v) > -1){
		    	p++;
		    	clientItems.push(createSearchItem(p, "search_item", clientsJSON.data[i].name, "c_" + clientsJSON.data[i].id));
		    }
		}
		
		addSearchItem(_canvas, p, clientItems, "客户");		
	}
	/*************************************************/
	
	/*********************插入分组选项****************************/
	if(groupjson != null && groupjson.data.length > 0){
		
		var groupItems = [];
		
		for(var i = 0, len = groupjson.data.length; i < len; i++){ 
		    if (groupjson.data[i].groupName.toLowerCase().indexOf(v) > -1){
		    	p++;
		    	groupItems.push(createSearchItem(p, "search_item", groupjson.data[i].groupName, "g_" + groupjson.data[i].id));
		    }
		}
		
		addSearchItem(_canvas, p, groupItems, "分组");	
	}
	/*************************************************/

	
	
	/*********************插入设备选项*****************/
	
	if(cartIds.length != 0){
		
		var carItems = [];
		
		var pvalue;		
		var cartIdsArr = cartIds.split(",");
		var curObject = null;
		
		for(var i=0;i<cartIdsArr.length;i++){
		  
			  curObject=equipmentObject[cartIdsArr[i]];			  
			  pvalue = curObject.vehicleName;
			  
		   	  if(lineStatus == 1 && curObject.online == 0) continue;							
			  if(lineStatus == 0 && curObject.online == 1) continue;	
			  
			  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++;				    				
					carItems.push(createSearchItem(p, "search_item", pvalue, curObject.id));
			   }  	   			
		}
		
		addSearchItem(_canvas, p, carItems, "设备");	
	}
	/*************************************************/
	
	if (p == 0) {
		_canvas.innerHTML = "<span style=\"bordercolor:white;\">" + "没有查询到数据" + "</span>";
	}
}

function addSearchItem(canvas,index ,items, separatorName){
	
	if(items.length > 0){
		
		canvas.appendChild(createSearchSeparator(separatorName));//插入一个分隔符
		
		for(var i = 0, len = items.length; i < len; i++){
			canvas.appendChild(items[i]);
		}
	}
}

function createSearchItem(index, className, name, id){
	
	var _div = document.createElement("div");

	_div.className = className;
	_div.innerHTML = name;
	_div.title = name;
	_div.setAttribute("username", name);
	_div.setAttribute("index", index);
	_div.setAttribute("userid", 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");
	};
	
	return _div;	
}

function createSearchSeparator(name){
	
	var _div = document.createElement("div");
		_div.className = "xiala_list_padding";
		_div.innerHTML = "--------------------" + name + "--------------------";
	
	return _div;
}


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(id.indexOf("_") > -1){//判断是否是分组或者客户
  	  var temp = id.split("_"),
  	 	  id = temp[1],
  	 	  type = temp[0];
  	   
  	   if(type == "g"){//分组
  	   
  	   		var groupTreeNode = $("#" + id);
  	   		
  	   		if(isIncludeClientNode()){//若显示分组所属客户,展开分组时先展开所属客户结点
  	   			var groupClientId = groupTreeNode.attr("clientId");
  	   			expandClientNode(groupClientId);
  	   		}  	   		
  	   		
  	   		if(groupTreeNode.next().is(":hidden")){
				groupTreeNode.click();					
			}
			treeNodePosition(id);//将选中分组结点调整到醒目位置			
  	   }else if(type == "c"){
  	   		expandClientNode(id);
			treeNodePosition("client_" + id);//将选中客户结点调整到醒目位置	
  	   }
  	   
  	  return;
  }
  
  var clientId = 0, groupId = 0, carId = 0;
  var groupNode = null, carNode = null;
  $.each(equipmentMap, function (k, v) {
  		
  		groupId = k;//组id
  		clientId = v.getClientId();		
		curGroupChildsIds = v.getChildsIds();
		
		for(i = 0, len = curGroupChildsIds.length; i < len; i++) {
			
			carId = curGroupChildsIds[i];
			
			if(carId == id){
				
				//展开所属客户结点
				if(isIncludeClientNode()){
	  	   			expandClientNode(clientId);
	  	   		}  	   		
	  	   		
	  	   		//展开所属分组结点
	  	   		groupNode = $("#" + groupId);
	  	   		if(groupNode.next().is(":hidden")){
					groupNode.click();					
				}
				
				//展开设备结点
				carNode = $("#" + groupId + "_" + carId );
				showMenu(carNode, 0);
			}
			
		}
  });
  
  /*
  $('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;
    		  
    		 }
         } 
     });

  });
  */

}

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 = treeSearchTitle;
	}
})(document);

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

