if (!Array.push) {
    Array.prototype.push = function (x) {
	this[this.length] = x;
    };
}

(function () {

var resizers = [];

/* for x in list: code
 * forin(list, function (x) {code}); */
function forin(list, f) {
    var i, n=list.length;
    for (i=0; i<n; i++) {
	f(list[i]);
    }
}
function sumf(xs, f) {
    var s = 0;
    forin(xs, function (x) {
	s += f(x) || 0;
    });
    return s;
}
/* DOM */
function search_nodes(nodes, visit) {
    forin(nodes, function (x) {
	if (visit(x)) {
	    search_nodes(x.childNodes, visit);
	}
    });
}
function nonempty(x) {
    if (x.nodeName == "#text") {
	return x.nodeValue;
    }
    x = x.firstChild;
    while (x) {
	if (nonempty(x)) { return true; }
	x = x.nextSibling;
    }
}
function span(text) {
    var elem = document.createElement("span");
    elem.appendChild(document.createTextNode(text));
    return elem;
}
function hspacer(n) {
    var sp = span("");
    sp.style.margin = "0";
    sp.style.padding = "0 " + n + "px 0 0";
    sp.style.border = "0";
    sp.style.background = "none";
    return sp;
}
/* toggle following elements (siblings) */
function show_hide(x, state) {
    function hidden(x) {
	var s = x.style;
	var d = x.style.display;
	return {
	    show: function () { s.display = d; }
	};
    }
    if (state.hidden) {
	forin(state.hidden, function (x) { x.show(); });
	delete state.hidden;
    } else {
	state.hidden = [];
	x = x.nextSibling;
	while (x) {
	    if (x.style) {
		state.hidden.push(hidden(x));
		x.style.display = "none";
	    }
	    x = x.nextSibling;
	}
    }
}

function icon_view(x) {
    var dblwidth = 2*x.width + "px";
    var dblheight = 2*x.height + "px";
    if (x.className == "icons16") {
	x.onclick = function () {
	    if (x.style.backgroundImage != "none") {
		x.style.backgroundImage = "none";
	    } else {
		if (x.style.width != dblwidth) {
		    x.style.width = dblwidth;
		    x.style.height = dblheight;
		    x.className += "-2x";
		} else {
		    x.style.width = "";
		    x.style.height = "";
		    x.className = x.className.slice(0,-3);
		}
		x.style.backgroundImage = "";
	    }
	}
    }
}

function init() {
    var body = document.body,
	floats = [];
    floats.update = function () {
	var i, n=this.length, x, y=this[0];
	for (i=1; i<n; i++) {
	    x = y;
	    y = this[i];
	    if (y.shaded) {
		y.node.style.clear = x.shaded ? "none" : "left";
	    } else {
		y.node.style.clear = x.shaded ? "left" : y.clear;
	    }	
	}
    };
    function nav_position(nav) {
	var max = nav.offsetLeft;
	resizers.push(function (view_width) {
	    var x = view_width - nav.clientWidth;
	    if (x < 0)   { x = 0; }
	    if (x > max) { x = max; }
	    nav.style.left = x + "px";
	});
    }
    if (body && body.childNodes) {
	search_nodes(body.childNodes, function (x) {
	    var isfloat = false;
	    if (x.nodeName == "DIV" && x.style) {
		if (x.id == "header") {
		    search_nodes(x.childNodes, function (x) {
			if (x.nodeName == "TABLE") {
			    nav_position(x);
			}
		    });
		    return false;
		}
		switch (x.className) {
		case "block1":
		case "block2":
		    isfloat = true;
		    break;
		default:
		    isfloat = x.id == "news";
		}
		if (isfloat) {
		    floats.push({node: x, shaded: false});
		}
		return !isfloat;
	    }
	});
    }
    forin(floats, function (x) {
	var width = x.node.style.width,
	    state = {},
	    b     = {},
	    title;
	function shade(bar) {
	    if (bar) {
		x.node.style.width = (sumf(bar.childNodes, function (x) {
		    return x.offsetWidth + (nonempty(x) && 16);
		}) + 4) + "px";
	    }
	    x.shaded = true;
	    floats.update();
	}
	function unshade() {
	    x.node.style.width = width;
	    x.shaded = false;
	    floats.update();
	}
	x.clear = x.node.style.clear;

	search_nodes(x.node.childNodes, function (x) {
	    if (x.nodeName == "DIV") {
		switch (x.className) {
		case "heading":
		    title = title || x;
		    break;
		default:
		    return true;
		}
	    } else if (body.id == "skins-icons") {
		if (x.nodeName == "P") {
		    return true;
		}
		if (x.nodeName == "IMG") {
		    icon_view(x);
		}
	    }
	});
	if (title) {
	    title.ondblclick = function () {
		if (state.hidden) {
		    b.className = "collapse";
		    unshade();
		} else {
		    b.className = "expand";
		    shade(title);
		}
		show_hide(title, state);
	    };
	    if (x.node.className == "block2") {
		/* pixel-perfect padding */
		search_nodes(title.childNodes, function (x) {
		    if (x.style && x.offsetWidth % 4) {
			x.appendChild(hspacer(4-(x.offsetWidth % 4)));
		    }
		});
		b = span("");
		b.className = "collapse";
		b.onclick = title.ondblclick;
		title.appendChild(b);
	    }
	}
    });
    window.onresize();
}

window.onload = init;

window.onresize = function () {
    var view_width;
    if (document.documentElement && document.documentElement.clientWidth) {
	view_width = document.documentElement.clientWidth;
    }
    if (view_width) {
	forin(resizers, function (f) { f(view_width); });
    }
};

})();
