loadCanvas();

/*document.getElementsByTagName("footer")[0].getElementsByTagName("div")[0].innerHTML += '<canvas id="game" width="500px" height="300px"></canvas>';

var theGame = new theGame();
theGame.setup(500,300);

var fire = getId("fire1");
fire.addEventListener("click",function(){
	var power = getId("power").value;
	if(power > 300){
		power = 300;
	}
	
	
	
	var name = getId("power").name;
	var num = name-1;
	var vel = 1;
	if(num > 0){
		if(theGame.player[num] > theGame.player[0]){
			vel = -1;
		}else{
			vel = 1;
		}
	}else{
		if(theGame.player[num] > theGame.player[1]){
			vel = -1;
		}else{
			vel = 1;
		}
	}
	theGame.player[num].fire(num-1,power,vel);
	(name == 1)?name=2:name=1;

	getId("power").name = name;
},false);
		
setInterval(function(){
	theGame.draw(theGame);
},1000/60);
*/
function loadCanvas(){
	var logo = getId("logoContainer");
	
	var width = parseInt(logo.offsetWidth);
	var height = parseInt(logo.offsetWidth);
	logo.innerHTML += "<canvas id='logo-canvas' height='"+height+"px' width='"+width+"px'></canvas>";
	getId("logo").addEventListener("mouseover",function(){
		var img = new Image();
		img.src = "/images/logo2.png";
		img.onload = function(){
			getId("logo").style.display = "none";
			if(!logo.getElementsByTagName("img")[0]){
				logo.innerHTML+= "<img src='/images/logo3.png'/>";
			}
			drawLogo(img,0);
			//moveTo(-1000,.01);
		}
	},false);

}

function drawLogo(img,rot){
	var canvas = document.getElementById("logo-canvas");
	canvas.width = canvas.width;
	var ctx = canvas.getContext("2d");
	
	ctx.beginPath();
	//ctx.fillRect(2,2,54,5);
	ctx.translate(50,50);
	ctx.rotate(rot * Math.PI / 180);
	ctx.drawImage(img,-50,-50,100,100);
	ctx.closePath();
	
	rot++;
	if(rot >= 360){
		rot = 0;
	}
	setTimeout(function(){
		drawLogo(img,rot);
	},100);
}


//MINIGAME
function theGame(){
	this.setup = function(width,height){
		this.height = parseInt(height);
		this.width = parseInt(width);
		this.gravity = .01;
		this.getGravity = function(){
			return gravity;
		}
		this.id = document.getElementById("game");
		this.ctx = this.id.getContext("2d");
		this.player = new Array();
		this.player.push(new this.user(this));
		this.player.push(new this.user(this));
		
		this.missile = new Array();
	};
	
	this.user = function(parent){
		this.x = Math.floor(Math.random()*parent.width);
		this.y = parent.height - 5;
		this.rotate = 0;
		this.fire = function(owner,power,vel){
			parent.missile.push(new parent.createMissile(this.x,this.y,power,owner.vel));
			parent.missile.sort();
		}
	}
	
	this.createMissile = function(x,y,power,owner,vel){
		this.owner = owner;
		this.x = x;
		this.y = y;
		this.xVel = vel;
		this.yVel = -1 * (power/100);;
		this.update = function(gravity){
			this.yVel += gravity;
			this.x += this.xVel;
			this.y += this.yVel;
		}
	}
	
	this.draw = function(parent){
		this.id.width = this.id.width;
		var ctx = this.ctx;
		ctx.beginPath();
		ctx.fillStyle = "#555";
		for(var i in this.player){
			ctx.fillRect(this.player[i].x,this.player[i].y,5,5);
		}
		
		ctx.fillStyle = "#FF0000";
		for(var i in this.missile){
			ctx.fillRect(this.missile[i].x,this.missile[i].y,3,3);
			
			
			for(var j in this.missile){
				for(var k in this.player){
					
					if(k != this.missile[j].owner){
						//console.log(parseInt(this.missile[j].x) + ' '+  this.player[k].x  + ' '+ parseInt(this.missile[j].y)  + ' '+ this.player[k].y)
						if(parseInt(this.missile[j].x) == this.player[k].x && parseInt(this.missile[j].y) == this.player[k].y){
							alert('HIT');
						}
					}
				}
			}
			
			this.missile[i].update(this.gravity);
			
			if(this.missile[i].y >= this.height){
				delete this.missile[i];
				this.missile.sort()
			}
			
			
		}
		
		ctx.closePath();
	};
}


