
var ReAudioPlayer = {
  playlistPosition: 0,
  playlist: [],
  isPlaying: false,
  currentVolume: 0.9,

  init: function() {
    // hook up rollovers
    $('.re-player-button').hover(function() { $(this).addClass('over') }, function() { $(this).removeClass('over') });
    $('.re-player-button').mousedown(function() { $(this).addClass('down') });
    $('.re-player-button').mouseup(function() { $(this).removeClass('down') });
    $('.re-player-button').mouseout(function() { $(this).removeClass('down') });

    // hook up button actions
    $('#re-player-play').click(function() { ReAudioPlayer.togglePlay(); });
    $('#re-player-prev').click(function() { ReAudioPlayer.prev(); });
    $('#re-player-next').click(function() { ReAudioPlayer.next(); });
    $('#re-player-volume-up').click(function() { ReAudioPlayer.volumeUp(); });
    $('#re-player-volume-down').click(function() { ReAudioPlayer.volumeDown(); });

    // build the playlist
    $('#re-player-tracks li a').each(function() {
      ReAudioPlayer.buildPlaylistItem(this);
    });

    // build the progress bar
    $('#re-player-progress-bar').click(function(evt) {
      var posX = evt.pageX - $(this).offset().left;
      var seek = Math.min(Math.max(posX - 3, 0), 100);
      ReAudioPlayer.seekTo(seek);
    });

    $('#re-jplayer').jPlayer({
      swfPath: '/wp-content/themes/reality-tv/js',
      preload: 'auto',
      ready: function() {
        ReAudioPlayer.loadTrack(0);
      },
      volumechange: function(evt) {
        // $('#re-player-track-counter').html(Math.round(evt.jPlayer.status.volume * 100) + '%');
      },
      timeupdate: function(evt) {
        var evtTime = evt.jPlayer.status.currentTime;
        var current = sprintf("%02d:%02d", evtTime / 60, evtTime % 60);
        $('#re-player-track-counter').html(current);
        $('#re-player-progress-bar-slider').css('left', (evt.jPlayer.status.currentPercentAbsolute - 104) + 'px');
      },
      ended: function(evt) {
        ReAudioPlayer.next();
      }
    });

    // ReAudioPlayer.play();
  },

  buildPlaylistItem: function(a) {
    this.playlist.push({
      url: $(a).attr('href'),
      title: $(a).find('span.track-title').text(),
      artist: $(a).find('span.track-artist').text()
    });
  },

  loadTrack: function(trackNumber) {
    if(trackNumber >= 0 && trackNumber < this.playlist.length) {
      this.playlistPosition = trackNumber;
      $('#re-player-current-track').text(this.currentTrack().title);
      $('#re-player-current-artist').text(this.currentTrack().artist);
      $('#re-jplayer').jPlayer("setMedia", { mp3: this.currentTrack().url }).jPlayer("load");
    }
  },

  currentTrack: function() {
    return this.playlist[this.playlistPosition];
  },

  togglePlay: function() {
    if(this.isPlaying) {
      this.pause();
    }
    else {
      this.play();
    }
  },

  play: function() {
    $('#re-player-play').removeClass('play').addClass('pause');
    $('#re-jplayer').jPlayer("play");
    this.isPlaying = true;
  },

  stop: function() {
    $('#re-player-play').removeClass('pause').addClass('play');
    $('#re-jplayer').jPlayer("stop");
    this.isPlaying = false;
  },

  pause: function() {
    $('#re-player-play').removeClass('pause').addClass('play');
    $('#re-jplayer').jPlayer("pause");
    this.isPlaying = false;
  },

  prev: function() {
    this.loadTrack((this.playlistPosition - 1) % this.playlist.length);
    this.play();
  },

  next: function() {
    this.loadTrack((this.playlistPosition + 1) % this.playlist.length);
    this.play();
  },

  seekTo: function(seek) {
    // $('#re-player-progress-bar-slider').css({ 'left': (seek - 104) + 'px', 'top': '0px' });
    $('#re-jplayer').jPlayer('playHead', seek);
  },

  volumeUp: function() {
    this.currentVolume = Math.min(1, this.currentVolume + 0.1);      
    $('#re-jplayer').jPlayer('volume', this.currentVolume);
  },

  volumeDown: function() {
    this.currentVolume = Math.max(0, this.currentVolume - 0.1);
    $('#re-jplayer').jPlayer('volume', this.currentVolume);
  }
}

