vp = {
	init: function() {
		ub.e.attach(ub, 'load', function(e) {
			soundManagerInit();
			this.play_theme();
			this.start_balls(100);
		}, false, this);

		ub.e.attach(document, 'click', function(e) {
			soundManager.play('vote');
			setTimeout(function() { 
				soundManager.play('pompous') 
			}, 500);
		}, false, this);
		
	},
	play_theme: function() {
		setTimeout(function() {
			soundManager.play('vote');
		}, ub.u.randNum(0, 2000));
		setTimeout(function() {
			soundManager.play('pompous');
		}, ub.u.randNum(0, 2000));
		setTimeout(function() {
			vp.play_theme();
		}, ub.u.randNum(2000, 3000));
	},
	balls: [],
	ball_interval: null,
	ball_bounds: null,
	ball_offset: null,
	ball_dims: [85,100],
	balls_free: false,
	viewport: ub.u.viewportDims(),
	start_balls: function(ball_count) {
		var container = ub.$('heads_container');
		var dims = ub.n.getDims(container);
		this.ball_bounds = [[0,0],[dims[0]-this.ball_dims[0],dims[1]-this.ball_dims[1]]];
		for(var i=0;i<ball_count;i++)
			this.make_ball([
				ub.u.randNum(this.ball_bounds[0][0], this.ball_bounds[1][0]),
				ub.u.randNum(this.ball_bounds[0][1], this.ball_bounds[1][1])
			]);
		if(!this.ball_interval) {
			this.ball_interval = setInterval(function() { vp.move_balls(); }, 1000/32);
		}
	},
	stop_balls: function() {
		if(this.ball_interval)
			clearInterval(this.ball_interval);
		delete this.ball_interval;
	},
	rand_num: function(floor, ceil) {
		return Math.floor((ceil-floor+1)*Math.random()) + floor;
	},
	make_ball: function(start_pos) {
		var container = ub.$('heads_container');
		var img = document.createElement('img');
		img.style['position'] = 'absolute';
		img.style['display'] = 'block';
		img.style['zIndex'] = '0';
		img.style['left'] = start_pos[0] +'px';
		img.style['top'] = start_pos[1] +'px';
		//img.setAttribute('style', 'left: '+ start_pos[0] +'px; top: '+ start_pos[1] +'px; display: block; position: absolute; z-index: -1;');
		container.appendChild(img);
		var obj = ub.a.append(this.balls, { 
			img: img, 
			pos: start_pos, 
			dir: [ub.u.randNum(0,1),ub.u.randNum(0,1)],
			speed: ub.u.randNum(5,20)
		});
		//initialize the first src
		obj.img.setAttribute('src', '/images/pompous_head.png');
		delete container;
		delete img;
	},
	move_balls: function() {
		var container = ub.$('heads_container');
		var dims = ub.n.getDims(container);
		var bounds = [[0,0],[dims[0]-this.ball_dims[0],dims[1]-this.ball_dims[1]]];

		var count = this.count++;
		ub.a.each(this.balls, function(ball, i) {
			//inc or dec ball X/Y position
			ball.pos[0] += (ball.dir[0]) ? ball.speed : -ball.speed;
			ball.pos[1] += (ball.dir[1]) ? ball.speed : -ball.speed;
			
			//keep position within boundaries
			if(
				(ball.pos[0] >= bounds[1][0])
			) {
				ball.dir[0] = 0;
				ball.pos[0] = bounds[1][0];
			}
			else if(
				(ball.pos[0] <= bounds[0][0])
			) {
				ball.dir[0] = 1;
				ball.pos[0] = bounds[0][0];
			}
			if(
				(ball.pos[1] >= bounds[1][1])
			) {
				ball.dir[1] = 0;
				ball.pos[1] = bounds[1][1];
			}
			else if(
				(ball.pos[1] <= bounds[0][1])
			) {
				ball.dir[1] = 1;
				ball.pos[1] = bounds[0][1];
			}
			
			//position the ball
			ball.img.style['left'] = ball.pos[0] + 'px';
			ball.img.style['top'] = ball.pos[1] + 'px';

		});
	}
};

vp.init();




