function Bug()
{
	this.init();
}

Bug.prototype = 
{
	init: function()
	{
		this.x = 0;
		this.y = 0;
		this.speed = 0;
		this.angle = 0;
		this.angleInc = 0;
		this.angleTarg = 0;
		this.radians = Math.PI / 180;
		this.p = null;
		this.speedScalar = 2;//1.5;
		this.selectBugPosition();
		this.canvas = document.getElementById("can");
		this.context = this.canvas.getContext("2d");
		this.bug = new Image();
		this.bug.src = this.initBug();
		this.bug.id = this.imageID;
		this.bug.onload = this.onImageLoad;
		
		this.updateInterval = setInterval( bargs( function( _this ) { _this.update(); return false; }, this ), 100 + (Math.random() * 500) );
		this.checkInterval = setInterval( bargs( function( _this ) { _this.checkPosition(); return false; }, this ), 2000 );
	},
	
	onImageLoad: function()
	{
		if(!this.context) return;
		this.context.drawImage(this.bug, this.x, this.y);
	},
	
	render: function()
	{
		if(!this.context) return;
		
		this.x += Math.cos(this.angle * this.radians) * this.speed;
		this.y += Math.sin(this.angle * this.radians) * this.speed;
		this.angle += (this.angleTarg - this.angle) * 0.2;
		
		var rotation = (this.angle + 90) * this.radians;
		this.context.save();
		this.context.translate(this.x, this.y);
		this.context.rotate(rotation);
		this.context.drawImage(this.bug, 0, 0);
		this.context.restore();
	},
	
	update: function()
	{
		var ran1 = Math.random() * this.angleInc;
		var ran2 = Math.random() * this.angleInc;
		this.angleTarg += (ran1 - ran2);
	},
	
	checkPosition: function()
	{
		var size = 40; 
		if((this.x < -size || this.x > window.innerWidth + size) || (this.y < -size || this.y > window.innerHeight + size))
		{
			this.p.removeBug(this);
		}
	},
	
	initBug: function()
	{
		var basePath = "assets/img/";
		var fileType = ".png";
		var images = ["bug1", "bug2", "bug3", "bug4", "bug5", "bug6", "bug7"]; 
		var speeds = [7, 8, 6, 5, 7, 10, 9];
		var angleIncrements = [75, 60, 90, 90, 80, 20, 80];
		var ran = Math.floor(Math.random() * images.length);
		this.speed = speeds[ran] * this.speedScalar;
		this.angleInc = angleIncrements[ran];
		
		return basePath + images[ran] + fileType;
	},
	
	selectBugPosition: function()
	{
		var side = Math.ceil(Math.random() * 4);
		
		switch(side)
		{
			case 1:
				this.x = -100;
				this.y = Math.round(Math.random() * window.innerWidth);
				this.angle = 0;
				this.angleTarg = 0;
				break;
				
			case 2:
				this.x = Math.round(Math.random() * window.innerWidth);
				this.y = -100;
				this.angle = 90;
				this.angleTarg = 90;
				break;
				
			case 3:
				this.x = window.innerWidth + 100;
				this.y = Math.round(Math.random() * window.innerHeight);
				this.angle = 180;
				this.angleTarg = 180;
				break;
				
			case 4:
				this.x = Math.round(Math.random() * window.innerWidth);
				this.y = window.innerHeight + 100;
				this.angle = 270;
				this.angleTarg = 270;
				break;
		}
	},
	
	destroy: function()
	{
		this.bug.src = null;	
		this.x = null;
		this.y = null;
		this.speed = null;
		this.angle = null;
		this.angleInc = null;
		this.angleTarg = null;
		this.radians = null;
		this.p = null;
		this.speedScalar = null;
		this.canvas = null;
		this.context = null;
		this.bug = null;
		
		clearInterval(this.checkInterval);
		clearInterval(this.updateInterval);
	}
}

function bargs( _fn )
{
	var args = [];
	for( var n = 1; n < arguments.length; n++ )
		args.push( arguments[ n ] );
	return function () { return _fn.apply( this, args ); };
}