/// <reference path="../ScriptReference/jquery-1.7.1-vsdoc.js"/>

/*
* jQuery UI PlayerVolume
*
* Depends:
*   jquery.ui.core.js
*   jquery.ui.widget.js	
*   jquery.imageButton.js
*   jquery.enableTouch.js
*   Mediasite.Player.Localization.js
*
*/
(function ($) {
    $.widget("ui.playerspeed", {
        options: {
            onPlaybackRateFaster: null,
            currentSpeed: 1
        },

        _setOption: function (key, value) {
            this.options[key] = value;
            this._super("_setOption", key, value);
        },

        _create: function () {
            var self = this,
                o = self.options;

            self.element.addClass("ui-playerspeed");

            self.element
                .attr({ title: Mediasite.Player.Localization.Buttons.PlaybackRate })
				.imageButton({ spriteOffset: 40 })
				.click(function () { self.TogglePlaybackRate(); });

            self.displayPlaybackRate(o.currentSpeed);
        },

        setPlaybackRate: function (speed) {
            var self = this,
                o = self.options;

            o.currentSpeed = speed;
            self.displayPlaybackRate(speed);
        },

        displayPlaybackRate: function (speed) {
            var self = this,
                o = self.options;

            // Speed should be displayed like "0.5x", "1x", "1.5x", "-0.5x", "-1x", "-1.5x"  (no quotes)
            var displaySpeed = Math.floor(speed * 10) / 10; // Truncate speed to 1 decimal point
            displaySpeed = displaySpeed.toString() + 'x';

            self.element.text(displaySpeed);
        },

        TogglePlaybackRate: function () {
            var self = this,
				o = self.options;

            o.onPlaybackRateFaster(true);
            // Wait for controlling view to notify this widget to update its view

        },

        disable: function () {
            this.element.imageButton('disable');
        },

        destroy: function () {
            this.element.imageButton('destroy');
            this.element.removeClass("ui-playerspeed");
        }
    });
})(jQuery);