/** * jquery.youtubeplaylist.js * * By Dan Drayne * https://github.com/dan0/youtubeplaylist/ * * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php */ (function ( $, window, document, undefined ) { var pluginName = 'ytplaylist'; var defaults = { holderId: 'ytvideo', playerHeight: 300, playerWidth: 450, addThumbs: false, thumbSize: 'small', showInline: false, autoPlay: true, showRelated: true, allowFullScreen: false, deepLinks: false, onChange: function(){}, start: 1, secure: 'auto' //false|true|'auto' }; /** * Get a youtube id from a url * * @param {String} url Url to youtube video. * @return {String|Null} Id of video, or null if none found. */ function youtubeid(url) { var id = null; if (url.indexOf('//www.youtube.com') !== -1) { id = url.match("[\\?&]v=([^&#]*)")[1]; } else if (url.indexOf('//youtu.be') !== -1){ id = url.substr(url.lastIndexOf("/") + 1); } return id; } /** * Main plugin contstructor * * @param {Object} element object representing UL element. * @param {Object} options Options object. */ function Plugin(element, options ) { this.element = element; this.options = $.extend( {}, defaults, options) ; this._defaults = defaults; this._name = pluginName; this._protocol = (this.options.secure === 'auto') ? window.location.protocol === 'https:' ? 'https://' : 'http://' : this.options.secure ? 'https://' : 'http://'; this._autoPlay = (this.options.autoPlay) ? '&autoplay=1' : ''; this._showRelated = (this.options.showRelated) ? '&rel=1' : ''; this._fullscreen = (this.options.allowFullScreen) ? '&fs=1' : ''; this.init(); } Plugin.prototype = { /** * Initialise gallery * * Loop through
  • elements, setting up click * handlers etc. */ init: function() { var self = this; var initialItem = self.options.deepLinks && window.location.hash.indexOf('#yt-gal-') !== -1 ? window.location.hash : null; // Setup initial classification of content $(self.element).find('li').each(function(index) { var listItem = $(this); var listIndex = index + 1; listItem.find('a:first').each(function() { var link = $(this); var ytid = youtubeid(link.attr('href')); var replacedText = listItem.text(); link.data('yt-href', link.attr('href')); link.attr('href', '#yt-gal-' + listIndex); if (ytid) { link.addClass('yt-vid'); link.data('yt-id', ytid); if (self.options.addThumbs) { if(self.options.thumbSize == 'small') { thumbUrl = self._protocol + 'img.youtube.com/vi/' + ytid + '/2.jpg'; } else { thumbUrl = self._protocol + 'img.youtube.com/vi/' + ytid + '/0.jpg'; } var thumbHtml = '' + replacedText + ''; link.empty().html(thumbHtml + replacedText).attr('title', replacedText); } } else { //must be an image link (naive) link.addClass('img-link'); if (self.options.addThumbs) { var $img = $('').attr('src', link.data('yt-href')); link.empty().html($img).attr("title", replacedText); } } if (!self.options.deepLinks) { link.click(function(e) { e.preventDefault(); self.handleClick(link, self.options); self.options.onChange.call(); }); } }); var firstLink = $(listItem.children('a')[0]); if (initialItem) { if (firstLink.attr('href') === initialItem) { self.handleClick(firstLink, self.options); } } else if (listIndex === self.options.start) { self.handleClick(firstLink, self.options); } }); if (self.options.deepLinks) { $(window).bind('hashchange', function(e) { var hash = window.location.hash; var clicked = $(self.element).find('a[href="' + hash + '"]'); if (clicked.length > 0) { self.handleClick(clicked, self.options); } else if (hash === '') { self.handleClick($(self.element).find('a:first'), self.options); } }); } }, /** * Get old-style youtube embed code * * @param {Object} options Plugin options object. * @param {String} id ID of youtube video. * @return {String} HTML embed code. */ getOldEmbedCode: function(options, id) { //throw a youtube player in var html = ''; html += ''; html += ' '; html += ' '; if(options.allowFullScreen) { html += ' '; } html += ''; html += ''; return html; }, /** * Get new-style youtube embed code * * @param {Object} options Plugin options object. * @param {String} id ID of youtube video. * @return {String} HTML embed code. */ getNewEmbedCode: function(options, id) { var html = ''; html += '