/*
 * js
 */

$(function () {
    if (! $.browser.safari) {
        $('.pagetop a').click(function () {
            $(this).blur();
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            return false;
        });
    }
    
    //トップ
    //ロールオーバー
    $("#home section#daily ol li,#estate section#bukkenlist ol li").hover(function(){
        $(this).css({
            "background-color":"#f6f6f6"
        });
    },
    function(){
        $(this).css({
            "background-color":"#ffffff"
        });
    });
    
    //トップかるーせる
    if(jQuery.support.opacity){
    //for chrome ff opera etc
    /*
    $('aside#featured article ul li').each(function(){
        $(this).hover(function(){
            $(this).children('.cover').stop().animate({
                'top' : '0px', 
                'left' : '0px',
                'background' : 'none'
            }, 0);
            $('img', this).stop().animate({
                'width' : '80px', 
                'margin-left' : '79px',
                'margin-top' : '24px'
            }, 0);
        },function(){
            $(this).children('.cover').stop().animate({
                'top' : '120px', 
                'left' : '120px'
            }, 0);
            $('img', this).stop().animate({
                'width' : '159px', 
                'margin-left' : '0px',
                'margin-top' : '0px'
            }, 0);
        });
    });
    */

    $('aside#featured article ul li').each(function(){
        $(this).hover(function(){
            //$("a img", this).stop().animate({"opacity":0.3}, 300);
            $(this).children('.cover').css({"background":"#ffffff"});
            $(this).children('.cover').stop().animate({
                'top' : '0px', 
                'left' : '0px'
            }, 300);
        },function(){
            $(this).children('.cover').stop().animate({
                'top' : '120px', 
                'left' : '120px'
            }, 300);
        });
    });

    }else{
        //for IE
        $('aside#featured article ul li').each(function(){
            $(this).hover(function(){
                $(".cover",this).stop().animate({
                    'top' : '0px', 
                    'left' : '0px'
                }, 0);
                $("a img",this).stop().animate({
                    'width' : '80px', 
                    'margin-left' : '79px',
                    'margin-top' : '45px'
                }, 0);
            },function(){
                $(".cover",this).stop().animate({
                    'top' : '120px', 
                    'left' : '120px'
                }, 0);
                $("a img",this).stop().animate({
                    'width' : '159px', 
                    'margin-left' : '0px',
                    'margin-top' : '0px'
                }, 0);
            });
        });
    }

    //画像無しの場合（テスト中）
    $(function () {
        $('img.th').error(function(){
            $(this).attr({
                src:'/common/images/missing.jpg',
                alt:'画像が見つかりません'
            });
        });
    });

    //------------------------------------------------------
    //物件検索
    //------------------------------------------------------
    
    estate_hold_check();
    
    //検索条件チェック
    $("input#checkall_station,input#checkall_label").click(function(){
        var myid = $(this).attr("id");
        var checked = $(this).attr("checked");
        if(checked){
            $("ul." + myid + " input").each(function(){
                $(this).attr({
                    "checked":true
                });
            });
        }else{
            $("ul." + myid + " input").each(function(){
                $(this).attr({
                    "checked":false
                });
            });
        }
    });
    
    //検索結果へのリンク
    $("#estate_search ol#posts-list li").each(function(){
        $(".linkbox", this).click(function(){
            //window.location=$(this).find("p.seedetail a").attr("href");
            //return false;
        }); 
    });
    
    //検索結果へソートオプション
    $("#condition_opt p span").each(function(){
        $("a.sort", this).click(function(){
            $("#condition_opt form input#opt_id").val($(this).parent().attr("id"));
            $("#condition_opt form input#opt_class").val($(this).attr("title"));
            $("#condition_opt form").submit();
            return false;
        })
    });
         
    $("p.addlist").each(function(){
        $("a", this).click(function(){
            //リストを作成
            //var ehtitle = $(this).attr("title");
            //var ehid = $(this).parent().attr("id");
            var op = "add";
            var adddata = {
                "op":op, 
                "id":$(this).parent().attr("id"), 
                "title":$(this).attr("title")
            };
            var deldata = {};
            //var response = '';
            $.ajax({
                type: "POST",
                url: "./index.php?m=data",
                data: adddata,
                dataType: "xml",
                success: function(xml){                        
                    //リストデータ削除
                    $("#estate_hold ul li").each(function(){
                        $(this).remove();
                    });
                        
                    var count = $(xml).find("item").size();
                    $("#estate_hold h2 em").text(count);
                    
                    $(xml).find("item").each(function(){
                        //response += $(this).attr("id") + $(this).text();

                        var ehtitle = $(this).text();
                        var ehid = "eh" + $(this).attr("id");
                        var listid = "list" + ehid;
                        
                        if($("#estate_hold h2").attr("class") == "show"){
                            //リスト追加
                            if($("#estate_hold ul").length == 0){
                                //alert('ないよ');
                                $("#estate_hold").append('<ul></ul>');
                            }
                            $("#estate_hold ul").append("<li id='" + listid + "' class='" + ehid + "'><a href='#' class='holdlist'>" + ehtitle + "</a>&nbsp;<span class='dellist'><a href='#'>[×]</a></span></li>");

                            estate_hold_check();
                        }
                    });
                }//success;
            });
            
            $(this).css({
                "display":"none"
            });
            return false;
        });
    });
    
    //------------------------------------------------------
    //物件詳細
    //------------------------------------------------------
    //
    //ロールオーバー
    $("#estate_detail #detail2 #photobox a").each(function(){
        $(this).hover(function(){
            $("img", this).animate({
                "opacity":0.7
            });
        },function(){
            $("img", this).animate({
                "opacity":1.0
            });
        });
    });
});

function estate_hold_check(){    
    $("#estate_hold ul li").each(function(){    
        var ehid = $(this).attr("class");
        
        //削除ボタンを表示
        $(this).hover(function(){
            $("span.dellist a", this).stop().css({
                "color":"#ff0000"
            });
        },function(){
            $("span.dellist a", this).stop().css({
                "color":"#000000"
            });
        });

        //削除ボタンを表示
        $("span.dellist a" ,this).click(function(){
            $("p#" + ehid + " a").css({
                "display":"inline"
            });
            
            var count = parseInt($("#estate_hold h2 em").text()) - 1;
            $("#estate_hold h2 em").text(count);
                            
            var op = "del";
            deldata = {
                "op":op, 
                "id":ehid
            };
            $.ajax({
                type: "POST",
                url: "./index.php?m=data",
                data: deldata,
                dataType: "text",
                success: function(msg){                                      
                    $("#estate_hold li#list" + ehid).remove();
                    $("#estate_search li.fav" + ehid).remove();//お気に入りページのみ
                    if($("#estate_hold ul li").length == 0){
                        $("#estate_hold ul").remove();
                    }
                }
            });
            return false;
        });
        
    });
    
}

function myfunction(){
    return false;
}
