// uses: objutl.js

// list browsing
function LSTGetNext(i) {
	var l, n, s = i._sls;
	if (s && s.Len()) return s.GetAt(0);
	while (l = i._lst) {
		n = l.GetInd(i)+1;
		if (n < l.Len()) return l.GetAt(n);
		i = l;
	}
	return null;
}
function LSTGetPrev(i) {
	var l = i._lst;
	if (!l) return null;
	var n = l.GetInd(i);
	if (!n) return l;
	i = l.GetAt(n-1);
	while (i._sls && i._sls.Len()) i = i._sls.GetAt(i._sls.Len()-1);
	return i;
}
function LSTvHit(l, y) {
	var i, t, n, b = l._pbox; y -= ouf_topos(b).y;
	if (y < 0) return null;
	n = l.Len();
	while (n--) {
		if ((t = ouf_topos((i = l.GetAt(n))._pbox, b).y) <= y) {
			if (l = i._sls) b = i._pbox, y -= t, n = l.Len();
			else return i;
		}
	}
	return l;
}

// generic list
DECL_CLASS("LSTStream");
function LSTStream() { this._baseinit(); this._sls=this; }
LSTStream.Build = function (box, horl, nobr, valign) {
	this._baseinit(); this._sls=this;
	this.nobr = nobr; this.valign=valign; this.horl = horl; this._pl=1;
	this._pbox = box;
	this._Lay();
}
D_CONSTRUCTOR(LSTStream, LSTStream.Build)
LSTStream.Init = function (horl, nobr, valign) {
	this._baseinit();
	this.nobr = nobr; this.valign=valign; this.horl = horl; this._pl=1;
}
D_CONSTRUCTOR(LSTStream, LSTStream.Init)
LSTStream.prototype._Lay = function () {
	ouf_clear(this._pbox);
	if (this.nobr) this._pbox.appendChild(this._chbox = document.createElement("NOBR"));
	else this._chbox = this._pbox;
}
LSTStream.prototype.Ins = function (i, bef) {
	var ip, b, n, c = this._chbox, s;
	if (this.horl) s = document.createElement("SPAN"); // , s.style.height = "1px";
	else s = document.createElement("DIV");
	if (bef) {
		n = this.GetInd(bef);
		bef._pbox.parentNode.insertBefore(s, bef._pbox);
		i._ll=0;
	}
	else {
		n = c.childNodes.length;
		c.appendChild(s);
		i._ll=1;
		if (n) { (ip=this.GetAt(n-1))._ll=0; if (ip.Update) ip.Update(); }
	}
	
	b = c.childNodes[n];
	if (this.valign) b.style.verticalAlign = this.valign;
	(b._it = i)._pbox = b; i._lst = this; i._hlst = this.horl;
	if (!i._fmt) i._fmt = this._fmt;
	i._Lay();
}
LSTStream.prototype.Del = function (i) {
	var ip; if ((i==this.GetAt(this.Len()-1)) && (this.Len() > 1)) { (ip=this.GetAt(this.Len()-2))._ll=1; if (ip.Update) ip.Update(); }
	i._pbox.parentNode.removeChild(i._pbox);
}
LSTStream.prototype.Len = function () { return this._chbox.childNodes.length; }
LSTStream.prototype.GetAt = function (ind) { return this._chbox.childNodes[ind]._it; }
LSTStream.prototype.GetInd = function (i) {
	var c = this._chbox.childNodes, j = c.length;
	while (j--) if (c(j)._it == i) return j; return -1;
}
LSTStream.prototype.Show = function (i, s) { i._pbox.style.display = (i._pbox._hidden = s) ? "inline" : "none"; }
LSTStream.prototype.Clear = function () { ouf_clear(this._chbox); }

// mouse tracing item
DECL_CLASS("LSTmtItem");
function LSTmtItem() { this._baseinit(); }
LSTmtItem.prototype.Act = function () {}
LSTmtItem.prototype._UpdSt = function (dn,ovr) {}
LSTmtItem.prototype._SetOvrBox = function (b) {
	(b._it = this)._obx = b;
	ouf_setevt(b, "mouseover", new Function("e", "e.currentTarget._it._MsOvr(e)"));
	ouf_setevt(b, "mouseout", new Function("e", "e.currentTarget._it._MsOut(e)"));
	ouf_setevt(b, "mousedown", new Function("e", "e.currentTarget._it._MsDn(e)"));
	ouf_setevt(b, "mouseup", new Function("e", "e.currentTarget._it._MsUp(e)"));
}
LSTmtItem.prototype._MsOvr = function (e) { if (!ouf_contains(this._obx, e.relatedTarget)) this._UpdSt(0, 1), this._ovr = 1, this._dn = 0; }
LSTmtItem.prototype._MsOut = function (e) { if (!ouf_contains(this._obx, e.relatedTarget)) this._UpdSt(0, 0), this._ovr = this._dn = 0; }
LSTmtItem.prototype._MsDn = function (e) { if (e.button == 0) this._UpdSt(1, 1), this._ovr = this._dn = 1; }
LSTmtItem.prototype._MsUp = function (e) { if ((e.button == 0) && this._dn) this.Act(), this._UpdSt(0, 1), this._ovr = 1, this._dn = 0; }

// static text item
DECL_CLASS("LSTtxtItem");
function LSTtxtItem() { this._baseinit(); }
LSTtxtItem.Init = function (tit, stl, nobr) {
	this._baseinit();
	this.tit = tit; this.stl = stl; this.nobr = nobr;
}
D_CONSTRUCTOR(LSTtxtItem, LSTtxtItem.Init)
LSTtxtItem.prototype._Lay = function () {
	ouf_clear(this._pbox);
	var t = document.createTextNode(this.tit);
	if (this.nobr) {
		var c = document.createElement("NOBR");
		c.appendChild(t);
		this._pbox.appendChild(c);
	}
	else this._pbox.appendChild(t);
	this._pbox.className = this.stl ? this.stl : this._fmt._stlST;
}

// attaching to ch-I
if (window['GI']) {
GI.LSTGetNext=LSTGetNext;
GI.LSTGetPrev=LSTGetPrev;
GI.LSTvHit=LSTvHit;
GI.LSTStream=LSTStream;
GI.LSTmtItem=LSTmtItem;
GI.LSTtxtItem=LSTtxtItem;
}
