(function() {
  var Player, Playlist;

  window.cl = function() {
    var arg, _i, _len, _results;
    if (console && console.log) {
      _results = [];
      for (_i = 0, _len = arguments.length; _i < _len; _i++) {
        arg = arguments[_i];
        _results.push(console.log(arg));
      }
      return _results;
    }
  };

  Player = {
    isPlaying: false,
    init: function(soundManager, opts) {
      if (opts == null) opts = {};
      this.sm = soundManager;
      this.opts = opts;
      return this;
    },
    play: function() {
      this.isPlaying = true;
      if (!this.song.paused) this.sm.stopAll();
      this.song.play();
      return this;
    },
    pause: function() {
      this.isPlaying = false;
      this.song.pause();
      return this;
    },
    togglePlaying: function() {
      if (this.isPlaying) {
        this.pause();
      } else {
        this.play();
      }
      return this;
    },
    loadFile: function(path) {
      var _this = this;
      this.song = this.sm.createSound({
        id: path,
        url: path,
        onfinish: function() {
          if (_this.opts['onfinish']) return _this.opts['onfinish']();
        }
      });
      return this;
    }
  };

  Playlist = {
    player: null,
    songs: [],
    current: 0,
    init: function(soundManager, list_elm) {
      var _this = this;
      this.player = Player.init(soundManager, {
        onfinish: function() {
          return _this.next();
        }
      });
      this.list = $(list_elm);
      this.songs = $.map(this.list.find("a"), function(a, i) {
        return a.href;
      });
      return this;
    },
    play: function(number) {
      this.current = number;
      this.player.loadFile(this.songs[number]).play();
      this.updateView();
      return this;
    },
    playPause: function() {
      if (!this.player.song) {
        this.play(0);
      } else {
        this.player.togglePlaying();
        this.updateView();
      }
      return this;
    },
    next: function() {
      var next;
      next = this.current === this.songs.length - 1 ? 0 : this.current + 1;
      this.play(next);
      return this;
    },
    prev: function() {
      var prev;
      prev = this.current === 0 ? this.songs.length - 1 : this.current - 1;
      this.play(prev);
      return this;
    },
    updateView: function() {
      this.list.find("li").removeClass("playing");
      $(this.list.find("li")[this.current]).addClass("playing");
      $("a#playPause").html(this.player.isPlaying ? "5" : "4");
      return this;
    }
  };

  soundManager.url = '/soundmanager2/swf/';

  soundManager.onload = function() {
    Playlist.init(soundManager, $(".playlist"));
    $(".playlist a").on('click', function(e) {
      e.preventDefault();
      return Playlist.play($(this).parent().index());
    });
    $("a.album").on('click', function(e) {
      e.preventDefault();
      return Playlist.play(0);
    });
    $("a#playPause").on('click', function(e) {
      e.preventDefault();
      return Playlist.playPause();
    });
    $("a[href = '#prev']").on('click', function(e) {
      e.preventDefault();
      return Playlist.prev();
    });
    return $("a[href = '#next']").on('click', function(e) {
      e.preventDefault();
      return Playlist.next();
    });
  };

}).call(this);

