
function availableHeight() {
	return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}


function availableWidth() {
	return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var view = "normal";
var shell = null;
var main = null;
var body_overflow = null;
function togglePrintView() {
	if(view == "normal") {
		var top = document.getElementById('top');
		main = document.getElementById('page-body3');
		shell = document.getElementById('shell');
		top.replaceChild(main, shell);
		main.style.width = "100%";
		body_overflow = document.body.style.overflow;
		document.body.style.overflow = "visible";
		view = "print";

	} else if(view == "print") {
		var top = document.getElementById('top');
		top.replaceChild(shell, main);
		var bot = document.getElementById('page-body2');
		bot.appendChild(main);
		document.body.style.overflow = body_overflow;
		view = "normal";
		correctHeight();

	}
}

function correctHeight() {
	var height = availableHeight() - 128;
	var width = availableWidth() - 166;
	if(view == "normal") {
		document.getElementById('page-table').style.height = height;
		document.getElementById('page-row').height = height;
		document.getElementById('page-body').style.height = height;
		document.getElementById('page-sidebar').height = height;
		document.getElementById('page-body2').style.height = height;
		document.getElementById('page-body2').style.width = width;
	}
}

function toggleDisplay(nodes) {
	for(var i = 0; i < nodes.length; i++) {
		var element = nodes[i];
		if(element.className == "collapsee") {
			if(element.style.display == "none")
				element.style.display = "block";
			else
				element.style.display = "none";
		}
	}
	return false;
}

function toggleClassDisplay(clazz) {
	var rulesPropName = "cssRules";
  var pattern = '.' + clazz;
	var matcher = new RegExp(pattern);
	for(var sheet_i = 0; sheet_i < document.styleSheets.length; sheet_i++) {
		var rules = document.styleSheets[sheet_i][rulesPropName];
		if(!rules) {
			rulesPropName = "rules";
			rules = document.styleSheets[sheet_i][rulesPropName];
		}
		if(rules) {
			for(var rule_i = 0; rule_i < rules.length; rule_i++) {
				var rule = rules[rule_i];
				if(matcher.test(rule.selectorText)) {
					var display = rule.style.display;
					if(display == "none") 
						rule.style.display = "block";
					else
						rule.style.display = "none";
				}
			}
		}
	}
	return false;
}

var formAction = null;

function addCoreIdQueryString(event) {
	var element = event.target || event.srcElement;
	var path = element.value;
	var coreId = coreIdFromPath(path);
	var form = element;
	while(form && form.nodeName.toUpperCase() != "FORM") {
		form = form.parentNode;
	}
	if(!formAction) {
		formAction = form.action;
	}
	form.action = formAction + "?coreId=" + coreId;	
	window.status = form.action;
	return true;
}

function coreIdFromPath(path) {
	return path.replace( /.*[\/\\]/, "" ).replace( /\.(xml|zip|ZIP|XML)$/, "");
}

