var world_array = new Array();
var moves_array = new Object;

var type_max = new Object;
type_max['infantry'] = 4;
type_max['tank'] = 4;
type_max['mtank'] = 4;
type_max['ftank'] = 4;
type_max['hippies'] = 4;
type_max['atmt'] = 4;
type_max['blast'] = 0;
type_max['cblast'] = 0;
type_max['bblast'] = 0;
type_max['house'] = 0;
type_max['business'] = 0;
type_max['skel'] = 0;
type_max['wreck'] = 0;
type_max['cp'] = 0;
type_max['silo'] = 0;
type_max['chopper'] = 0;

//shoot_point is offset from top & left where the shot gif goes..
//FIXME - all wrong now . . .
var shoot_point = new Object;
shoot_point['infantry'] = new Object;
shoot_point['infantry']['x'] = 35; 
shoot_point['infantry']['y'] = 10;
shoot_point['atmt'] = new Object;
shoot_point['atmt']['x'] = 35; 
shoot_point['atmt']['y'] = 10;
shoot_point['tank'] = new Object;
shoot_point['tank']['x'] = 35; 
shoot_point['tank']['y'] = 15;
shoot_point['mtank'] = new Object;
shoot_point['mtank']['x'] = 25; 
shoot_point['mtank']['y'] = 15;
shoot_point['hippies'] = new Object;
shoot_point['hippies']['x'] = 35; 
shoot_point['hippies']['y'] = 15;
shoot_point['ftank'] = new Object;
shoot_point['ftank']['x'] = 35; 
shoot_point['ftank']['y'] = 15;
shoot_point['silo'] = new Object;
shoot_point['silo']['x'] = 18; 
shoot_point['silo']['y'] = 30;



var bgs = new Object;

function init_bgs(dir) {

	bgs['chopper'] = new Object;
	bgs['chopper'][0] = "transparent url( " + dir + "/chopper.gif) 0 0 no-repeat";
	
	bgs['blast'] = new Object;
	bgs['blast'][0] = "transparent url( " + dir + "/blast.gif) 0 0 no-repeat";
	// rest not needed now.
	return;


	bgs['infantry'] = new Object;
	bgs['infantry'][0] = "transparent url( " + dir + "/infantry.gif) 0 0 no-repeat";
	bgs['infantry'][1] = "transparent url( " + dir + "/infantry.gif) -40px 0 no-repeat";
	bgs['infantry'][2] = "transparent url( " + dir + "/infantry.gif) -80px 0 no-repeat";
	bgs['infantry'][3] = "transparent url( " + dir + "/infantry.gif) -120px 0 no-repeat";
	bgs['infantry'][4] = "transparent url( " + dir + "/infantry.gif) -160px 0 no-repeat";
	
	bgs['ftank'] = new Object;
	bgs['ftank'][0] = "transparent url( " + dir + "/ftank.gif) 0 0 no-repeat";
	bgs['ftank'][1] = "transparent url( " + dir + "/ftank.gif) -40px 0 no-repeat";
	bgs['ftank'][2] = "transparent url( " + dir + "/ftank.gif) -80px 0 no-repeat";
	bgs['ftank'][3] = "transparent url( " + dir + "/ftank.gif) -120px 0 no-repeat";
	bgs['ftank'][4] = "transparent url( " + dir + "/ftank.gif) -160px 0 no-repeat";
	
	bgs['hippies'] = new Object;
	bgs['hippies'][0] = "transparent url( " + dir + "/hippies.gif) 0 0 no-repeat";
	bgs['hippies'][1] = "transparent url( " + dir + "/hippies.gif) -40px 0 no-repeat";
	bgs['hippies'][2] = "transparent url( " + dir + "/hippies.gif) -80px 0 no-repeat";
	bgs['hippies'][3] = "transparent url( " + dir + "/hippies.gif) -120px 0 no-repeat";
	bgs['hippies'][4] = "transparent url( " + dir + "/hippies.gif) -160px 0 no-repeat";
	
	bgs['mtank'] = new Object;
	bgs['mtank'][0] = "transparent url( " + dir + "/mtank.gif) 0 0 no-repeat";
	bgs['mtank'][1] = "transparent url( " + dir + "/mtank.gif) -40px 0 no-repeat";
	bgs['mtank'][2] = "transparent url( " + dir + "/mtank.gif) -80px 0 no-repeat";
	bgs['mtank'][3] = "transparent url( " + dir + "/mtank.gif) -120px 0 no-repeat";
	bgs['mtank'][4] = "transparent url( " + dir + "/mtank.gif) -160px 0 no-repeat";
	
	bgs['atmt'] = new Object;
	bgs['atmt'][0] = "transparent url( " + dir + "/atmt.gif) 0 0 no-repeat";
	bgs['atmt'][1] = "transparent url( " + dir + "/atmt.gif) -40px 0 no-repeat";
	bgs['atmt'][2] = "transparent url( " + dir + "/atmt.gif) -80px 0 no-repeat";
	bgs['atmt'][3] = "transparent url( " + dir + "/atmt.gif) -120px 0 no-repeat";
	bgs['atmt'][4] = "transparent url( " + dir + "/atmt.gif) -160px 0 no-repeat";
	
	bgs['tank'] = new Object;
	bgs['tank'][0] = "transparent url( " + dir + "/tank.gif) 0 0 no-repeat";
	bgs['tank'][1] = "transparent url( " + dir + "/tank.gif) -40px 0 no-repeat";
	bgs['tank'][2] = "transparent url( " + dir + "/tank.gif) -80px 0 no-repeat";
	bgs['tank'][3] = "transparent url( " + dir + "/tank.gif) -120px 0 no-repeat";
	bgs['tank'][4] = "transparent url( " + dir + "/tank.gif) -160px 0 no-repeat";
	
	bgs['blast'] = new Object;
	bgs['blast'][0] = "transparent url( " + dir + "/blast.gif) 0 0 no-repeat";
	bgs['bblast'] = new Object;
	bgs['bblast'][0] = "transparent url( " + dir + "/bblast.gif) 0 0 no-repeat";
	bgs['cblast'] = new Object;
	bgs['cblast'][0] = "transparent url( " + dir + "/cblast.gif) 0 0 no-repeat";
	
	bgs['cp'] = new Object;
	bgs['cp'][0] = "transparent url( " + dir + "/cp.gif) 0 0 no-repeat";
	
	bgs['silo'] = new Object;
	bgs['silo'][0] = "transparent url( " + dir + "/silo.gif) 0 0 no-repeat";
	
	bgs['wreck'] = new Object;
	bgs['wreck'][0] = "transparent url( " + dir + "/wreck.gif) 0 0 no-repeat";
	
	bgs['skel'] = new Object;
	bgs['skel'][0] = "transparent url( " + dir + "/skel.gif) 0 0 no-repeat";
	
	bgs['house'] = new Object;
	bgs['house'][0] = "transparent url( " + dir + "/house.gif) 0 0 no-repeat";
	
	bgs['business'] = new Object;
	bgs['business'][0] = "transparent url( " + dir + "/business.gif) 0 0 no-repeat";
	
	bgs['shot'] = new Object;
	bgs['shot'][0] = "transparent url( " + dir + "/shot.gif) 0 0 no-repeat";
	bgs['shot'][1] = "transparent url( " + dir + "/shot.gif) -20px 0 no-repeat";
	bgs['shot'][2] = "transparent url( " + dir + "/shot.gif) -40px 0 no-repeat";
	bgs['shot'][3] = "transparent url( " + dir + "/shot.gif) -60px 0 no-repeat";
	bgs['shot'][4] = "transparent url( " + dir + "/shot.gif) -80px 0 no-repeat";
	
	bgs['hit'] = new Object;
	bgs['hit'][0] = "transparent url( " + dir + "/hit.gif) 0 0 no-repeat";
	bgs['hit'][1] = "transparent url( " + dir + "/hit.gif) -50px 0 no-repeat";
	bgs['hit'][2] = "transparent url( " + dir + "/hit.gif) -100px 0 no-repeat";
	bgs['hit'][3] = "transparent url( " + dir + "/hit.gif) -150px 0 no-repeat";
	bgs['hit'][4] = "transparent url( " + dir + "/hit.gif) -200px 0 no-repeat";

	bgs['mshot'] = new Object;
	bgs['mshot'][0] = "transparent url( " + dir + "/mshot.gif) 0 0 no-repeat";
	bgs['mshot'][1] = "transparent url( " + dir + "/mshot.gif) -40px 0 no-repeat";
	bgs['mshot'][2] = "transparent url( " + dir + "/mshot.gif) -80px 0 no-repeat";
	bgs['mshot'][3] = "transparent url( " + dir + "/mshot.gif) -120px 0 no-repeat";
	bgs['mshot'][4] = "transparent url( " + dir + "/mshot.gif) -160px 0 no-repeat";

}


Sprite.prototype.toString = function () {
	return this.name + " : " + this.type + " - POS " +this.x_coord +" , " + this.y_coord + "\n";
}




function Sprite(_name,type,x,y){

	//alert("New Sprite" + type + " x= " + x + " y = " + y);
	this.name = _name;
	//todo map x & y coord to top and left . . . 
	this.top =  y ;
	this.left =  x ;
	this.x_coord = x;
	this.y_coord = y;
	this.type = type;
	this.move_index = 0;
	this.shoot_index = 1;
	this.hit_index = 1;
	this.age = 0;
	//this.shoot_index = 0;
	//this.hit_index = 0;
	this.move_array = new Array();
	this.curr_frame = 0;
	this.frame_count = type_max[type];
	if (shoot_point[type]){
		this.shoot_x = shoot_point[type]['x'];
		this.shoot_y = shoot_point[type]['y'];
	} else {
		this.shoot_x = 20;
		this.shoot_y =  20;
	}

	var div = document.createElement('div');
	div.style.display = "block";
	div.style.height = "40";
	div.style.width = "40";
	if (! bgs[this.type]){
			alert("Type " + this.type + " has no props ");
	}
	div.style.background = bgs[this.type][this.curr_frame];
	div.style.position = "absolute";
	if (type == 'blast' || type == 'cblast' || type == 'bblast'){
		div.style.zIndex = 2;
		div.style.display = "block";
	} else if (type == 'cp' || type == 'infantry' || type == 'tank' || type == 'atmt' || type == 'mtank' || type == 'ftank' || type == 'hippies' || type == 'silo'){
		div.style.zIndex = 3;
		div.style.display = "block";
	} else {
		div.style.zIndex = 1;
		div.style.display = "block";
	}
	div.style.top =   y ;
	div.style.left =    x ;
//	alert("New Sprite" + type + " x= " + x + " y = " + y + "div top,left = " + div.style.top + "," + div.style.left + " my top,left = " + this.top + "," + this.left);
	this.sdiv = div;
	var w = document.getElementById('the_map');
	w.appendChild(this.sdiv);

}


Sprite.prototype.makeShotDiv = function () {
	var div = document.createElement('div');
	div.style.display = "block";
	div.style.height = "20";
	div.style.width = "20";
	this.shoot_index = 0;
	div.style.background = bgs['shot'][0];
	div.style.position = "absolute";
	div.style.zIndex = 2;
	div.style.top =  this.top + this.shoot_y + "px"; 
	div.style.left = this.left + this.shoot_x + "px";
	this.shoot_div = div;
	//alert("appending div . . . T = " + div.style.top + " L= " + div.style.left );
	var w = document.getElementById('the_map');
	w.appendChild(this.shoot_div);

}

Sprite.prototype.makeMissileShotDiv = function () {
	var div = document.createElement('div');
	div.style.display = "block";
	div.style.height = "150";
	div.style.width = "40";
	this.shoot_index = 0;
	div.style.background = bgs['mshot'][0];
	div.style.position = "absolute";
	div.style.zIndex = 3;
	div.style.top =  this.top - 100  + "px"; 
	div.style.left =   this.left  + "px";
	this.mshoot_div = div;
	//alert("appending div . . . T = " + div.style.top + " L= " + div.style.left );
	var w = document.getElementById('the_map');
	w.appendChild(this.mshoot_div);

}



Sprite.prototype.makeHitDiv = function (x,y) {
	var div = document.createElement('div');
	div.style.display = "block";
	div.style.height = "50";
	div.style.width = "50";
	this.hit_index = 0;
	div.style.background = bgs['hit'][0];
	div.style.position = "absolute";
	div.style.zIndex = 2;
	div.style.top =   ((y  * 40) - 10) + "px"; 
	div.style.left =   ((x * 40) - 10) + "px";
	this.hit_div = div;
	//alert("appending hit div . . . T = " + div.style.top + " L= " + div.style.left );
	var w = document.getElementById('the_map');
	w.appendChild(this.hit_div);

}

Sprite.prototype.top;
Sprite.prototype.x_coord;
Sprite.prototype.y_coord;
Sprite.prototype.shoot_x;
Sprite.prototype.shoot_y;
Sprite.prototype.shoot_index;
Sprite.prototype.shoot_div;
Sprite.prototype.mshoot_div;
Sprite.prototype.hit_index;
Sprite.prototype.hit_div;
Sprite.prototype.name;
Sprite.prototype.left;
Sprite.prototype.type;
Sprite.prototype.frame_count;
Sprite.prototype.sdiv;
Sprite.prototype.curr_frame;

Sprite.prototype.nextFrame = function () {
	//alert("nextframe, count = " + this.curr_frame);
	if (this.curr_frame >= this.frame_count){
		this.curr_frame = 0;
	} else {
		this.curr_frame = this.curr_frame + 1;
	}
	this.sdiv.style.background = bgs[this.type][this.curr_frame];
	
}

//fixme, check
//WTF
Sprite.prototype.moveSouth = function () {
	if (this.top <= 518){
		this.sdiv.style.top = this.top + 1 + "px";
		this.top = this.top + 1;
		this.nextFrame();
	}
}
Sprite.prototype.moveNorth = function () {
	if (this.top >= 2){
		this.sdiv.style.top = this.top - 2 + "px";
		this.top = this.top - 2;
		this.nextFrame();
	}
}
Sprite.prototype.moveWest = function () {
	if (this.left >= 2){
		this.sdiv.style.left = this.left - 1 + "px";
		this.left = this.left - 1;
		this.nextFrame();
	}
}
Sprite.prototype.moveEast = function () {
	//alert("me " + );
	if (this.left <= 2600){
		this.sdiv.style.left = this.left + 1 + "px";
		this.left = this.left + 1;
		this.nextFrame();
	}
}

function moveSprite(sprite,direction){
	if (direction == "N"){
		sprite.moveNorth();
	} else if ( direction == "S"){
		sprite.moveSouth();
	} else if (direction == "W"){
		sprite.moveWest();
	} else if (direction == "E"){
		sprite.moveEast();
	}
	sprite.move_index = sprite.move_index + 1;
}

Sprite.prototype.nextShotFrame = function () {
	if (this.shoot_index > 9){
		this.shoot_index = 1;
	} else {
		this.shoot_index = this.shoot_index + 1;
	}
	this.shoot_div.style.background = bgs['shot'][Math.round(this.shoot_index/2) -1];
	
}

Sprite.prototype.nextmShotFrame = function () {
	if (this.shoot_index > 9){
		this.shoot_index = 1;
	} else {
		
		this.shoot_index = this.shoot_index + 1;
	}
	//alert("frame:" + shoot_index);
	this.mshoot_div.style.background = bgs['mshot'][Math.round(this.shoot_index/2) -1];
	
}

Sprite.prototype.nextHitFrame = function () {
	if (this.hit_index > 9){
		this.hit_index = 1;
	} else {
		this.hit_index = this.hit_index + 1;
	}
	this.hit_div.style.background = bgs['hit'][Math.round(this.hit_index/2) -1];
}

function clearMap(){
	var w = document.getElementById("the_map");
	w.innerHTML = "";
}

function dieSprite(sprite){
	//sprite.sdiv.display = "none";
	var w = document.getElementById("the_map");
	alert (sprite.name  + " - " + sprite.type + "Dying at " + sprite.x_coord + "," + sprite.y_coord);
	if (sprite.sdiv){
		w.removeChild(sprite.sdiv);
	}
	if (sprite.type == "infantry" || sprite.type == "hippies"){

		var ts = new Sprite(sprite.name,"skel",sprite.x_coord,sprite.y_coord);
		sprite = ts;
	} else {
		var ts = new Sprite(sprite.name,"wreck",sprite.x_coord,sprite.y_coord);
		alert("new sprite ts = " + ts.sdiv);
		sprite = ts;
	}
}
function mshootSprite(sprite){
	//alert("shooting")
	if (sprite.mshoot_div){
		sprite.nextmShotFrame();
	} else {
		sprite.makeMissileShotDiv();
		sprite.nextmShotFrame();
	}
}

function shootSprite(sprite){
	//alert("shooting")
	if (sprite.shoot_div){
		sprite.nextShotFrame();
	} else {
		sprite.makeShotDiv();
		sprite.nextShotFrame();
	}
}
function hitSprite(sprite,x,y){
	//alert("shooting")
	if (sprite.hit_div){
		sprite.nextHitFrame();
	} else {
		sprite.makeHitDiv(x,y);
		sprite.nextHitFrame();
	}
}
