Source: widgets/uibuilder/movie.js

define('application/widgets/uibuilder/movie', [
    'application/widgets/pointerfocusablebutton',
    'antie/runtimecontext',
    'rofl/widgets/label',
    'rofl/lib/l10n',
    'rofl/widgets/container',
    'application/managers/session',
    'rofl/lib/utils'
], function (
    Button,
    RuntimeContext,
    Label,
    L10N,
    Container,
    SessionManager,
    Utils
) {
    'use strict';

    var layout = RuntimeContext.getCurrentApplication().getLayout(),
        l10n = L10N.getInstance();

    return Button.extend({

        /**
         * Initialises the widget.
         *
         * @param {Object} item - The Broadcast model.
         */
        init: function init (item) {
            init.base.call(this);

            this.addClass('movie');
            this._buildMovieItem(item);

            this.setDataItem(item);
        },

        /**
         * Checks whether asset is parental protected.
         *
         * @returns {boolean} - True if is child protected.
         */
        isLocked: function () {
            var item = this.getDataItem(),
                vodItem = item.item || item;

            return Utils.isFunction(vodItem.isLocked) && vodItem.isLocked();
        },

        /**
         * Builds the labels.
         *
         * @param {Object} item - The Broadcast model.
         * @private
         */
        _buildMovieItem: function (item) {
            var container = new Container(),
                vodItem = item.item || item,
                imageContainer = this._imageContainer = new Container(),
                title = this._itemTitle = vodItem.getTitle(),
                year = vodItem.getYear(),
                duration = vodItem.getDuration(),
                durationText = '',
                minutes,
                hours;

            hours = Math.floor(duration / 3600);
            duration = duration - hours * 3600;
            minutes = Math.floor(duration / 60);

            if (hours) {
                durationText += hours + l10n.get('hours') + ' ';
            }

            if (minutes) {
                durationText += minutes + l10n.get('minutes');
            }

            container.addClass('metadata-container');
            imageContainer.addClass('bg-image');

            this.appendChildWidget(imageContainer);

            this._titleElement = this._buildLabel(title, 'title');
            container.appendChildWidget(this._titleElement);
            container.appendChildWidget(this._buildLabel(year, 'year'));
            container.appendChildWidget(this._buildLabel(durationText, 'duration'));
            this.appendChildWidget(container);
        },

        /**
         * Lock asset.
         *
         * @private
         */
        _lockAsset: function () {
            var lockContainer = this._lockedContainer = new Container(),
                item = this.getDataItem(),
                background = item.background;

            item.background = Utils.addToQueryStringOfURL(background, {
                blurred: true
            });

            lockContainer.addClass(['locked-icon', 'icon-lock']);

            if (this._isRendered) {
                this.setBackgroundImage(item);
            }

            this.appendChildWidget(lockContainer);
            this._imageContainer.addClass('locked');
            this._titleElement.setText(l10n.get('settings.parental.title'));
        },

        /**
         * Unlocks asset.
         *
         */
        unlockAsset: function () {
            var item = this.getDataItem(),
                background = item.background;

            if (this._lockedContainer) {
                this.removeChildWidget(this._lockedContainer);
            }

            if (background) {
                item.background = background.replace('?blurred=true', '');
            }

            this._imageContainer.removeClass('locked');
            this._titleElement.setText(this._itemTitle);

            if (this._isRendered) {
                this.setBackgroundImage(item);
            }
        },

        unlock: function () {
            this.unlockAsset();
        },

        /**
         * Builds the label.
         *
         * @param {string} text - The text.
         * @param {string|Array} classes - The class(es) to add.
         * @returns {Object} - The label.
         * @private
         */
        _buildLabel: function (text, classes) {
            var label = new Label(text);

            label.addClass(classes);

            return label;
        },

        /**
         * Renders the widget.
         *
         * @param {Object} device - The device.
         * @returns {Object} - The output element.
         */
        render: function render (device) {
            var outputElement = render.base.call(this, device),
                item = this.getDataItem();

            if (!this._isRendered) {
                this._isRendered = true;
                this.setBackgroundImage(item);

                if (this.isLocked()) {
                    this._lockAsset();
                }
            }

            return outputElement;
        },

        setBackgroundImage: function (item) {
            var image = item.background ? item.background : item.getImage('manual', {
                width: layout.movie.width,
                height: layout.movie.height
            });

            this._imageContainer.setStyleTo('backgroundImage', 'url(' + image + ')');
        }
    });
});