var Spark = function() {
  var self = this;
  this.b = 'images/';
  this.s = ['boule2_70.png','boule2_70.png'];
  this.i = this.s[this.random(this.s.length)];
  this.f = this.b + this.i; 
  this.n = document.createElement('img');  
          
  this.newSpeed().newPoint().display().newPoint().fly();
 
};

Spark.prototype.display = function() {
  jQuery(this.n)
   .attr('src', this.f)
   .css('position', 'absolute')
   .css('height','40px')
   .css('width','40px')
   .css('z-index', this.random(20))
   .css('top', this.pointY)
   .css('left', this.pointX);
                
  //jQuery(document.body).append(this.n);
  jQuery('.encart_70_panier').append(this.n);
  
  return this;
};

Spark.prototype.fly = function() {
  var self = this;
  jQuery(this.n).animate({
      "top": this.pointY,
      "left": this.pointX,
  }, this.speed, 'linear', function(){
    self.newSpeed().newPoint().fly();  
  });
};

Spark.prototype.newSpeed = function() {
  this.speed = (this.random(10) + 5) * 400;
  
  return this;
};

Spark.prototype.newPoint = function() {
  this.pointX = this.random( jQuery('.encart_70_panier').width() - 40);
  this.pointY = this.random(jQuery('.encart_70_panier').height() - 40);
        
  return this;
};

Spark.prototype.random = function(max) {
  return Math.ceil(Math.random() * max) - 1;
}

jQuery(function(){
  // Sparks
  var totalSparks = 9;
  var sparks = [];
  
  for (i = 0; i < totalSparks; i++){  
    sparks[i] = new Spark();
  }
 
});