AICMedia.Scroller = {
    $curFrame: null,
    $scroller: null,
    $carousel: null,
    isAnimated: null,

    initialize: function() {
        this.$curFrame = AICMedia.PageLoader.currentFrame;
        this.$scroller = this.$curFrame.find('.b-scroller.js-scroller');
        this.$carousel = this.$scroller.find('.j-carousel.item-carousel');

        if (!this.$carousel.length || this.$carousel.data('jcarousel')) {
            return;
        }

        var SLIDE_DURATION = Math.pow(this.$carousel.find('li.item').width() * 5.3, 0.75);
        //var visible = Math.round(this.$carousel.width() / this.$carousel.find('li.item').width());

        this.$carousel.jcarousel({
            buttonNextHTML: null,
            buttonPrevHTML: null,
            animation: AICMedia.ie8 ? 0 : SLIDE_DURATION,
            scroll: 1,
            //visible: visible,
            initCallback: $.proxy(function(carousel) {
                carousel.list.find('> li').css(AICMedia.ie8 ? {visibility: 'hidden'} : {opacity: 0});

                carousel.funcResize = null;
            }, this),

            itemFirstInCallback: {
                onBeforeAnimation: $.proxy(function() {
                    this.isAnimated = true;
                }, this),

                onAfterAnimation: $.proxy(function(carousel) {
                    if (carousel.buttonPrevState && carousel.buttonNextState) {
                        AICMedia.SideArrows.setState('both');
                    } else if (carousel.buttonPrevState) {
                        AICMedia.SideArrows.setState('left');
                    } else if (carousel.buttonNextState) {
                        AICMedia.SideArrows.setState('right');
                    } else {
                        AICMedia.SideArrows.setState('none');
                    }

                    this.isAnimated = false;
                }, this)
            },

            itemVisibleInCallback: {
                onBeforeAnimation: function(carousel, li) {
                    if (AICMedia.ie8) {
                        $(li).css({visibility: 'visible'});
                    } else {
                        $(li).css({opacity: 0});
                        setTimeout(function() {
                            $(li).stop().fadeTo(SLIDE_DURATION / 4 * 3, 1);
                        }, SLIDE_DURATION / 4);
                    }
                }
            },

            itemVisibleOutCallback: {
                onBeforeAnimation: function(carousel, li) {
                    if (AICMedia.ie8) {
                        $(li).css({visibility: 'hidden'});
                    } else {
                        $(li).stop().fadeTo(SLIDE_DURATION / 4 * 3, 0);
                    }
                }
            }
        });

        this.$scroller.css({top: $('#top-menu').outerHeight()});
        this.$carousel.css({visibility: 'visible'});
    }
};

AICMedia.SideArrows = {
    $leftArrow: null,
    $rightArrow: null,
    $hint: null,
    $both: null,
    showState: null,
    timer: null,
    isEnabled: false,
    carousel: null,
    isHovered: false,

    start: function() {
        var $scroller = AICMedia.PageLoader.currentFrame.find('.b-scroller');
        if (!$scroller.find('.j-carousel').length) {
            return;
        }
        this.carousel = $scroller.find('.j-carousel').data('jcarousel');
        this.$leftArrow = $scroller.find('.carousel-prev img');
        this.$rightArrow = $scroller.find('.carousel-next img');
        this.$hint = AICMedia.PageLoader.currentFrame.find('.b-scroller-hint').css(AICMedia.ie8 ? {display: 'none'} : {opacity: 0});
        this.$both = this.$leftArrow.add(this.$rightArrow);
        this.$both.addClass('disabled').css(AICMedia.ie8 ? {display: 'none'} : {opacity: 0}).hover($.proxy(function() {
            this.isHovered = true;
            this._updateHint();
        }, this), $.proxy(function() {
            this.isHovered = false;
        }, this));

        this._updateState();

        this.$leftArrow.closest('a').bind('click', $.proxy(this._prev, this));
        this.$rightArrow.closest('a').bind('click', $.proxy(this._next, this));
        $(window).keydown($.proxy(this._keyDown, this));

        this.isEnabled = true;

        $scroller.mousemove($.proxy(function() {
            this._updateButtons();
            this._timer();
        }, this));
    },

    _keyDown: function(event) {
        if (event.ctrlKey && event.keyCode == 37) {
            this.$leftArrow.closest('a').click();
            event.preventDefault();
        } else if (event.ctrlKey && event.keyCode == 39) {
            this.$rightArrow.closest('a').click();
            event.preventDefault();
        }
    },

    _updateState: function() {
        if (this.carousel.buttonPrevState && this.carousel.buttonNextState) {
            this.showState = 'both';
        } else if (this.carousel.buttonPrevState) {
            this.showState = 'left';
        } else if (this.carousel.buttonNextState) {
            this.showState = 'right';
        } else {
            this.showState = 'none';
        }
    },

    _prev: function(event) {
        if (!AICMedia.Scroller.isAnimated && this.carousel.buttonPrevState) {
            this.carousel.prev();
        }
        event.preventDefault();
    },

    _next: function(event) {
        if (!AICMedia.Scroller.isAnimated && this.carousel.buttonNextState) {
            this.carousel.next();
        }
        event.preventDefault();
    },

    stop: function() {
        if (this.isEnabled) {
            this.carousel = null;
            this._hide(this.$both);
            this.isEnabled = false;
            this.$leftArrow.closest('a').unbind('click');
            this.$rightArrow.closest('a').unbind('click');
            this.$both.addClass('disabled').removeClass('show, hide');
        }
    },

    setState: function(state) {
        if (this.isEnabled) {
            this.showState = state;
            this._updateButtons();
            this._updateHint();
        }
    },

    _updateHint: function() {
        if (this.isHovered && !this.$hint.hasClass('show')) {
            if (AICMedia.ie8) {
                this.$hint.show();
            } else {
                this.$hint.removeClass('hide').addClass('show').stop().show().fadeTo(300, 1, function() {
                    $(this).removeClass('show');
                });
            }
        } else if (!this.isHovered && !this.$hint.hasClass('hide')) {
            if (AICMedia.ie8) {
                this.$hint.hide();
            } else {
                this.$hint.removeClass('show').addClass('hide').stop().fadeTo(1000, 0, function() {
                    $(this).hide();
                    $(this).removeClass('hide');
                });
            }
        }
    },

    _updateButtons: function() {
        switch(this.showState) {
        case 'both':
            this.$both.removeClass('disabled');
            break;
        case 'left':
            this.$leftArrow.removeClass('disabled');
            this.$rightArrow.addClass('disabled');
            break;
        case 'right':
            this.$rightArrow.removeClass('disabled');
            this.$leftArrow.addClass('disabled');
            break;
        case 'none':
        default:
            this.$both.addClass('disabled');
            break;
        }

        this._show(this.$both.filter(':not(.disabled)'));
        this._hide(this.$both.filter('.disabled'));
    },

    _show: function($buttons) {
        $buttons = $buttons.filter(':not(.show)');
        if (this.isEnabled && $buttons.length) {
            $buttons.removeClass('hide').addClass('show');
            $buttons.closest('a').css({display: 'block'});

            if (AICMedia.ie8) {
                $buttons.show();
                $buttons.removeClass('show');
            } else {
                $buttons.stop().animate({opacity: 1}, 300, function() {
                    $buttons.removeClass('show');
                });
            }
        }
    },

    _hide: function($buttons) {
        $buttons = $buttons.filter(':not(.hide)');
        if (this.isEnabled && $buttons.length) {
            $buttons.removeClass('show').addClass('hide');

            if (AICMedia.ie8) {
                $buttons.hide();
                $buttons.closest('a').css({display: 'none'});
                $buttons.removeClass('hide');
            } else {
                $buttons.stop().fadeTo(1000, 0, function() {
                    $buttons.closest('a').css({display: 'none'});
                    $buttons.removeClass('hide');
                });
            }
        }
    },

    _timer: function() {
        clearTimeout(this.timer);
        this.timer = setTimeout($.proxy(function() {
            this._updateHint();
            if (!this.isHovered) {
                this._hide(this.$both);
            }
        }, this), 1000);
    }
};
