jQuery(document).ready(function () {
    initGallery();
    initLightBox();

    jQuery('.spot.rotator-image-small').find('div').each(function () {
        var divHeight = jQuery(this).height();
        if (divHeight > jQuery('.spot.rotator-image-small').height()) {
            jQuery('.spot.rotator-image-small').height(divHeight + jQuery('.spot.rotatingcontent.spotcontent h4').height() + 20);
        }
    });

    var elms;
    if ((elms = jQuery('.rotator-image-small .rotatingcontent')).length > 0) {
        elms.dumbCrossFade({ slideType: 'fade', showTime: 3500, transitionTime: 1200 });
    }

    $('a.print').click(function (event) {
        event.preventDefault();
        window.print();
    });
});

function initGallery() {
    jQuery('.slideshow').SlideShow({
        speed: 800,
        duration: 4000,
        list: 'ul.slideshow-holder > li',
        pager: 'ul.switcher',
        ieVersion: 6
    });
    jQuery('.lightbox .gallery').SlideShow({
        speed: 800,
        duration: false,
        counter: '.info',
        list: '> ul > li',
        ieVersion: 6
    });
};

var i = -1;
function initLightBox() {
    var $ = jQuery;
    var _speed = 300;
    var _ieVersion = 7;
    var _openers = $('.gallery-block .visual-list a');
    var _lay = $('<div class="overlay"></div>').appendTo($('body'));
    if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
        _lay.css({ opacity: 0.7 }).hide();
    } else { _lay.css({ opacity: 0 }).hide(); }
    var _light = $('.lightbox').appendTo($('body'));
    _openers.click(function () {
        var _body = $('body').height();
        Position();
        var _opener = $(this);
        i = _openers.index(_opener);
        // $('.lightbox .gallery li').removeClass('active').eq(i).addClass('active');
        $('.lightbox .gallery li').css({ opacity: 0 }).eq(i).css({ opacity: 1 });
        $('.lightbox .info').text($('.lightbox .gallery li').eq(i).find('img').attr('alt') + ' ' + (i + 1) + '/' + $('.lightbox .gallery li').length);

        if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
            _light.show();
            _lay.show();
        } else {
            _light.css({ opacity: 0 }).show().animate({ opacity: 1 }, { queue: false, duration: _speed });
            _lay.show().animate({ opacity: 0.7 }, { queue: false, duration: _speed });
        }

        _lay.click(function () { Close(); return false; });
        _light.find('.link-close, .btn-close, .close').click(function () { Close(); return false; });
        function Close() {
            if (jQuery.browser.msie && jQuery.browser.version < _ieVersion) {
                _light.hide();
                _lay.hide();
            } else {
                _light.animate({ opacity: 0 }, { queue: false, duration: _speed, complete: function () { $(this).hide(); } });
                _lay.animate({ opacity: 0 }, { queue: false, duration: _speed, complete: function () { $(this).hide(); } });
            }
        }

        $(window).resize(function () { Position(); });
        $(window).scroll(function () { Position(); });

        function Position() {
            var _w = _light.outerWidth(true);
            var _h = _light.outerHeight(true);
            if (window.innerHeight) { var _wx = window.innerWidth; var _wy = window.innerHeight; }
            else { _wx = document.documentElement.clientWidth; _wy = document.documentElement.clientHeight; };
            if (_body < _wy) { _lay.css({ height: _wy }) }
            else { _lay.css({ height: _body }); };
            var _top = (_wy - _h) / 2 + $(document).scrollTop();
            if (_top + _h > _body) _top = _body - _h - 8;
            _light.css({ left: (_wx - _w) / 2, top: _top });
        };
        return false;
    });
};
jQuery.fn.SlideShow = function (_options) {
    // default options
    var _options = jQuery.extend({
        speed: 1200,
        duration: 4000,
        list: 'ul.fade>li',
        prev: 'a.link-prev',
        next: 'a.link-next',
        pager: 'ul.switcher',
        dinamicPagination: false,
        counter: '.counter',
        pause: '.pause',
        ieVersion: 6
    }, _options);

    return this.each(function () {
        // options
        var _hold = jQuery(this);
        var _speed = _options.speed;
        var _duration = _options.duration;
        var _list = _hold.find(_options.list);
        var _prev = _hold.find(_options.prev);
        var _next = _hold.find(_options.next);
        var _pause = _hold.find(_options.pause);
        var _counter = _hold.find(_options.counter);
        var _ie = _options.ieVersion;
        var _f = true;
        var _p = _options.dinamicPagination;
        /*--------CREATING THUMBNAILS----------*/
        var _num = _hold.find(_options.pager);
        if (_p) {
            _num.empty();
            _list.each(function (i) {
                $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(_num);
            });
        };
        var _thumb = _num.find('li').not(':first');
        /*-------------------------------------------------*/
        var _a = _list.index(_list.filter('.active'));
        if (_a == -1) { _a = _thumb.index(_thumb.filter('.active')); } if (_a == -1) { _a = 0; }
        _list.removeClass('active').eq(_a).addClass('active');
        _thumb.removeClass('active').eq(_a).addClass('active');
        _counter.text(_list.eq(_a).find('img').attr('alt') + ' ' + (_a + 1) + '/' + _list.length);
        var _i, _old = _a, _t;
        if (jQuery.browser.msie && jQuery.browser.version < _ie) {
            _list.hide().eq(_a).show();
        } else { _list.show().css({ opacity: 0 }).eq(_a).css({ opacity: 1 }); }
        if (_duration) { if (_f) { Run(_a); } }
        function Run(_a) {
            _t = setTimeout(function () {
                _a++; if (_a >= _list.length) { _a = 0 }
                ChangeFade(_a);
            }, _duration);
        };
        function ChangeFade(_new) {
            if (_new != _old) {
                if (jQuery.browser.msie && jQuery.browser.version < _ie) {
                    _list.eq(_old).removeClass('active').hide();
                    _list.eq(_new).addClass('active').show();
                } else {
                    _list.eq(_old).removeClass('active').animate({ opacity: 0 }, { queue: false, duration: _speed });
                    _list.eq(_new).addClass('active').animate({ opacity: 1 }, { queue: false, duration: _speed });
                }
                _thumb.eq(_old).removeClass('active');
                _thumb.eq(_new).addClass('active');
                _counter.text(_list.eq(_new).find('img').attr('alt') + ' ' + (_new + 1) + '/' + _list.length);
                _old = _new; _a = _new;
                if (_t) clearTimeout(_t);
                if (_duration) { if (_f) { Run(_new); } }
            };
        };

        _pause.click(function () {
            _f = false;
            clearTimeout(_t);
            return false;
        });
        _thumb.hover(function () {
            _i = _thumb.index($(this));
            ChangeFade(_i);
            _a = _i;
        });
        _next.click(function () {
            if (i > 0) {
                _a = i;
                i = -1;
            }
            _a++; if (_a == _list.length) { _a = 0 }
            ChangeFade(_a);
            return false;
        });
        _prev.click(function () {
            if (i > 0) {
                _a = i;
                i = -1;
            }
            _a--; if (_a == -1) { _a = _list.length - 1 }
            ChangeFade(_a);
            return false;
        });
    });
};
