// Copyright (c) 2003-2005 Juerg Lehni, Lineto.com. All rights reserved.
// Copying and reuse is strictly prohibited.

var fix = {
	shadow: is.nav4 || (is.win && is.ie5),
	scrollbars: is.nav4,
	hideOnMove: is.nav4 || (is.ie && is.win),
	move: is.ie && is.mac,
	showContent: is.nav4 && is.mac,
	select: is.ie && is.mac,
	blur: is.sfr,
	autoscroll: is.sfr,
	pattern: is.ie,
	images: is.win && is.ie5up && !is.ie5,
	text: is.ie && is.mac,
	tm: is.opera
};

// cbe
__cbe.setRect=function(x, y, w, h){this.moveTo(x, y);this.sizeTo(w, h);}
__cbe.calcWidth=function(){this.w=null; return this.width();}
__cbe.calcHeight=function(){this.h = null;return this.height();}
__cbe.createChild = function(v) {
	var e = document.cbe.createElement("div");
	this.appendChild(e);
	if (is.nav4 && v==null) v=1;
	if (v!=null) e.cbe.visibility(v);
	return e.cbe;
}

__cbe.innerText = function(text, align, tags) {
	if (!tags) tags = [];
	if (document.createTextNode) {
		var e = fix.text ? null : document.createTextNode(text), c = this.ele.firstChild;
		if (fix.text && tags.length == 0) {
			e=document.createElement('span');
			e.innerText = text;
		} else {
			var col = tags.color;
			for (var n in tags) if (n != 'color') {
				var t = tags[n];
				var ne = document.createElement(n);
				for (var j in t) ne[j == 'class' ? 'className' : j] = t[j];
				if (e != null) ne.appendChild(e);
				else ne.innerText = text;
				e = ne;
				if (col) e.style.color = col;
				col = null;
			}
		}
		if (c) c.replaceChild(e, c.firstChild);
		else this.ele.appendChild(document.createElement("span")).appendChild(e);
		if (align && this.ele.style) this.ele.style.textAlign=align;
	} else {
		if (tags.color) text = '<font color="' + tags.color + '">' + text + '</font>';
		for (var n in tags) if (n != 'color') {
			var t = tags[n];
			var s = '<' + n;
			for (var j in t) s += ' ' + j + '="' + t[j] + '"';
			text = s + '>' + text + '</' + i + '>';
		}
		if (align) text=getTable(text, this.width(), 0, "text", align); 
		else text = '<span>'+text+'</span>';
		this.innerHtml(text);
	}
}

// gui

var topZ = 50;
var gui = {};
var zList = [];
var lineHeight = 15;

NONE = 0;
VISIBLE = 1;
RESIZEABLE = 2;
SCROLLABLE = 4;
CLOSABLE = 8;
STAYONTOP = 16;
HASTEXT = 32;
HASMORE = 64;
FIXCONTENT = 128;
NOTITLEBAR = 256;
NOSHADOW = 512;
CHOOSER = 1024;

function Window(id, x, y, width, height, skin, flags, html, margin, bgImage) {
	if (!id) return;
	var obj = cbeGetElementById(id);
	obj = obj ? obj.cbe : null;
	if (obj) {
		var c = cbeGetElementById(id + 'Content');
		if (c) this.content = c.cbe;
		else this.content = obj;
		this.content.show();
		var s = obj.ele.style;
	} else {
		obj = document.cbe.createChild(false);
		obj.id = obj.ele.id = id;
		var s = obj.ele.style;
		if (s) s.width=s.height="100%";
		this.content = obj.createChild(false);
		this.content.ele.offsetLeft; // ie mac fix
	}
	this.id = id;
	this.obj = obj;
	obj.win = this;
	this.isPage = id == 'page';
	gui[id] = this;

	this.scrollX = this.scrollY = 0;
	this.clipW = this.clipH = 0;
	this.scrollStep = lineHeight;

	if (skin) {
		this.skin = skin;
		var margins = this.margins = clone(skin.margins);
		if (!(flags & NOTITLEBAR)) {
			if (skin.hor) { margins.top += skin.size; margins.ver += skin.size; }
			else { margins.left += skin.size; margins.hor += skin.size; }
		}
		this.flags = flags;
		this.resizeable = (flags & RESIZEABLE) && skin.resize != null;
		this.scrollable = (flags & SCROLLABLE) && skin.scrollDecUp != null;
		this.closable = (flags & CLOSABLE) && skin.closeUp != null;
		this.hasMore = (flags & HASMORE) && skin.moreUp != null;
		this.hasText = flags & HASTEXT;
		this.content.visible = this.fixContent = flags & FIXCONTENT;
		this.margin = skin.margin + (margin ? margin : 0);

		this.setHtml(html);
		
		this.winW = width ? width : this.content.width() + margins.hor;
		this.winH = height ? height : this.content.height() + margins.ver;

		if (!bgImage) bgImage = skin.bgImage;
		obj.background(skin.bgColor, bgImage ? '/img/skins/backgrounds/' + bgImage : null);

		if (!(flags & NOSHADOW)) {
			this.shadow = {};
			this.shadow.all = createShadow(document.cbe, id);
			this.shadow.size = (flags & NOTITLEBAR) ? 11 : 22;
			with (this.shadow) {
				if (fix.shadow) {
					all.background('#444444');
					this.shadow.dx = this.shadow.dy = 2;
				} else {
					this.shadow.corners = [];
					this.shadow.lines = [];
					for (var i = 0; i < 4; i++) {
						lines[i] = cbeGetElementById(id + 's' + i);
						corners[i] = cbeGetElementById(id + 'sc' + i);
					}
					this.shadow.dx = this.shadow.dy = 0;
				}
				all.zIndex(topZ++);
				var h = ['mouseDown','mouseUp','dragStart','drag','dragEnd'];
				for (var i in h) all.addEventListener(h[i], shadowListener);
			}
		}
		
		if (skin.frames) {
			this.frames = [];
			for (var i = 0; i < 4; i++) {
				var frame = obj.createChild();
				frame.background(skin.frames[i]);
				this.frames[i] = frame;
			}
		}

		var size = skin.size;
		var hor = skin.hor;
		var length = hor ? this.winW : this.winH;

		if (this.resizeable) {
			var rs = obj.createChild();
			rs.size = size;
			rs.target = this;
			rs.doResize = true;
			rs.handleMouse = true;

			with (rs) {
				sizeTo(size, size);
				moveTo(0, 0);
				background(skin.color, skin.resize.src);
				addEventListener('dragStart', winDragStart);
				addEventListener('drag', winDrag);
				addEventListener('dragEnd', winDragEnd);
			}
			this.resize = rs
		}

		if (this.scrollable) {
			this.scrollDec = createButton(obj, this, 'scrollDec', skin, btnScrollDown, btnScrollUp, btnScrollUp);
			this.scrollDec.add = -this.scrollStep;
			this.scrollInc = createButton(obj, this, 'scrollInc', skin, btnScrollDown, btnScrollUp, btnScrollUp);
			this.scrollInc.add = this.scrollStep;
			this.scrollVisible = true;
		}

		var dragObj = (flags & CHOOSER) ? null : obj;
		if (!(flags & NOTITLEBAR)) {
			var start = createButton(obj, this, 'expand', skin, null, btnZoomUp);
			start.length = size;
			start.moveTo(0, 0);
			start.sizeTo(hor ? start.length : size, hor ? size : start.length);
	
			var end = null;
			if (this.closable) end = createButton(obj, this, 'close', skin, null, btnCloseUp);
			else if (this.hasMore) end = createButton(obj, this, 'less', skin, null, btnExpandUp);
			if (end) end.length = skin.size;
			else if (skin.endSize > 0) {
				end = obj.createChild();
				end.length = skin.endSize;
				end.sizeTo(hor ? end.length : size, hor ? size : end.length);
				end.background(skin.color, skin.end.src);
			}

			if (skin.grip) {
				var grip = obj.createChild();
				grip.moveTo(hor ? start.length : 0, hor ? 0 : start.length);
				grip.background(skin.color, skin.grip.src);
				dragObj = grip;
			}
			// rest in resizeTo & zoom!
			this.bar = {start: start, end: end, grip: grip};
		}

		if (dragObj) {
			dragObj.target = this;
			dragObj.doResize = false;
			with (dragObj) {
				addEventListener('dragStart', winDragStart);
				addEventListener('drag', winDrag);
				addEventListener('dragEnd', winDragEnd);
				if (is.ie && is.win || is.opera) addEventListener('dblClick', winZoom);
			}
			dragObj.handleMouse = true;
		}

		this.content.moveTo(margins.left, margins.top);
		obj.addEventListener('click', winMoveToFront);
		obj.zIndex(topZ++);
		this.showContent(false);
		this.zoom(true);
		this.moveTo(x != null ? x : obj.left(), y != null ? y : obj.top(), true);
		this.visible = false;
		this.closed = true;
		this.show(flags & VISIBLE);

		this.minW = size * (this.scrollable ? 3:2);
		this.minH = size * 2;
		this.maxW = 640;
		this.maxH = 480;

		if (!(this.flags & STAYONTOP)) this.index = push(zList, this);
	} else {
		obj.addEventListener('click', pageClick);
		this.margin = 0;
		this.margins = getMargins(0, 0, 0, 0);
		this.open = this.content.visible = this.visible = true;
		this.closed = false;
	}
}

__win = Window.prototype;
__win.width = function() { return this.obj.width(); }
__win.height = function() { return this.obj.height(); }
__win.left = function() { return this.obj.left(); }
__win.top = function() { return this.obj.top(); }

__win.setHtml = function(html, margin, init, loaded) {
	if (html != null) {
		if (loaded) {
			if (!this.isPage) this.show(false);
			this.scrollTo(0, 0);
		}
		if (fix.images) {
			var reg = new RegExp('\<img src="([a-zA-Z0-9_\/]*?\.png)"', 'gi'), r, c = 0, i = 0, n, str = "", imgs = [];
			while ((r = reg.exec(html)) != null) {
				n = reg.lastIndex;
				str += html.substring(i, n - r[0].length) + '<img src="/img/spacer.gif" id="i' + c + '"';
				push(imgs, [c++, r[1]]);
				i = n;
			}
			if (str) {
				html = str + html.substring(i, html.length);
				this.imgs = imgs;
				this.fixImages(0);
			}
		}
		if (margin != null) this.margin = margin;
		var c = this.content;
		if (is.nav4) c.innerHtml(getTable(html, c.width(), this.margin, this.hasText ? "text" : null));
		else {
			if (is.ie && is.mac && this.isPage) {
				if (this.fixObj) c.removeChild(this.fixObj.ele);
				c = c.createChild(html);
				c.ele.style.position="relative";
				this.fixObj = c;
			} else if (is.sfr) c.innerHtml(' ');
			var d = '';
			if (this.margin) d += ' style="margin:' + this.margin + 'px"';
			if (this.hasText) d += ' class="text"';
			if (d) html = '<div' + d + '>' + html + '</div>';
			c.innerHtml(html);
		}
		if (fix.select) fixSelects();
	}
	this.html = html;
	if (init) {
		if (!loaded && this.loader) this.stopLoading();
		var scroll = true;
		if (this.scrollable) this.resizeTo(this.width(), this.height());
		else if (this.scrollbars) this.layoutScrollbars();
		else {
			if (this.isPage) with(this.obj.ele) { scrollLeft = scrollTop = 0 }
			scroll = false;
		}
		if (scroll) this.scrollTo(this.scrollX, this.scrollY);
	}
	if (loaded && html != null) {
		if (!this.isPage) this.show(true);
		else if (fix.move) setTimeout(this.getRef() + ".fixMove();", 1);
		if (this.onEndLoad) this.onEndLoad();
	}
}

__win.getRef = function() {
	return "gui." + this.id;
}

__win.fixImages = function(n) {
	var m = Math.min(n, this.imgs.length);
	for (var c=0; c<m; c++) {
		var i = this.imgs.shift();
		var o = cbeGetImageByName("i" + i[0]);
		if (o) o.src = i[1];
	}
	if (m==n) setTimeout(this.getRef() + ".fixImages(4)", 1);
}

__win.renderSelect = function(name, size, width, params) {
	var str = '<select size="' + size + '" id="' + name + '" name="' + name + '"';
	for (var i in params) str += ' ' + i + '="' + params[i] + '"';
	if (is.nav4) {
		str += '><option>';
		var l = width / (is.ua.indexOf("mac") == -1 ? 5 : 7); 
		for (var j = 0; j < l; j++) str += '&nbsp;';
		str += '</option>';
	} else {
		str += ' style="margin:0px; width:' + width + 'px;">';
	}
	return str + '</select>';
}

__win.stopLoading = function() {
	stopLoading(this.loader);
}

__win.fixMove = function() {
	if (this.open) {
		if (this.isPage) {
			var s = this.obj.ele.style;
			this.obj.resizeTo(100000, 100000);
			this.obj.w = this.obj.h = null;
			s.width=s.height="100%";
		} else {
			this.obj.resizeBy(1, 1);
			this.obj.resizeBy(-1, -1);
		}
	}
}

__win.moveBy = function(dx, dy) {
	this.obj.moveBy(dx, dy);
	if (this.shadow) this.shadow.all.moveBy(dx, dy);
	this.realX += dx;
	this.realY += dy;
}

__win.moveTo = function(x, y, init) {
	if (!this.isPage) {
		var pg = gui.page, w = pg.width(), h = pg.height();
		if (init) {
			if (x < 0) x += w - this.realW;
			if (y < 0) y += h - this.realH;
		} else {
			var s = this.skin;
			if (this.flags & CHOOSER) {
				w -= this.width();
				h -= this.height();
			} else if (s) {
				w -= (s.hor ? 2 * s.size : s.size);
				h -= (s.hor ? s.size : 2 * s.size);
			}
			x = Math.min(Math.max(x, 0), w);
			y = Math.min(Math.max(y, 0), h);
		}
	}
	this.realX = x;
	this.realY = y;
	if (this.shadow) this.shadow.all.moveTo(x + this.shadow.dx, y + this.shadow.dy);
	this.obj.moveTo(x, y);
	if (fix.move && this.fixContent) this.fixMove();
}

__win.resizeTo = function(w, h) {
	this.realW = w;
	this.realH = h;
	if (this.skin) {
		var size = this.skin.size;
		if (this.resizeable) this.resize.moveTo(w - size, h - size);
		if (this.shadow) {
			if (fix.shadow) this.shadow.all.resizeTo(w, h); 
			else {
				with (this.shadow) {
					var round = size;
					var max = Math.min(w, h);
					if (round >= max) round = max - 1;
					for (var i in corners) corners[i].width = corners[i].height = round;
					lines[0].width = lines[2].width = round;
					lines[0].height = lines[2].height = h - round;
					lines[1].width = lines[3].width = w - round;
					lines[1].height = lines[3].height = round;
					dx = -Math.round(round / 2);
					dy = -Math.round(6 * round / 22);
					if (this.realX != null) all.moveTo(this.realX + dx, this.realY + dy);
				}
			}
		}
		this.obj.resizeTo(w, h);
		var hor = this.skin.hor;
		var length = hor ? w : h;
		if (this.bar) {
			var pos = length;
			with (this.bar) {
				if (end) {
					pos -= end.length;
					end.moveTo(hor ? pos : 0, hor ? 0 : pos);
				}
				if (grip) {
					pos -= start.length;
					grip.sizeTo(hor ? pos : size, hor ? size : pos);
				}
			}
		}
	
		with (this.margins) {
			w = Math.max(0, w - hor);
			h = Math.max(0, h - ver);
			var f = this.frames;
			if (f) {
				f[0].setRect(left - 1, top, 1, h);
				f[1].setRect(left - 1, top - 1, w + 2, 1);
				f[1].clip(0, w + 2, 1, 0);
				f[2].setRect(left + w, top - 1, 1, h + 1);
				f[3].setRect(left - 1, top + h, w + 2, 1);
			}
		}
	
		if (this.hasText) h -= h % lineHeight + this.margin;
		
		this.clipW = w;
		this.clipH = h;
	
		if (this.scrollable) {
			var pos = Math.round((length - size) / 2);
			this.scrollDec.moveTo(pos, 0);
			this.scrollInc.moveTo(pos, this.realH - size);
			this.updateScrolling();
			this.content.width(w);
		}
		this.scrollTo(this.scrollX, this.scrollY);
	} else this.obj.resizeTo(w, h);
}

__win.autoSize = function() {
	var f = 0;
	if (is.ie && is.mac) {
		this.obj.width(1);
		f = this.margin;
	} else {
		var s = this.obj.ele.style;
		if (s) s.width=s.height="100%";
	}
	var c = this.content;
	var m = this.margins;
	this.resizeTo(c.width() + m.hor + f, c.height() + m.ver);
	this.winW = this.realW;
	this.winH = this.realH;
}

__win.show = function(v) {
	this.showContent(v);
	if (!this.visible ^ !v) {
		this.obj.visibility(v);
		if (this.shadow) this.shadow.all.visibility(v);
		if (v) this.closed = false;
		this.visible = v;
	}
}

__win.showContent = function(v) {
	var c = this.content;
	var m = 4000;
	if (!c.visible ^ !v) {
		if (v) {
			if (this.fixContent) {
				c.moveBy(m, m);
				if (fix.showContent) setTimeout(this.getRef() + ".content.show();", 50);
				else c.show();
				if (fix.move && this.fixContent) this.fixMove();
			} else c.show();
		} else {
			if (this.fixContent) c.moveBy(-m, -m);
			if (!this.fixContent || !is.sfr) c.hide();
		}
		c.visible = v;
	}
}

__win.zIndex = function(z) {
	if (z) {
		this.obj.zIndex(z);
		this.shadow.all.zIndex(z - 1);
	} else return this.obj.zIndex();
}

__win.zoom = function(open) {
	if (!this.open ^ !open) {
		this.open = open;
		if (this.resize) this.resize.visibility(open);
		if (open) var w = this.winW, h = this.winH;
		else {
			this.winW = this.realW;
			this.winH = this.realH;
			var w = this.skin.hor ? this.winW : this.skin.size, h = this.skin.hor ? this.skin.size : this.winH;
		}
		this.showContent(open);
		this.resizeTo(w, h);
		if (this.bar) with (this.bar.start) {
			image = open ? 'expand' : 'collapse';
			change();
		}
		if (this.onZoom) this.onZoom(open);
	}
}

__win.expand = function(more) {
	if (!this.more ^ !more) {
		this.more = more;
		var d = more ? 1 : -1;
		if (this.onExpand) this.onExpand(more);
		this.resizeTo(this.realW + this.diffW * d, this.realH + this.diffH * d);
		this.winW += this.diffW * d;
		this.winH += this.diffH * d;
		if (this.bar) with (this.bar.end) {
			image = more ? 'more' : 'less';
			change();
		}
	}
}

__win.close = function() {
	this.closed = true;
	this.show(false);
}

__win.updateScrolling = function() {
	var c = this.content;
	this.scrollW = c.width();
	this.scrollH = c.height();
	this.maxX = Math.max(0, this.scrollW - this.clipW - (this.hasText ? 2 * this.margin : 0));
	this.maxY = Math.max(0, this.scrollH - this.clipH - (this.hasText ? 2 * this.margin : 0));
	if (this.scrollable) {
		var step = this.scrollStep;
		this.maxX = Math.ceil(this.maxX / step) * step;
		this.maxY = Math.ceil(this.maxY / step) * step;
	}
}

__win.scrollTo = function(x, y) {
	if (this.scrollable || this.scrollbars) {
		if (x < 0) x = 0;
		else if (x > this.maxX) x = this.maxX;
		if (y < 0) y = 0;
		else if (y > this.maxY) y = this.maxY;
		this.scrollX = x;
		this.scrollY = y;
		this.content.clip(y, x + this.clipW, y + this.clipH, x);
		if (this.content.visible) this.content.moveTo(this.margins.left - x, this.margins.top - y);
		if (this.scrollable) {
			this.scrollDec.visibility(y > 0 && this.open);
			this.scrollInc.visibility(y < this.maxY && this.open);
		}
		if (this.scrollbars) this.updateScrollbars();
		return true;
	}
	return false;
}

__win.moveToFront = function(caller) {
	if (this.dontMoveToFront) {
		delete this.dontMoveToFront;
		return;
	}
	var e = zList.length - 1;
	if (!(this.flags & STAYONTOP) && this != zList[e]) {
		if (zList[e].flags & CHOOSER) zList[e].show(false);
		var z = this.zIndex();
		for (var i = this.index; i < e; i++) {
			var w = zList[i+1];
			w.zIndex(z);
			w.index = i;
			zList[i] = w;
			z += 2;
		}
		this.zIndex(z);
		this.index = e;
		zList[e] = this;
	}
}

__win.stayOnTop = function() {
	if ((this.flags & STAYONTOP) && this != zList[zList.length - 1]) this.zIndex(topZ+=2);
}

function winDragStart(e) {
	var hl = e.cbeCurrentTarget;
	var win = hl.target;
	var now = getNow();
	if (hl.doResize) {
		win.winW = win.realW;
		win.winH = win.realH;
		if (is.nav4) {
			hl.count = 0;
			hl.last = now;
		}
	} else {
		// dblclick?
		win.winX = win.realX;
		win.winY = win.realY;
		if (hl.clk && now - hl.clk < 300) {
			win.zoom(!win.open);
			hl.clk = null;
		} else hl.clk = now;
	}
	win.moveToFront();
	if (fix.blur) blurAllForms();
}

function winDrag(e) {
	var hl = e.cbeCurrentTarget;
	var win = hl.target;
	if (fix.hideOnMove && win.fixContent && win.content.visible) win.showContent(false);
	var doit = 1;
	if (hl.last) {
		var now = getNow();
		if (now - hl.last >= 50) hl.last = now;
		else doit = 0;
	}
	with(win) {
		if (hl.doResize) {
			winW += e.dx;
			winH += e.dy;
			if (doit) {
				var num = 2;
				if (scrollable) num++;
				resizeTo(Math.min(maxW, Math.max(minW, winW)), Math.min(maxH, Math.max(minH, winH)));
				if (html && is.nav4) {
					if (++hl.count > 2) {
						setHtml(html);
						hl.count = 0;
					}
				}
			}
		} else {
			winX += e.dx;
			winY += e.dy;
			if (doit) {
				moveTo(winX, winY);
				if (win.onMove) win.onMove(e.dx, e.dy);
			}
		}
	}
}

function winDragEnd(e) {
	var hl = e.cbeCurrentTarget;
	var win = hl.target;
	if (hl.doResize && win.html && is.nav4) win.setHtml(win.html);
	if (fix.hideOnMove && win.fixContent && !win.content.visible) {
		win.showContent(true);
		win.resizeTo(win.realW, win.realH);
	}
}

function winZoom(e) {
	var win = e.cbeCurrentTarget.win;
	win.zoom(!win.open);
}

function winMoveToFront(e) {
	setTimeout(e.cbeCurrentTarget.win.getRef() + ".moveToFront();", 1);
}

function pageClick(e) {
	var w = zList[zList.length - 1];
	if (w && (w.flags & CHOOSER)) w.show(false);
}

// buttons

function btnChange(down) {
	this.background(this.skin.color, this.skin[this.image + ((down ^ this.invert) ? 'Down' : 'Up')].src);
	this.down = down;
}

function btnIsRollover(e) {
	return this.contains(e.clientX, e.clientY);
}

function createButton(parent, target, image, skin, onDown, onUp, onUpOutside, invert) {
	btn = parent.createChild();
	btn.sizeTo(skin.size, skin.size);
	btn.image = image;
	btn.onDown = onDown;
	btn.onUp = onUp;
	btn.onUpOutside = onUpOutside;
	btn.target = target;
	btn.skin = skin;
	btn.addEventListener('dragStart', btnDragStart);
	btn.addEventListener('drag', btnDrag);
	btn.addEventListener('dragEnd', btnDragEnd);
	btn.change = btnChange;
	btn.isRollover = btnIsRollover;
	btn.handleMouse = true;
	btn.invert = invert;
	btn.change();
	return btn;
}

function btnDragStart(e) {
	var btn = e.cbeCurrentTarget;
	btn.change(true);
	if (btn.onDown) btn.onDown();
}

function btnDrag(e) {
	var btn = e.cbeCurrentTarget;
	var down = btn.isRollover(e);
	if (down != btn.down) {
		btn.change(down);
		if (down) {
			if (btn.onDown) btn.onDown();
		} else {
			if (btn.onUpOutside) btn.onUpOutside();
		}
	}
}

function btnDragEnd(e) {
	var btn = e.cbeCurrentTarget;
	if (btn.isRollover(e)) {
		btn.change(false);
		if (btn.onUp) btn.onUp();
	} else if (btn.onUpOutside) btn.onUpOutside();
}

function btnCloseUp() {
	this.target.close();
}

function btnZoomUp() {
	var win = this.target;
	win.zoom(!win.open);
}

function btnExpandUp() {
	var win = this.target;
	win.expand(!win.more);
}

var scrollBtn = null;
function btnScrollDown() {
	var win = this.target;
	win.scrollTo(win.scrollX + (this.hor ? this.add : 0), win.scrollY + (this.hor ? 0 : this.add));
	this.counter = 0;
	this.mul = 1;
	scrollBtn = this;
	this.onUp();
	this.scroll = btnScrollInterval;
	this.timeout = setTimeout("scrollBtn.interval = setInterval('scrollBtn.scroll()', 50);", 400);
}

function btnScrollInterval() {
	var win = this.target;
	win.scrollTo(win.scrollX + (this.hor ? this.add * this.mul : 0), win.scrollY + (this.hor ? 0 : this.add * this.mul));
	if (++this.counter >= 10 && this.mul < 16) {
		this.mul++;
		this.counter = 0;
	}
}

function btnScrollUp() {
	if (this.timeout) clearTimeout(this.timeout);
	if (this.interval) clearInterval(this.interval);
	this.timeout = this.interval = null;
}

// shadow

function createShadow(obj, id) {
	var sh = obj.createChild(false);
	var s = 22;
	if (!fix.shadow) {
		sh.innerHtml('<table border="0" cellpadding="0" cellspacing="0"><tr>' +
		shadowImg('sc0', id, s, s) +
		shadowImg('s1', id, 1, s) +
		shadowImg('sc1', id, 1, s) + '</tr><tr>' +
		shadowImg('s0', id, s, 1) + '<td></td>' +
		shadowImg('s2', id, s, 1) + '</tr><tr>' +
		shadowImg('sc3', id, s, s) +
		shadowImg('s3', id, 1, s) +
		shadowImg('sc2', id, s, s) + '</tr></table>');
	}
	return sh;
}

function shadowImg(n, id, w, h) {
	return '<td><img id="'+id+n+'" src="/img/shadow/'+n+'.png" width="'+w+'" height="'+h+'" alt="" border="0"></td>';
}

function shadowListener(e) {
	var obj = cbeGetNodeFromPoint(e.clientX, e.clientY, e.cbeCurrentTarget);
	if (obj.handleMouse) e.cbeCurrentTarget = obj;
}

// skins

var stdImg = 'expandUp,expandDown,collapseUp,collapseDown,end,';
var closeImg = 'closeUp,closeDown,';
var moreImg = 'moreUp,moreDown,lessUp,lessDown,';
var scrollImg = 'scrollDecUp,scrollDecDown,scrollIncUp,scrollIncDown,';
var checkImg = 'checkOnUp,checkOnDown,checkOffUp,checkOffDown,';
var radioImg = 'radioOnUp,radioOnDown,radioOffUp,radioOffDown,';
var skinImages = {
	system: {
		hor: stdImg + closeImg + 'grip,',
		ver: stdImg + moreImg + 'grip,'
	},
	gradient: {
		hor: stdImg + closeImg + scrollImg + 'resize,'
	},
	paper: {
		hor: stdImg + closeImg + scrollImg + checkImg + radioImg + 'resize,'
	}
}

function Skin(style, dir, bgColor, bgImage, size, endSize, margins, margin, frames) {
	var images = skinImages[style];
	if (images[dir]) images = images[dir];
	images = images.split(',');
	images.length--;
	var pref = '/img/skins/' + style + '/' + dir + '/';
	for (var i in images) this[images[i]] = pref + images[i] + '.gif';
	pref = style + '_' + dir;
	for (var i in this) this[i] = {src: this[i], obj: cbeNewImage(pref + i, this[i])};
	this.dir = dir;
	this.hor = (dir == 'hor');
	this.color = 'transparent';
	this.bgColor = bgColor;
	this.bgImage = bgImage;
	this.size = size;
	this.endSize = endSize;
	this.margins = margins;
	this.margin = margin;
	this.frames = frames;
}

var skins = {
	system: { hor: new Skin('system', 'hor', '#dddddd', null, 12, 2, getMargins(1, 1, 1, 1), 0, ['#f8f8f8', '#f8f8f8', '#999999', '#999999']), ver: new Skin('system', 'ver', '#dddddd', null, 12, 2, getMargins(1, 1, 1, 1), 0, ['#f8f8f8', '#f8f8f8', '#999999', '#999999']) },
	gradient: new Skin('gradient', 'hor', '#eeeeee', 'gradient1.jpg', 13, 0, getMargins(12, 2, 12, 12), 0, null),
	paper: new Skin('paper', 'hor', fix.shadow ? '#fdfdfd' : '#ffffff', 'squared.gif', 13, 0, getMargins(12, 2, 12, 12), 0, null)
}

// general

bgObject = {color: null, image: null};
function setBackground(col, img) {
	bgObject.color = col;
	bgObject.image = img;
	if (is.gecko) {
		document.body.background = img ? img : '/null';
		document.body.bgColor = col ? col : '#dddddd';
	} else gui.page.obj.background(col ? col : 'transparent', img)
}

function fixSelects() {
	var s = cbeGetElementsByTagName('select');
	var a = [];
	for (var i = s.length - 1; i >= 0; i--) {
		var e = s[i];
		if (!e.fixed) {
			var v = e.size;
			if (v > 0 && e.id != '') {
				var m = e.multiple;
				e.style.display = "none";
				if (m) e.multiple = 0;
				e.size = 1;
				e.size = v;
				push(a, {e:e, m:m});
			}
			e.fixed = 1;
		}
	}
	for (var i in a) {
		var d = a[i];
		if(d.m) d.e.multiple = 1;
		d.e.style.display = "";
		d.e.selectedIndex = -1;
	}
}

function getTable(t, w, m, c, a) {
	c = c ? ' class="' + c + '"' : '';
	a = a ? ' align="' + a + '"' : '';
	return '<table width="' + w + '" border="0" cellpadding="0" cellspacing="' + m + '"><tr valign="top"><td' + a + c + '>' + t + '</td></tr></table>';
}


function getMargins(left, top, right, bottom) {
	return {left: left, top: top, right: right, bottom: bottom, hor: left + right, ver: top + bottom};
}

if (fix.scrollbars) document.write('<scr'+'ipt src="/js/scrollbar.js" type="text/javascript"></scr'+'ipt>');