/*globals $, jQuery, window, document, Base64 */
var AudioPlayer = (function ($) {
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) {
var fileName, path, parts = [];
if (file.indexOf('.mp3') === -1 || file.indexOf('.ogg') === -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;
}
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('href');
} else if (doOgg) {
audioFile = ogg.attr('href');
}
if (!audioFile) {
audioFile = mp3.attr('href');
}
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/',
preload: 'none',
ready: function () {
playlistInit();
},
errorAlerts: true,
warningAlerts: true,
nativeSupport: 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;
return {
};
};
}(jQuery));
(function ($) {
$(document).ready(function () {
$('.audio-playlist').each(function () {
return new AudioPlayer(this);
});
});
}(jQuery));