$(function(){
    initSlideShow();
    initNavigation();
    $('a[rel="external"]').live('click',function() {
        window.open( $(this).attr('href'), "Äëÿ ïå÷àòè", "width=500,height=600,scrollbars=1");
        return false;
    });
});
function initNavigation () {
    $('#nav a:not(li ul a)').click(function(){
        if ($(this).next().is('ul')) {
            var _this = $(this);
            _this.parents('.panel').wrap('<div class="wrap" style="width:9999px;">');
            _this.parents('.panel').after('<div class="panel"><ul class="subnav"><li>'+_this.parent('li').html()+'</li></ul><a href="#" class="btn-back">ÐÐ°Ð·Ð°Ð´</a></div>')
            _this.parents('.panel').animate({marginLeft:'-222px'},1000)
            return false;
        }
    });
    $('.btn-back').live('click',function(){
        $(this).parents('.wrap').find('.panel:first').animate({marginLeft:'0'},1000,function(){
            $(this).next('.panel').remove();
            $(this).unwrap('<div class="wrap">');
        });
    });
}
function initSlideShow() {
    $('ul.slideshow').fadeGallery({
        slideElements:'li',
        pauseOnHover:true,
        autoRotation:true,
        switchTime:2000,
        duration:650,
        event:'click'
    })
}
jQuery.fn.fadeGallery = function(_options){
    var _options = jQuery.extend({
        slideElements:'div.slideset > div',
        pagerLinks:'ul.switcher a',
        btnNext:'a.next',
        btnPrev:'a.prev',
        btnPlayPause:'a.play-pause',
        pausedClass:'paused',
        playClass:'playing',
        activeClass:'active',
        pauseOnHover:true,
        autoRotation:false,
        autoHeight:false,
        switchTime:3000,
        duration:650,
        event:'click'
    },_options);

    return this.each(function(){
        var _this = jQuery(this);
        var _slides = jQuery(_options.slideElements, _this);
        var _pagerLinks = jQuery(_options.pagerLinks, _this);
        var _btnPrev = jQuery(_options.btnPrev, _this);
        var _btnNext = jQuery(_options.btnNext, _this);
        var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
        var _pauseOnHover = _options.pauseOnHover;
        var _autoRotation = _options.autoRotation;
        var _activeClass = _options.activeClass;
        var _pausedClass = _options.pausedClass;
        var _playClass = _options.playClass;
        var _autoHeight = _options.autoHeight;
        var _duration = _options.duration;
        var _switchTime = _options.switchTime;
        var _controlEvent = _options.event;

        var _hover = false;
        var _prevIndex = 0;
        var _currentIndex = 0;
        var _slideCount = _slides.length;
        var _timer;
        if(!_slideCount) return;
        _slides.hide().eq(_currentIndex).show();
        if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
        else _this.removeClass(_playClass).addClass(_pausedClass);

        if(_btnPrev.length) {
            _btnPrev.bind(_controlEvent,function(){
                prevSlide();
                return false;
            });
        }
        if(_btnNext.length) {
            _btnNext.bind(_controlEvent,function(){
                nextSlide();
                return false;
            });
        }
        if(_pagerLinks.length) {
            _pagerLinks.each(function(_ind){
                jQuery(this).bind(_controlEvent,function(){
                    if(_currentIndex != _ind) {
                        _prevIndex = _currentIndex;
                        _currentIndex = _ind;
                        switchSlide();
                    }
                    return false;
                });
            });
        }

        if(_btnPlayPause.length) {
            _btnPlayPause.bind(_controlEvent,function(){
                if(_this.hasClass(_pausedClass)) {
                    _this.removeClass(_pausedClass).addClass(_playClass);
                    _autoRotation = true;
                    autoSlide();
                } else {
                    if(_timer) clearTimeout(_timer);
                    _this.removeClass(_playClass).addClass(_pausedClass);
                }
                return false;
            });
        }

        function prevSlide() {
            _prevIndex = _currentIndex;
            if(_currentIndex > 0) _currentIndex--;
            else _currentIndex = _slideCount-1;
            switchSlide();
        }
        function nextSlide() {
            _prevIndex = _currentIndex;
            if(_currentIndex < _slideCount-1) _currentIndex++;
            else _currentIndex = 0;
            switchSlide();
        }
        function refreshStatus() {
            if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
            _slides.eq(_prevIndex).removeClass(_activeClass);
            _slides.eq(_currentIndex).addClass(_activeClass);
        }
        function switchSlide() {
            _slides.eq(_prevIndex).fadeOut(_duration);
            _slides.eq(_currentIndex).fadeIn(_duration);
            refreshStatus();
            autoSlide();
        }

        function autoSlide() {
            if(!_autoRotation || _hover) return;
            if(_timer) clearTimeout(_timer);
            _timer = setTimeout(nextSlide,_switchTime+_duration);
        }
        if(_pauseOnHover) {
            _this.hover(function(){
                _hover = true;
                if(_timer) clearTimeout(_timer);
            },function(){
                _hover = false;
                autoSlide();
            });
        }
        refreshStatus();
        autoSlide();
    });
}
