var key = 
{
	W: 87,
	A: 83,
	S: 65,
	D: 68
};

var a2d = 
{
	context : null,
	width: 0,
	height: 0,
	frametimer: 0,	
	fps: 0,
	world: null,
	init : function(canvasid)
	{
		this.canvas = document.getElementById(canvasid);
		if(this.canvas.getContext != undefined)
		{		
			a2d.context = this.canvas.getContext('2d');
			var w = $(window).width();
			var h = $(window).height(); 
			$("#" + canvasid).attr("width", w);
			$("#" + canvasid).attr("height", h);
			a2d.width = w;
			a2d.height = h;										
		}	
		else
		{
			//alert("You need canvas support for this stuff.");
		}	
	},
	clear : function()
	{
		var date = new Date();
		var t = date.getTime();
		var d = t - a2d.frametimer;
		a2d.fps = 1000.0 / d;
		a2d.frametimer = t;
		a2d.context.clearRect(0, 0, a2d.width, a2d.height);
	},
	drawImage : function(img, x, y)
	{
		a2d.context.drawImage(img, x, y);
	},
	drawText : function(text, x, y)
	{},
	rad2deg : function(rad)
	{
		return rad * (180 / Math.PI);
	},
	direction : function(rad)
	{		
		return new b2Vec2(Math.cos(rad), Math.sin(rad));
	}
}

var resources = 
{	
	lookup: [],
	loadImage: function(name, imgsrc)
	{
		resources[name] = new Image();
		resources[name].src = imgsrc;
		resources.lookup.push(name);
	}
}

var control = 
{
	down : new Array(),
	key : null,
	press: function(pkey)
	{
		control.down[pkey] = true;
	},
	release: function(pkey)
	{
		control.down[pkey] = false;
	},
	update : function()
	{
		/*if(control.down[key.W]) player.forward();
		if(control.down[key.A]) player.back();
		if(control.down[key.S]) player.left();
		if(control.down[key.D]) player.right();	*/
		
	},
}
var game = 
{
	objects : new Array(),
	lastmove: new Date(),
	redraw : function()
	{
		a2d.clear();		
		control.update();
		for(var i = game.objects.length - 1; i >= 0; i--)
		{
			if(game.objects[i].alive)
			{
				game.objects[i].draw();
			}
			else
			{
				game.objects.splice(i, 1);
			}
			
		}
		//$('#debug').html("flowers: " + game.objects.length);
		//$('#debug').html("fps: " + (a2d.fps).toFixed(2));
	},
	GameObject: function(img, x, y)
	{		
		this.img = img;
		this.x = x;
		this.y = y;
		this.rotation = 0;
		this.scale = 0.0;
		this.creationTime = (new Date()).getTime();
		this.alive = true;
		this.endOfLife = 5000;
		this.animationLength = 1000;
		this.alpha = 0.0;
		this.draw = function()
		{			
			var now = new Date();
			now = now.getTime() - this.creationTime;
			if(now < this.animationLength)
			{
				// x: ??, t: current time, b: begInnIng value, c: change In value, d: duration
				this.scale = $.easing["easeOutCirc"](0, now, 0, 1.0, this.animationLength);
				this.rotation = $.easing["easeOutCirc"](0, now, 0, 6.0, this.animationLength);
				this.alpha = $.easing["easeOutCirc"](0, now, 0, 1.0, this.animationLength);
			}
			if(now > this.endOfLife && now < this.endOfLife + this.animationLength)
			{
				// x: ??, t: current time, b: begInnIng value, c: change In value, d: duration
				this.scale = $.easing["easeOutCirc"](0, now - this.endOfLife, 1.0, -1.0, this.animationLength);
				this.rotation = $.easing["easeOutCirc"](0, now - this.endOfLife, 3.0, -3.0, this.animationLength);
				this.alpha = $.easing["easeOutCirc"](0, now - this.endOfLife, 1.0, -1.0, this.animationLength);				
			}
			if(now > this.endOfLife + this.animationLength)
			{
				this.alive = false;
			}
			a2d.context.save();
			a2d.context.translate(this.x, this.y);
			var angle = this.rotation;
			a2d.context.rotate(angle);
			a2d.context.scale(this.scale, this.scale);
			a2d.context.globalAlpha = this.alpha;
			a2d.drawImage(this.img, - (this.img.width  / 2), - (this.img.height / 2));			
			a2d.context.restore();
		}
	},
	addObject : function(img, x, y, shape, car)
	{
		var nob = new game.GameObject(img, x, y);
		game.objects.push(nob);		
		return nob;
	},
	click : function(e)
	{ 
		var mx; var my;
		if(e.pageX){
		mx = e.pageX;
		my = e.pageY;}else{
		mx = e.clientX;
		my = e.clientY;}
		
		var idx = Math.floor(Math.random() * resources.lookup.length); 
		game.addObject(resources[resources.lookup[idx]], mx, my);
	},
	move : function(e)
	{ 
		var now = new Date();
		if(now.getTime() - game.lastmove.getTime() > 150)
		{
			var idx = Math.floor(Math.random() * resources.lookup.length); 
			game.addObject(resources[resources.lookup[idx]], e.pageX, e.pageY);
			game.lastmove = now;
		}
	}
	
}

var player = null;

$(function()
{	
	resources.loadImage("flower1", "designs/flowers_flower1.png");
	resources.loadImage("flower2", "designs/flowers_flower2.png");
	resources.loadImage("flower3", "designs/flowers_flower3.png");
	resources.loadImage("flower8", "designs/flowers_flower8.png");
	resources.loadImage("flower9", "designs/flowers_flower9.png");
	a2d.init('game');	

	setInterval("game.redraw()", 64);	
	/* wasd */
	document.onkeydown = function(e){
		control.press(e.keyCode);
	}
	document.onkeyup = function(e){ 		
		control.release(e.keyCode);
	}	

	//a2d.canvas.addEventListener("click", game.click, false);
	a2d.canvas.onclick =  game.click;
});



