/* Library of JavaScript functions available to any
   page that feels like including them.
*/


function WindowPopSmall(URL) {
     window.open(URL, 'popupsmall', "resizable,scrollbars,height=250,width=350");
}

function WindowPopMedium(URL) {
     window.open(URL, 'popupmedium', "resizable,scrollbars,height=275,width=300");
}

function WindowPopLarge(URL) {
     window.open(URL, 'popuplarge', "resizable,scrollbars,height=350,width=450");
}


function popup(URL, width, height) {
	width = (width != null) ? width : 400;
	height = (height != null) ? height : 350;
	if (typeof URL != "string") {
		// assume we were passed an A element
		URL = URL.href;
	}
//	alert("width: " + width + ", height: " + height);
	window.open(URL, 'popup', "resizable,scrollbars,height="+height+",width="+width);
}


function id2elem(id) {
	if (typeof(id) != 'string') {
		return id;
	}
	if (document.getElementById) {
		id = document.getElementById(id);
	} else if (document.all) {
		id = document.all[id];
	} else {
		id = false;
	}
	return id;
}

 
function firstRealChild(elem) {
	for (var i = 0; i < elem.childNodes.length; i++) {
		if (elem.childNodes[i].tagName) {
			return elem.childNodes[i];
		}
	}
}

function get_xhr_obj() {
	var xhr;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("I'm sorry, your browser doesn't seem to support the XMLHttpRequest object, either as part of the W3C DOM or as an ActiveX object. I regret to say that this means you won't be able to use the features on this site.");
	}
	return xhr;
}

function addClass(elem, cname) {
//alert("addClass(" + elem.id + ", " + cname + ")");
	if (elem.className.search(/\bcname\b/) == -1) {
		elem.className += ' ' + cname;
	}
}

function removeClass(elem, cname) {
//alert("removeClass(" + elem.id + ", " + cname + ")");
	elem.className = elem.className.replace(/\bcname\b/g, '');
}


function getHeight(elem) {
	// Gets an element's pixel height as set in its style, regardless
	// of platform. Return value is suitable for plugging into an 
	// "elem.style.height = " construction, after appending 'px'.
	
	var elemheight;
	if (window.getComputedStyle) {
		var estyle = window.getComputedStyle(elem, '');
		elemheight = estyle.height.toString().replace(/[^0-9\.-]/g, '');
	} else {
		elemheight = elem.clientHeight.toString().replace(/[^0-9\.-]/g, '');
	}
	if (isNaN(elemheight)) {
		return elem.offsetHeight;
	} else {
		return parseFloat(elemheight);
	}
}

function getPosX(elem) {
	// Gets an element's position relative to the document body.
	if (elem.tagName.toLowerCase() == 'body' || elem.tagName.toLowerCase() == 'html') {
		return 0;
	} else {
		return getPosX(elem.offsetParent) + elem.offsetLeft;
	}
}

function getPosY(elem) {
	// Gets an element's position relative to the document body.
	if (elem.tagName.toLowerCase() == 'body' || elem.tagName.toLowerCase() == 'html') {
		return 0;
	} else {
		return getPosY(elem.offsetParent) + elem.offsetTop;
	}
}

function getWidth(elem) {
	// Gets an element's pixel width as set in its style, regardless
	// of platform. Return value is suitable for plugging into an 
	// "elem.style.width = " construction, after appending 'px'.
	var elemwidth;
	if (window.getComputedStyle) {
		var estyle = window.getComputedStyle(elem, '');
		elemwidth = estyle.width.toString().replace(/[^0-9\.-]/g, '');
	} else {
		elemwidth = elem.clientWidth.toString().replace(/[^0-9\.-]/g, '');
	}
	if (isNaN(elemwidth)) {
		return elem.offsetWidth;
	} else {
		return parseFloat(elemwidth);
	}
}



function set_joint() {
	function sjoint(pos) {
		id2elem('navbar_joint').style.top = pos + 'px';
		id2elem('navbar_joint').style.visibility = 'visible';
	}
	var posy;
    var sections = ['services', 'practitioners', 'providers', 'fosters', 'articles', 'links'];
    for (var s = 0; s < sections.length; s++) {
    	if (id2elem('subnav_' + sections[s])) {
    		posy = getPosY(id2elem('subnav_' + sections[s])) + id2elem('subnav_' + sections[s]).offsetHeight;
    		if (s == (sections.length - 1)) id2elem('nav_admin').style.marginTop = '4em';
    	}
    }
	sjoint(posy - 15);
}


