//gallery slide
$(function(){
    var timerID;
    
    var $ul = $('div.slide_window ul.gallery');
    var move = $ul.children('li').outerWidth({margin:true});
    
    $ul.css("width", move * ($ul.children('li').size() + 1)+"px");//li.width*数
    //$ul.children('li:last').prependTo($ul);//最後のカラムを先頭へ移動
    $ul.css("margin-left", -move + "px");//移動

    $.gallery_slide = {
        prev: function(){
            $ul.animate({
                    marginLeft:parseInt($ul.css("margin-left")) + move + "px"
                },"normal","swing",
                function(){
                    $ul.css("margin-left", -move + "px");
                    $ul.children('li:last').prependTo($ul);
                    $('div.topimage p.pict').html($ul.children('li:first').html());
                    $('div.topimage p.pict img').attr({width:'360', height:'270'});
                    $('div.topimage h3').html($('div.topimage p.pict div.overture').html());
            });
        },
        next: function(){
            $ul.children('li:first').clone(true).appendTo($ul);
            $ul.animate({
                    marginLeft:parseInt($ul.css("margin-left")) -move + "px"
                },"normal","swing",
                function(){
                    $ul.css("margin-left", -move + "px");
                    
                    
                    $ul.children('li:first').remove();
                    $('div.topimage p.pict').html($ul.children('li:first').html());
                    $('div.topimage p.pict img').attr({width:'360', height:'270'});
                    $('div.topimage h3').html($('div.topimage p.pict div.overture').html());
                    
            });
        }
    };

    
    $ul.children('li').each(function(){
        $pop = $('<span>');
        $pop.addClass('pop');
        $pop.html($(this).find('img').attr('alt'));
        $pop.css('opacity', '0.7');
        $(this).children('a').append($pop);
    });
    $ul.children('li').hover(function(){
        $(this).find('span.pop').show();
    },function(){
        $(this).find('span.pop').hide();
    });
    
    $('div.topimage p.arrow_left').hover(function(){
        clearInterval(timerID);
    },function(){
        timerID = setInterval(function(){
            $.gallery_slide.next();
        },7000);
    }).click(function(){
        if(! $ul.is(':animated')){
            $.gallery_slide.next();
        }
    });
    
    $('div.topimage p.arrow_right').hover(function(){
        clearInterval(timerID);
    },function(){
        timerID = setInterval(function(){
            $.gallery_slide.next();
        },7000);
    }).click(function(){
        if(! $ul.is(':animated')){
            $.gallery_slide.prev();
        }
    });
    
    $ul.hover(function(){
        clearInterval(timerID);
    }, function(){
        timerID = setInterval(function(){
            $.gallery_slide.next();
        },7000);
    });
    $('div.topimage p.pict').hover(function(){
        clearInterval(timerID);
    }, function(){
        timerID = setInterval(function(){
            $.gallery_slide.next();
        },7000);
    });
    timerID = setInterval(function(){
        $.gallery_slide.next();
    }, 7000);
});



$(function () {
    $img = $('<img>')
        .attr({
            src:'img/navi_extra_top_off.png',
            alt:'このページのトップへ',
            id:'back_top'
        })
        .css({
            cursor:'pointer',
            marginLeft:'-90px',
            position:'fixed',
            zIndex:'9999',
            bottom:'30px',
            display:'none'
        });
    $img.appendTo('#contents');
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
             $('#back_top').fadeIn();
        } else {
            $('#back_top').fadeOut();
        }
    });
    $('#back_top').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 900);
        return false;
    });
});

// グラフ制御
var workArray = {};
var ghArray   = {};
var conf = {
    trg_class    : "gh_art",
    gh_num_class : "gh_art_num",
    ac_speed     : 10,
    wd_upnum     : 2
}
function ghArt(){
    var stop = 0;
    var wd   = 0;

    var cnt  = 0;
    // グラフ
    $("."+conf.trg_class+" img").each(function(){
        workArray[cnt] = {gh : parseInt($(this).attr("width")), num : 0};
        $(this).attr("width", "0");
        cnt++;
    });
    // アクセス数
    cnt = 0;
    $("."+conf.gh_num_class).each(function(){
        workArray[cnt]["num"] = $(this).html();
        $(this).html(0);
        cnt++;
    });

    // グラフ
    $("."+conf.trg_class+" img").each(function(){
        $(this).attr("width", "0");
        cnt++;
    });
    // アクセス数
    cnt = 0;
    $("."+conf.gh_num_class).each(function(){
        $(this).html(0);
        cnt++;
    });

    var myTimer = $.timer();
    myTimer.stop();
    myTimer.set({
        action : function() {
            stop = 0;
            cnt = 0;
            // グラフ
            $("."+conf.trg_class+" img").each(function(){
                wd = wd+ conf.wd_upnum * 0.1;
                if(workArray[cnt]["gh"] > parseInt($(this).attr("width"))+wd ){
                    $(this).attr("width", parseInt($(this).attr("width")) + Math.floor(wd));
                    ghArray[cnt] = parseInt($(this).attr("width"));
                    stop = 1;
                }else{
                    $(this).attr("width", workArray[cnt]["gh"]);
                    ghArray[cnt] = parseInt(workArray[cnt]["gh"]);
                    $(".",conf.gh_num_class).val(workArray[cnt]["num"]);
                }
                cnt++;
            });
            // アクセス数
            cnt = 0;
            $("."+conf.gh_num_class).each(function(){
                rate = parseInt(ghArray[cnt]) / parseInt(workArray[cnt]["gh"]);
                $(this).html(Math.floor(parseInt(workArray[cnt]["num"]) * rate) );
                cnt++;
            });

            if(stop == 0) myTimer.stop();
        },
        time : conf.ac_speed
    }).play(); 
};

