$(document).ready(function() {

    // gallery *****************************************************************
    var _slideParent = $('div.gallery ul');
    var _slides = $('div.visual ul li');
    var _thumbLi = $('li', _slideParent);
    var _duration = 500;   
    //var _autoRotationTime = 9000;
    var _autoRotationTime = -1;
    //var _timeAfterClick = 30000;
    var _timeAfterClick = -1;
    var _currentSlide = 0;
    var _prev = $('a.link-prev');
    var _next = $('a.link-next');
    var _timer, _clickTimer, _currentSlide, _hoverClick = false;

    var _firstThumb = _thumbLi.clone();
    var _lastThumb = _thumbLi.clone();
    _slideParent.append(_firstThumb);
    _slideParent.prepend(_lastThumb);

    var _thumb = $('div.gallery ul a');
    _slideParent.css('marginLeft', -240 * _thumbLi.length);

    _thumb.each(function(i, link) {
        var _id = $(link).attr('href');
        _id = _id.substr(_id.indexOf('#'));

        if (i == _thumbLi.length) {
            _currentSlide = _thumb.index($(link));
            $(link).addClass('down').animate({ 'marginTop': 95 }, { duration: _duration, complete: function() {
                $(link).animate({ 'marginTop': 0 }, { duration: _duration });
            }
            });
            $(_id).animate({ 'top': 0 }, { duration: _duration });
        };

        $(link).click(function() {
            if (!_thumb.is(':animated')) {
                _currentSlide = _thumb.index($(link));
                _slideThis();

                if (_timer) clearTimeout(_timer);
                _timer = setTimeout(function() { autoRotation() }, _timeAfterClick);
            }
            return false;
        });
    });

    _next.click(function() {
        if (!_thumb.is(':animated')) {
            _thumb.eq(_currentSlide + 1).trigger('click');
        }
        return false;
    });
    _prev.click(function() {
        if (!_thumb.is(':animated')) {
            _thumb.eq(_currentSlide - 1).trigger('click');
        }
        return false;
    });

    function _slideThis() {
        var _id = $(_thumb).eq(_currentSlide).attr('href');
        _id = _id.substr(_id.indexOf('#'));

        _slideParent.animate({ 'marginLeft': -240 * _currentSlide }, { duration: _duration });
        _slides.not(_id).animate({ 'top': -271 }, { duration: _duration });

        _thumb.filter('.down').removeClass('down').animate({ 'marginTop': 0 }, { duration: _duration, complete: function() {
            $(_thumb).eq(_currentSlide).addClass('down').animate({ 'marginTop': 95 }, { duration: _duration, complete: function() {
                $(_thumb).eq(_currentSlide).animate({ 'marginTop': 0 }, { duration: _duration, complete: function() {
                    var _more = _currentSlide - _thumbLi.length * 2;
                    if (_currentSlide >= _thumbLi.length * 2) {
                        _currentSlide = _thumbLi.length + _more;
                        _slideParent.css('marginLeft', -240 * _currentSlide);
                    }
                    if (_currentSlide < _thumbLi.length) {
                        _currentSlide = _thumbLi.length * 3 + _more;
                        _slideParent.css('marginLeft', -(_thumbLi.length * 3 + _more) * 240);
                    }
                }
                });
            }
            });
            $(_id).animate({ 'top': 0 }, { duration: _duration });

        }
        });
    }

    _timer = setTimeout(function() { autoRotation() }, _autoRotationTime);
    function autoRotation() {
        //_next.trigger('click');
        _timer = setTimeout(function() { autoRotation() }, _autoRotationTime);
    }
});

