// JavaScript1.1 required

function Banner(iid, img, url, alt, tgt)
{
	this.iid = iid;
	this.img = img;
	this.url = url;
	this.alt = alt;
	this.tgt = tgt;
	this.rn = Math.floor(Math.random() * 1000);
}

function Banners(name)
{
	this.name = name;	// not used
	this.item = new Array();
	this.f_item = new Array();
	this.add = _addBanner;
	this.random = _randomBanner;
}

function _addBanner(iid, img, url, alt, tgt, f)
{
	var b = new Banner(iid, img, url, alt, tgt);
	if (f) this.f_item[this.f_item.length] = b;
	else this.item[this.item.length] = b;
}

function _compareBanner(a, b)
{
	return a.rn - b.rn;
}

function _selectBanner(a, n, eid)
{
	var b = new Array();
	a.sort(_compareBanner);
	for (var i = 0, j = 0; i < a.length && j < n; i++)
		if (typeof(eid) == 'undefined' || a[i].iid != eid)
			b[j++] = a[i];
	return b;
}

function _randomBanner(n, except_id)
{
	if (n > this.f_item.length + this.item.length)
		n = this.f_item.length + this.item.length;
	var a = _selectBanner(this.f_item, n, except_id);
	var b = _selectBanner(this.item, n - a.length, except_id);
	for (var i = 0; i < a.length; i++) // merge a & b
		b[b.length] = a[i];
	b.sort(_compareBanner);
	return b.length ? b : null;
}

// vi:set ts=4 sw=4 ai sm:

