
function _pr(a,b) {
	if (a == null) {
		a = '';
	} else if (a._title == '') {
		return;
	} else {
		a = a._title;
	}
	if (b == null) {
		b = '';
	} else if (b._title == '') {
		return;
	} else {
		b = b._title;
	}

	var xh = XHTTP();
	xh.open('POST', document._webshell, true);
	xh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xh.onreadystatechange = function() {
		return true;
	};
	var s = escape('config::WS_PRE') + '=' + escape(a)
		+ '&' + escape('config::WS_POST') + '=' + escape(b);
	xh.send(s);
}

function _e(a) { if (typeof a == 'undefined') a = window.event; if (typeof a.layerX == 'undefined')a.layerX=a.offsetX; if (typeof a.layerY=='undefined')a.layerY=a.offsetY;return a; }
function _o(a,c) { var b=0; while (a != null) { b += a['offset'+(c?'Left':'Top')];a=a.offsetParent; } return b; }

function toolbox_setup() {
	var i,j;

	var a;
	var ao = [];
	var reorder = function() {
		a = document.getElementsByClass('toolbox');
		for (i = 0; i < a.length; i++) {
			ao[i] = _o(a[i], false);
		}
	};
	reorder();


	var d;
	if (!document._drawbox) {
		d = document.createElement('DIV');
		d.style.display = 'none';
		d.style.position = 'absolute';
		d.style.cursor = 'move';
		d.style.border = '2px solid #999999';
		d.style.innerHTML = '';
		document._drawbox = d;
		document.body.appendChild(document._drawbox);
	} else {
		d = document._drawbox;
	}

	var first = -1;
	for (i = 0; i < a.length; i++) {
		var box = a[i];
		var head;
		if (!box.childNodes) continue;
		for (j = 0; j < box.childNodes.length; j++) {
			if (box.childNodes[j].className && box.childNodes[j].className == 'head') {
				head = box.childNodes[j];
				break;
			}
		}
		if (!head) continue;

		if (first < 0) {
			first = i;
			continue; /*mwahahahah */
		}

		box._title = head.innerHTML;

		head.style.cursor = 'move';

		head._box = box;
		head.onmousedown = function(e) {
			e = _e(e);
			if (e.preventDefault) e.preventDefault();
			if (e.stopPropagation) e.stopPropagation();

			var box = this._box;

			var sa, sb, sc, sd, se;

			var moved = 0;

			var scrollme = function() {
				var y = d._lasty;

				var wh = window.innerHeight;
				if (!wh) wh = window.height;

				var qh = Math.sqrt(wh);
				if (qh < 16) qh = 16;

				var bh = wh - qh;
				if ((moved > box.offsetHeight / 4) && box.offsetHeight < wh/2 && box.offsetHeight < bh) {
					bh -= (box.offsetHeight / 2);
				}

				var z, dd;
				if (y < qh) {
					dd = -1;
					z = qh - y;
				} else if (y > bh) {
					dd = 1;
					z = y - bh;
				} else {
					return true;
				}

				/* qh -> 0 */
				z = (z+Math.log(z)) * dd;
				z = parseInt(z);

				var wt = window.pageYOffset;
				if (!wt && document.documentElement)
					wt = document.documentElement.scrollTop;
				if (!wt && document.body)
					wt = document.body.scrollTop;

				window.scrollBy(0, z);

				var nt = window.pageYOffset;
				if (!nt && document.documentElement)
					nt = document.documentElement.scrollTop;
				if (!nt && document.body)
					nt = document.body.scrollTop;

				z = (nt - wt);
				var dtop = parseInt(d.style.top) + z;
				wh -= 4;
				if (dtop + box.offsetHeight > wt+wh) {
					dtop = (wt+wh) - box.offsetHeight;
				}

				d.style.top = dtop + 'px';
				return true;
			};

			var interval = window.setInterval(scrollme, 100);

			var s = box.innerHTML;
			if (s.length > 10000) {
				d.innerHTML = '';
				d.style.MozOpacity = 1.0;
				d.style.opacity = 1.0;
				if (d.filters && d.filters.alpha)
					d.filters.alpha.opacity = 100;
			} else {
				d.innerHTML = box.innerHTML;
				d.style.MozOpacity = 0.5;
				d.style.opacity = 0.5;
				if (d.filters && d.filters.alpha)
					d.filters.alpha.opacity = 50;
			}
			d.className = 'toolbox';
			d.style.display = 'block';
			d.style.left = _o(box, true) + 'px';
			d.style.top = _o(box, false) + 'px';
			d.style.width = box.offsetWidth + 'px';
			d.style.height = box.offsetHeight + 'px';

			d._lastx = e.clientX;
			d._lasty = e.clientY;

			sa = document.onmouseup;
			sb = document.onmousemove;
			sc = document.body.style.cursor;
			sd = document.onselectstart;
			se = document.onselect;

			document.body.style.cursor = 'move';

			document.onmouseup = function(e) {
				e = _e(e);

				window.clearInterval(interval);

				d.style.display = 'none';

				document.onmouseup = sa;
				document.onmousemove = sb;
				document.body.style.cursor = 'default';
				document.body.style.cursor = sc;
				document.onselectstart = sd;
				document.onselect = se;

				if (e.preventDefault) e.preventDefault();
				if (e.stopPropagation) e.stopPropagation();

				return false;
			};

			document.onselectstart = function(e) {
				e = _e(e);

				if (e.preventDefault) e.preventDefault();
				if (e.stopPropagation) e.stopPropagation();

				return false;
			};
			document.onselect = function(e) {
				e = _e(e);

				if (e.preventDefault) e.preventDefault();
				if (e.stopPropagation) e.stopPropagation();

				return false;
			};

			document.onmousemove = function(e) {
				e = _e(e);

				if (e.preventDefault) e.preventDefault();
				if (e.stopPropagation) e.stopPropagation();

				/* discard selection if there is one */
				if (window.getSelection)
					window.getSelection().removeAllRanges();

				if (document.selection
				&& document.selection.empty)
					document.selection.empty();

				if (document.__operahack) {
					document.__operahack();
				}

				var pp = box.parentNode;
				var fz = box.offsetHeight;

				var bt = _o(box,false);

				var f = parseInt(d.style.top);
				var g = parseInt(d.style.left);
				var dy = e.clientY - d._lasty;
				var dx = e.clientX - d._lastx;
				f += dy; g += dx;
				d._lasty = e.clientY;
				d._lastx = e.clientX;

				var wh = window.innerHeight;
				if (!wh) wh = window.height;

				moved += Math.sqrt((dy*dy)+(dx*dx));

				var wt = window.pageYOffset;
				if (!wt && document.documentElement)
					wt = document.documentElement.scrollTop;
				if (!wt && document.body)
					wt = document.body.scrollTop;

				wh -= 4;
				if (f + fz > (wt+wh)) {
					f = (wt+wh)-fz;
				}

				d.style.top = f + 'px';
				d.style.left = g + 'px';

				f = e.clientY+wt;

				var lt = 0;
				for (g = first+1; g < ao.length; g++) {
					var bq = ao[g];
					if (bt <= ao[g]) {
						bq = ao[g] + (a[g].offsetHeight / 2);
					} else {
						bq = ao[g] + (fz / 2);
					}

					if (f <= bq) {
						if (a[g] == box) return false;
						if (box.nextSibling == a[g]) {
							if (g == ao.length-1) {
								pp.removeChild(box);
								pp.appendChild(box);
								_pr(box, null);
								reorder();
							}
							return false;
						}

						pp.removeChild(box);
						a[g].parentNode.insertBefore(box, a[g]);
						_pr(box, box.nextSibling);
						reorder();
						return false;
					}
					lt = bq + a[g].offsetHeight;
				}

				return false;
			};

			return false;
		};

	}
}



