var arkanoid;

function block(x, y) {
	this.element = document.createElement('input');
	this.element.type = 'button';
	this.element.className = 'block';
	this.x = x;
	this.y = y;
	this.element.style.left = this.x + 'px';
	this.element.style.top = this.y + 'px';
	arkanoid.element.appendChild(this.element);
}

function random(min, max) {
	return min + (max-min) * Math.random();
}

arkanoid = {
	init: function() {
		this.logo = $('logo').down('img');
		this.element = $('arkanoid');
		this.resize();
		this.ball = $('ball');
		this.vaus = $('vaus');
		this.starter = $('start');
		
		this.blocks = [];
		this.ball_pos = {x: 0, y: 0};
		this.ball_vel = {x: 0, y: 0};
		this.vaus_pos = this.size().width / 2;
		this.score = 0;
		this.easymode = false;
		this.amin_step = 0;
		this.last_update = (new Date() - 0);
		
		this.create_blocks();
		this.reset();
		this.scroll();
		this.update();
		this.resize();
		
		Event.observe(this.starter, 'click', function() {
			arkanoid.start();
		});
		Event.observe(window, 'resize', function() {
			arkanoid.resize();
		});
	},
	create_blocks: function() {
		var min = 3, max = 7, b = min + 1, reverse = false, sx = (max - b) / 2,
			start_x = (this.size().width) / 2 - (70 * max) / 2 - 115,
			start_y = 150;
		
		for (var y=0; y<5; y++) {						
			for (var x=0; x<b - (b == 8 ? 2 : (b == 6 ? 1 : 0)); x++) {
				var bl = new block(start_x + x * 70 + sx * 70 + x * 70, y * 35 + start_y);
				this.blocks.push(bl);
			}
			
			if (b < 7 && !reverse) {
				b += 2;
			} else {
				reverse = true;
				b -= 2;
			}
			sx = (max - b) / 2;
		}
	},
	start: function() {
		this.reset();
		this.starter.style.display = 'none';
		this.ball.checked = true;
		if (this.score == 0) {
			this.ball_vel = {x: 0, y: 5};
		}
		this.tick = setInterval(function() {
			arkanoid.update();
		}, 10);
	},
	reset: function() {
		$('score').hide();
		$('score').select('div').invoke('remove');
		
		this.last_update = null;
		this.ball.checked = false;
		this.dead_blocks = 0;
		if (this.score == 0) {
			this.ball_pos.x = (this.size().width - 40) / 2;
			this.ball_pos.y = (this.size().height / 4) * 3;
		}
		
		for (var i=0; i<this.blocks.length; i++) {
			var bl = this.blocks[i];
			bl.element.style.display = 'block';
			bl.dead = false;
			bl.wounded = false;
			bl.element.className = 'block';
			bl.element.disabled = false;
		}
	},
	stop: function() {
		this.reset();
		this.starter.style.display = 'block';
		clearInterval(this.tick);
	},
	update: function() {
		if (!this.last_update) {
			this.last_update = (new Date() - 0);
		}
		
		var now = (new Date() - 0),
			ms = (now - this.last_update) / 10;
		
		this.scroll_to_point();
		this.bounce();
		this.ball_pos.x += this.ball_vel.x * ms;
		this.ball_pos.y += this.ball_vel.y * ms;
		this.ball_vel.y *= (1 + this.score / 1000000);
		
		this.ball.style.left = this.ball_pos.x + 'px';
		this.ball.style.top = this.ball_pos.y + 'px';
		this.ball.style.display = 'block';
		this.last_update = now;
	},
	bounce: function() {
		var s = {
			size: 16,
			x: this.ball_pos.x,
			y: this.ball_pos.y,
			width: this.size().width,
			height: this.size().height,
			friction: -1,
			wall_score: 1,
			block_scroe: 5,
			logo_score: 50
		}
		
		this.logo_test(s);
		this.blocks_test(s);
		this.bounds_test(s);
		
		if (this.dead_blocks == this.blocks.length) {
			this.win();
		}
	},
	logo_test: function(s) {
		var bp = this.ball_pos;
		if (bp.x > s.width-120 && bp.x < s.width-60 && bp.y > 50 && bp.y < 120) {
			this.shake(this.ball_vel.x, this.ball_vel.y);
			this.ball_vel.x *= s.friction;
			this.ball_vel.y *= s.friction;
			this.score += s.logo_score;
		}
	},
	shake: function(dx, dy) {
		new Effect.Move(this.logo, {x: dx, y: dy, duration: 0.07, mode: 'relative', queue: 'end'});
		new Effect.Move(this.logo, {x: -dx*2, y: -dy*2, duration: 0.07, mode: 'relative', queue: 'end'});
		new Effect.Move(this.logo, {x: dx, y: dy, duration: 0.07, mode: 'relative', queue: 'end'});
	},
	blocks_test: function(s) {
		for (var i=0; i<this.blocks.length; i++) {
			var b = this.blocks[i];
			
			if (!b.dead) {
				if (this.hit_test(b)) {					
					this.score += s.block_scroe;
					this.ball_vel.y *= this.easymode ? 1 : s.friction;
					if (this.easymode) {
						this.ball_vel.y *= 0.5;
					}
					
					if (b.wounded) {
						b.dead = true;
						b.element.style.display = 'none';
						this.dead_blocks++;
					} else {
						b.wounded = true;
						b.element.className = 'block disabled';
						b.element.disabled = 'true';
					}
				}
			}
		}
	},
	bounds_test: function(s) {
		if (s.x + s.size > s.width) {
			this.ball_pos.x = s.width - s.size;
			this.ball_vel.x *= s.friction;
			this.score += s.wall_score;
		} else if (s.x < 0) {
			this.ball_pos.x = 0;
			this.ball_vel.x *= s.friction;
			this.score += s.wall_score;
		}
		
		if (s.y + s.size * 2 > s.height) {
			if (this.ball_pos.x >= this.vaus_pos && this.ball_pos.x <= this.vaus_pos + this.vaus_size) {
				this.ball_vel.x = -(this.vaus_pos + this.vaus_size / 2 - this.ball_pos.x) / 10;
				this.ball_pos.y = s.height - s.size * 2;
				this.ball_vel.y *= s.friction;
				this.score += s.wall_score;
			} else {
				this.score = 0;
				this.stop();
			}
		} else if (s.y < 0) {
			this.ball_pos.y = 0;
			this.ball_vel.y *= s.friction;
			this.score += s.wall_score;
		}
	},
	win: function() {
		$('score').show();
		this.starter.style.display = 'block';
		clearInterval(this.tick);
		write(this.score + '');
	},
	hit_test: function(b) {
		var bp = this.ball_pos;		
		return bp.x > b.x && bp.x < b.x + 70 && bp.y > b.y && bp.y < b.y + 20;
	},
	scroll_to_point: function() {
		if (!this.vaus_size) {
			this.vaus_size = this.size().width / 7 + 16;
		}
		this.vaus_pos = this.vaus.scrollLeft / this.vaus.scrollWidth * this.size().width - 16;
	},
	scroll: function() {
		var p = this.vaus_pos / this.size().width * (this.vaus.scrollWidth - this.size().width);
		this.vaus.scrollLeft = p;
	},
	resize: function() {
		this.previous = this.dimensions;
		this.dimensions = document.viewport.getDimensions();
		this.vaus_size = this.size().width / 7 + 16;
		
		if (this.previous && this.previous.width != this.dimensions.width) {
			var dx = this.previous.width - this.dimensions.width;
			for (var i=0; i<this.blocks.length; i++) {
				var bl = this.blocks[i];
				bl.x -= dx/2;
				bl.element.style.left = bl.x + 'px';
			}
		}
		
		if (this.score == 0) {
			this.ball_pos.x = (this.size().width - 40) / 2;
			this.ball.style.left = this.ball_pos.x + 'px';
			
			this.vaus_pos = this.size().width / 2;
			var p = this.vaus_pos / this.size().width * (this.vaus.scrollWidth - this.size().width);
			this.vaus.scrollLeft = p;
		}
	},
	size: function() {
		return this.dimensions;
	},
	easy: function() {
		this.easymode = true;
	}
}