/*globals $, jQuery, window, document, Base64 */ var AudioPlayer = (function ($) { "use strict"; var instance = 1; function playsMP3() { var a = document.createElement('audio'); return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, '')); } function playsOgg() { var a = document.createElement('audio'); return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')); } function maybe_decode(file) { return Base64.decode(file); } function createInstance(indx) { return ['
', '
', '
', '
', '', '', '', '', '
', '
'].join(''); } return function (wrapper) { var elem = $(wrapper), player, thisInstance = instance, markup, songs, oggs, matches, doOgg = false, bNext, bPrev, bPlay, bPause, playItem = 0, playlist = [], oggFiles = 0, nowPlaying, AUTOPLAY = false; function playlistInit() { if (AUTOPLAY) { playlistChange(playItem); } else { playlistConfig(playItem); } } function playlistConfig(index) { var track = playlist[index], meta; meta = ['', track.song, '', '' !== track.album ? (' from ' + track.album + ' ') : '', 'by ', track.artist].join(''); nowPlaying.html(meta); playItem = index; if (doOgg) { player.jPlayer('setMedia', { oga: playlist[playItem].source }); } else { player.jPlayer('setMedia', { mp3: playlist[playItem].source }); } } function playlistChange(index) { playlistConfig(index); player.jPlayer('play'); } function playlistNext() { var index = (playItem + 1 < playlist.length) ? playItem + 1 : 0; playlistChange(index); } function playlistPrev() { var index = (playItem - 1 >= 0) ? playItem - 1 : playlist.length - 1; playlistChange(index); } function doComplete() { playlistNext(); } function doPrev() { playlistPrev(); $(this).blur(); return false; } function doNext() { playlistNext(); $(this).blur(); return false; } function doPlay() { } songs = elem.find('.haudio'); oggs = elem.find('a[type="audio/ogg"]'); matches = songs.length === oggs.length; doOgg = matches && playsOgg(); songs.each(function () { var elem = $(this), audioFile, mp3, ogg; ogg = elem.find('a[type="audio/ogg"]'); mp3 = elem.find('a[type="audio/mpeg"]'); if (playsMP3()) { audioFile = mp3.attr('data-src'); } else if (doOgg) { audioFile = ogg.attr('data-src'); } if (!audioFile) { audioFile = mp3.attr('data-src'); } playlist.push({ song : elem.find('.fn').eq(0).text(), album: elem.find('.album').text(), artist: elem.find('.org').text(), source: maybe_decode(audioFile) }); elem.click(function () { playlistChange(songs.index(this)); }); }); markup = createInstance(instance); elem.prepend($(markup)); nowPlaying = elem.find('.now-playing'); bNext = elem.find('.jp-next'); bPrev = elem.find('.jp-previous'); bPlay = elem.find('.jp-play'); bPause = elem.find('.jp-pause'); if (!player) { player = $('#jquery_jplayer_' + instance); player .jPlayer({ swfPath: 'http://' + window.location.host + '/wp-content/plugins/audio/js/jplayer-02272012.swf', preload: 'none', ready: playlistInit, nativeSupport: true, noFullScreen: true, noVolume: true, oggSupport: doOgg, supplied : (doOgg ? 'oga' : 'mp3'), cssSelectorAncestor: '#jp_interface_' + instance }) .bind($.jPlayer.event.play, function () { $(this).jPlayer('pauseOthers'); }) .jPlayer('onSoundComplete', doComplete); } bPrev.click(doPrev); bNext.click(doNext); bPlay.click(doPlay); instance += 1; }; }(jQuery)); (function ($) { $(document).ready(function () { $('.audio-playlist').each(function () { return new AudioPlayer(this); }); }); }(jQuery));