var search_panel = null;
var error_timer = null;
var add_timer = null;
var del_timer = null;

$(function () {

$("#loading").remove();

search_panel = new SearchPanel(1);

var func_search_exec = function () {
    $("#search_form").submit();
};

$("#rtb").change(func_search_exec);
$("#rtt").change(func_search_exec);
$("#ly").change(func_search_exec);
$("#mab").change(func_search_exec);
$("#mat").change(func_search_exec);
$("#bk").change(func_search_exec);
$("#by").change(func_search_exec);
$("#sw").change(func_search_exec);
$("#pd").change(func_search_exec);
$("input:checkbox").click(function () {
    if ($(this).attr('class') == 'cb_room') {
        return;
    } else if ($(this).attr('class') == 'cb_room_all') {
        if ($("#"+$(this).attr('id')+":checked").size()) {
            $(".cb_room").attr('checked', 'checked');
            $(".cb_room_all").attr('checked', 'checked');
        } else {
            $(".cb_room").removeAttr('checked');
            $(".cb_room_all").removeAttr('checked');
        }
    } else {
        func_search_exec();
    }
});

$("#oc_equip").mouseover(function () {
    $(this).css('cursor', 'pointer');
});
$("#oc_equip").click(function () {
    $("#oc_equip > span").toggle();
    $("#other_equip").toggle();
});

$(".btn_inquiry,.btn_inquiry_lump").mouseover(function () {
    $(this).css('cursor', 'pointer');
});
$(".btn_inquiry").click(function (e) {
    $("#form_inquiry").append('<input type="hidden" name="room_com_ids" id="room_com_ids" value="'+e.target.id.substr(11)+'" />');
    $("#form_inquiry").submit();
});
$(".btn_inquiry_lump").click(function (e) {
    if ($(".cb_room:checked").size() == 0) {
        alert('該当の物件にチェックを入れてください。');
        return false;
    }
    if ($("#room_com_ids").size() > 0) $("#room_com_ids").remove();
    $("#form_inquiry").submit();
});

var func_add_favorite_room = function (e, params) {
    $.post('./ajax/add_favorite_room.php', params, function (xml) {
        if ($("#favorite_error").size() > 0) {
            $("#favorite_error").stop();
            $("#favorite_error").remove();
            if (error_timer) {
                clearTimeout(error_timer);
            }
        }
        if (xml) {
            if ($("error", xml).size() > 0) {
                $("body").append(
                    '<div id="favorite_error"'+
                    ' style="position:absolute;top:'+ (e.pageY - 57) +'px;left:'+ (e.pageX - 82) +'px;">'+
                    '<img src="images/body/favorit_noadded.gif" alt="" width="92" height="57" /></div>'
                );
                $("#favorite_error").fadeIn('fast');
                error_timer = setTimeout(function () {
                    $("#favorite_error").fadeOut('slow', function () {$("#favorite_error").remove();});
                    clearTimeout(error_timer);
                }, 1000);
                return false;
            } else {
                $("body").append(
                    '<div id="favorite_add"'+
                    ' style="position:absolute;top:'+ (e.pageY - 57) +'px;left:'+ (e.pageX - 82) +'px;">'+
                    '<img src="images/body/favorit_added.gif" alt="" width="92" height="57" /></div>'
                );
                $("#favorite_add").fadeIn('fast');
                var add_ids = $("success", xml).text().split(',');
                for (var i = 0; i < add_ids.length; i++) {
                    $("#wrap_favorite_btn"+add_ids[i]).html(
                        '<img src="images/body/btn_favorit_added.gif" alt="" width="64" height="17"'+
                        ' id="btn_favorite_del'+ add_ids[i] +'" class="btn_favorite_del" />'
                    );
                    $("#btn_favorite_del"+add_ids[i]).mouseover(function () {
                        $(this).css('cursor', 'pointer');
                    });
                    $("#btn_favorite_del"+add_ids[i]).click(func_del_favorite);
                }
                add_timer = setTimeout(function () {
                    $("#favorite_add").fadeOut('slow', function () {$("#favorite_add").remove();});
                    clearTimeout(add_timer);
                }, 1000);
                
            }
        }
    });
};

$(".btn_favorite,.btn_favorite_lump").mouseover(function () {
    $(this).css('cursor', 'pointer');
});
$(".btn_favorite").click(function (e) {
    func_add_favorite_room(e, {room_com_ids:e.target.id.substr(12)});
});
$(".btn_favorite_lump").click(function (e) {
    if ($(".cb_room:checked").size() == 0) {
        alert('該当の物件にチェックを入れてください。');
        return false;
    }
    func_add_favorite_room(e, $(".cb_room").serialize());
});

var func_del_favorite = function (e) {
    var target_id = e.target.id.substr(16);
    $.post('./ajax/delete_favorite_room.php', {room_com_id:target_id}, function (xml) {
        if ($("#favorite_error").size() > 0) {
            $("#favorite_error").stop();
            $("#favorite_error").remove();
            if (error_timer) {
                clearTimeout(error_timer);
            }
        }
        if (xml) {
            if ($("error", xml).size() > 0) {
                $("body").append(
                    '<div id="favorite_error"'+
                    ' style="width:250px;height:20px;padding:30px 0;border:2px solid #ff0000;'+
                    'background-color:#ffffcc;color:#ff0000;font-weight:bold;text-align:center;'+
                    'position:absolute;top:'+ (e.pageY - 57) +'px;left:'+ (e.pageX - 82) +'px;">'+
                    $("error", xml).text()+'</div>'
                );
                $("#favorite_error").fadeIn('fast');
                error_timer = setTimeout(function () {
                    $("#favorite_error").fadeOut('slow', function () {$("#favorite_error").remove();});
                    clearTimeout(error_timer);
                }, 1000);
                return false;
            } else {
                $("body").append(
                    '<div id="favorite_del"'+
                    ' style="position:absolute;top:'+ (e.pageY - 57) +'px;left:'+ (e.pageX - 82) +'px;">'+
                    '<img src="images/body/favorit_delete.gif" alt="" width="102" height="57" /></div>'
                );
                $("#favorite_del").fadeIn('fast');
                $("#wrap_favorite_btn"+target_id).html(
                    '<img src="images/body/btn_favorit_off.gif" alt="" width="64" height="17"'+
                    ' alt="お気に入りに登録" id="btn_favorite'+ target_id +'" class="btn_favorite" />'
                );
                $("#btn_favorite"+target_id).mouseover(function () {
                    $(this).css('cursor', 'pointer');
                });
                $("#btn_favorite"+target_id).click(function (e) {
                    func_add_favorite_room(e, {room_com_ids:e.target.id.substr(12)});
                });
                del_timer = setTimeout(function () {
                    $("#favorite_del").fadeOut('slow', function () {$("#favorite_del").remove();});
                    clearTimeout(del_timer);
                }, 1000);
            }
        }
    });
}

$(".btn_favorite_del").mouseover(function () {
    $(this).css('cursor', 'pointer');
});
$(".btn_favorite_del").click(func_del_favorite);

});

