Pole.prototype = new Layer();
Pole.prototype.constructor = Pole;
Pole.superclass = Layer.prototype;

function Pole(id, imgTop, imgMid)
{
	//alert("Pole: Constructor");
	this.SetId(id);
	this.imagesLoaded = 0;
	this._offsetLeft = null; //Left pos. offset from horz-center. Init to null so isNaN reacts
	this._offsetTop = null; //Top pos. offset from vert-center
	this._imgTop = null;
	this._imgMid = null;
	
	this.Build();
	this.SetImages(imgTop, imgMid);		
		
	//---- Collision Detection ----
	this._cdCurrCollidable = null;
}

Pole.prototype.ClassName = function()
{
	return "Pole";
}

Pole.prototype.SetImages = function(imgTop, imgMid)
{
	if (imgTop == null)
	{
		alert("Pole: Top image invalid.");
		return;
	}
	else if (imgMid == null)
	{
		alert("Pole: Mid image invalid.");
		return;
	}	
	else if (!imgTop.complete || !imgMid.complete)
	{
		alert("Pole: ERROR: Not all images loaded.");
		return;
	}
	
	this._imgTop = imgTop;
	this._imgMid = imgMid;
	
	document.getElementById(this.GetId() + "top").style.backgroundImage = "url('" + imgTop.src + "')";
	document.getElementById(this.GetId() + "mid").style.backgroundImage = "url('" + imgMid.src + "')";
	
	this.SetWidth(imgTop.width);
	document.getElementById(this.GetId() + "mid").style.width = imgMid.width + "px";
}

Pole.prototype.Build = function()
{
	var elem = document.createElement("div");
	
	var attr_Id = document.createAttribute("id");
	attr_Id.value = this.GetId();
	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);
	
	elem.innerHTML = 
		"<table id=\"" + this.GetId() + "top\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\" style=\"margin: 0; background-repeat: no-repeat; background-position: center\">" +
		"<tr><td>&nbsp;</td></tr></table>" +
		"<table id=\"" + this.GetId() + "mid\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"margin: 0\">" +
		"<tr><td>&nbsp;</td></tr></table>";	
	
	document.body.appendChild(elem);
}

Pole.prototype.Position = function(offsetTop, offsetLeft, doc)
{			
	this._offsetTop = offsetTop;
	this._offsetLeft = offsetLeft;	

	this.OnResize(); //Don't delete
}

Pole.prototype.OnResize = function()
{
	if (isNaN(this._offsetTop) || isNaN(this._offsetLeft))
	{
		alert("Pole::Update: Position is not set.");
		return;
	}
	
	//alert(this._offsetTop);
			
	this.SetTop(parseInt(window.innerHeight/2 + this._offsetTop));
	this.SetLeft(window.innerWidth/2 - this.GetWidth()/2 + this._offsetLeft);	
	
	//alert(parseInt(doc.getElementById(this.GetId() + "top").style.height));
	//alert(document.body.offsetHeight + " " + this.GetTop());
	
	//alert(window.innerHeight + " " +
	//		this.GetTop());
	
	this.SetHeight(window.innerHeight - this.GetTop()); //Must be after SetTop	
	document.getElementById(this.GetId() + "mid").style.height =  (this.GetHeight() - this._imgTop.height) + "px"; //+2
	
	//parseInt(doc.getElementById(this.GetId() + "top").style.height);
}

//------------ Collision Detection ------------------------------
Pole.prototype.CDSetCurrCollidable = function(currCollidable)
{
	this._cdCurrCollidable = currCollidable;
}

Pole.prototype.CDGetCurrCollidable = function()
{
	return this._cdCurrCollidable;
}


