function debug(debugString) {
if (typeof console != "undefined" && typeof console.debug != "undefined") {
console.debug(debugString);
}
}
function loadAlbumList() {
$.ajaxSetup({'beforeSend': function(xhr){
if (xhr.overrideMimeType)
xhr.overrideMimeType("text/plain");
}
});
debug('Loading Alum Lists');
$.getJSON('albums/albums.json',
{'id': 'hello'},
processAlbumList);
debug('I am the walru');
}
function processAlbumList(data, c) {
for (var album in data) {
var newAlbum = $('
' + data[album].title + ' ').clone();
var clickAlbum = (function (a) {
return function () {
loadAlbum(a);
$('.albumlist li').removeClass('selected');
$(this).addClass('selected');
};
})(data[album].contents);
debug("Album: " + album + "/" + data[album].title);
$('.albumlist').append(newAlbum);
$(newAlbum).click(clickAlbum);
}
if (navigateDirect !== undefined) {
navigateDirect();
}
}
function loadAlbum(jsonURL) {
$.ajaxSetup({'beforeSend': function(xhr){
if (xhr.overrideMimeType)
xhr.overrideMimeType("text/plain");
}
});
debug('Loading Album from ' + jsonURL);
$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
$.getJSON(jsonURL,
null,
processAlbumData);
switchToAlbumBox();
}
function processAlbumData(data, c) {
debug("Processing album data");
$('.albumlinernotes p').remove();
$('.albumlinernotes').append(data.linernotes);
$('.albumdetails h1').html(data.title);
if (data.flattr) {
debug('flattr found: ' + data.flattr);
$('.albumdetails h1').prepend(data.flattr);
}
$('.albumdetails .songlist tr').remove();
if (data.playlist) {
$('.albumdetails .songlist').append(
"Play Full Album ");
}
for (var i in data.songs) {
var song = data.songs[i];
// TODO: do this when html5 audio works better
// $('.albumdetails .songlist').append(""+ song[1] + " ");
var songID = "song" + i;
if (song[0] && song[0].length > 0) {
$('.albumdetails .songlist').append(
""
// + " "
// + " "
+""+ song[1] + " ");
$('#' + songID + " .addPlayButton").click((function(songFile, songName) {
return function() {
addTrackToPlay(songFile, songName);
};
})(song[0], song[1]));
$('#' + songID + " .playButton").click((function(songFile, songName) {
return function() {
playTrack(songFile, songName);
};
})(song[0], song[1]));
} else {
$('.albumdetails .songlist').append(
""
+""+ song[1] + " ");
}
}
$('.imageContainer img').remove();
var width = "20%";
if (data.images != null) {
if (data.images.length == 2) {
width = "30%";
} else if (data.images.length == 1) {
width = "65%";
}
}
for (var j in data.images) {
$('.imageContainer').append(
" ");
}
}
function switchToInfoBox() {
$('.albuminfo').hide();
$('.generalInfo').show();
$('.topContentBox').addClass('infoBox');
}
function switchToAlbumBox() {
$('.topContentBox').removeClass('infoBox');
$('.generalInfo').hide();
$('.albuminfo').show();
}
function initInfoLinks() {
debug("dfsdf");
$('a.infoLink').click(function() {
$.get($(this).attr('href'),
null,
function(data) {
$('.generalInfo').html(data);
switchToInfoBox();
});
return false;
});
}
var niftyPlayerActive = false;
var trackNameList = [];
var trackList = [];
var trackListPos = 0;
function refreshTrackListUL() {
$('.trackList').empty();
for (var i in trackNameList) {
var trackTD;
if (i == trackListPos) {
trackTD = "" + trackNameList[i] + " ";
} else {
trackTD = "" + trackNameList[i] + " ";
}
var upTD = " ";
var downTD = " ";
var trashTD = " ";
$('.trackList').append("" + trackTD + upTD + downTD + trashTD + " ");
$('#playList' + i).click((function(songFile, songName) {
return function() {
playExistingTrack(songFile, songName);
};
})(trackList[i], trackNameList[i]));
$('#playlist_trash' + i).click((function(index) {
return function() {
playlistTrashEntry(index);
};
})(i));
$('#playlist_down' + i).click((function(index) {
return function() {
playlistMoveEntryDown(index);
};
})(i));
$('#playlist_up' + i).click((function(index) {
return function() {
playlistMoveEntryUp(index);
};
})(i));
}
}
function playlistTrashEntry(index) {
debug("Deleting playlist entry#" + index);
trackList.splice(index, 1);
trackNameList.splice(index, 1);
var deletingCurrentTrack = (trackListPos == index);
if (trackListPos >= index) {
trackListPos--;
}
if (deletingCurrentTrack) {
playNextTrack();
}
refreshTrackListUL();
}
function playlistMoveEntryDown(index) {
debug("Moving down from " + index);
if (index < trackList.length-1) {
var tmp = trackList[+index+1];
trackList[+index+1] = trackList[index];
trackList[index] = tmp;
var tmp2 = trackNameList[+index+1];
trackNameList[+index+1] = trackNameList[index];
trackNameList[index] = tmp2;
if (index == trackListPos) {
trackListPos++;
}
refreshTrackListUL();
}
}
function playlistMoveEntryUp(index) {
debug("Moving up from " + index);
if (index > 0) {
var tmp = trackList[+index-1];
trackList[+index-1] = trackList[index];
trackList[index] = tmp;
var tmp2 = trackNameList[+index-1];
trackNameList[+index-1] = trackNameList[index];
trackNameList[index] = tmp2;
if (index == trackListPos) {
trackListPos--;
}
refreshTrackListUL();
}
}
function playNextTrack() {
debug("Play next track");
trackListPos++;
if (trackListPos < trackList.length) {
niftyplayer("antimonNiftyPlayer").loadAndPlay(trackList[trackListPos]);
}
refreshTrackListUL();
}
function initNiftyPlayer() {
if (niftyPlayerActive === undefined || !niftyPlayerActive) {
debug("Init Nifty Player");
niftyPlayerActive = true;
niftyplayer("antimonNiftyPlayer").registerEvent("onSongOver", "playNextTrack()");
niftyplayer("antimonNiftyPlayer").registerEvent("onBufferingStarted", "bufferingStarted()");
niftyplayer("antimonNiftyPlayer").registerEvent("onBufferingComplete", "bufferingComplete()");
}
}
function bufferingStarted() {
alert("Buffering started");
}
function bufferingComplete() {
alert("Buffering complete");
}
function playTrack(songFile, songName) {
initNiftyPlayer();
debug("Play track: " + songFile);
niftyplayer("antimonNiftyPlayer").loadAndPlay(songFile);
niftyplayer("antimonNiftyPlayer").pause();
setTimeout(function(){
niftyplayer("antimonNiftyPlayer").play(songFile);
},1500);
trackList.push(songFile);
trackNameList.push(songName);
trackListPos = trackList.length - 1;
refreshTrackListUL();
}
function playExistingTrack(songFile, songName) {
initNiftyPlayer();
debug("Play Existing track: " + songFile);
niftyplayer("antimonNiftyPlayer").loadAndPlay(songFile);
trackListPos = $(".trackList tr:contains('" + songName + "')").index();
debug("Clicked index " + trackListPos);
refreshTrackListUL();
}
function addTrackToPlay(songFile, songName) {
debug("Add track: " + songFile);
trackList.push(songFile);
trackNameList.push(songName);
refreshTrackListUL();
}
$(document).ready(function(){
loadAlbumList();
initInfoLinks();
debug("can do html5 audio: " + !!document.createElement('audio').canPlayType);
});