function CitizenImage(image, centerOffset, bottomLine)
{
	this.image = image;
	this._centerOffset = centerOffset;
	this._bottomLine = bottomLine;
}

CitizenImage.prototype.GetCenterOffset = function()
{
	return parseInt(this._centerOffset);
}

CitizenImage.prototype.GetBottomLine = function()
{
	return parseInt(this._bottomLine);
}

CitizenImage.prototype.GetHeight = function()
{
	return parseInt(this.image.height);
}

CitizenImage.prototype.GetWidth = function()
{
	return parseInt(this.image.width);
}

CitizenImage.prototype.GetSrc = function()
{
	return this.image.src;
}

//--------------------------------------------------------------------------------------------------------------------

Citizen.prototype = new Layer();
Citizen.prototype.constructor = Pole;
Citizen.superclass = Layer.prototype;

function Citizen(id, images, startImage)
{
	var elem = document.createElement("div");
	var attr_Id = document.createAttribute("id");
	attr_Id.value = id;
	elem.setAttributeNode(attr_Id);	
	var attr_Style = document.createAttribute("style");
	attr_Style.value = "position:absolute; visibility: hidden";
	elem.setAttributeNode(attr_Style);
	var attr_Align = document.createAttribute("align");
	attr_Align.value = "center";
	elem.setAttributeNode(attr_Align);
	document.body.appendChild(elem);
	elem.innerHTML = "<img id=\"" + id + "_image\">";	//Strange: when there´s an image instead of backgroundimage, the wait cursor does not appear as much (IE)
	
	this.SetId(id);	
	this._images = images;
	this._startImage = startImage;
	this._currImage = startImage;
	this._object = elem; //doc.getElementById(this.GetId());
	this._imageElem = document.getElementById(id + "_image");

	this._object.style.backgroundRepeat = 'no-repeat'; 
	//style.backgroundAttachment='fixed'; 
	//style.backgroundPosition='';
	
	//Actions
	this._jumpImg = null;
	
	//Event handlers
	var _this = this;
	this._funcPtrJumpCitizen = function() { _this.JumpCitizen() };
	
	//Check if all images are loaded, they should be
	var ok = true;
	for (var i = 0; i < this._images.length; ++i)
		if (!this._images[i].image.complete)
		{
			alert("Citizen::Constructor: Error: Image: " + this._images[i].image.src + " is not loaded!");
			ok = false;
		}
	if (ok)	
		this.SetImage(startImage);	
		
	//---- Collision Detection ----
	this._cdCollidables = null;
	this._cdCurrCollidable = null;
	this._cdEnabled = true;		
}

Citizen.prototype.ClassName = function()
{
	return "Citizen";
}

//____ Constants_____

Citizen.prototype.JUMP = function()
{
	return 0;
}

Citizen.prototype.EXTENSION = function()
{
	return -1;
}

//____ Functions_____
Citizen.prototype.SetImage = function(imageIndex)
{	
	if (isNaN(parseInt(imageIndex)))
	{
		alert(this.GetId() + ": SetImage: jump image not set");
		return;
	}
	
	if (imageIndex > this._images.length - 1)
	{
		alert("Citizen::SetImage: Image index out of range. Id: " + this.GetId());
		return;
	}
	this._currImage = imageIndex;	
	
	this._imageElem.src = this._images[this._currImage].image.src;
	this.SetWidth(this._images[this._currImage].GetWidth());
	this.SetHeight(this._images[this._currImage].GetHeight());
}

Citizen.prototype.GetImages = function()
{
	return this._images;
}

Citizen.prototype.OnResize = function()
{
	if (this._cdCurrCollidable.ClassName() == "Pole")
	{
		this.SetTop(this._cdCurrCollidable.GetTop() - this._images[this._currImage].GetHeight() + this._images[this._currImage].GetBottomLine());
		this.SetLeft(this._cdCurrCollidable.GetLeft() - this._images[this._currImage].GetCenterOffset());	
		this.SetZIndex(this._cdCurrCollidable.GetZIndex()-1);
	}
	/*
	if (isNaN(this._collisionLayers[this._currCollisionLayer].GetTop()) ||
		isNaN(this._collisionLayers[this._currCollisionLayer].GetLeft())
	)
	{
		alert("Citizen::Update: One or more values from collisionLayers are NaN");
		return;
	}
																	 
	this.SetTop(this._collisionLayers[this._currCollisionLayer].GetTop() - this._images[this._currImage].GetHeight() + this._images[this._currImage].GetBottomLine());
	this.SetLeft(this._collisionLayers[this._currCollisionLayer].GetLeft() - this._images[this._currImage].GetCenterOffset());	*/
}
/*
Citizen.prototype.Update = function(doc)
{
	this.SetTop(this._collisionLayers[this._currCollisionLayer].GetTop() - this._images[this._currImage].GetHeight() + this._images[this._currImage].GetBottomLine());
	this.SetLeft(this._collisionLayers[this._currCollisionLayer].GetLeft() - this._images[this._currImage].GetCenterOffset());
}*/

//____ Actions _____

Citizen.prototype.SetJumpImg = function(imageIndex)
{
	this._jumpImg = imageIndex;
}

//_____________________ Set event handlers _______________________________________________________________________
//
// Seems like a global variable must be used when giving the function references in attachEvent and SetTimeout.
// Either this.Jump... or Jump...(this) works

Citizen.prototype.SetMouseOverAction = function(action, _this)
{
	switch (action)
	{		
	case this.JUMP(): 
		this._object.addEventListener("mouseover", this._funcPtrJumpCitizen, false);
		break;			
	case this.EXTENSION():
		this._object.addEventListener("mouseover", _this, false );
		//alert(_this);
		break;
	default:
		alert('Citizen::SetMouseOverAction: action unknown');		
	}
}

Citizen.prototype.DelMouseOverAction = function(action, _this)
{
	switch (action)
	{		
	case this.JUMP(): //Does not work
		this._object.removeEventListener("mouseover", this._funcPtrJumpCitizen, false);
		break;			
	case this.EXTENSION():
		this._object.removeEventListener("mouseover", _this, false);
		//alert(_this);
		break;
	default:
		alert('Citizen::SetMouseOverAction: action unknown');		
	}
}

Citizen.prototype.JumpCitizen = function()
{
	//alert('JumpCitizen, param: ' + this);
	this.SetImage(this._jumpImg);
	this.OnResize();
	var _this = this;
	setTimeout( function() { _this.JumpTimeOut() }, 300); //Works
}

Citizen.prototype.JumpTimeOut = function()
{
	//alert(this);
	this.SetImage(this._startImage);
	this.OnResize();
}

//------------ Collision Detection ------------------------------
Citizen.prototype.CDSetCollidables = function(collidables)
{
	this._cdCollidables = collidables;	
}

Citizen.prototype.CDSetCurrCollidable = function(currCollidable)
{
	this._cdCurrCollidable = currCollidable;
	//this._cdCurrCollidable.CDSetCurrCollidable(this);
}

Citizen.prototype.CDGetCurrCollidable = function()
{
	return this._cdCurrCollidable;
}

Citizen.prototype.CDSetPos = function(x,y)
{
	//window.status = "Citizen::CDSetPos: id: " + this.GetId();
	
	this.SetTop(y);
	this.SetLeft(x);
	
	if (this._cdEnabled)
	{
		//if (this._cdCurrCollidable == null) 	//Pole only fits one at a time
			this.CDCheck();
	}
}

Citizen.prototype.CDCheck = function()
{
	if (this._cdCollidables == null || this._cdCollidables.length <= 0)
	{
		alert("CDCheck: No collidables set");
		return;
	}
	
	for (var i = 0; i < this._cdCollidables.length; i++)
	{
		//If collision
		if (this.CDCheckPole(this, this._cdCollidables[i]))
		{
			//If citizen is riding
			if (this._cdCurrCollidable != null && this._cdCurrCollidable.ClassName() == "Hovercraft")
			{
				this._cdCurrCollidable.CDDisable(10000);  //Time until this hovercraft will accept another rider
				this._cdCurrCollidable.CDSetCurrCollidable(null);				
			}
			//If the object collided with is not in a collision state
			//if (this._cdCollidables[i].CDGetCurrCollidable() == null)
			{
				this._cdCurrCollidable = this._cdCollidables[i];
				//this._cdCurrCollidable.CDSetCurrCollidable(this);
				this.CDUpdatePos();
			}			
			//return;
			break;
		}
	}
	
	//window.status = this.CDCheckPole(this, this._cdCollidables[0]) + " " + this.CDCheckPole(this, this._cdCollidables[1]) 
	//				+ " " + this._cdCurrCollidable.ClassName() + " " + this._cdCurrCollidable.GetId();

}

Citizen.prototype.CDCheckPole = function(obj1, obj2)
{
	var obj1_VCenter = obj1.GetLeft() + obj1.GetWidth()/2;
	var obj1_Bottom = obj1.GetTop() + obj1.GetHeight();
	
	if ( obj1_VCenter > (obj2.GetLeft() + obj2.GetWidth() + 30) )
		return false;
	if ( obj1_VCenter < (obj2.GetLeft() - 30) ) 
		return false;
	if ( obj1_Bottom < (obj2.GetTop() - 30) ) // bottom of obj1 is over top of obj2
		return false
	if ( obj1_Bottom > (obj2.GetTop() + 40) ) // bottom of obj1 is below top of obj2
		return false;
		
	return true;	
}

Citizen.prototype.CDDisable = function(time)
{
	this._cdEnabled = false;
	var _this = this;
	setTimeout( function() { _this._cdEnabled = true; }, time);
}

Citizen.prototype.CDUpdatePos = function()
{
	//If collided with a pole, citizen should stand on pole
	if (this._cdCurrCollidable.ClassName() == "Pole")
	{
		this.SetTop(this._cdCurrCollidable.GetTop() - this._images[this._startImage].GetHeight() + this._images[this._startImage].GetBottomLine());
		this.SetLeft(this._cdCurrCollidable.GetLeft() - this._images[this._startImage].GetCenterOffset());	
		this.SetZIndex(this._cdCurrCollidable.GetZIndex()-1);
		this.SetImage(this._startImage);
		this.SetMouseOverAction(this.JUMP(), this);
		if (this.onJumpToPole)
		    this.onJumpToPole();
	}
}

Citizen.prototype.SetEvtJumpToPole = function(func)
{
    this.onJumpToPole = func;
}
