$(function ( ){
    ACMS.Config.themesRoot   = $('script[src$="top_nucba.js"]').attr('src').replace(/js\/top_nucba\.js$/, '');
		
		// galleryview
    var $galleryview    = $('#galleryview');
    if ( $galleryview.size() ) {
        ACMS.Load(ACMS.Config.themesRoot + 'js/galleryview/jquery.easing.1.3.js')();
        ACMS.Load(ACMS.Config.themesRoot + 'js/galleryview/jquery.galleryview-2.1.1.js')();
        ACMS.Load(ACMS.Config.themesRoot + 'js/galleryview/jquery.timers-1.2.js')();

        $galleryview.galleryView({
            nav_theme       : 'light',
            pointer_size    : 0,
            panel_width     : 940,
            panel_height    : 300,
            frame_width     : 63,
            frame_height    : 18,
            frame_gap       : 10,
            pause_on_hover  : true
        });
    }
});

// 全ページに適用
dispatcher('.*', function() {
													
		//-----
    // nav
    var $nav    = $('#nav, #sideMune');
    if ( $nav.size() ) {
        $('li:has(ul)', $nav).addClass('branch'); 
        $('li', $nav).hover(function ( )
        {
            $(this).children('a').addClass('hover');
            $('ul:first', this).fadeIn();
        }, function ( )
        {
            $(this).children('a').removeClass('hover');
            $('ul:first', this).fadeOut('fast');
        });
        $('ul', $nav).each(function ( )
        {
            var level    = $(this).parents('ul').size();
            $(this).addClass('level' + level);
        });
        $('li', $nav).each(function ( )
        {
            if ( !$(this).children('a').size() ) {
                $(this).html('<span>' + $(this).text() + '</span>');
            }
        });
    }
    //-------------
    // slide units
    $('.js-slide_units').each(function() {
        var $parent  = $(this),
            $panels  = $('.js-slide_units-panel', this),
            $pager   = $('.js-slide_units-pager', this),
            $wrapper = $('<div class="js-slide_units-wrapper" />'),
            $next    = $('a[rel="next"]', $pager),
            $prev    = $('a[rel="prev"]', $pager),
            $pages   = $('.page', $pager),
            position = 0,
            maxborder= $panels.size(),
            stdWidth = $panels.attr('data-width') || $panels.innerWidth(),
            slideFunc= function(param) {
                for ( var i = 0, iz = maxborder; i<iz; i++ ) {
                    $panels.eq(i).animate(param, 'fast', 'swing');
                }
            };

        $panels.wrapAll($wrapper);

        // レイアウト初期化
        for ( var i = 0, iz = maxborder; i<iz; i++ ) {
            $panels.eq(i).css({
                top     : 0,
                left    : (stdWidth*i)+'px'
            });
        }
        // ページ番号の付与
        for ( var i = 0, iz = $pages.size(); i<iz; i++ ) {
            $pages.eq(i).addClass('pos'+i);
        }
        // 次へ（右）
        $next.click(function() {
            if ( position == (maxborder-1) ) {
                
            } else {
                position += 1;
                slideFunc({left: '-='+stdWidth+'px'});
                $('.stay', $pager).next().addClass('stay').prev().removeClass('stay');
            }
            return false;
        });
        // 前へ（左）
        $prev.click(function() {
            if ( position == 0 ) {
                
            } else {
                position -= 1;
                slideFunc({left: '+='+stdWidth+'px'});
                $('.stay', $pager).prev().addClass('stay').next().removeClass('stay');
            }
            return false;
        });
        // 番号（飛）
        $pages.click(function() {
            if ( this.className.indexOf('stay') === -1 && this.className.match(/pos(\d{1})/) ) {
                // クリックされたページ番号から再計算して割り付け
                position = RegExp.$1 * 1;

                for ( var offset = position * -1, i = 0, iz = maxborder; i<iz; i++ ) {
                    $panels.eq(i).animate({
                        left    : (stdWidth*(i+offset))+'px'
                    }, 'fast', 'swing');
                }

                $pages.removeClass('stay');
                $(this).addClass('stay');
            }
            return false;
        });
    });
});

function hrefStartWith (elm) {
    var href = $('a', elm).get(0).href,
        loc  = location,
        curr = loc.protocol+'//'+loc.hostname+loc.pathname;
    if ( 0 === href.indexOf(curr) ) { $(elm).addClass('stay'); }
}

function dispatcher (path, func) {
    dispatcher.path_func = dispatcher.path_func || []
    if (func) return dispatcher.path_func.push([path, func]);
    for( var i = 0, l = dispatcher.path_func.length; i < l; ++i) { 
        var func  = dispatcher.path_func[i];
        var match = path.match(func[0]);
        match && func[1](match);
    };
};

function fadeSequence($unit, duration) {
    if(!jQuery.support.checkOn){
        var i = 0,
           iz = $unit.size(),
         unit = null
        timer = null;
        
        if ( iz === 0 ) {
            return false;
        }

        timer = setInterval(function() {
            $($unit.get(i)).css('-webkit-opacity', 1);
            i++;
            if ( !(i < iz) ) {
                clearInterval(timer);
            }
        }, duration);
    }
}
													
$(function() {
    dispatcher(location.pathname);
});
