/*
 * 検索ページ
 */

$(document).ready(function () {
    var cancelButtonHtml = '<img alt="" src="/content/images/cancel.gif" class="cancel">';

    // 現在の条件で指定されているものを input に適応する
    function setCurrentSearchState() {
        // 病名
        $("div.search-current div.disease div img.cancel:visible").each(function () {
            var diseaseID = $(this).parent().attr("id").split("-")[1];

            $("#disease-" + diseaseID).attr("checked", true);
            $("#table-disease label." + diseaseID + " span").addClass("on");
        });

        // 住所
        $("div.search-current div.address img.cancel").each(function () {
            // addressID の例 : adr4
            var addressID = $(this).parent().attr("id").split("-")[1];

            $("#" + addressID).attr("checked", true);
            $("label[for='" + addressID + "']").addClass("selected");
        });

        // 拠点病院
        $(".search-current .medical img.cancel").each(function () {
            // medicalID の例 : m26
            var medicalID = $(this).parent().attr("id").split("-")[1];

            $("#" + medicalID).attr("checked", true);
            $("label[for='" + medicalID + "']").addClass("selected");
        });
    }

    // 病名選択エリア
    $("#table-disease input:checkbox").click(function (event) {
        if (this.checked == true) {
            switchImageDisease($(this).attr("id").split("-")[1], "on");
        } else {
            switchImageDisease($(this).attr('id').split('-')[1], 'off');
        }
    });
    // 病名条件表示エリア
    $("div.disease img.cancel").click(function () {
        switchImageDisease($(this).parent().attr("id").split("-")[1], "off");
    });


    /*
    * 病気の表示画像切り替え(選択エリア内の画像と条件エリア内の画像を変更)
    * @param string targetID イベント発火元のID
    * @param string stateTo 変更先の状態(onとoff)
    */
    function switchImageDisease(targetID, stateTo) {
        var imgCancel = $("div#cancel-" + targetID + " img.cancel");
        var imgDisease = $("div#cancel-" + targetID + " img:not(.cancel)");
        var imgBaseName = "cancer" + targetID.substring(1);
        //alert(imgBaseName);

        switch (stateTo) {
            case "on":
                $("#table-disease label." + targetID + " span").addClass("on");
                imgCancel.css("display", "block");
                imgDiseaseSrc = "/Content/images/" + imgBaseName + "_on.png";
                break;
            case "off":
                $("input#disease-" + targetID).attr("checked", "");
                $("#table-disease label." + targetID + " span").removeClass("on");
                imgCancel.css("display", "none");
                imgDiseaseSrc = "/content/images/" + imgBaseName + ".png";
                break;
            default:
                break;
        }
        imgDisease.attr("src", imgDiseaseSrc);
    }


    // 地域選択エリア
    $("#table-address input:checkbox").click(function () {
        var cnum = $("#table-address input:checkbox:checked").length;
        if (cnum > 3) {
            alert("地域は3箇所まで選択できます");
            return false;
        } else {
            if ($(this).attr('checked') == true) {
                // 選択する場合
                $(this).parent().addClass("selected");
                $("div.search-current div.address").append(
                    '<div id="' + 'cancel-' + $(this).attr("id") + '">' +
                    '<p>' + $(this).parentsUntil("ul").parent().siblings("h3").text() + $(this).prev().text() + '</p>' +
                    cancelButtonHtml +
                    '</div>'
                );

                $("#adr-no-select").hide();
            } else {
                // 選択を解除する場合

                $(this).parent().removeClass("selected");
                $("div#cancel-" + $(this).attr("id")).remove();

                // 最後の一つを解除した場合は「指定されていません」という文言を表示します
                if (cnum <= 0) {
                    $("#adr-no-select").show();
                }
            }
            return true;
        }
    });

    $("div.search-current div.address img.cancel").live("click", function () {
        // 選択を解除する場合

        $("input#" + $(this).parent().attr("id").split("-")[1]).attr("checked", "");
        $(this).parent().remove();
        $("input#" + $(this).parent().attr("id").split("-")[1]).parent().removeClass("selected");

        // 最後の一つを解除した場合は「指定されていません」という文言を表示します
        var cnum = $("#table-address input:checkbox:checked").length;
        if (cnum <= 0) {
            $("#adr-no-select").show();
        }
    });


    // 拠点病院選択エリア
    $("#table-medical input:checkbox").click(function () {
        var cnum = $("#table-medical input:checkbox:checked").length;
        if (cnum > 1) {
            alert("拠点病院は1つのみ選択できます");
            return false;
        } else {
            if ($(this).attr('checked') == true) {
                $(this).parent().addClass("selected");
                $(".search-current .medical").append(
                    '<div id="' + 'cancel-' + $(this).attr("id") + '">' +
                    '<p>' + $(this).prev().text() + '</p>' +
                    cancelButtonHtml +
                    '</div>'
                );

                $("#med-no-select").hide();
            } else {
                $(this).parent().removeClass("selected");
                $("div#cancel-" + $(this).attr("id")).remove();

                // 最後の一つを解除した場合は「指定されていません」という文言を表示します
                if (cnum <= 0) {
                    $("#med-no-select").show();
                }
            }
            return true;
        }
    });

    $(".search-current .medical img.cancel").live("click", function () {
        var inputID = $("input#" + $(this).parent().attr("id").split("-")[1]);
        inputID.attr("checked", "");
        inputID.parent().removeClass("selected");
        $(this).parent().remove();

        // 最後の一つを解除した場合は「指定されていません」という文言を表示します
        var cnum = $("#table-medical input:checkbox:checked").length;
        if (cnum <= 0) {
            $("#med-no-select").show();
        }
    });

    // 検索フォーム(条件選択エリア)のinput:checkboxのチェックをはずす
    $("#search-input-form input:checkbox").each(function () {
        $(this).attr("checked", false);
    });

    // 現在の条件で指定されているものを input に適応する
    setCurrentSearchState();

    // 「この条件で検索する」ボタンのホバー画像を設定する
    FCLibrary.hoverBackground(
      ".search div.search-current input",
      "/content/images/search_button_hover.gif"
    );

    // IEのみ戻るボタンに関する注意書きを表示する
    if (!jQuery.support.htmlSerialize) {
        $("p.warn-search").css("display", "block");
    }

    function l(str) {
        console.log(str);
    }

});

