$(document).ready(function() {
	$("#sidebar_menu").treeview({
		animated: "medium",
		collapsed: true,
		unique: true,
		persist: "location",
		toggle: adjustMainRightHeight
	});
	window.onresize = function() {
		if (document.all) {
			window.resizeEnd = (window.resizeEnd == null)? (new Object()): window.resizeEnd;
			clearTimeout(window.resizeEnd);
			window.resizeEnd = setTimeout(onResizeBody, 1);
		} else {
			onResizeBody();
		}
	}
	onResizeBody();
});

function onResizeBody() {
	if ($.browser.msie && ($.browser.version == "7.0") && (location.pathname == "/")) {
		$("#bg_page").css("left", Math.round(($(document).width() - $("#bg_page").width() - .5) / 2) + "px");
	}
	hideMainLeftScrollBar();
	adjustMainRightHeight();
}

function hideMainLeftScrollBar() {
	// hide scrollbar in main_left
	var scrBarWidth = getScrollBarWidth();
	var el = document.getElementById('scr_bar_cover');
	el.style.left = (240 - scrBarWidth - 1) + 'px';
	el.style.width = (scrBarWidth + 2) + 'px';
}

function getScrollBarWidth() {
	var inner = document.createElement('p');
	inner.style.width = "100%";
	inner.style.height = "400px";

	var outer = document.createElement('div');
	outer.style.position = "absolute";
	outer.style.top = "0px";
	outer.style.left = "0px";
	outer.style.visibility = "hidden";
	outer.style.width = "400px";
	outer.style.height = "300px";
	outer.style.overflow = "hidden";
	outer.appendChild(inner);

	document.body.appendChild(outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 == w2) {
		w2 = outer.clientWidth;
	}

	document.body.removeChild(outer);

	return (w1 - w2);
}

function adjustMainRightHeight() {
	// make sure main_right is at least as high as main_left, so main_left can be
	// scrolled also, if it's regular contents were higher than main_right's
	var bl = Math.round($("#main_left").scrollTop() + $("#bottom_left").offset().top);
	var br = Math.round($("#main_right").scrollTop() + $("#bottom_right").offset().top);
	$("#spacer").height(Math.max($("#spacer").height() + (bl - br), 0));
}

