$(function ( )
{
    //-------
    // works
    var $works  = $('#works');
    if ( $works.size() ) {
        var $li = $works.find('li');
        if ( 1 < $li.size() ) {
            $('#worksAmount').text($li.size());
        
            $li.first().addClass('current');
            $works.animate({'height' : $li.filter('.current').height() + 'px'}, function ( )
            {
                $li.first().nextAll().css('opacity', 0);            
            });
            $('#worksNavi').fadeIn();

            $('#worksPrev, #worksNext').click(function ( )
            {
                if ( ACMS.Config.worksAutoplay ) { ACMS.Dispatch._worksStop(); }

                var $current    = $li.filter('.current');
                if ( $(this).is('#worksPrev') ) {
                    $target = $current.prev();
                    if ( !$target.size() ) { $target = $li.last(); }
                } else {
                    $target = $current.next();
                    if ( !$target.size() ) { $target = $li.first(); }
                }

                $('#worksCurrent').text($target.prevAll().size() + 1);                
                $current.animate({'opacity' : 0}, function ( )
                {
                    $works.animate({'height' : $target.height() + 'px'}, function ( )
                    {
                        $current.parent().css('top', parseInt(-1 * $target.position().top, 10) + 'px');
                        $target.animate({'opacity' : 1});
                        $current.removeClass('current');
                        $target.addClass('current');
                        if ( ACMS.Config.worksAutoplay ) { ACMS.Dispatch._worksStart(); }
                    });
                });

                return false;
            });
            
            $works.hover(function ( )
            {
                ACMS.Dispatch._worksStop();
            }, function ( )
            {
                ACMS.Dispatch._worksStart();
            });
        }
    }

    ACMS.Dispatch._worksStart   = function ( )
    {
        if ( !ACMS.Config.worksAutoplay ) { return true; }
        var interval    = ('undefined' != typeof(ACMS.Config.worksInterval)) ? ACMS.Config.worksInterval : 6000;
        ACMS.Dispatch._worksId  = setTimeout(function ( )
        {
            $('#worksNext').click();
        }, ACMS.Config.worksInterval);
    }

    ACMS.Dispatch._worksStop    = function ( )
    {
        clearInterval(ACMS.Dispatch._worksId);
    }

    if ( ACMS.Config.worksAutoplay ) { ACMS.Dispatch._worksStart(); }

    //----
    // bg
    if ( !ACMS.Config.bg ) {
        return false;
    }
    var len = ACMS.Config.bg.length;
    if ( !len ) {
        return false;
    }
    var stack   = new Array();

    function fillStack ( ) {
        while ( stack.length < len ) {
            var i = Math.floor(Math.random() * len);
            if ( 0 <= $.inArray(i, stack) ) {
                continue;
            }
            stack.push(i);
        }
    }
    fillStack();

    var img = new Image();
    $(img).load(function ( )
    {
        setTimeout(function ( ) {
            var recursive   = arguments.callee;
            var $forward    = $('#bg-forward');
            var $back       = $('#bg-back');
            $forward.animate({opacity:0}, 1000, function ( )
            {
                $forward.css('z-index', 2);
                $back.css('z-index', 3);
                $forward.css('opacity', 1);
                var i   = stack.pop();
                if ( !stack.length ) {
                    fillStack();
                    while ( i === stack[len - 1] ) {
                        stack   = new Array();
                        fillStack();
                    }
                }
                $forward.empty();
                $forward.css('background-image', 'url("' + ACMS.Config.bg[i] + '")');
                $forward.attr('id', 'bg-back');
                $back.attr('id', 'bg-forward');
                if ( 2 <= len ) {
                    setTimeout(recursive, ACMS.Config.bgInterval);
                }
            });
        }, 0);
        $(img).remove();
    });
    var i   = stack.pop();
    $('#bg-back').css('background-image', 'url("' + ACMS.Config.bg[i] + '")');
    img.src = ACMS.Config.bg[i];
});

