/*
** Marginalia Placing Code
** Places the marginalia next to their appropriate resource in the page.
**
** Marginalia is coded into the page after the footnav and before the relatedLinks in the format,
** 
** 		<h3 class="marginaliaDescription">Resource Descriptions</h3>
**		<div id="res1Desc" class="marginaliaDescription">Text in the margin.</div>
**		<div id="res2Desc" class="marginaliaDescription">Text in the margin.</div>
**
** Where the <h3> tag is the title (only displays in browsers without css) 
** and the <div> tags contain the id in the format *Desc, 
** Where '*' is the resource name. 'Desc' must be tagged onto the end.
*/

var d = document;
var occupationArray = new Array();
var activityOccupationArray = new Array();
var maxActivityWidth = 442;
//var pageContentX = 0;
var positionsArray = new Array();
var macie5 = document.all && !document.mimeType;

// Function called from init();
function placeMarginalia(){
	positionsArray = new Array();
	/*activityOccupationArray = new Array();
	occupationArray = new Array();*/
	
	var el  = document.getElementById("pagecontent");
	if(el == "undefined" || el == null) {
		el = document.getElementById("pagecontentPopUp");
	}
	//pageContentX = findPosX(el);
	var divArray = document.getElementsByTagName("DIV");
	for (var j=0;j<divArray.length;j++){
		if(divArray[j].className == "mainpagecontent"){
			probeMainContentForElementsToLabel(divArray[j]);
		}
	}
	
	if(macie5) window.setTimeout("placeDescsMacIE5()",2000);
}

function placeDescsMacIE5() {
	for(var i=0;i<positionsArray.length;i++){
		//alert(positionsArray[i]);
		positionsArray[i][0].style.left = positionsArray[i][1];
		positionsArray[i][0].style.top = positionsArray[i][2];
	}
}

function probeMainContentForElementsToLabel(n) {
    if(document.getElementById(n.id+"Desc") != null){
		myEl = document.getElementById(n.id);
		myDesc = document.getElementById(n.id+"Desc");
		// sets the position of the margin element (myDesc).
		setObjPos(myEl,myDesc);
	} else {
	    var children = n.childNodes;
    	for(var i=0; i < children.length; i++) {
    	    probeMainContentForElementsToLabel(children[i]);
    	}
	}
}

function setObjPos(obj, desc){
	// sets newY to the y position of the resource.
	var newY = findPosY(obj);
	//var newX = pageContentX+40;
	var newX = findPosX(obj)-160;

	newY = newY+"px";
	newX = newX+"px";
	
	if(macie5){
		tempArray = [desc,newX,newY];
		positionsArray[positionsArray.length] = tempArray;
	} else {
		desc.style.top = newY;
		desc.style.left = newX;		
	}
}

function isIE6() {
	if (document.all){
		var detect = navigator.userAgent.toLowerCase();
		var place = detect.indexOf('msie') + 1;
		var thestring = 'msie';
		if (place) {
			browser = detect.substr(place + thestring.length,3);
			if(browser == "6.0")
				return true;
			else
				return false;
		}
		return false;
	}
	return false;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}else if (obj.y)
		curtop += obj.y;
	if(isIE6() || macie5)
		curtop += 12;
	return curtop;
}




function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}else if (obj.y)
		curleft += obj.x;
	return curleft;
}