//热门城市区域
var hotCityAreas = [["天河区","TH"],["越秀区","YX"],["海珠区","HZ"],["荔湾区","LW"],
				["白云区","BY"],["番禺区","PY"],["花都区","HD"],["萝岗区","LG"]];
				
// 加载目的地区域提示List
function loadAuotCityList() {
	$.ajax({
		type: "POST",
		url: "/js/autocity/areaList.js",
		dataType:"json",
		success: function(result){
			if(result.success) {
				apdata = eval(result.data);
			} else {
				alert("载入城市区域数据时出错:" + result.data);
			}
		}
	});
}
$(function(){	
	loadAuotCityList();	
	initAutoCity("areaName");
	$("input[autocomplete='true']").each(function(){
		initAutoCity($(this).attr("id"));
	});
});

var autoCity = null;
function initAutoCity(inputId){
	autoCity=new AutoCity();
	autoCity.init(inputId);
}
function gE(x){return document.getElementById(x);}
function gETop(x){var t=0;while(x){t+=x.offsetTop;x=x.offsetParent;}return t;}
function gEBottom(x){return (x.offsetHeight+gETop(x));}
function gELeft(x){var l=0;while(x){l+=x.offsetLeft;x=x.offsetParent;}return l;}
function gERight(x){return (x.offsetWidth+gELeft(x));}

function isUpDownKey(kc){
	if(kc==40 || kc==38 || kc == 33 || kc== 34){//down,up,pageup,pagedown,enter
		return true;
	}else{
		return false;
	}
}
function isCtrlKey(kc){
	if(kc==16 || kc==17 || kc == 18){//shift,ctrl,alt
		return true;
	}else{
		return false;
	}
}

var apdata = "";

function AutoCity() {
	var inputobj = null;
	var sugdiv = null;
	var focusBySelect = false;
	var currInputObj = null;
	var prevInputValue = null;
	var showarr = null;//保存城市列表索引
	var showfocus = -1;//当前索引
	var showingHot = false;//标志是否正在显示热门城市
	var FOCity = null;
	var onDocumentClick = null;
}

AutoCity.prototype.init = function(inputId){

	if(this.sugdiv==null){
		this.sugdiv=document.createElement('DIV');
		this.sugdiv.style.display='none';	 
		this.sugdiv.className='sug';
		this.sugdiv.style.zIndex=99;
 		this.sugdiv.innerHTML='';
		this.sugdiv.onscroll=OnSugScroll;		
		var outDiv = document.createElement('DIV');
		outDiv.className='sugp';
		outDiv.appendChild(this.sugdiv);
		var bs=document.getElementsByTagName('BODY');
		bs[0].appendChild(outDiv);
	}
	this.focusBySelect = false;
	//this.currInputHdn = hdnInputId;
	var obj = gE(inputId);
	obj.onkeydown = OnInputKeyDown;
	obj.onfocus = OnInputFocus;
	//obj.onblur = OnInputBlur;
}
function OnSugScroll(){
	if(autoCity.currInputObj==null){return;}
	autoCity.focusBySelect=true;
	autoCity.currInputObj.focus();
}
function OnDocClick(e){
	var x,y,t,b,l,r;
	var d;
	if(window.event){
		e=window.event;
		x=e.clientX+document.body.parentNode.scrollLeft;
		y=e.clientY+document.body.parentNode.scrollTop;
	}else{
		x=e.pageX;
		y=e.pageY;
	}
	d=autoCity.currInputObj;
	if(!d){document.onclick=autoCity.onDocumentClick;return;}
	t=gETop(d);
	l=gELeft(d);
	b=gEBottom(d);
	r=gERight(d);
	if(x>=l && x<r && y>=t && y<b){return;}
	d=autoCity.sugdiv;
	t=gETop(d);
	l=gELeft(d);
	b=gEBottom(d);
	r=gERight(d);
	//if(x>=l && x<r && y>=t && y<b){return;}
	//在sugdiv外的地方点击，隐藏sugdiv
	if(x <l || x >= r || y < t || y >= b){autoCity.hide();return;}
	autoCity.select();
}
var global_focus_flag=false;

function OnInputFocus(){

	global_focus_flag = true;
	autoCity.currInputObj = this;
	autoCity.prevInputValue = null;

	if(autoCity.focusBySelect){
		autoCity.focusBySelect=false;
		return;
	}
	autoCity.focusBySelect=false;		
	InputChecking();
	//显示热门城市
	if(autoCity.sugdiv.style.display === "none"){
		if(autoCity.currInputObj.value.trim() === "" || autoCity.currInputObj.value.trim() === "不限"){
			autoCity.showHot();
		}else{
			global_focus_flag = false;
			autoCity.update(autoCity.currInputObj.value);
		}
	}
}

function OnInputBlur(){
	return;
}
function InputChecking(){
	if(autoCity.currInputObj == null) {
		return;
	}
	var v=autoCity.currInputObj.value;
	if(autoCity.prevInputValue==null || autoCity.prevInputValue!=v){
		if(v != "") {
			autoCity.update(v);
			autoCity.showingHot = false;//置显示热门为false
		} else {
			if(!autoCity.showingHot){
				autoCity.hide();
			}
		}
	}
	setTimeout("InputChecking()", 10);
}
function OnInputKeyDown(e){
	if(autoCity.currInputObj==null){
		autoCity.currInputObj = this;
		autoCity.prevInputValue = null;
		InputChecking();
	}
	var bCtrlDown=0;
	if(!e && window.event){
		e=window.event;
	}
	if(window.event){
		bCtrlDown=e.ctrlKey;
	}else{
		bCtrlDown=(e.modifiers & Event.CONTROL_MASK) > 0;
	}
	if(bCtrlDown){
		return true;
	}
	var kc=e.keyCode;
	if(isCtrlKey(kc)){
		return true;
	}
	return (InputRealKeyDown(kc));
}
function InputRealKeyDown(kc) {
	global_focus_flag = false;
	if(isUpDownKey(kc)){
		if(kc==38){// up
			autoCity.move(1,0);
		}else if(kc==40){//down
			autoCity.move(1,1);
		}else if(kc==33){//pageup
			autoCity.move(0,0);
		}else if(kc==34){//pagedown
			autoCity.move(0,1);
		}
		return false;
	}else if(kc==13){//enter
		autoCity.select();
		return false;
	}else if(kc==9){//tab
		autoCity.select();
		return true;
	}else if(kc==27){//esc
		autoCity.hide();
		return true;
	}
	return true;
}
function OnSugClick(){
	autoCity.select();
}
AutoCity.prototype.select = function() {

	if(this.sugdiv.style.display=='none'){return false;}	
	if(this.showingHot){		
		var i=this.showfocus;
		this.currInputObj.value = hotCityAreas[i][0];		
		this.hide();
		return;
	}
	if(this.showfocus>=0 && this.showarr!=null && this.showfocus<this.showarr.length && this.currInputObj!=null){
		var i=this.showarr[this.showfocus];
		this.currInputObj.value=apdata[i][0];
		//gE(this.currInputHdn).value = apdata[i][0];
		this.focusBySelect=true;
		this.currInputObj.focus();
		if(window.event){
			var r=this.currInputObj.createTextRange();
			r.move("character", r.text.length);
			r.select();
		}else{
			var v=apdata[i][0].length;
			this.currInputObj.setSelectionRange(v, v);
		}
	}
	this.hide();
}
AutoCity.prototype.highlight = function(idx) {
	if(this.sugdiv.style.display=='none'){return false;}
	if(idx==this.showfocus){return true;}
	var i;
	var ls = this.sugdiv.getElementsByTagName('LI');
	if(this.showfocus>=0&&this.showfocus<ls.length){
		ls[this.showfocus].className='';
	}
	if(idx>=0&&this.showfocus<ls.length){
		ls[idx].className='hover';
	}
	this.showfocus=idx;
}
AutoCity.prototype.move = function(m,n){
	var a=this.showfocus;
	if(m==1&&n==0){
		a--;
	}else if(m==1&&n==1){
		a++;
	}else if(m==0&&n==0){
		a-=5;
	}else if(m==0&&n==1){
		a+=5;
	}
	if(a<0){
		a=0;
	}else if(a>=this.showarr.length){
		a=this.showarr.length-1;
	}
	var i,j,k;
	i=(this.sugdiv.scrollTop)/20;
	if(a<=i){
		this.sugdiv.scrollTop=a*20;
	}else if(a>=i+10){
		this.sugdiv.scrollTop=(a-9)*20;
	}
	this.highlight(a);
}
AutoCity.prototype.hide = function(v) {
	if(this.sugdiv.style.display!='none'){
		this.sugdiv.style.display='none';
		this.sugdiv.parentNode.style.display='none';
		document.onclick=this.onDocumentClick;
	}
}
AutoCity.prototype.update = function(v) {

	this.prevInputValue = v;
	v=v.toLowerCase();
	this.showfocus = -1;
	if(this.showarr==null){
		this.showarr = new Array();
	}else{
		this.showarr.length = 0;
	}
	var i,j,s;
	if(v.length!=0){
		for(i=0;i<apdata.length;i++){
			for(j=0;j<apdata[i].length;j++){
				if(apdata[i][j].toLowerCase().indexOf(v) == 0){
					this.showarr[this.showarr.length]=i;
					break;
				}
			}
		}
	}

	if(this.showarr.length<=0||(this.showarr.length==1&&v==apdata[this.showarr[0]][0])){
		this.hide();
		return;
	}
	if(global_focus_flag){
		return;
	}   
	
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt
        (navigator.appVersion) >= 4)){
        this.sugdiv.parentNode.style.top=(gEBottom(this.currInputObj)+3) +"px";
		this.sugdiv.parentNode.style.left=(gELeft(this.currInputObj)+2)+"px";
	}else{
		this.sugdiv.parentNode.style.top=(gEBottom(this.currInputObj)+2) +"px";
		this.sugdiv.parentNode.style.left=gELeft(this.currInputObj)+"px";
	}
	//this.sugdiv.style.width=this.currInputObj.offsetWidth;

	s='<ul>';	
	var bigWidth = 0;
	for(i=0;i<this.showarr.length;i++){
		s+='<li'+(i==0?' class="hover"':'')+' onmousemove="autoCity.highlight('+i+')" onclick="OnSugClick();">'+apdata[this.showarr[i]][0]+'&nbsp;&nbsp;('+apdata[this.showarr[i]][1]+')</li>';
		var temp = apdata[this.showarr[i]][0].lengthzh() + apdata[this.showarr[i]][1].lengthzh() + 4;
		bigWidth = temp > bigWidth?temp:bigWidth;
	}
	s+='</ul>';
	bigWidth = bigWidth * 9;
	this.sugdiv.style.width=bigWidth + "px";
	this.sugdiv.innerHTML=s;

	this.showfocus=0;
	this.sugdiv.style.zIndex=999;
	this.sugdiv.scrollTop = 0;

	$(this.sugdiv).parent().bgiframe({width:bigWidth + "px",height:"230px"});//解决ie6 div被select挡住问题
	this.sugdiv.parentNode.style.display='block';
	this.sugdiv.style.display='block';

	this.onDocumentClick=document.onclick;
	document.onclick=OnDocClick;
}
AutoCity.prototype.showHot = function() {		
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt
        (navigator.appVersion) >= 4)){
        this.sugdiv.parentNode.style.top=(gEBottom(this.currInputObj)+3) +"px";
		this.sugdiv.parentNode.style.left=(gELeft(this.currInputObj)+2)+"px";
	}else{
		this.sugdiv.parentNode.style.top=(gEBottom(this.currInputObj)+2) +"px";
		this.sugdiv.parentNode.style.left=gELeft(this.currInputObj)+"px";
	}
	//this.sugdiv.style.width=this.currInputObj.offsetWidth;
	
	if(this.showarr){
		this.showarr.length = 0;
	}else{		
		this.showarr = new Array();
	}
	s='<ul>';	
	var bigWidth = 0;
	for(i=0;i<hotCityAreas.length;i++){
		s+='<li'+(i==0?' class="hover"':'')+' onmousemove="autoCity.highlight('+i+')" onclick="OnSugClick();">'+hotCityAreas[i][0]+'&nbsp;&nbsp;('+hotCityAreas[i][1]+')</li>';
		this.showarr[this.showarr.length]=i;
		var temp = hotCityAreas[i][0].lengthzh() + hotCityAreas[i][1].lengthzh() + 4;
		bigWidth = temp > bigWidth?temp:bigWidth;
	}
	s+='</ul>';
	bigWidth = bigWidth * 9;
	this.sugdiv.style.width=bigWidth + "px";
	this.sugdiv.innerHTML=s;
	this.showfocus=0;
	this.sugdiv.style.zIndex=999;
	this.sugdiv.scrollTop = 0;

	$(this.sugdiv).parent().bgiframe({width:bigWidth + "px",height:"230px"});//解决ie6 div被select挡住问题
	this.sugdiv.parentNode.style.display='block';
	this.sugdiv.style.display='block';
	this.showingHot = true;//置显示热门为true		
	this.onDocumentClick=document.onclick;
	document.onclick=OnDocClick;
}
//取中文字符长度
String.prototype.lengthzh = function() {
	var cArr = this.match(/[^x00-xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length); 
}
//去掉字符串左右空格
String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}  