//楼盘名的动态提示
//LFANG.COM	2011.03
var lpSchHighLightIndex = -1;
var lpSchTimeoutId;
$(document).ready(function(){
    $('#lpSchName').bind('keyup',lpSchNameProcessKeyup);
});
//lpHiteNameNeedPos未定义或者false时，不进行填充重定位
function lpSchHintInit(){
  $("lpSchNameContent").hide();
  if (undefined!= typeof lpHiteNameNeedPos || !lpHiteNameNeedPos){return true;}
  var lpSchNamePos = $("#lpSchName").position();
  var lpSchNameContentTop = lpSchNamePos.top + $("#lpSchName").height() +10;
  var lpSchNameContentLeft = lpSchNamePos.left;
  $("#lpSchNameContent").show();
  $("#lpSchNameContent").offset({left:lpSchNameContentLeft,top:lpSchNameContentTop});
  $("#lpSchNameContent").width($("#lpSchName").width()+6);
  $("#lpSchNameContent").hide();
}
function lpSchNameProcessKeyup(event){
  var myEvent = event || windows.event;
   var keyCode = myEvent.keyCode;
   //输入是字母，或者退格键则需要重新请求
  //48-57:数字，8退格，32空格
   if((keyCode >= 65 && keyCode <= 90) || (keyCode >= 48 && keyCode <= 57) || keyCode == 8 || keyCode== 32){
		lpSchHighLightIndex = -1;
		clearTimeout(lpSchTimeoutId);
		lpSchTimeoutId = setTimeout(lpSchNameProcessAjaxRequest,500);
   //处理上下键(up,down)
   }else if(keyCode == 38 || keyCode == 40){
	   lpSchNameProcessKeyUpAndDown(keyCode);
   //按下了回车键
   }else if(keyCode == 13){
	   schHouse();
	   $("#lpSchHintContent").html("");
	   $("#lpSchNameContent").hide();
   } 
}
function changeToWhite(){
 if(lpSchHighLightIndex != -1){
	 $('#lpSchHintContent').children().eq(lpSchHighLightIndex).removeClass("on");
 }
}  
function schContentFnOver(){
  changeToWhite();
  $(this).addClass("on");
  lpSchHighLightIndex = $(this).index();  
}
function schContentFnOut(){
  $(this).removeClass("on");
}
function autoFixLpSchName(){
  if(lpSchHighLightIndex != -1){  
	$('#lpSchName').val($(this).html());
	$("#lpSchHintContent").html("");
	$("#lpSchNameContent").hide();
}
}
function lpSchNameProcessAjaxRequest(){              
  $.getJSON("http://lp.lfang.com/interface_house.php?ac=getLpSchNames&lp="+encodeURI($("#lpSchName").val())+"&schCode="+Math.random()+"&jsoncallback=?",function(data){
    lpSchHintInit();
	  $("#lpSchHintContent").html("");
	  var xmlData = eval(data);
      $(xmlData).each(function(){
          var hintLpName = this.houseName;
		   var schContent = $("<li></li>");
		  schContent.html(hintLpName);
		  schContent.hover(schContentFnOver,schContentFnOut);
		  schContent.click(autoFixLpSchName);
		  $("#lpSchHintContent").append(schContent);
      });
      $("#lpSchNameContent").show();
  });
}

function lpSchNameProcessKeyUpAndDown(keyCode){
  var lpSchWords = $("#lpSchHintContent").children();
  var num = lpSchWords.length;
  if (num<=0) {return false;}
  changeToWhite();
  lpSchHighLightIndex = ((keyCode != 38 ? num + 1:num - 1)+lpSchHighLightIndex) % num;
  lpSchWords.eq(lpSchHighLightIndex).addClass("on");
 $('#lpSchName').val(lpSchWords.eq(lpSchHighLightIndex).html());
}
