(function ($) {
var id = '#jquery_jplayer',
wrapper, songs,
playItem = 0, playlist = [],
playTime, totalTime,
bNext, bPrev, bPlay, bPause,
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;
$(id).jPlayer('setFile', playlist[playItem].mp3);
}
function playlistChange(index) {
playlistConfig(index);
$(id).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 doProgress(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, tTime) {
playTime.text($.jPlayer.convertTime(playedTime));
totalTime.text($.jPlayer.convertTime(tTime));
}
function doComplete() {
playlistNext();
}
function doPrev() {
playlistPrev();
$(this).blur();
return false;
}
function doNext() {
playlistNext();
$(this).blur();
return false;
}
function maybe_decode(file) {
var fileName, path, parts = [];
if (file.indexOf('.mp3') === -1) {
if (file.indexOf('/') > -1) {
parts = file.split('/');
fileName = Base64.decode(parts[parts.length - 1]);
parts.pop();
parts.push(fileName);
file = parts.join('/');
} else {
file = Base64.decode(file);
}
}
if (file.indexOf('http://') === -1) {
file = 'http://' + window.location.host + decodeURIComponent(file);
}
return file;
}
$(document).ready(function () {
wrapper = $('.audio-playlist');
songs = wrapper.find('.haudio');
nowPlaying = $('#now-playing');
// load the playlist into playlist[]
// then remove DOM nodes
songs.each(function () {
var elem = $(this);
playlist.push({
song : elem.find('.fn').eq(0).text(),
album: elem.find('.album').text(),
artist: elem.find('.org').text(),
mp3 : maybe_decode(elem.find('a').attr('href'))
});
elem.click(function () {
playlistChange(songs.index(this));
});
});
// Local copy of jQuery selectors, for performance
playTime = $("#jplayer_play_time");
totalTime = $("#jplayer_total_time");
bNext = $('#jplayer_next');
bPrev = $('#jplayer_previous');
bPlay = $('#jplayer_play');
bPause = $('#jplayer_pause');
$(id)
.jPlayer({
swfPath: '/wp-content/plugins/audio/js/',
preload: 'none',
ready: function () {
playlistInit();
},
errorAlerts: true,
warningAlerts: true,
// Safari 5 breaks, hopefully will fix in future release
nativeSupport: !((Object.hasOwnProperty.call(navigator, 'vendor') &&
navigator.vendor.indexOf('Apple') > -1) &&
navigator.appVersion.indexOf('5.') > -1 &&
navigator.appVersion.indexOf('iPhone') === -1
),
oggSupport: false
})
.jPlayer('onProgressChange', doProgress)
.jPlayer('onSoundComplete', doComplete);
bPrev.click(doPrev);
bNext.click(doNext);
});
}(jQuery))