//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gBackground = new Background();

function Background()
{
	this.preRenders = new Array();
	this.kMaxLayers = 3;
	this.style = 0;
	this.time = 0;

	this.Clear = function()
	{
		this.preRenders = new Array();
	}
	
	this.Redraw = function()
	{
		//Pre Render background
		for(var layer = 0; layer < this.kMaxLayers; layer++)
		{
			var canvas = document.createElement('canvas');
			var layerScale = this.GetLayerScale(layer);
			canvas.width = gDraw.canvas.width + (gTileWidth * layerScale);
			canvas.height = gDraw.canvas.height + (gTileHeight * layerScale);
			var context = canvas.getContext('2d');
			
			//background
			if(layer == 0)
			{
				context.fillStyle = gStyles.GetBackgroundFill();
				context.fillRect(0, 0, canvas.width, canvas.height);
			}
			
			//Draw pattern
			this.pattern(context, layer, canvas.width, canvas.height);
			
			this.preRenders[layer] = canvas;
			
			//window.open(canvas.toDataURL('image/png'));
		}
	}
	
	this.GetStrokeStyle = function()
	{
		return gStyles.backgroundFill.GetAdjusted(-10).GetColor();
	}
	
	this.Draw = function()
	{
		this.DrawContext(gDraw.background);
	}
	
	this.DrawContext = function(context)
	{
		for(var layer = 0; layer < this.kMaxLayers; layer++)
		{
			var render = this.preRenders[layer];
			if(render)
			{
				context.save();
				var x = 0;
				var y = 0;
				var layerScale = this.GetLayerScale(layer);
				var firstPlayer = gPlayerSystem.players[0];
				if (firstPlayer)
				{
					x = firstPlayer.x * layerScale;
					y = firstPlayer.y * layerScale;
				}
				else
				{
					x = (gTileWidth/2);
					y = (gTileHeight/2);
					var radius = Math.min(x, y);
					var speed = 0.001;
					switch(layer)
					{
						case 0:
						x += Math.sin(this.time * speed) * radius;
						y -= Math.cos(this.time * speed) * radius;
						break;
						case 1:
						x -= Math.sin((this.time * speed) + Math.PI) * radius;
						y += Math.cos((this.time * speed)+ Math.PI) * radius;
						break;
						case 2:
						x += Math.sin((this.time * speed) + Math.PI) * radius;
						y += Math.cos(this.time * speed) * radius;
						break;
					}
					x *= layerScale;
					y *= layerScale;
					this.time++;
				}
				context.translate(-x, -y);
				context.drawImage(render, 0, 0);
				context.restore();
			}
		}
	}
	
	this.GetLayerScale = function(layer)
	{
		return kTileSize * 0.1 * (layer + 1);
	}
	
	this.Pixels = function(context, layer, width, height)
	{
		var backgroundTileSize = (kTileSize / 6.3) * (layer + 1);
		
		context.fillStyle = gStyles.backgroundFill.GetAdjusted(4 * (layer + 2)).GetAlphaColor(0.5);
		context.beginPath();
		var count = Math.round(2048/(layer + 1));
		var variable = count * (layer + 1);
		for(var i = 0; i < count; i++)
		{
			var randx = (variable * 8437) % 7919
			var randy = (variable * 6421) % 7823
			variable += randx + randy;
			var scale = ((variable * 1609 % 1051) + kTileSize * 10) / 1551;
			var size = scale * backgroundTileSize;
			var x = (randx * backgroundTileSize) % width;
			var y = (randy * backgroundTileSize) % height;
			
			context.beginPath();
			switch(this.style)
			{
				default:
				context.rect(x, y, size, size);
				break;
				case 1:
				context.arc(x, y, size/1.5, 0, Math.PI * 2);
				break;
				case 2:
				var sized2 = size/2;
				context.moveTo(x + sized2, y);
				context.lineTo(x + size, y + sized2);
				context.lineTo(x + sized2, y + size);
				context.lineTo(x, y + sized2);
				break;
			}
			context.fill();
		}
	}
	
	this.Prev = function()
	{
		var prevIndex = this.patterns.indexOf(this.pattern) - 1;
		if(prevIndex < 0)
		{
			prevIndex = this.patterns.length - 1;
		}
		gStorage.setItem("options", "background", prevIndex);
		this.pattern = this.patterns[prevIndex]
		this.redraw = true;
	}
	
	this.Next = function()
	{
		var nextIndex = this.patterns.indexOf(this.pattern) + 1;
		if(nextIndex >= this.patterns.length)
		{
			nextIndex = 0;
		}
		gStorage.setItem("options", "background", nextIndex);
		this.pattern = this.patterns[nextIndex]
		this.redraw = true;
	}

	this.patterns = new Array(this.Pixels)
	this.pattern = this.Pixels;
	/*var prevBackground = gStorage.getItem("options", "background")
	if(prevBackground)
	{
		this.pattern = this.patterns[parseInt(prevBackground)];
	}*/
	
}