var isfenceSearchFocus = false;
var fencesearchFocusTimer=null;
function fencesearchFocus(_input) {
	isfenceSearchFocus = true;
	if (_input.value == electronicFenceLanguage.qsrwlm) {
		_input.value = "";
		_input.setAttribute("presearvalue", "?");
	}
	var search = function() {
		if (_input.getAttribute("presearvalue") != _input.value) {
			_input.setAttribute("presearvalue", _input.value);
			fencesearchListOut(_input.value);
		}
		fencesearchFocusTimer = setTimeout(search, 300);
	};
	search();
}
function fenceviewSearchListAll() {
	isfenceSearchFocus = true;
	fencesearchListOut("");
}
function fencetimeHiddenSearch() {
		var hidden = function() {
			if (!isfenceSearchFocus) {
				document.getElementById("fencesearchListOutCanvas").style.display = "none";
			}
	    };
	setTimeout(hidden, 200);
}
function fenceviewSearchListAllBlur(){
isfenceSearchFocus = false;
}
function fencesearchBlur(_input) {
	isfenceSearchFocus = false;
	window.clearTimeout(fencesearchFocusTimer);
	if (_input.value == "") {
		_input.value = electronicFenceLanguage.qsrwlm;
	}
}
function fencesearchListOut(v) {
	fenceoverindex = 0;
	var _canvas = document.getElementById("fencesearchListOutCanvas");
	_canvas.innerHTML = "";
	_canvas.style.display = "block";
	_canvas.scrollTop = 0;
	var p = 0;
	var pvalue;
	var curfenceObject=null;
	var json = electronicFence.tree.fenceJson ;
		for(var i=0;i<json.data.length;i++){
		    curfenceObject=json.data[i];
		    if(curfenceObject!=null && curfenceObject.fenceType != fenceType_){
	         	pvalue = curfenceObject.name;
				if ((pvalue.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", curfenceObject.name);
					_div.setAttribute("index", i);
					_div.setAttribute("userid",curfenceObject.id);
					_div.onclick = function() {
						fenceselectSearchItem(this.getAttribute("username"), this.getAttribute("userid"));///////////
					};
					_div.onmouseover = function() {
						this.className = "search_item_over";
						fenceoverindex = this.getAttribute("index");
					};
					_div.onmouseout = function() {
						this.className = "search_item";
						fenceoverindex = this.getAttribute("index");
					};
					_canvas.appendChild(_div);
				}  
			} 	   
			
		}
	if (p == 0) {
		_canvas.innerHTML = "<span style=\"background: #FFF;width: 207px;\">" + language.mycdsj + "</span>";
	}
}
function fenceselectSearchItem(username, userid) {
	document.getElementById("fencesearchinput").value = username;
	//document.getElementById("userid").value = userid;
	document.getElementById("fencesearchListOutCanvas").style.display = "none";
	document.getElementById("fencesearchinput").setAttribute("presearvalue", username);
	
	fencesearchshow(userid);
}
function fencefencerootItemStyle() {
	var _canvas = document.getElementById("fencesearchListOutCanvas");
	for ( var i = _canvas.childNodes.length - 1; i >= 0; i--) {
		_canvas.childNodes[i].className = "search_item";
	}
}
function fencesearchshow(id){
  var flag=false;
	
  if(mapType==1){

	  showfencesBaidu(id,false);
	  openfenceBaidu(id);
	  
	   var curNode=electronicFence.tree.treeObject.getNodeByParam("id",id);
	   if(curNode!=null){
	       var parentNode=curNode.getParentNode();
	       if(parentNode!=null){
	       		electronicFence.tree.treeObject.expandNode(parentNode, true, true, true);	       		
	       }
	       electronicFence.tree.treeObject.selectNode(curNode);//选中指定节点	       
	   }
	  
	  flag=true;
      return false; 
  }else{
  		   closeVehicleInfoWindow();
		   electronicFence.draw.showFence(id);
		   
		   
		   var curNode=electronicFence.tree.treeObject.getNodeByParam("id",id);
		   
		   if(curNode!=null){
		   
		       var parentNode=curNode.getParentNode();
		       
		       if(parentNode!=null){
		       
		       		electronicFence.tree.treeObject.expandNode(parentNode, true, true, true);	  
		       		     		
		       }
		       
		       //electronicFence.tree.treeObject.selectNode(curNode);//选中指定节点	       
		   		electronicFence.tree.OnClick(null,null,curNode);
		   		
		   	   
		   
		   }
		   
		   
		   
		   flag=true;
		   
	       return false; 

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

(function(doc){
	var ele = doc.getElementById("fencesearchinput");
	if(ele && ele.value === ""){
		ele.value = electronicFenceLanguage.qsrwlm;
	}
})(document);

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

