/*
热门城市/按字母分类城市插件 obj1为显示城市inputId  obj2为城市编码隐藏域Id 
thisObj为点击打开此插件的小图标
（可选参数）initialIdx为按拼音首字母索引分类开关，存在此参数时为分类，否则为不分类
dateType为显示数据类型（city\international\country）
（可选参数）airport为区分机场和城市参数，存在为机场，否则为城市
（可选参数）arr_cityHot为热门城市数组   arr_city为所有城市数组
*/ 
function setFillObj(obj1,obj2,thisObj,initialIdx,dateType,airport,arr_cityHot,arr_city){
	tInputId=obj1;hInputId=obj2;
	if(airport){//如果airport存在，调用机场函数，否则调用城市函数
		switch(dateType){
			case "international":
				commonCityHot=internatAirport;
				break;
			case "country":
				commonCityHot=commonCountry;
				break;
			default:
				commonCityHot=commonAirport;
				break;	
		}
		arrAllCity=getAirportArr(dateType);
	}
	else{
		switch(dateType){
			case "international":
				commonCityHot=internationalCommonCity;
				break;
			case "country":
				commonCityHot=commonCountry;
				break;
			default:
				commonCityHot=commonCity;
				break;	
		}
		arrAllCity=getCityArr(dateType);
	}
	
	if(arr_cityHot){
		commonCityHot=arr_cityHot;
	}
	if(arr_city){
		arrAllCity=arr_city;
	}
	setPos($(thisObj));
	creatDate(initialIdx);

}
var tInputId,hInputId;
var commonCityHot = new Array(),arrAllCity = new Array();


//var strHotCitySort="ABCDEFGHIJK|L|MNOPQRST|WXYZ";//字母分类接口
if(typeof(strHotCitySort)=="undefined"){
var strHotCitySort="ABCDEFG|HIJKL|MNOPQRST|WXYZ";
}
var arrSort=strHotCitySort.split("|"),A_G=arrSort[0],H_L=arrSort[1],M_T=arrSort[2],W_Z=arrSort[3];
var cityInx=[],arrA_G=[],arrH_L=[],arrM_T=[],arrW_Z=[];
var hotCityContent="",ContentA_G="",ContentH_L="",ContentM_T="",ContentW_Z="";

function creatDate(initialIdx){
	arrA_G=[];arrH_L=[];arrM_T=[];arrW_Z=[];
	hotCityContent="";ContentA_G="";ContentH_L="";ContentM_T="";ContentW_Z="";
	
	//热门城市
	for(var i=0;i<commonCityHot.length;i++){
		hotCityContent+="<li name="+commonCityHot[i][1]+">"+commonCityHot[i][2]+"</li>";
	}
	hotCityContent+="<div class=\"clearFloat\"></div>";
	$(".cityList").eq(0).html(hotCityContent);
	
	if(initialIdx){
		$(".hotCityNav span").show();
		$(".hotCityNav span").eq(1).html(A_G.charAt(0)+"-"+A_G.charAt(A_G.length-1));
		$(".hotCityNav span").eq(2).html(H_L.charAt(0)+"-"+H_L.charAt(H_L.length-1));
		$(".hotCityNav span").eq(3).html(M_T.charAt(0)+"-"+M_T.charAt(M_T.length-1));
		$(".hotCityNav span").eq(4).html(W_Z.charAt(0)+"-"+W_Z.charAt(W_Z.length-1));
		for(var i=0;i<arrAllCity.length;i++){
			cityInx[i]=arrAllCity[i][4].substring(0,1);//取得省市的拼音首字母
			var curCity = cityInx[i].toUpperCase();//把首字母转成大写再比较
			if(A_G.indexOf(curCity)>=0){arrA_G.push(arrAllCity[i])}
			if(H_L.indexOf(curCity)>=0){arrH_L.push(arrAllCity[i])}
			if(M_T.indexOf(curCity)>=0){arrM_T.push(arrAllCity[i])}
			if(W_Z.indexOf(curCity)>=0){arrW_Z.push(arrAllCity[i])}
		}
		//其他城市
		var maxLength=Math.max(arrA_G.length,arrH_L.length,arrM_T.length,arrW_Z.length);
		for(var i=0;i<maxLength;i++){
			if(arrA_G[i]){ContentA_G+="<li name="+arrA_G[i][1]+">"+arrA_G[i][2]+"</li>";}
			if(arrH_L[i]){ContentH_L+="<li name="+arrH_L[i][1]+">"+arrH_L[i][2]+"</li>";}
			if(arrM_T[i]){ContentM_T+="<li name="+arrM_T[i][1]+">"+arrM_T[i][2]+"</li>";}
			if(arrW_Z[i]){ContentW_Z+="<li name="+arrW_Z[i][1]+">"+arrW_Z[i][2]+"</li>";}
		}
	
		ContentA_G+="<div class=\"clearFloat\"></div>";
		ContentH_L+="<div class=\"clearFloat\"></div>";
		ContentM_T+="<div class=\"clearFloat\"></div>";
		ContentW_Z+="<div class=\"clearFloat\"></div>";
		$(".cityList").eq(1).html(ContentA_G);
		$(".cityList").eq(2).html(ContentH_L);
		$(".cityList").eq(3).html(ContentM_T);
		$(".cityList").eq(4).html(ContentW_Z);
	}
	else{
		$(".hotCityNav span").hide();
	}
}

$(function(){
	$(".hotCityNav span").click(function(){
		$(".hotCityNav span").removeClass();
		$(this).addClass("active");
		$(".cityList").hide();
		var indx=$(".hotCityNav span").index(this);
		$(".cityList").eq(indx).show();
		switch(indx){
		case 1:
		$("#txtCityInfo").html("拼音"+A_G.charAt(0)+"-"+A_G.charAt(A_G.length-1)+"城市");
		break;
		case 2:
		$("#txtCityInfo").html("拼音"+H_L.charAt(0)+"-"+H_L.charAt(H_L.length-1)+"城市");
		break;
		case 3:
		$("#txtCityInfo").html("拼音"+M_T.charAt(0)+"-"+M_T.charAt(M_T.length-1)+"城市");
		break;
		case 4:
		$("#txtCityInfo").html("拼音"+W_Z.charAt(0)+"-"+W_Z.charAt(W_Z.length-1)+"城市");
		break;
		default:
		$("#txtCityInfo").html("热门城市");
		break;
		}
	})
	
	$(".cityList li").live("click",function(){
		fillHotCity($(this));
		resetHotCity();
	})
	
	$("#hotCity img").click(function(){
	resetHotCity();					 
	})
})

//插件复位
function resetHotCity(){
	$("#txtCityInfo").html("热门城市");
	$("#hotCity").hide();
	$(".cityList").hide();
	$(".cityList").eq(0).show();	
	$(".hotCityNav span").removeClass();
	$(".hotCityNav span").eq(0).addClass("active");	
}

function fillHotCity(tar){
	if(typeof(tInputId)=="object"){
		tInputId.val(tar.text());
	}
	else{
		$("#"+tInputId).val(tar.text());
	}
	if(typeof(hInputId)=="object"){
		hInputId.val(tar.attr("name"));
	}
	else{
		$("#"+hInputId).val(tar.attr("name"));
	}
	
}



//获取DOM对象的绝对位置
function getAbsPoint(e) {   
    var x = e.offsetLeft;   
    var y = e.offsetTop;   
    while(e = e.offsetParent) {   
        x += e.offsetLeft;   
        y += e.offsetTop;   
    }   
    return {x:x,y:y};   
} 

//给插件div定位
function setPos(thisObj){
	$("#hotCity").show();
	$("#hotCity").css("position","absolute");
	var inputObj=thisObj.prev();//取得当前input对象
	var offset=getAbsPoint(inputObj[0]);
	var X=offset.x+"px";
	var Y=offset.y+inputObj.height()+"px";
	var intX=getAbsPoint(thisObj[0]).x+360-$(window).width();
	if(intX<0){
		$("#hotCity").css("left",X);
		$("#hotCity").css("top",Y);
	}
	else{//修正不同分辨率下控件位置
		X = getAbsPoint(thisObj[0]).x-366+"px"; 
		$("#hotCity").css("left",X);
		$("#hotCity").css("top",Y);
	}
}
