/// <reference path="../ScriptReference/jquery-1.5.2.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'
            },
            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("ui-playerlinkspanel");
            var markup = '<p id="' + o.ids.header + '">' + Mediasite.Player.Localization.Buttons.Links + '</p>' +
							'<div id="' + o.ids.scrollWrapper + '">' +
								'<div id="' + o.ids.scroll + '">' +
									'<ul class="ui-playerlinkspanel-list" />' +
								'</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 () {
            this.element.removeClass("ui-playerlinkspanel");
            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);