﻿// 机票查询框 公共JScript脚本
var MousOnCity = false;

//选择城市
function ShowSearchDiv(obj){
    var Offset  = $(obj).offset();
    var DIVLeft = Offset.left;
    var DIVTop  = Offset.top + 20;
    $("#CityDIV").show();
    $("#CityDIV").css({left:DIVLeft+"px",top:DIVTop+"px"});
    $("#CityDIV").attr("too",$(obj).attr("id"));
}
function CityListEvent(){
    $("#CityList li").mouseover(function(){
        $(this).css({"background-color":"#e7f1fd","border-top":"solid 1px #7f9db9","border-bottom":"solid 1px #7f9db9"});
        MousOnCity = true;
    })
    .mouseout(function(){
        $(this).css({"background-color":"","border-top":"solid 1px #FFF","border-bottom":"solid 1px #FFF"});
        MousOnCity = false;
    })
    .click(function(){
        var ObjID = $("#CityDIV").attr("too");
        if(ObjID == "CfCity"){
            $("#CfCity").val($(this).find("span").html());
            $("#Chufa").val($(this).attr("id"));
        }else{
            $("#DdCity").val($(this).find("span").html());
            $("#Daoda").val($(this).attr("id"));
        }
        $("#CityDIV").hide();
    });
}
//获取城市列表
function GetCityList(SKW){
    $.getJSON("./Handler/AirPort.aspx?callback=?",{KW:SKW},function(data){
        var ListCount = 0;
        var ListHTML = "";
        if($(data)[0].RT == 0){
            //alert("无可用数据(x001)");
        }else{
            $.each(data, function(i,item){
                if(i > 0){
                    ListHTML += "<li id=\""+item.CD+"\"><span>"+item.NM+"</span>"+item.PY+"</li>";
                    ListCount++;
                }
            });
            if(ListCount>7){
                var CurH = ListCount * 23;
                $("#CityDIV").css("height",CurH+"px");
            }else{
                $("#CityDIV").css("height","160px");
            }
            $("#CityList").html(ListHTML);
        }
        CityListEvent();
     });
}
//自动填充单个城市
function InputCity(iObj,vObj) {
    var SKW = $(iObj).val();
    var KWT = "zhongwen"; //zhongwen:中文名；yingwen:英文名；pinyin:拼音；daima:城市三字码；kong:空字符
    if (SKW == null || SKW == "") KWT = "kong";
    if (KWT == "kong") {
        $(vObj).val("");
    } else {
        $.getJSON("./Handler/OneAirPort.aspx?callback=?", { KW: SKW, KT: KWT }, function (data) {
            if ($(data)[0].RT == 0) {
                //alert("无可用数据(x001)");
            } else {
                var Item = $(data)[1];
                $(vObj).val(Item.CD);
                $(iObj).val(Item.NM);
            }
        });
    }
}

//加载日历控件
function ShowCalendar(obj){
	$('#'+obj).datepicker({
		numberOfMonths: 2,
		showButtonPanel: true,
		dateFormat:"yy-mm-dd",
		minDate: 0,
		showAnim:""
	});
}

//初始化加载
$(document).ready(function () {
    $("#CfCity").focus(function () { ShowSearchDiv($(this)); })
    .keyup(function () {
        var SKW = $(this).val();
        if (SKW != "") GetCityList(SKW);
    })
    .blur(function () {
        var FocusObj = document.activeElement;
        var CurFocus = document.activeElement.id;
        //if(!(CurFocus=="CityDIV" || CurFocus=="CityList") && $(FocusObj).parent().parent().attr("id")!="CityList")
        //    $("#CityDIV").hide();
        if (!(CurFocus == "CfCity" || MousOnCity == true)) $("#CityDIV").hide();
    })
    .change(function () {
        InputCity($(this), $("#Chufa"));
    });
    $("#DdCity").focus(function () { ShowSearchDiv($(this)); })
    .keyup(function () {
        var SKW = $(this).val();
        if (SKW != "") GetCityList(SKW);
    })
    .blur(function () {
        var FocusObj = document.activeElement;
        var CurFocus = document.activeElement.id;
        //if(!(CurFocus=="CityDIV" || CurFocus=="CityList") && $(FocusObj).parent().parent().attr("id")!="CityList")
        //    $("#CityDIV").hide();
        if (!(CurFocus == "DdCity" || MousOnCity == true)) $("#CityDIV").hide();
    })
    .change(function () {
        InputCity($(this), $("#Daoda"));
    });
    $("#CityDIV").blur(function () {
        var CurFocus = document.activeElement.id;
        if (!(CurFocus == "CfCity" || CurFocus == "DdCity"))
            $("#CityDIV").hide();
    });

    CityListEvent();

    $("#CfCity").val("重庆");
    $("#Chufa").val("CKG");
    ShowCalendar("CfDate");
    ShowCalendar("FcDate");
});

