/*
 *  Functions about text management.
 */

function print_one_line(ct, text){

	var len = ct.mozMeasureText(text);

	ct.fillStyle = "rgba(200,200,0,0.8)";
	ct.mozDrawText(text);
	ct.translate(0,20);
}

/*this function center the text on the canvas and do fade*/
function text_fx(ct, text){
	this.alpha = 0.0;
	this.text = text;
	this.ct = ct;
	this.timeout_id = null;
	this.fx = function(){
		this.ct.save();
		this.ct.fillStyle = 'rgb(255,255,255)';
		this.ct.fillRect(0,0, this.ct.canvas.width, this.ct.canvas.height);
		this.ct.mozTextStyle = '30pt Serif';
		var len = this.ct.mozMeasureText(this.text);
		this.ct.fillStyle = 'rgba(200,0,0,'+this.alpha+')';
		this.ct.translate((this.ct.canvas.width-len)/2,this.ct.canvas.height/2);
		this.ct.mozDrawText(text);
		this.ct.restore();
		this.alpha += .1;
		if(this.alpha < 0)
			clearInterval(this.timeout_id);
	}
}

function banner(ct){
	ct.save();
	ct.mozTextStyle = '60pt Serif';
	var text = "Break-in";
	var len = ct.mozMeasureText(text);

	ct.translate((ct.canvas.width-len)/2,ct.canvas.height/2);
	ct.fillStyle = "rgba(200,0,0,.5)";
	ct.mozDrawText(text);

	ct.mozTextStyle = '10pt Serif';
	ct.translate(100,50);
	print_one_line(ct, "'p'             - pause/resume");
	print_one_line(ct, "'a'             - information");
	print_one_line(ct, "'left/right' - move pad");
	ct.restore();
}

/*Principal object of game*/
function score(context){
	this.context = context;
	this.score = 0;
	this.live = 3;
	this.ball = new ball(this);
	this.pad = new pad(this);
	this.blocks = [];
	this.text_fx =
	this.update = function(hit){
		this.score += hit;
	}

	/*this update the scene*/
	this.update_all = function(){
		this.pad.update();
		this.ball.update();
	}

	/*this draw the scene*/
	this.draw_all = function(){
		var ct = this.context;
		clear_all(this.context.canvas);
		this.blocks.forEach(function(b){b.draw(ct);});
		this.ball.draw(this.context);
		this.pad.draw(this.context);
		this.draw(this.context);
	}

	this.draw = function(ct){
		ct.save();
		ct.translate(0, ct.canvas.height-10);
		ct.fillStyle = 'rgba(200,200,200,.6)';
		ct.fillRect(0,0,ct.canvas.width,10);
		ct.mozTextStyle = '10pt bold monospace';
		ct.translate(20, 10);
		ct.fillStyle = 'rgba(0,0,0,.8)';
		ct.mozDrawText(this.score);
		ct.translate(ct.canvas.width - 100, 0);
		ct.mozDrawText(this.live + ' lives' );
		ct.restore();
	}
	this.lose_live = function(){
		this.live--;
		if(this.live < 0)
			this.game_over();
		this.pad.init();
		this.ball.is_on_pad = true;
	}
	this.game_over = function(){
	}
}

