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

/*
 * jQuery UI PlayerLinksPanel
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 *   Mediasite.Player.Localization.js
 *
 */
(function ($) {
    $.widget("ui.playerlinkspanel", {
        options: {
            links: null,
            ids: {
                header: 'ui-playerlinkspanel-header',
                scroll: 'ui-playerlinkspanel-scroll',
                scrollWrapper: 'ui-playerlinkspanel-scroll-wrapper'
            },
            classes: {
                panel: 'ui-playerlinkspanel',
                linklist: 'ui-playerlinkspanel-list',
                header: 'panel-header'
            },
            scrollbarColor: 'rgba(255,255,255,0.5)'
        },

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

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

            self.element.addClass(o.classes.panel);
            var markup = '<p id="' + o.ids.header + '" class="' + o.classes.header + '">' + Mediasite.Player.Localization.Buttons.Links + '</p>' +
							'<div id="' + o.ids.scrollWrapper + '">' +
								'<div id="' + o.ids.scroll + '">' +
									'<ul class="' + o.classes.linklist + '" />' +
								'</div>' +
							'</div>';
            self.divMarkup = $(markup).appendTo(self.element);

            $(window).resize(function () {
                self.updateLayout();
            });
        },

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

            var $scrollWrapper = $('#' + o.ids.scrollWrapper);
            var availableHeight = self.element.height();   // Start with panel's inner height
            // Subtract height of pertinent siblings
            availableHeight -= $('#' + o.ids.header).outerHeight(true) + 10; // KLUDGE: discrepancies between displayed and hidden height calculations
            // availableHeight is now space allotted for the scroll wrapper

            // Allow for scroll wrapper's padding and margin
            availableHeight -= parseInt($scrollWrapper.css('margin-top'), 10) || 0;
            availableHeight -= parseInt($scrollWrapper.css('margin-bottom'), 10) || 0;
            availableHeight -= parseInt($scrollWrapper.css('padding-top'), 10) || 0;
            availableHeight -= parseInt($scrollWrapper.css('padding-bottom'), 10) || 0;

            availableHeight = availableHeight > 0 ? availableHeight : 20; // just in case

            $scrollWrapper.height(availableHeight);


            $('#' + o.ids.scroll).iscroll({ scrollbarColor: o.scrollbarColor });
        },

        destroy: function () {
            var o = this.options;

            this.element.removeClass(o.classes.panel);
            this.divMarkup.remove();
        },

        _init: function () {
            this._bindLinks();
            this.updateLayout();
        },

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

            if (links) {
                $.each(links, function (i) {
                    var item = $('<li><a href="' + links[i].Url + '" target="new" title="' + links[i].Url + '">' + links[i].Description + '</a></li>');
                    $('ul', self.element).append(item);
                });
            }
        }
    });
})(jQuery);