function bind_print() {
    $("a#print_").bind(
        "click",
         function() {
            var url = $(this).attr("href");
            window.open(url);
            return false; 
        } 
    );
}

function bind_map() {
    $("a.map_").bind(
        "click",
        function() {
            var url = $(this).attr("href");
            window.open(url, null, "height=508, width=570, top=126, left=142");
            return false; 
        } 
    );
}

function bind_add_scrap() {
    // 追加済みの物件の検討リストへ追加するボタンを非アクティブにする関数
    // （コントローラやテンプレートなどの実装に依拠しているアドホックな実装）
    function unactivate() {
        var timestamp = '?timestamp='+(new Date()).getTime();
        $.getJSON('/scrap/view' + timestamp, function(data) {
             var oi2ni = {1: 2, 2: 6, 4: 5};

             $.each(data, function() {
                 $('a.add_scrap@[href$="' + this + '"]').each(function(){
                      var obj = $(this);
                      var img = obj.children('img').clone();
                      var oldsrc = img.attr('src');
                      var oi = oldsrc.match('0([0-9]).gif')[1];
                      var ni = oi2ni[oi];
                      var newsrc = oldsrc.replace(oi, ni);
                      var oldalt = img.attr('alt');
                      var newalt = oldalt + "済";
                      img.attr('src', newsrc).attr('alt', newalt);
                      obj.after(img).remove();
                 });
             });
        });
    }

    function regist(elem, codes) {
        if (codes) {
            if (codes.length == 0) {
                alert("物件を1つ以上チェックしてください。");
                return false; 
            }

            var params = {
                code: $.map(codes, function(node, i) { return $(node).val() })
            };
        } else {
            var params = {};
        }

        var url = $(elem).attr("href");
        $.ajax({
            type: "POST",
            url: url,
            data: params,
            dataType: "text",
            success: function(data, dataType){
                if (data == "1") {
                    alert("検討中リストへ登録しました\n解除する場合は検討中リストから削除してください");
                    unactivate();
                } else {
                    alert("エラー: 検討中リストへの追加に失敗しました");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("エラー: 検討中リストへの追加に失敗しました");
            }
        });
    }

    $("a.add_scrap").bind(
        "click",
        function() {
            regist(this);
            return false;
        } 
    );

    $("a.add_scrap_all").bind(
        "click",
        function() {
            var codes = $("input:checked[name='code']");
            regist(this, codes);
            return false;
        } 
    );

    unactivate();
}

function bind_delete_scrap() {
    $("a.delete_scrap").bind(
        "click",
        function() {
            return confirm("この物件を検討中リストから削除します");
        } 
    );
}

function bind_contact_all() {
    $("#contact_all").bind(
        "submit",
        function() {
            if ($("input:checked[name='code']").length > 10) {
                alert("物件が11件以上チェックされています。まとめてお問合せできるのは10件までです。");
                return false; 
            } else if ($("input:checked[name='code']").length == 0) {
                alert("物件を1つ以上チェックしてください。");
                return false; 
            }
        }
    );
}

function bind_search(form){
    form.bind(
        "submit",
        function() {
            var rent_min = $(":input[name='rent_min'] > option:selected").attr("value");
            var rent_max = $(":input[name='rent_max'] > option:selected").attr("value");
            var footprint_min = $(":input[name='footprint_min'] > option:selected").attr("value");
            var footprint_max = $(":input[name='footprint_max'] > option:selected").attr("value");

            if (rent_min == "") { rent_min = null; }
            if (rent_max == "") { rent_max = null; }
            if (footprint_min == "") { footprint_min = null; }
            if (footprint_max == "") { footprint_max = null; }

            if (rent_min != null) { rent_min = parseInt(rent_min); }
            if (rent_max != null) { rent_max = parseInt(rent_max); }
            if (footprint_min != null) { footprint_min = parseInt(footprint_min); }
            if (footprint_max != null) { footprint_max = parseInt(footprint_max); }

            msg = new Array;
            if (rent_min != null && rent_max != null && rent_min > rent_max) {
              msg.push("賃料の設定を変更してください");
            }
            if (footprint_min != null && footprint_max != null && footprint_min > footprint_max) {
              msg.push("専有面積の設定を変更してください");
            }
            if (msg.length > 0) {
              alert(msg.join("\n"));
              return false;
            }
        }   
    );
}


function bind_panorama() {
    $("#panorama_").bind(
        "click",
        function() {
            var url = $(this).attr("href");
            window.open(url, null, 'width=780, height=580, screenX=10, screenY=10, left=10, top=10, menubar=no, location=no, toolbar=no, scrollbars=no, status=no, directories=no, resizable=no');
            return false;
        }
    );
}

