/////////////////////////////////////////////////////////////////////////////////
// Bubble
// 泡っぽい背景イメージを描画
// 
// Copyright (c) 2010 Minoru Nakanow
// Licensed under the MIT licenses.
// http://www.opensource.org/licenses/mit-license.html
// 
////////////////////////////////////////////////////////////////////////////////
(function() {
	//--------------------------------------------------------------------------
	//  Class
	//--------------------------------------------------------------------------
	/**
	 * 3つのランダムカラーの組み合わせ
	 */
	function RandomColor() {
		this.push(_randomColor());
		this.push(_randomColor());
		this.push(_randomColor());
	}
	
	RandomColor.prototype = new Array();
	
	/**
	 * ランダムな座標位置
	 */
	function RandomPoint(x, y) {
		this.x = Math.floor(Math.random() * x);
		this.y = Math.floor(Math.random() * y);
		this.radius = Math.floor(Math.random() * 600 + 500);
	}
	
	/**
	 * 座標に加算する乱数
	 */
	function RandomLine() {
		var x = Math.floor(Math.random() * 10) / 10;
		var y = Math.floor(Math.random() * 10) / 10;
		if (Math.floor(Math.random()*2)) x = -x;
		if (Math.floor(Math.random()*2)) y = -y;
		this.x = x;
		this.y = y;
	}
	
	//--------------------------------------------------------------------------
	//  Bubble
	//--------------------------------------------------------------------------
	/**
	 * 泡っぽい背景イメージを描画
	 * 
	 * @param {Object} canvas キャンバス
	 */
	window.Bubble = function (canvas) {
		this.canvas = canvas;
		this.context = canvas.getContext("2d");
		this.context.globalCompositeOperation = "xor";
	};
	
	/**
	 * 描画
	 */
	Bubble.prototype.draw = function() {
		var width = this.canvas.width = $(window).width();
		var height = this.canvas.height = $(window).height();
		var colors = [new RandomColor(), new RandomColor(), new RandomColor(), new RandomColor()];
		var points = [new RandomPoint(width/3, height/3), new RandomPoint(width, height), new RandomPoint(width, height)];
		//var lines = [new RandomLine(), new RandomLine(), new RandomLine()];
		var context = this.context;
		
		context.clearRect(0, 0, width, height);
		
		context.fillStyle = _linearGradient(context, colors[2], height);
		context.fillRect(0, 0, width, height);
		
		context.fillStyle = _radialGradient(context, colors[0], points[0]);
		context.fillRect(0, 0, width, height);
		
		context.fillStyle = _radialGradient(context, colors[1], points[1]);
		context.fillRect(0, 0, width, height);
		
		context.fillStyle = _radialGradient(context, colors[2], points[2]);
		context.fillRect(0, 0, width, height);
	};
	
	//--------------------------------------------------------------------------
	//  Private methods
	//--------------------------------------------------------------------------
	/**
	 * 線形グラデーション
	 */
	function _linearGradient(context, color, height) {
		var g = context.createLinearGradient(0, 0, 0, height);
		g.addColorStop(0, "rgba("+ color[0] +", 0.4)");
		g.addColorStop(0.5, "rgba("+ color[1] +", 0.2)");
		g.addColorStop(1, "rgba("+ color[2] +", 0.4)");
		return g;
	}
	
	/**
	 * 円形グラデーション
	 */
	function _radialGradient(context, color, point) {
		var g = context.createRadialGradient(point.x, point.y, 100, point.x, point.y, point.radius);
		g.addColorStop(0, "rgba("+ color[0] +", 0.2)");
		g.addColorStop(0.8, "rgba("+ color[1] +", 0.4)");
		g.addColorStop(0.9, "rgba("+ color[2] +", 0.7)");
		g.addColorStop(1, "rgba(255, 255, 255, 0)");
		return g;
	}
	
	/**
	 * 適当に色を生成
	 */
	function _randomColor() {
		return ""+
		Math.floor(Math.random() * 255) +","+
		Math.floor(Math.random() * 255) +","+
		Math.floor(Math.random() * 255);
	}
	
	/**
	 * 更新
	 * Point に Line を加算
	 */
	function _updata(point, line) {
		for (var i=0; i<point.length; i++) {
			point[i].x += line[i].x;
			point[i].y += line[i].y;
			
			if (point[i].x < -100 || point[i].x > _maxX ||
				point[i].y < -100 || point[i].y > _maxY) {
					line[i].x = -line[i].x;
					line[i].y = -line[i].y;
			}
		};
	}
	
})();
