var _BANNERS = [];

function Banner(parentId, initSpacing) {
	this.parentElement = document.getElementById(parentId);
	this.spacing = initSpacing;

	this.buffer = document.createElement('div');
	this.buffer.style.height = 50 + 'px';
	this.buffer.style.position = 'relative';
	this.buffer.style.overflow = 'hidden';
	this.parentElement.appendChild(this.buffer);

	this.ID = _BANNERS.length;
	_BANNERS[this.ID] = this;

	this.images = [];
	this.lefts = [];

	this.addImage = function(src) {
		tempImg = new Image();
		tempImg.src = src;
		this.images[this.images.length] = tempImg;
	}

	this.ready = function() {
		for (i = 0; i < this.images.length; i++) {
			if (!this.images[i].complete) {
				return false;
			}
		}
		return true;
	}

	this.arrange = function() {
		x = 0;
		for (i = 0; i < this.images.length; i++) {
			this.images[i].style.position = 'absolute';
			this.images[i].style.top = 0 + 'px';
			this.images[i].style.left = x + 'px';
			this.buffer.appendChild(this.images[i]);
			this.lefts[i] = x;
			x += this.images[i].width + this.spacing;
		}
	}

	this.start = function() {
		if (this.ready()) {
			this.arrange();
			this.shift();
		}
		else {
			setTimeout('_BANNERS[\'' + this.ID + '\'].start()', 100);
		}
	}

	this.shift = function() {
		if (this.lefts[0] + this.images[0].width <= 1) {
			tempImg = this.images[0];
			for (i = 0; i < this.images.length - 1; i++) {
				this.images[i] = this.images[i + 1];
				this.lefts[i] = this.lefts[i + 1];
			}
			this.images[i] = tempImg;
			this.lefts[i] = this.lefts[i - 1] + this.images[i - 1].width;
		}
		for (i = 0; i < this.images.length; i++) {
			this.lefts[i]--;
			this.images[i].style.left = this.lefts[i] + 'px';
		}
		setTimeout('_BANNERS[\'' + this.ID + '\'].shift()', 30);
	}
}
