var audio; var utid; $(function(){ $.extend($.fn.disableTextSelect = function() { return this.each(function(){ if ($.browser.mozilla) {//Firefox $(this).css('MozUserSelect','none'); } else if ($.browser.msie) {//IE $(this).bind('selectstart',function(){return false;}); } else {//Opera, etc. $(this).mousedown(function(){return false;}); } }); }); audio = document.getElementsByTagName('audio')[0]; $('#player .play').click(function(){ if (! audio.src) return; audio.play(); utid = window.setTimeout(updatePlayer, 100); $('#player .pause').show(); $('#player .play').hide(); }).disableTextSelect(); $('#player .pause').click(function(){ audio.pause(); clearTimeout(utid); $('#player .pause').hide(); $('#player .play').show(); }).disableTextSelect(); $('.tracks .play').click(function(){ addToPlaylist( $('h1.artist').html(), $(this).parent().find('.track-name').html(), Math.round($(this).parent().find('.duration').attr('data-length') / 1000), $(this).parent().attr('id') ); }) $('#player .prev').click(function(){ playPrev(); }).disableTextSelect(); $('#player .next').click(function(){ playNext(false); }).disableTextSelect(); $('#player .shuffle, #player .repeat').click(function(){ $(this).toggleClass('on'); }).disableTextSelect(); $('#player .playlist').click(function(){ $('#playlist').toggle(); //$('#playlist').data('jsp').reinitialise(); }) $('#playlist').hide(); }) function updatePlayer() { duration = audio.duration; cur_time = audio.currentTime; if (cur_time == duration) { playNext(true); } loaded = 0; if ((audio.buffered != undefined) && (audio.buffered.length != 0)) { loaded = Math.round((audio.buffered.end(0) / audio.duration) * 390); $('#player .time-played').html(formatTime(cur_time)); $('#player .time-left').html(formatTime(duration - cur_time)); progress = Math.round((cur_time / duration) * 390); $('#player .progress-loaded').css('width', loaded +'px') $('#player .progress-point').css('margin-left', progress +'px') } utid = window.setTimeout(updatePlayer, 100); } function formatTime(sec) { sec = Math.round(sec); m = Math.floor(sec / 60); s = sec - (m * 60); return m +':'+ (s < 10 ? '0' : '') +s; } function playTrack(artist, track, id) { $(audio).attr('src', '/listen/'+ id +'.mp3'); $('#player .track-title').html(artist +' — '+ track); $('#player .time-played').html('0:00'); $('#player .time-left').html('0:00'); $('#player .progress-loaded').css('width', 0 +'px') $('#player .progress-point').css('margin-left', 0 +'px') $('#player .play').trigger('click'); } function playPrev() { if ($('#playlist ul.list li').length == 0) return false; if ($('#playlist ul.list li.now-playing').prev().length == 0) { $('#playlist ul.list li:last').dblclick(); } else { $('#playlist ul.list li.now-playing').prev().dblclick(); } } function playNext(auto) { if ($('#playlist ul.list li').length == 0) return false; if ($('#player .shuffle').hasClass('on')) { $('#playlist ul.list li').rand().dblclick(); } if ($('#playlist ul.list li.now-playing').next().length == 0) { if ((auto && $('#player .repeat').hasClass('on')) || !auto) { $('#playlist ul.list li:first').dblclick(); } } else { $('#playlist ul.list li.now-playing').next().dblclick(); } } function addToPlaylist(artist, track, length, id) { $('#playlist ul.list').append($( '