﻿function ImagesChain(IDcontainer)
{
	this.container = Get(IDcontainer);
	this.images = new Array();
}

ImagesChain.prototype.AddImage = function(image, alt)
{
	this.images[this.images.length] = {url:image, alt:(alt == null)?'':alt};
};

ImagesChain.prototype.Load = function(index)
{
	if(this.container == null)
		return;

	if(index == null)
		index = 0;
		
	//alert('going to load ' + index + ' - ' + this.images[index].url);

	//
	var a = document.createElement('A');
	a.href = this.images[index].url.split('/big_').join('/lightbox_');
	a.href = a.href.split('/r_').join('/big_');
	a.rel = 'lightbox[' + this.container.id + ']';
	
	//
	var img = document.createElement('IMG');
	img.owner = this;
	img.src = this.images[index].url;
	img.alt = this.images[index].alt;
	img.index = index;
	img.onload = function()
	{
		//alert(this.index + ': ' + this.parentNode.offsetWidth + "/" + this.offsetWidth + ' [' + (this.index + 1) + '/' + this.owner.images.length + ']');
		//alert(this.index + ' loaded');
		//this.parentNode.style.width = (this.parentNode.offsetWidth + this.offsetWidth + 10) + 'px';
		this.parentNode.parentNode.style.width = (this.parentNode.parentNode.offsetWidth + this.offsetWidth + 10) + 'px';
		if(this.index < this.owner.images.length - 1)
		{
			//alert('load next - ' + (this.index + 1));
			this.owner.Load(this.index + 1);
		}
	};
	img.onerror = function()
	{
		if(this.index < this.owner.images.length - 1)
			this.owner.Load(this.index + 1);
		this.parentNode.removeChild(this);
	};
	//this.container.appendChild(img);
	a.appendChild(img);
	this.container.appendChild(a);
};
