/* Flies on webpage */

var globalUpdateTimeMS = 30;
var globalFrameCounter = 0;
var globalFadeFlies = false;
var globalFadeFliesTime = 4000;
var globalMaxMovementSpeed = 5.0;
var flies = new Array();

var GameState = new Array (
    // walking animations
	{value: 0, dirX: -0.85, dirY: -1.0, animOffset: 0, length: 6},
    {value: 1, dirX: 0.5, dirY: -1.0, animOffset: 6, length: 6},
    {value: 2, dirX: 1.0, dirY: -0.85, animOffset: 12, length: 6},
    {value: 3, dirX: 1.0, dirY: 0.5, animOffset: 18, length: 6},
    {value: 4, dirX: 0.85, dirY: 1.0, animOffset: 24, length: 6},
    {value: 5, dirX: -0.5, dirY: 1.0, animOffset: 30, length: 6},
    {value: 6, dirX: -1.0, dirY: 0.5, animOffset: 36, length: 6},
    {value: 7, dirX: -1.0, dirY: -0.5, animOffset: 42, length: 6},
    // static animations
	{value: 8, dirX: 0, dirY: 0, animOffset: 48, length: 5},
	{value: 9, dirX: 0, dirY: 0, animOffset: 54-1, length: 5},
	{value: 10, dirX: 0, dirY: 0, animOffset: 60-2, length: 5},
	{value: 11, dirX: 0, dirY: 0, animOffset: 66-3, length: 5},
	{value: 12, dirX: 0, dirY: 0, animOffset: 72-4, length: 5},
	{value: 13, dirX: 0, dirY: 0, animOffset: 78-5, length: 5},
	{value: 14, dirX: 0, dirY: 0, animOffset: 84-6, length: 5},
	{value: 15, dirX: 0, dirY: 0, animOffset: 90-7, length: 5},
	// stop state
	{value: 16, dirX: 0, dirY: 0, animOffest: -1, length: 1}
);

/* Basic fly class */
function Fly(x, y, id) {
    this.x = x;
    this.y = y;
	var randState = Math.floor(Math.random()*16);
    this.state = GameState[randState];
	this.prevState = this.state;
    this.animFrame = 0;
    this.timeUntilStateChange = 15 + Math.floor(Math.random()*60);
	this.fadeTime = -1;
	this.maxFadeTime = 0;
	this.id = id;
	this.isSlow = Math.floor(Math.random()*2);
			
	this.init = function() {
		$("div #flies").append('<div style="position:absolute; width:60px; height:60px; left:0px; right:0px;" id="fly' + this.id+'"></div>');
	}
	    
    this.update = function() {
        this.x += this.state.dirX * globalMaxMovementSpeed;
        this.y += this.state.dirY * globalMaxMovementSpeed;
        if (this.state.value < 8)
			this.animFrame++;
		else
			this.animFrame++;
		this.animFrame %= this.state.length;
        
		this.timeUntilStateChange--; 
		if ( this.timeUntilStateChange <= 0 ) {
			
			var randMovement = Math.floor(Math.random()*8);
			
			var rand = Math.floor(Math.random()*3);
			if ( rand == 2 )
				randMovement += 8;
			
			this.changeState(randMovement);
		}
		
		// Get width and height of our window
		var docWidth = document.body.clientWidth;
		var docHeight = document.body.clientHeight;
		
		// Limit movement of flies to window borders
		if ( this.x <= 8 )
			this.changeState(2);
		else if ( this.x >= docWidth-68 )
			this.changeState(6);
		
		if ( this.y <= 8 )
			this.changeState(5);
		else if ( this.y >= docHeight-68 )
			this.changeState(0);
			
		// Fade out flies (optional)
		/*
		if ( globalFadeFlies == true && globalFrameCounter > globalFadeFliesTime && this.fadeTime == -1 ) {
			this.fadeOut(50);
		}
		*/
		
		if ( this.fadeTime == 0 ) {
			$("#fly" + this.id).remove();
		} else if ( this.fadeTime > 0 ) {
			this.fadeTime--;
			$("#fly" + this.id).css("opacity", this.fadeTime / this.maxFadeTime); 
		}
						
		var name = "#fly" + this.id;
		$(name).css("background-image", "url(img/spritesheet.png)");
		
		var index = this.animFrame + this.state.animOffset; 
		
		var xpos = -(index % 8) * 60;
		var ypos = -((index - (index%8)) / 8) * 60;
		$(name).css("background-position", xpos +"px " + ypos +"px");
		$(name).offset({left: this.x, top: this.y});
		$(name).css("z-index", 1000 + this.id);
    }
	
	/* Fade out */
	this.fadeOut = function(fadeTime) {
		this.fadeTime = fadeTime;
		this.maxFadeTime = fadeTime;
	}
    
	/* Change state */
	this.changeState = function(newState) {
		if ( this.prevState.value == newState ) {
			newState = (newState + 1) % 16;
		}
		
		if ( this.prevState.value >=0 && this.prevState.value < 8 ) {
			newState = 16;
		} else if ( this.prevState == GameState[16] ) {
			// new state
		}
		
		if ( newState == 16 ) {
			if ( this.isSlow == 0 )
				this.timeUntilStateChange = 25 + Math.floor(Math.random()*40);
			else
				this.timeUntilStateChange = 5;
		} else if ( newState >= 8 ) {
			this.timeUntilStateChange *= 2;
		}
				
		this.prevState = this.state;
		this.state = GameState[newState];
		this.timeUntilStateChange = 15 + Math.floor(Math.random()*40);
	}
}

/*
$(function() {
	addFlies(12);
});
*/

function updateFlies() {
    for (var i=0; i<flies.length; ++i) {
		flies[i].update();
	}
	globalFrameCounter++;
}

function addFliesBottom(numFlies) {
	var docWidth = document.body.clientWidth;
	var docHeight = document.body.clientHeight;
		
	for (var i=0; i<numFlies; ++i) {
		var x = Math.floor(Math.random()*40) - 20 + docWidth/2;
		var y = Math.floor(Math.random()*40) - 108 + docHeight;
		var fly = new Fly(x, y, i);
		fly.init();
		flies.push(fly);
	}
	
    setInterval("updateFlies()", globalUpdateTimeMS); 
}

function addFliesCenter(numFlies) {
	var docWidth = document.body.clientWidth;
	var docHeight = document.body.clientHeight;
		
	for (var i=0; i<numFlies; ++i) {
		var x = Math.floor(Math.random()*40) - 20 + docWidth/2;
		var y = Math.floor(Math.random()*40) - 20 + docHeight/2;
		var fly = new Fly(x, y, i);
		fly.init();
		flies.push(fly);
	}
	
    setInterval("updateFlies()", globalUpdateTimeMS); 
}

function fadeFlies(fadeTime) {
	
	for (var i=0; i<flies.length; ++i) {
		flies[i].fadeOut(fadeTime);
	}
}


