/* global soundManager */

require('./lib/soundmanager2');

/**
 * Class for loading music
 * @constructor
 */
var Assets = function() {
  /**
   * Loaded sounds
   * @type {object}
   */
  this.sounds = {};

  /**
   * default path for assets
   * @type {string}
   */
  this.basePath = 'assets/';

  /**
   * Default path for sound
   * @type {string}
   */
  this.soundsPath = 'sounds/';
};

/**
 * Method that creates sound object
 * @param {string} path - sound path (without base path)
 * @param {function} callback - callback function that is called when sound is loaded
 */
Assets.prototype.createSound = function(path, callback) {
  var self = this;

  soundManager.onload = function() {
    var sound = soundManager.createSound({
      autoLoad: true,
      autoPlay: false,
      url: self.basePath + self.soundsPath + path
    }).load({ onload: callback });

    self.sounds[path] = sound;
  };
};

/**
 * Load specified data
 * @param {object} data - data to load
 * @param {function} callback - function that is called when everything is loaded
 */
Assets.prototype.load = function(data, callback) {
  this.soundsPath = data.soundsPath || this.soundsPath;
  this.assetsPath = data.assetsPath || this.assetsPath;

  var soundsToLoad = data.sounds.length;

  var soundLoaded = function() {
    soundsToLoad -= 1;
    if (soundsToLoad <= 0) {
      callback();
    }
  };

  for (var i=0, len=data.sounds.length; i<len; i++) {
    var path = data.sounds[i];
    this.createSound(path, soundLoaded);
  }
};

/**
 * Get sound with specified name
 * @param {string} name - sound name
 * @return {object} sound object
 */
Assets.prototype.getSound = function(name) {
  return this.sounds[name];
};

module.exports = Assets;

@@ -1106,37 +1106,37 @@
 lback) %7B%0A  this.
-sound
+asset
 sPath = data.sou
@@ -1132,21 +1132,21 @@
  = data.
-sound
+asset
 sPath %7C%7C
@@ -1155,32 +1155,84 @@
 his.
-soundsPath;%0A  this.asset
+assetsPath;%0A%0A  if (!data.sounds) %7B%0A    callback();%0A  %7D else %7B%0A    this.sound
 sPat
@@ -1232,37 +1232,37 @@
 undsPath = data.
-asset
+sound
 sPath %7C%7C this.as
@@ -1259,29 +1259,29 @@
 %7C%7C this.
-asset
+sound
 sPath;%0A%0A
   var so
@@ -1268,24 +1268,26 @@
 oundsPath;%0A%0A
+  
   var sounds
@@ -1318,16 +1318,18 @@
 gth;%0A%0A  
+ 
+ 
 var soun
@@ -1355,16 +1355,18 @@
 ) %7B%0A    
+  
 soundsTo
@@ -1380,16 +1380,18 @@
  1;%0A    
+ 
+ 
 if (soun
@@ -1409,24 +1409,26 @@
  0) %7B%0A      
+  
 callback();%0A
@@ -1435,16 +1435,22 @@
     
+ 
+ 
 %7D%0A
+  
   %7D;%0A%0A
+  
   fo
@@ -1499,24 +1499,26 @@
  i++) %7B%0A    
+  
 var path = d
@@ -1532,24 +1532,26 @@
 nds%5Bi%5D;%0A    
+ 
+ 
 this.createS
@@ -1575,16 +1575,22 @@
 oaded);%0A
+    %7D%0A
   %7D%0A%7D;%0A%0A

fix bug when there is no sound to load