<script language="javascript" type="text/javascript">
    var fixedIndexs=$('.fixedIndexs');
    var hs = $('#cnblogs_post_body h2');
    function openorclose(a) {
        $("#indexs").slideToggle("fast");
        var text=$(a).text();
        if(text=='[-]'){
            $(a).text("[+]");
            return;
        }
        $(a).text("[-]");
    }
    function createIndexs(){
        var indexs_container=$('<div style="border:solid 1px #ccc; background:#eee;width:180px;padding:4px 10px;"></div>');
        var indexs_controller=$('<p style="text-align:right;margin:0;"><span style="float:left;">目录<a onclick="javascript:openorclose(this);" style="cursor: pointer">[-]</a></span><a href="#top" style="text-align: right;color: #444">返回顶部</a></p>');
        var indexs=$('<ol id="indexs" style="margin-left: 14px; padding-left: 14px; line-height: 160%; display: block;"></ol>');
        indexs_container.append(indexs_controller).append(indexs);
        $.each(hs,function(i,h){
            $(h).before('<a name="index_'+i+'"></a>');
            indexs.append('<li style="list-style:decimal"><a href="#index_'+i+'" id="active_'+i+'">'+$(h).text()+'</a></li>');
        });
        if(hs.length!=0){
            fixedIndexs.append(indexs_container);
            //get home div right offset
            fixedIndexs.css('right',$("#home").offset().left+32+'px');
        }
    }
    createIndexs();
    $(window).scroll(function(event){
        //clear all active
        $("#indexs li a").removeClass("active");
        //set active
        $.each(hs,function (i, h) {
            var next_active_top;
            i<(hs.length-1)?next_active_top=hs.eq(i+1).offset().top:hs.last().offset().top;
            if($(h).offset().top<$(window).scrollTop()+30&&$(window).scrollTop()+30<next_active_top){
                $("#active_"+i).addClass("active");
            }
            if(i+1==hs.length&&$(window).scrollTop()+30>hs.last().offset().top){
                $("#active_"+i).addClass("active");
            }
        });
        //auto display
        if($(window).scrollTop()>$(window).height()){
            fixedIndexs.show();
            return;
        }
        fixedIndexs.hide();
    });
    $(window).resize(function (event) {
        fixedIndexs.hide();
        fixedIndexs.css('right',$("#home").offset().left+32+'px');
        if($(window).scrollTop()>$(window).height()){
            fixedIndexs.show();
        }
    })
</script>