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

/*
 * jQuery UI PlayerInfoPanel
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 *   jquery.iscroll.js
 *   Mediasite.Player.Localization.js
 *
 */
(function ($) {
    $.widget("ui.playerinfopanel", {
        options: {
            presentaton: null,
            onChapterChanged: null,
            ids: {
                scroll: 'ui-playerinfopanel-scroll',
                scrollWrapper: 'ui-playerinfopanel-scroll-wrapper',
                header: 'ui-playerinfopanel-header',
                footer: 'ui-playerinfopanel-footer',
                presenters: 'ui-playerinfopanel-presenters',
                description: 'ui-playerinfopanel-description',
                date: 'ui-playerinfopanel-date',
                time: 'ui-playerinfopanel-time',
                duration: 'ui-playerinfopanel-duration',
                dateInfoSection: 'ui-playerinfopanel-dateinfo',
                presenterImage: 'ui-playerinfopanel-presenterimage',
                privacyPolicySection: 'ui-playerinfopanel-privacypolicy'
            },
            classes: {
                panel: 'ui-playerinfopanel',
                header: 'panel-header'
            },
            scrollbarColor: 'rgba(255,255,255,0.5)',
            defaultPresenterImageSrc: null,
            hideDateTime: false,
            privacyPolicyUrl: ''
        },

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

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


            this.element.addClass(o.classes.panel);
            var markup =
                        '<div id="' + o.ids.header + '" class="' + o.classes.header + '">Presentation Info</div>' +
						'<div id="' + o.ids.scrollWrapper + '">' +
							'<div id="' + o.ids.scroll + '">' +
								'<div id="' + o.ids.presenters + '">' +
									'<ul />' +
								'</div>' +
								'<p id="' + o.ids.description + '" />' +
							'</div>' +
						'</div>' +
                        '<div id="' + o.ids.footer + '">' +
						    '<div id="' + o.ids.dateInfoSection + '"><span id="' + o.ids.date + '"></span><span id="' + o.ids.time + '"></span>' + Mediasite.Player.Localization.PresentationCard.Duration + '&nbsp;<span id="' + o.ids.duration + '"></span></div>' +
                            '<div id="' + o.ids.privacyPolicySection + '"><a target="_blank">' + Mediasite.Player.Localization.Common.PrivacyPolicy + '</a></div>' +
                        '</div>'

            this.divMarkup = $(markup).appendTo(this.element);

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

        destroy: function () {
            var o = this.options;
            this.element.removeClass(o.classes.panel);
            this.divMarkup.remove();
        },

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

            // need to set the height of description wrapper for iscroll to work properly
            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);
            availableHeight -= $('#' + o.ids.footer).outerHeight(true);

            // availableHeight is now space allotted for the scroll wrapper
            // Allow for scroll wrapper's padding and margin (assuming these are set in px)
            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);

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

        updateDuration: function (duration) {
            var self = this;
            var o = self.options;
            $('#' + o.ids.duration, this.divMarkup).text(Mediasite.Player.TimeHelper.getDisplayFromDuration(duration, true));
        },

        _init: function () {
            var privacyPolicySection, privacyPolicyLink;

            this._bindPresentation();

            if (this.options.hideDateTime) {
                $('#' + this.options.ids.date, this.divMarkup)
                .add('#' + this.options.ids.time, this.divMarkup)
                    .hide();
            }

            privacyPolicySection = $('#' + this.options.ids.privacyPolicySection);
            privacyPolicyLink = $('#' + this.options.ids.privacyPolicySection + ' a');
            if (this.options.privacyPolicyUrl) {
                privacyPolicyLink.prop('href', this.options.privacyPolicyUrl);
                privacyPolicySection.show();
            } else {
                privacyPolicySection.hide();
            }

            this.updateLayout();
        },

        _bindPresentation: function () {
            var self = this,
                o = self.options,
                p = o.presentation;

            if (p) {
                if (p.Title) {
                    $('#' + o.ids.header).text(p.Title);
                }

                if (p.Description) {
                    var desc = p.Description.formatAsHtml();
                    $('#' + o.ids.description, this.divMarkup).html(desc);
                }

                if (p.Date) {
                    $('#' + o.ids.date, this.divMarkup).text(p.Date);
                }

                if (p.Time) {
                    $('#' + o.ids.time, this.divMarkup).text(p.Time);
                }

                this.updateDuration(p.Duration || 0);

                if (p.Presenters && p.Presenters.length > 0) {
                    var first = p.Presenters[0];

                    if (first.ImageUrl) {
                        $('<img id="' + o.ids.presenterImage + '" src="' + first.ImageUrl + '" alt="' + first.Name + '" title="' + first.Name + '" class="ui-corner-all" />').prependTo($('#' + o.ids.presenters));
                    } else if (o.defaultPresenterImageSrc) {
                        $('<img id="' + o.ids.presenterImage + '" src="' + o.defaultPresenterImageSrc + '" alt="' + first.Name + '" title="' + first.Name + '" class="ui-corner-all" />').prependTo($('#' + o.ids.presenters));
                    }

                    var $item;

                    $.each(p.Presenters, function (i) {
                        if (p.Presenters[i].BioUrl && p.Presenters[i].BioUrl.length > 0) {
                            $item = $('<li><div class="bullet"></div><a href="' + p.Presenters[i].BioUrl + '" target="_blank">' + p.Presenters[i].Name + '</a></li>');
                        }
                        else {
                            $item = $('<li><div class="bullet"></div>' + p.Presenters[i].Name + '</li>');
                        }

                        // on mouseover swap out new presenter img
                        $item.mouseover(function () {
                            var presenter = p.Presenters[i];
                            if (presenter.ImageUrl) {
                                $('#' + o.ids.presenterImage).attr({ 'src': presenter.ImageUrl, 'title': presenter.Name, 'alt': presenter.Name });
                            } else if (o.defaultPresenterImageSrc) {
                                $('#' + o.ids.presenterImage).attr({ 'src': o.defaultPresenterImageSrc, 'title': presenter.Name, 'alt': presenter.Name });
                            }
                        });

                        $('#' + o.ids.presenters + ' ul').append($item);
                    });
                }
            }
        }
    });
})(jQuery);