﻿//HF Index Search Box Class
(function () {
    var SB = function (citydiv, citytxt, areadiv, areatxt, areatype, diff_x, diff_y, orig_x, orig_y, pagename) {
        this.citydiv = citydiv; this.citytxt = citytxt;
        this.areadiv = areadiv; this.areatxt = areatxt;
        this.areatype = areatype;
        this.diff_x = diff_x; this.diff_y = diff_y; this.orig_x = orig_x; this.orig_y = orig_y;
        this.pagename = pagename;
    };
    SB.prototype.initialization = function () {
        CitySelect(this.citydiv, this.citytxt, this.diff_x, this.diff_y, this.orig_x, this.orig_y, this.pagename);
        AreaSelect(this.citytxt, this.areadiv, this.areatxt, this.diff_x, this.diff_y, this.orig_x, this.orig_y, this.areatype, this.pagename);
    };
    var CitySelect = function (citydiv, citytxt, x, y, ox, oy, pagename) {
        $("#" + citydiv).click(function () {
            if ($("#div_city_house").css("display") == "none") {
                GetCityData($("#hd_city").val(), pagename);
                ShowOptionDiv(citytxt, "div_city_house", x, y, ox, oy);
            }
            if ($("#div_area_house").css("display") == "block") { $("#div_area_house").hide(); }
        });
    };

    var AreaSelect = function (citytxt, areadiv, areatxt, x, y, ox, oy, areatype, pagename) {
        $("#" + areadiv).click(function () {
            var city = $("#" + citytxt).html();
            if (city.replace("縣　市", "") == "") {
                if ($('#div_city_house').css("display") == "none") {
                    GetCityData($("#hd_city").val(), pagename);
                    ShowOptionDiv(citytxt, "div_city_house", x, y, ox, oy);
                }
            }
            else {
                if ($("#div_area_house").css("display") == "none") {
                    GetAreaData($("#hd_city").val(), $("#hd_area").val(), areatype, pagename);
                    ShowOptionDiv(areatxt, "div_area_house", this.diff_x, this.diff_y, this.orig_x, this.orig_y);
                }
                if ($("#div_city_house").css("display") == "block") { $("#div_city_house").hide(); }
            }
        });
    };
    var GetCityData = function (cityname, pagename) {
        $.ajax({
            url: "../../Controls/HF_Index/GetLocation.ashx",
            type: "POST", dataType: "html", cache: false,
            data: { type: "1", cityname: cityname, pagename: pagename },
            success: function (html) { $("#div_citycontent_house").html(html); }
        });
    };
    var GetAreaData = function (cityname, areaname, method, pagename) {
        $.ajax({
            url: "../../Controls/HF_Index/GetLocation.ashx",
            type: "POST", dataType: "html", cache: false,
            data: { type: "2", cityname: cityname, areaname: areaname, method: method, pagename: pagename },
            success: function (html) { $("#div_areacontent_house").html(html); }
        });
    };
    SB.prototype.ClickCity = function (cityname, cityid) {
        $("#" + this.citytxt).html(cityname);
        $("#hd_city").val(cityname);
        $("#hd_cityid").val(cityid);
        if (this.areatype == '1') { $("#" + this.areatxt).html("行政區"); };
        if (this.areatype == '2') { $("#" + this.areatxt).html("捷運"); };
        if (this.areatype == '3') { $("#" + this.areatxt).html("行政區/捷運"); };
        $("#hd_area").val("");
        $("#hd_areaid").val("");
        $("#div_city_house").hide();
        GetAreaData($("#hd_city").val(), $("#hd_area").val(), this.areatype, this.pagename);
        ShowOptionDiv(this.areatxt, "div_area_house", this.diff_x, this.diff_y, this.orig_x, this.orig_y);
    };
    SB.prototype.ClickArea = function (type, areaname, areaid) {
        $("#" + this.areatxt).html(areaname);
        $("#hd_area").val(areaname);
        $("#hd_areaid").val(areaid);
        $("#hd_type").val(type);
        if ($("#div_area_house").css("display") == "block") { $("#div_area_house").hide(); };
        $("#div_city_area").hide();
        $("#txt_keyword_h1").removeAttr("disabled");
    };
    var ShowOptionDiv = function (otxt, odiv, x, y, ox, oy) {
        var diff_x = 0; var diff_y = 0; var orig_x = ox; var orig_y = oy;
        if ($.browser.msie) { diff_x = x; diff_y = y };
        var offset = $("#" + otxt).position();
        offset.top += 43;
        $("#" + odiv).css({
            top: parseInt(offset.top, 10) + parseInt(diff_y, 10) + parseInt(orig_y, 10) + "px",
            left: parseInt(offset.left, 10) + parseInt(diff_x, 10) + parseInt(orig_x, 10) + "px"
        }).show();
    };
    _SearchBox = SB;
})();
