﻿function tabChange(b, id) {
    if (b.className.indexOf("-") > -1)
        return;
    var bList = $(b.parentNode.parentNode).find("b");

    for (var i = 0; i < bList.length; i++) {
        var l = bList[i];
        var css = l.className;
        var index = css.indexOf("-");
        if (index > -1)
            l.className = css.substring(0, index);
    }
    b.className = b.className + "-current";

    var visibleDiv = $("div.searchDIV").children("div:visible").get(0);
    var showDiv = $("#" + id);

    if (visibleDiv) {
        $(visibleDiv).animate({ marginLeft: '-400px' }, "fast", function () {
            $(this).hide();
            showDiv.css("marginLeft", "400px");
            showDiv.show();
            showDiv.animate({ marginLeft: '0px' }, "fast");
        });
    }
    return;
}

function openInfoDIV(txt, id, loadId, typeID, txtID, txtHtml, txtStationDIV) {

    if (txtID == "downStation") {
        fiterCity = 1;
    }

    id = "#" + id;
    var div = $(id);
    var pos = $(txt).position();

    div.css("top", (pos.top + 24) + "px");
    div.css("left", pos.left + "px");
    div.show();

    $(document).mousedown(function (e) {
        documentMouseDown(e, id);
    });


    if (loadId) {
        var txtVal = $("#" + loadId).val();
        $.ajax({
            async: false,
            type: "post",
            url: "/index.aspx?ajax=0&getStation=" + (typeID == 1 ? 1 : 0) + "&cityName=" + escape(txtVal),
            dataType: "html",
            success: function (data) {
                if (data == "")
                    hideDIV(id);
                else
                    $(div).html(data);
            }
        });
    }

    div.find("a").unbind("click");
    div.find("a").click(function () {
        var val = $(this).html();
        if (txt.value != val) {
            $(txt).val(val);
            if (txtID) {
                $("#" + txtID).val(txtHtml);
                $("#" + txtStationDIV).html("");
                var downcity = $("#downCity");
                if (txtID == "upStation" && this.outerText == "香港") {
                    if (downcity.val() == "香港") {
                        downcity.val("广州");
                    }
                }
                else if (txtID == "upStation" && this.outerText != "香港") {
                    if (downcity.val() != "香港") {
                        downcity.val("香港");
                    }
                }
            }
        }
        hideDIV(id);
    });
    div.find("a.active").attr("class", "");
    var a = div.find("a:contains('" + txt.value + "')").get(0);
    if (a) {
        a.className = "active";
    }
}

function documentMouseDown(e, id) {
    e = e ? e : window.event;
    var tag = e.srcElement ? e.srcElement : e.target;

    var div = $(id).get(0);
    if (tag.id == id)
        return;

    var parent = tag.parentNode;
    while (parent) {
        if (parent == div)
            return;
        parent = parent.parentNode;
    }
    hideDIV(id);
}

function hideDIV(id) {
    $(document).mousedown(function () { });
    $(id).hide();
}

function changeInputInfo(upId, upSId, downId, downSId, ucHint, udHint, dcHint, ddHint) {
    var upCity = $("#" + upId);
    var upStation = $("#" + upSId);
    var downCity = $("#" + downId);
    var downStation = $("#" + downSId);
    var upCityText = upCity.val();
    var upStationText = upStation.val();
    var downCityText = downCity.val();
    var downStationText = downStation.val();

    if (downCityText == dcHint)
        upCity.val(ucHint)
    else
        upCity.val(downCityText);

    if (downStationText == ddHint)
        upStation.val(udHint);
    else
        upStation.val(downStationText);

    if (upCityText == ucHint)
        downCity.val(dcHint);
    else
        downCity.val(upCityText);

    if (upStationText == udHint)
        downStation.val(ddHint);
    else
        downStation.val(upStationText);
    return;
}

function submitSearch(upStationText, downStationText, val) {
    var date = fid("lineDate").value;
    var txtGo = fid("upStation").value;
    var txtTo = fid("downStation").value;
    var txtGoCiyt = fid("upCity").value;
    var txtToCiyt = fid("downCity").value;

    if (val == 1) {
        txtGo = fid("upStationGD").value;
        txtTo = fid("downStationGD").value;
        txtGoCiyt = fid("upCityGD").value == "请选择出发城市..." ? "" : fid("upCityGD").value;
        txtToCiyt = fid("downCityGD").value == "请选择到达城市..." ? "" : fid("downCityGD").value;
        date = "";
    }
    //出发、到达站点为空不执行
    //    if (txtGo == upStationText && txtTo == downStationText)
    //        return;

    if (txtGo == upStationText)
        txtGo = "";

    if (txtTo == downStationText)
        txtTo = "";
    window.location.href = "/SearchLine.aspx?d=" + escape(date) + "&g=" + escape(txtGo) + "&t=" + escape(txtTo) + "&tag=" + escape(val) + "&gc=" + escape(txtGoCiyt) + "&tc=" + escape(txtToCiyt);
}

function changeLineType(input, val) {
    if (input.checked) {
        var idHK = "#searchLineHK,#searchLineHKTel";
        var idGD = "#searchLineGD,#searchLineGDTel"

        if (val == 1) {
            idGD = "#searchLineHK,#searchLineHKTel";
            idHK = "#searchLineGD,#searchLineGDTel";
        }
        $(idHK).show();
        $(idGD).hide();
    }
}
