//Particle
var bubblePath = '/images/bubble.png';
var bubbleNum = 15;

var wrapH = 600;
var bodyW;

var Particle = function(x, y, vy) {
	this.vx = 0;
	this.vy = Math.floor(Math.random() * -2) - 1;
	this.width = Math.floor(Math.random() * 3) + 3;
	this.x = x - this.width / 2;
	this.y = y - this.width / 2;
	this.id = "ptc" + Particle.initID;
	Particle.initID++;
};
Particle.initID = 1;
Particle.prototype = {
	update : function() {
		this.x += this.vx;
		this.y += this.vy;
		var thisObj = $('#' + this.id);
		if(this.y - this.width < 0) {
			this.y = wrapH;
			this.x += 50;
			if(this.x > bodyW) {
				this.x -= bodyW;
			}
			thisObj.css({
				top : Math.floor(this.y),
				left : Math.floor(this.x)
			});
		} else {
			thisObj.css({
				top : Math.floor(this.y),
				left : Math.floor(this.x)
			});
		}
	},
	src : function() {
		var s = '<div id="' + this.id + '"><img src="' + bubblePath + '" /></div>';
		return s;
	},
	setStyle : function() {
		$('#' + this.id).css({
			position : 'absolute',
			top : Math.floor(this.y),
			left : Math.floor(this.x)
		}).find('img').css({
			height : this.width + 'px',
			width : this.width + 'px'
		});

	}
}

//Particles
var Particles = function() {
};
Particles.data = {};
Particles.add = function(perticle) {
	Particles.data[perticle.id] = perticle;
}
Particles.remove = function(perticleID) {
	delete Particles.data[perticleID];
}
//Enterframe
var Enterframe = function() {
}
Enterframe.timer = null;
Enterframe.start = function() {
	clearInterval(Enterframe.timer);
	var fps = Math.floor(1000 / 60);
	Enterframe.timer = setInterval(Enterframe.update, fps);
}
Enterframe.update = function() {
	for(var particle in Particles.data) {
		Particles.data[particle].update();
	}
}
//Base
var Base = function() {
};
Base.height = 0;
Base.prototype = {
	update : function() {
		Base.height = $('#contents').height();
	}
}

//init
$(function() {
	Enterframe.start();
	Particles.add(new Base());
	bodyW = $('body').width();
	var wrap = $('#bubbleWrap');
	var p;
	for(var j = 0; j < bubbleNum; j++) {
		var third = bodyW / 3;
		var startX = Math.floor((Math.random() * third) + third * (j % 3));
		var startY = Math.floor(Math.random() * wrapH) + 300;
		for(var i = 0; i < 20; i++) {
			startY = startY + (i - 1);
			p = new Particle(startX, startY);
			wrap.append(p.src());
			p.setStyle();
			Particles.add(p);
		}
	}
});

