/*
* Javascript file
*
*/

//initialize global variables here
var maxHorBounds;
var minHorBounds;
var maxVertBounds;
var minVertBounds;
var offSetX;
var offSetY;
var speed = 5;
var toStop = false;
var numInJail = 0;
var Images = new Array();
var boardHeight = 400;
var boardWidth = 800;
var imageHeight = 84;
var imageWidth = 75;
var borderHeight = 10;
var borderWidth  = 10;
var totalClicks = 0;
var timer = 0;
var win = false;
// images object that tells us where the image is going or if its in jail
function Image(imageId, imageNum){
	this.id = imageId;
	this.num = imageNum;
	this.inJail = false;
	this.changeX;
	this.changeY;	
}

function arenaClick(){
	totalClicks++;
}
/*Image.prototype.changeX = function(){
	var rand = Math.floor(Math.random()*360);
	return Math.cos(rand*Math.PI/180);	
}
Image.prototype.changeY = function(){
	var rand = Math.floor(Math.random()*360);
        return Math.sin(rand*Math.PI/180);
}
*/
function initChangeX(id){
        var rand = Math.floor(Math.random()*360);
        Images[id].changeX =  Math.cos(rand*Math.PI/180);
}
function initChangeY(id){
     	var rand = Math.floor(Math.random()*360);
        Images[id].changeY =  Math.sin(rand*Math.PI/180);
}

// stuff we want to figure out once the page loads
function preLoad(){
	offSetX = document.getElementById("andrea").offsetLeft;
	offSetY = document.getElementById("andrea").offsetTop;

	minHorBounds  = offSetX;
	minVertBounds = offSetY;
	maxHorBounds  = offSetX  + boardWidth - imageWidth - borderWidth;
	maxVertBounds = offSetY  + boardHeight - imageHeight - borderHeight;

	var debug = document.getElementById("debug");
//debug.innerHtml = "\n horBounds" + horBounds + " vertBounds: " + vertBounds ; 
}
function moveToJail(id){
	// gets the elements needed for the trip to jail
	var toMove = returnIdByUser(id);
	var jail = document.getElementById("jail");
	var prisoner = document.getElementById(toMove);

	// checks to see if they are already in jail
	if(!Images[id].inJail){	
		Images[id].inJail = true;
		jail.appendChild(prisoner);
	
		prisoner.style.top = jail.offsetTop + 5 + "px";
       		prisoner.style.left = jail.offsetLeft +  5 + numInJail * 75 + "px";
		numInJail++;
	
		checkWin();
	}
}
// checks to see if there is a winner
function checkWin(){
	var jail = document.getElementById("jail");
	var children = jail.childNodes;
	var newTotalClicks = totalClicks + 8;
	var clickRate = 8/(newTotalClicks)*100;
	clickRate += '';
	clickRate = clickRate.substr(0,4);
	if(children.length == 9){
		alert("You have won the game! Total Clicks: " + newTotalClicks + " w/ "+ clickRate +"% Click Rate");
		win = true;
	}
}
// gets the id of the user based on the number from Move to Jail
function returnIdByUser(id){
	if(id == 0){		return "andrea";
        }else if(id == 1){	return "remzi";
        }else if(id == 2){	return "estan";
        }else if(id == 3){	return "horwitz";
        }else if(id == 4){	return "liblit";
        }else if(id == 5){	return "sohi";
        }else if(id == 6){	return "solomon";
        }else if(id == 7){	return "swift";
	}
}
// generates a random boards and sends the images into motion
function play(){
	toStop = false;
	win = false;
	//reseet game Variables
	numInJail = 0;
	totalClicks = 0;
	// take all iamges in the jail and put them back in the arena
	var arena = document.getElementById("arena");
	// reinitialize the board
	for(var i = 0; i< 8; i++){
		arena.appendChild( document.getElementById(returnIdByUser(i)) );
		Images[i].inJail = false;
                randomBoardPos(returnIdByUser(i));
        }
	// starts moving the images
	moveImages();
		
}
// stops all movement of images
function stop(){
	toStop = true;
}
// initializes the game board
function initializeTheGame(){
	preLoad();
	for(var i =0; i <8; i++){
		randomBoardPos(returnIdByUser(i));
		Images[i] = new Image(returnIdByUser(i) , i);
	}
	for(var i =0; i <8; i++){
		initChangeX(i);
                initChangeY(i);
	}
}

// places an images randomly on the board within the arena
function randomBoardPos(id){
	var image = document.getElementById(id);
	var hor  = Math.floor(Math.random()* ( boardWidth  - imageWidth ) ) + offSetX  + "px";
	var vert = Math.floor(Math.random()* ( boardHeight - imageHeight) ) + offSetY  + "px";
	
	image.style.position="absolute";	
	image.style.top = vert;
	image.style.left = hor;
}
// figures out a direction x to go to
function changeDirectionX(id, rand){
        var image = document.getElementById(returnIdByUser(id));
        var curHor =  image.style.left.substring(0, image.style.left.length-2)*1;

	var newDir =  Math.cos(rand*Math.PI/180);

        // sends the images into the right direction
        if(curHor <= minHorBounds ){
                newDir = Math.abs(newDir);
        }else if(curHor >= maxHorBounds){
                if(newDir >= 0){
                        newDir = newDir * -1;
                }
        }

        Images[id].changeX = newDir;
}
function changeDirectionY(id, rand){
	var image = document.getElementById(returnIdByUser(id));
        var curVert = image.style.top.substring(0, image.style.top.length-2)*1;
	var newDir =  Math.sin(rand*Math.PI/180);

	// sends the images into the right direction
	if(curVert <= minVertBounds ){
		newDir = Math.abs(newDir);
	}else if(curVert >= maxVertBounds){
		if(newDir >= 0){
			newDir = newDir * -1;
		}
	}	
	
	Images[id].changeY = newDir;
}

function moveImages(){
	// stop the movement
	if(toStop == true) return;
	if(win == true) return;

	for( var i = 0; i < 8; i++){ 
		
		var image = document.getElementById(returnIdByUser(i));
                var curHor =  image.style.left.substring(0, image.style.left.length-2)*1;
                var curVert = image.style.top.substring(0, image.style.top.length-2)*1;	

		if(Images[i].inJail == false){
		//alert("X:" + Images[i].changeX + " Y: " + Images[i].changeY );
	
		// if out of bounds, change the direciton
		if(curHor > maxHorBounds  || curHor < minHorBounds || curVert < minVertBounds || curVert > maxVertBounds){
                	var rand = Math.floor(Math.random()*360);
			changeDirectionX(i, rand);
			changeDirectionY(i, rand);
        	}	

		//alert("X:" + Images[i].changeX() + " Y: " + Images[i].changeY());	
		var changeinX = speed * Images[i].changeX * .5;
		var changeinY = speed * Images[i].changeY * .5;

		var newHor  = curHor  + changeinX;
        	var newVert = curVert + changeinY;

		var realHor  = newHor  + "px";
		var realVert = newVert + "px";

// 		alert("speed: " + speed + " " + curHor + " " + curVert + " new:" + newHor + " " +newVert + " " );	
		image.style.left = realHor;
		image.style.top = realVert;
		}
	}
	setTimeout("moveImages()" ,speed/2 );
}

