/**
Adds pulsing feature to player-pause button.
*/
Player.prototype.override({
    initialize: function ($super, elem, params) {
        $super(elem, params);
        this.pp = { // for variables needed for this script
            vis: app.player.visualization,
            player: app.player,
            scaleKoef: undefined,
            lastScaleKoef: 0,
            animationOn: true
        };
        this.pp.vis.registerVisTypes(['vumeter']);
        var pp = this.pp;

        this.pp.update = function () {
            if (!pp || !pp.playerBtn)
                return;

            if (!pp.animationOn || !pp.vis || (pp.currState !== 'play') || (!this.sd) || (this.sd.isVideo)) {
                pp.playerBtn.style.webkitTransform = '';
                return;
            }

            var tm = pp.player.trackPositionMS;
            pp.currData = pp.vis.getNextResult(tm, pp.currData);
            if (pp.currData) {
                pp.scaleKoef = 100 + Math.min(Math.floor(Math.max(pp.currData.maxLeft - 8000, pp.currData.maxRight - 8000, 0) / 250), 50);
                if (pp.scaleKoef !== pp.lastScaleKoef) {
                    pp.lastScaleKoef = pp.scaleKoef;
                    pp.playerBtn.style.webkitTransform = 'scale(' + pp.scaleKoef / 100 + ')';
                }
            }
            this.requestTimeout(function () {
                if (pp.currState === 'play') {
                    this.requestFrameMM(pp.update, 'pulsingPauseFrame');
                }
            }.bind(this), 20, 'pulsingPauseTimeout', true);
        }.bind(this);
    },
});

window.playerControlsHandlers.pause.override({
    init: function ($super, el, playerCtrl) {
        $super(el, playerCtrl);
        el.controlClass.addToContextMenu(function () {
            var buttonMenuAct;
            if (playerCtrl.pp.animationOn) {
                buttonMenuAct = {
                    action: {
                        title: 'Disable button animation',
                        execute: function () {
                            playerCtrl.pp.vis.unregisterVisTypes(['vumeter']);
                            playerCtrl.pp.animationOn = false;
                        },
                        visible: function () {
                            return playerCtrl && playerCtrl.sd && !playerCtrl.sd.isVideo;
                        }
                    },
                    order: 10,
                    grouporder: -10
                }
            } else {
                buttonMenuAct = {
                    action: {
                        title: 'Enable button animation',
                        execute: function () {
                            playerCtrl.pp.animationOn = true;
                            playerCtrl.pp.vis.registerVisTypes(['vumeter']);
                            if (playerCtrl.pp.currState === 'play') {
                                playerCtrl.pp.update();
                            };
                        },
                        visible: function () {
                            return playerCtrl && playerCtrl.sd && !playerCtrl.sd.isVideo;
                        }
                    },
                    order: 10,
                    grouporder: -10 // to be separated first
                }
            }
            return [buttonMenuAct];
        });
    },

    onPlaybackState: function ($super, el, playerCtrl, state) {
        $super(el, playerCtrl, state);
        if (!playerCtrl.pp)
            return;
        switch (state) {
            case 'unpause':
            case 'play':
                if (playerCtrl.pp.currState !== 'play') {
                    playerCtrl.pp.currState = 'play';
                    // find only inner image of button, so we will not overflow button container by increasing its size, #16944
                    var btnImage = qetag(el, 'svg');
                    if (btnImage && btnImage[0])
                        btnImage = btnImage[0]
                    else {
                        btnImage = qetag(el, 'img');
                        if (btnImage && btnImage[0])
                            btnImage = btnImage[0]
                        else
                            btnImage = el;
                    }
                    playerCtrl.pp.playerBtn = btnImage;
                    if (playerCtrl.pp.animationOn)
                        playerCtrl.pp.update();
                }
                break;
            case 'stop':
                playerCtrl.pp.currState = state;
                break;
            case 'pause':
                playerCtrl.pp.currState = state;
                break;
        }
    }
});
