
var gDraw = new DrawContext();
var gResizeCount = 0;

function DrawContext()
{
	this.kWatchDefault = 90;
	this.kWatchStep = 10;
	this.watch_x = 0;
	this.watch_y = this.kWatchDefault;
	this.horizontalCenterOffset = 0;
	this.verticalCenterOffset = 0;
	this.zoom = 1;

	
	this.log = new Array(5);
	
	this.DrawCallback;
	
	this.Draw = function()
	{
		this.watch_y = this.kWatchDefault;
		
		this.DrawCallback();

		var urlVars = getUrlVars();
		if(urlVars["visualconsole"])
		{
			for(var i  = 0; i < this.log.length; i++)
			{
				this.Watch(this.log[i]);
			}
		}
	}
	
	this.InitializeCanvases = function()
	{
		var canvases = document.getElementsByTagName('canvas');
		for(var i = 0; i < canvases.length; i++)
		{
			this.AddDrawContext(canvases[i].id);
		}
	}
	
	this.Scale = function(scale)
	{
		var canvases = document.getElementsByTagName('canvas');
		for(var i = 0; i < canvases.length; i++)
		{
			var canvas = canvases[i];
			canvas.width = canvas.width * scale;
			canvas.height = canvas.height * scale;
			gDraw[canvas.id].scale(scale, scale);
		}
		//gDraw.canvas.width = gDraw.canvas.width * scale;
		//gDraw.canvas.height = gDraw.canvas.height * scale;
	}

	this.AddDrawContext = function(contextName)
	{
		var element = document.getElementById(contextName);
		gDraw[contextName] = element.getContext("2d");
		InitContextDrawLibrary(gDraw[contextName]);
		gDraw.canvas = element;
		gDraw.input = element;
	}
	
	this.Watch = function(message)
	{
		this.WatchAt(this.watch_x, this.watch_y, message);
		this.watch_y += this.kWatchStep;
	}

	this.WatchAt = function(x, y, message)
	{
		this.debug.font	 = '10px sans-serif';
		this.debug.textAlign	 = 'start';
		this.debug.textBaseline	 = 'alphabetic';
		this.debug.fillTextShadow(message, x, y, "rgb(255, 255, 255)", "rgb(0, 0, 0)");
	}
	
	this.WatchLog = function(message)
	{
		var urlVars = getUrlVars();
		if(urlVars["visualconsole"])
		{
			for(var i = 0; i < this.log.length ; i++)
			{
				if(i == (this.log.length - 1))
				{
					this.log[i] = message;
				}
				else
				{
					this.log[i] = this.log[i+1];
				}
			}
		}
	}
}



window.addEventListener('resize', resizeCanvas, false);

function resizeCanvas(zoomfactor)
{
	if(gResize == true)
	{
		if(isNaN(zoomfactor))
		{
			var storedZoom = gStorage.getItem("options", "graphics", "zoom");
			zoomfactor = storedZoom ? parseFloat(storedZoom) : 1;
		}
		else
		{
			gStorage.setItem("options", "graphics", "zoom", zoomfactor);
		}
		
		gResizeCount++
		var width = Math.max(window.innerWidth, 320);
		var height = Math.max(window.innerHeight, 240);
		var div = document.getElementsByTagName("div")[0]
		div.style.zoom = zoomfactor;
		width /= zoomfactor;
		height /= zoomfactor;

		var canvases = document.getElementsByTagName('canvas');
		for(var i = 0; i < canvases.length; i++)
		{
			var canvas = canvases[i];
			canvas.width = width;
			canvas.height = height;
		}
			
		var tileWidth = width/gTileWidth;
		var tileHeight = height/gTileHeight;
		var tileSize = Math.round(Math.min(tileWidth, tileHeight));
		
		kTileSize = tileSize;
		kTileSizeD2 = tileSize/2;
		
		var horizontalCenterOffset = (width - (tileSize * gTileWidth))/2;
		var verticalCenterOffset = (height - (tileSize * gTileHeight))/2;
		gDraw.tilebg.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.tiles.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.particles.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.context.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.player.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.foreground.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.foreparticles.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.hubs.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.debug.translate(horizontalCenterOffset, verticalCenterOffset);
		gDraw.zoom = zoomfactor;
	
		gConsole.Log(1, "[RSZ] " + width + ", " + height + " - TileSize " + kTileSize);
		
		gDraw.horizontalCenterOffset = horizontalCenterOffset;
		gDraw.verticalCenterOffset = verticalCenterOffset;
	
		RedrawGame();
		
		gPerformanceOverlay.IgnorePerformance();
	}
}
