+function addWindow(window)
+{
+ if (windowList == null)
+ windowList = new Array(0);
+
+ windowList.push(window);
+ relayout();
+}
+
+function removeWindow(window)
+{
+ var i;
+ for (i = 0; i < windowList.length; ++i) {
+ if (windowList[i] == window)
+ break;
+ }
+
+ var index = windowList[i].index;
+ var dim = Math.ceil(Math.sqrt(windowList.length));
+ var maxY = Math.floor((windowList.length-1) / dim);
+
+ var shrinking = Math.ceil(Math.sqrt(windowList.length - 1)) != dim;
+
+ while (true) {
+ var ix = index % dim;
+ var iy = Math.floor(index / dim);
+
+ console.log("index: " + ix + " " + iy);
+
+ if (shrinking) {
+ if (iy > 0)
+ --iy;
+ else if (++ix == dim)
+ break;
+ } else {
+ if (iy < maxY) {
+ if (ix > 0)
+ --ix;
+ else
+ ++iy;
+ } else {
+ ++ix;
+ }
+ }
+
+ var next = iy * dim + ix;
+
+ var currentIndex = indexes[index];
+ var nextIndex = indexes[next];
+
+ if (nextIndex == null)
+ break;
+
+ var temp = windowList[currentIndex];
+ windowList[currentIndex] = windowList[nextIndex];
+ windowList[currentIndex].index = currentIndex;
+ windowList[nextIndex] = temp;
+
+ index = next;
+ }
+
+ windowList.splice(indexes[index], 1);
+ relayout();
+}