// public ImageBorder(String[] imageUrls);
function ImageBorder(imageUrls)
{
	this.imageUrls = imageUrls;
}

ImageBorder.prototype.install = function(widget)
{
	var doUpdate = false;
	
	widget.borderImages = new Array(8);
	for(var i = 0; i < this.imageUrls.length; i++)
	{
		var imageUrl = this.imageUrls[i];
		
		if(imageUrl)
		{
			doUpdate |= !Toolkit.getImage(imageUrl, getMethodPointer(this, function(url, image, async, index) {			
				widget.borderImages[index] = image;
				widget.getDOMContainer().appendChild(image);
				
				if(async)
				{
					this.update(widget);
				}
			}), i);
		}
	}
	
	if(doUpdate)
	{
		this.update(widget);
	}
}

ImageBorder.prototype.uninstall = function(widget)
{
	if(widget.borderImages)
	{
		for(var i = 0; i < widget.borderImages.length; i++)
		{
			var image = widget.borderImages[i];
			
			if(image)
			{
				widget.getDOMContainer().removeChild(image);
			}
		}

		delete widget.borderImages;
	}
}

ImageBorder.prototype.update = function(widget)
{
	if(widget.updateSuspended)
	{
		return;
	}
	
	var images	= widget.borderImages;
	var w		= widget.getWidth();
	var h		= widget.getHeight();

	try
	{
		if(images[ImageBorder.N])
		{
			var imgx = (images[ImageBorder.NW] ? parseInt(images[ImageBorder.NW].style.width) : 0);
			var imgw = Math.max(w - (images[ImageBorder.NE] ? parseInt(images[ImageBorder.NE].style.width) : 0) - imgx, 0);
			
			with(images[ImageBorder.N].style)
			{
				left	= imgx;
				top		= 0;
				width	= imgw;
			}
		}
		
		if(images[ImageBorder.NE])
		{
			with(images[ImageBorder.NE].style)
			{
				left	= w - parseInt(width);
				top		= 0;
			}
		}
		
		if(images[ImageBorder.E])
		{
			var imgy = (images[ImageBorder.NE] ? parseInt(images[ImageBorder.NE].style.height) : 0);
			var imgh = Math.max(h - (images[ImageBorder.SE] ? parseInt(images[ImageBorder.SE].style.height) : 0) - imgy, 0);
			
			with(images[ImageBorder.E].style)
			{
				top		= imgy;
				left	= w - parseInt(images[ImageBorder.E].style.width);
				height	= imgh;
			}
		}
		
		if(images[ImageBorder.SE])
		{		
			with(images[ImageBorder.SE].style)
			{
				left	= w - parseInt(width);
				top		= h - parseInt(height);
			}
		}
		
		if(images[ImageBorder.S])
		{
			var imgx = (images[ImageBorder.SW] ? parseInt(images[ImageBorder.SW].style.width) : 0);
			var imgw = Math.max(w - (images[ImageBorder.SE] ? parseInt(images[ImageBorder.SE].style.width) : 0) - imgx, 0);
	
			with(images[ImageBorder.S].style)
			{
				left	= imgx;
				top		= h - parseInt(height);
				width	= imgw;
			}
		}
		
		if(images[ImageBorder.SW])
		{
			with(images[ImageBorder.SW].style)
			{
				left	= 0;
				top		= h - parseInt(height);
			}
		}
		
		if(images[ImageBorder.W])
		{
			var imgy = (images[ImageBorder.NW] ? parseInt(images[ImageBorder.NW].style.height) : 0);
			var imgh = Math.max(h - (images[ImageBorder.SW] ? parseInt(images[ImageBorder.SW].style.height) : 0) - imgy, 0);
			
			with(images[ImageBorder.W].style)
			{
				top		= imgy;
				left	= 0;
				height	= imgh;
			}
		}
		
		if(images[ImageBorder.NW])
		{
			with(images[ImageBorder.NW].style)
			{
				left	= 0;
				top		= 0;
			}
		}
	} catch (e)
	{
	}
}

ImageBorder.prototype.toString = function()
{
	return "ImageBorder [imageUrls = " + this.imageUrls + "]";
}

ImageBorder.N	= 0;
ImageBorder.NE	= 1;
ImageBorder.E	= 2;
ImageBorder.SE	= 3;
ImageBorder.S	= 4;
ImageBorder.SW	= 5;
ImageBorder.W	= 6;
ImageBorder.NW	= 7;
