/* jqIzeYoutubePlaylist - (c) 2010 Emil Müller http://ize.webhop.net/home/jquery-plugins/jqizeyoutubeplayer Released under the GPL License http://www.gnu.org/licenses/gpl-3.0.txt requires: - jQuery 1.4.x */ (function ($) { $.fn.IzeYoutubePlaylist = function (settings) { var currentItem = null; var config = { width: 260, height: 'auto', playlistID: '37646B34DB2C47DB', maxResults: 50, //50 is max onPlaylistLoaded: function () { }, onItemClick: function () { } }; if (settings) $.extend(config, settings); init = function (el) { setupMainContainer(el); loadPlaylist(el, 1); } setupMainContainer = function (el) { $(el).css({ width: config.width, height: config.height, position: 'relative' }); } loadPlaylist = function (el, startindex) { //load youtube playlist in json format //eg. http://gdata.youtube.com/feeds/api/playlists/021B87CCA16D5E96?v=2&max-results=50&alt=json //clear current playlist $(el).empty(); //pass format=5 to retrieve only videos that may be embedded $.getJSON('http://gdata.youtube.com/feeds/api/playlists/' + config.playlistID + '?v=2&format=5&start-index=' + startindex + '&max-results=' + config.maxResults + '&alt=json-in-script&callback=?', function (data) { //populate playlist var playlist = $("
").attr("id", "playlist").attr("start", startindex).attr("results", data.feed.openSearch$totalResults.$t); for (var i = 0; i < data.feed.entry.length; i++) { var entry = $("
").addClass("item"); entry.attr("url", data.feed.entry[i].media$group.media$content[0].url) .attr("videoid", data.feed.entry[i].media$group.yt$videoid.$t) .attr("index", i) .click(function () { doItemClick($(this)); }); if (i == 0) entry.attr("first", "true"); if (i == data.feed.entry.length - 1) entry.attr("last", "true"); var title = $('
').addClass("title").text(data.feed.entry[i].title.$t); var description = $('
').addClass("description").text(data.feed.entry[i].media$group.media$description.$t); var thumbnail = $('
').addClass("thumbnail").css({ width: '120px', height: '70px', background: "url('" + data.feed.entry[i].media$group.media$thumbnail[0].url + "') no-repeat center center" }); var duration = $('
').addClass("duration").text(secondsToString(data.feed.entry[i].media$group.yt$duration.seconds)); entry.append(thumbnail); entry.append(title); entry.append(description); entry.append(duration); playlist.append(entry); } var playlistNavigation = $("
").attr("id", "playlistnavigation"); //should we display previous link for playlist? /*if ((data.feed.openSearch$startIndex.$t) > data.feed.openSearch$itemsPerPage.$t) playlistNavigation.append($('').click(function () { loadPlaylist(el, startindex - config.maxResults); })); //should we display next link for playlist? if ((data.feed.openSearch$startIndex.$t + data.feed.openSearch$itemsPerPage.$t) <= data.feed.openSearch$totalResults.$t) playlistNavigation.append($('').click(function () { loadPlaylist(el, startindex + config.maxResults); })); playlist.append(playlistNavigation); */ $(el).append(playlist); //set current item to first currentItem = $("#playlist div").first(); doPlaylistLoaded(el); }); } secondsToString = function (seconds) { var hours = Math.floor(parseInt(seconds) / (60 * 60)); var minutes = Math.floor((parseInt(seconds) - (hours * 60 * 60)) / 60); seconds = parseInt(seconds) - (hours * 60 * 60) - (minutes * 60); if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; if (hours > 0) return hours + ":" + minutes + ":" + seconds; else return minutes + ":" + seconds; } doPlaylistLoaded = function (el) { if ($.isFunction(config.onPlaylistLoaded)) config.onPlaylistLoaded.call(this); } doItemClick = function (item) { currentItem = item; $("#playlist div").removeClass("current"); currentItem.addClass("current"); if ($.isFunction(config.onItemClick)) config.onItemClick.call(this, item.attr("index"), item.attr("videoid"), item.attr("url")); } this.seekFirst = function () { if (currentItem != null) { currentItem = $("#playlist div").first(); currentItem.trigger('click'); } } this.seekPrevious = function () { if (currentItem != null) { if (currentItem.attr("first") != "true") { currentItem = currentItem.prev(); currentItem.trigger('click'); } else { this.seekEnd(); } } } this.seekNext = function () { if (currentItem != null) { if (currentItem.attr("last") != "true") { currentItem = currentItem.next(); currentItem.trigger('click'); } else { this.seekFirst(); } } } this.seekEnd = function () { if (currentItem != null) { currentItem = $("#playlist div").last(); currentItem.trigger('click'); } } this.each(function () { //setup element init(this); }); //put instance of this object in data $(this).data("ytplaylist", this); return this; }; })(jQuery);