(function () {
    'use strict';
    if (window.relatedLinks === undefined) {
        window.relatedLinks = {
            isInternalLink: function ($this) {
                return $this.attr('href').startsWith('/');
            },
            isPdfResource: function ($this) {
                return $this.attr('href').toLowerCase().indexOf('.pdf') > 0;
            },
            isDocResource: function ($this) {
                return $this.attr('href').toLowerCase().indexOf('.doc') > 0;
            },
            isDone: function ($this) {
                return $this.html().indexOf('<i') >= 0;
            },
            appendSpans: function ($this) {
                var _hasSpan = $this.html().indexOf('<span') >= 0;

                return (!_hasSpan ? '<span>' : '') + $this.html() + (!_hasSpan ? '</span>' : '');
            },
            appendIcons: function ($this) {
                var _isInternalLink = window.relatedLinks.isInternalLink($this),
                    _isDownloadableResource = (window.relatedLinks.isDocResource($this) || window.relatedLinks.isPdfResource($this)),
                    _iconMarkup = '<i aria-hidden="true" class="material-icons">#</i>',
                    _linkIconMarkup = _iconMarkup
                        .replace('#',
                            _isDownloadableResource
                                ? 'cloud_download'
                                : _isInternalLink
                                    ? 'arrow_forward'
                                    : 'launch'),
                    _pdfIconMarkup = _iconMarkup.replace('#', 'picture_as_pdf'),
                    _docIconMarkup = _iconMarkup.replace('#', 'description');


                var _icon = ""; //(_isDownloadableResource ? _pdfIconMarkup : '') + $this.html() + _linkIconMarkup;
                if (window.relatedLinks.isDocResource($this)) {
                    _icon += _docIconMarkup;
                }
                if (window.relatedLinks.isPdfResource($this)) {
                    _icon += _pdfIconMarkup;
                }
                return _icon + $this.html() + _linkIconMarkup;
            },
            insertIcons: function () {                
                $('.link-block-list li a').each(function () {
                    var $this = $(this);
                    if (window.relatedLinks.isDone($this)) {
                        return;
                    }
                    $this.html(window.relatedLinks.appendSpans($this));
                    $this.html(window.relatedLinks.appendIcons($this));
                });
            }
        }
    }   
})();
