(function($, undefined) {
    $.fn.extend({
        img_rollover: function(options) {

            /* 初期値の設定 */
            options = $.extend({
                suffix_out:  '_off',
                suffix_over: '_on'
                }, options);

            return this.each(
                function () {
                    var target = $(this);
                    var src    = { out: target.attr('src') };

                    /* src属性が空、もしくは存在しない場合、ロールオーバさせない */
                    if (!src) { return; }

                    /* マウスオーバー画像名取得 */
                    var _src_over
                        = src.out.match(
                            new RegExp('^(.+)(' + options.suffix_out + '){1}(\\..+)$')
                            );
                    /* ファイル名が正しくない場合、ロールオーバさせない */
                    if (!_src_over) { return; }
                    src.over = _src_over[1] + options.suffix_over + _src_over[3];

                    /* マウスオーバー画像のプリロード処理 */
                    $('<img>').attr('src', src.over);

                    target.hover(
                        function () { target.attr('src', src.over); },
                        function () { target.attr('src', src.out); });
                });
        }
    });
})(jQuery);


/* fancybox表示制御 */
$(function () {
    if (!$().fancybox) { return; }

    var groupies = $('[data-groupe^=photos]');
    $.each(groupies,
           function () {
               var groupe = $(this);
               var rel    = groupe.attr('data-groupe');
               $.each(groupe.find('a'),
                      function () {
                          $(this).attr('rel', rel);
                      });

               $('a[rel=' + rel + ']').fancybox({
                   'transitionIn'  : 'none',
                   'transitionOut' : 'none',
                   'titlePosition' : 'over',
                   'titleFormat'   :
                       function(title, currentArray, currentIndex, currentOpts) {
                           return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
                       }
               });

               $('a[rel=' + rel + ']:gt(0)').css({display: 'none'});
           });

});


$(function () {
    /* 画像のみロールオーバーさせる */
    $('img.rollover').img_rollover();
    $('img.rollover').mouseout();/* Fx対応 */


    /* div.main、div.sideの高さを揃える */
/*    if (!$('body').hasClass('single_pane')) {
        (function () {
            var num = !!($('body').attr('data-layout') === 'homepage') ? 0 : 55;

            var height_main = $('#content div.main').height();
            var height_side = $('#content div.side div.inner').height();
            var height = height_main - height_side + num;

            if (height > 0) {
                $('#content div.main').height(height_main);
                $('#content div.side div.inner').height(height_main + 55);
            }
            else {
                $('#content div.main').height(height_side - 55);
                $('#content div.side div.inner').height(height_side);
            }
        })();
    }*/


    /* グローバルナビの表示制御 */
    (function () {
        if (!$('body').attr('data-current')) { return; }

        var target = $('#global_navigator li:nth-child(' + (Number($('body').attr('data-current')) + 1) + ') img');
        target.mouseover();
        target.unbind('mouseout');
    })();


    /* go_pagetop */
    $('#pagetop span, #pagetopNEW span, .mainpagetop span').click(function () {
        window.scrollTo(0, 0);
    });
});


/* サイドナビの表示制御 */
$(function() {
    (function() {
        if (!$('body').attr('data-sub-current')) { return; }

        var target
            = $('#content div.side div.menu_box li:nth-child(' + $('body').attr('data-sub-current') + ') img');
        if (target.length === 0) { return; }

        var _src
            = target.attr('src').match(
                new RegExp('^(.+)(_o(?:ff|n)){1}(\\..+)$')
                );
        target.attr('src', _src[1] + '_act' + _src[3]);

        target.unbind('mouseout');
        target.unbind('mouseover');
        })();
});

