var Spark = function() {
	var self = this;
	this.b = './images/';
	this.s = ['spark.png', 'spark2.png', 'spark3.png', 'spark4.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.newSpeed = function() {
	this.speed = (this.random(10) + 5) * 1100;

	return this;
};

Spark.prototype.newPoint = function() {
	var w_width = $(window).width();
	this.pointX = this.random(w_width-100);
	this.pointY = this.random(350);

	return this;
};

Spark.prototype.display = function() {
	$(this.n)
	.attr('src', this.f)
	.css('position', 'absolute')
	.css('z-index', this.random(20))
	.css('top', this.pointY)
	.css('left', this.pointX);

	$(document.body).append(this.n);

	return this;
};

Spark.prototype.fly = function() {
	var self = this;
	$(this.n).animate({
	"top": this.pointY,
	"left": this.pointX
	}, this.speed, 'linear', function(){
	    self.newSpeed().newPoint().fly();
	});
};

Spark.prototype.random = function(max) {
	var random = Math.ceil(Math.random() * max) - 1;
	if(random < 0 )
	    random = random * -1;
	return random;
}

$(document).ready(function()
{
	// Sparks
	var totalSparks = 40;
	var sparks = new Array();

	for (i = 0; i < totalSparks; i++){
		sparks[i] = new Spark();
	}
	
	setTimeout(function (){
		$(".g_image").animate({opacity:'1'},2500)
	},500);
  
});

