//JAVASCRIPT FOR POPUP WINDOW (DETAIL WINDOW)
// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
// Obtained from http://developer.apple.com/internet/webcontent/examples/popup.txt
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
//

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
		return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
		return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
    } else {
		return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.visibility = newVisibility;
	    styleObject.display="block";
		return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.left = newXCoordinate;
		styleObject.top = newYCoordinate;	
		return true;
    } else {
	// we couldn't find the object, so we can't very well move it
		return false;
    }
} // moveObject

// Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.
// ********************************
// application-specific functions *
// ********************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 20;
var yOffset = 12;

//on IE on PC, the X,Y coordinates should be 472:386
//on IE on MAC, the X=231, Y=211: differences of X-241 and Y-175

// ********************************************************************************
// the showPopup function requires a hidden DIV defined in your page and an element
// that contains the following JavaScript:
// onmouseover="showPopup('name_of_DIV',event, true);" 
// onmouseout="hideCurrentPopup();"
//
// the hidden DIV can be named anything, but be sure to reference it in the above
// javascript ('name_of_DIV'). The following javascript is required for the DIV:
// onmouseout="javascript:if (checkMouseLeave(this, event)) {hideCurrentPopup();}"  
// onmouseover="showPopup('name_of_triggering_element',event,false);" 
// onclick="showPopup('name_of_triggering_element',event,false);"
// The 'name_of_triggering_element' is the element that has the javascript mentioned
// up above assigned to it: the element that is firing the mouseover event.
// ********************************************************************************
function showPopup (targetObjectId, eventObj, blnResize) {
	var newXCoordinate;		//holds the new x position of the popup element.
	var newYCoordinate;		//holds the new y position of the popup element.
	var currentX;			//holds the current x position of the element.
	var currentY; 			//holds the current y position of the element.
	var currentEventElement;			//holds the DIV element that defines the popup 'box'.
	var blnMac = false;		//flag to identify if this is running in IE 5 Mac or not.
	
	//check if the browser is IE on MAC, if so then display popup differently due to bug in offsetTop and offsetLeft:
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 	&& (navigator.platform.indexOf('Mac') != -1) && getStyleObject('blankDiv')) 
	{
		//set IE Mac browser flag:
		blnMac = true;
	}
	
	if (blnMac==true) {
		//alert("using IE Mac...");
		//set the offset values for x,y (include the beginning offset values set up above!) 
		//because we're on a MAC:
		xOffset = -200;
		yOffset = 2;
		
		if(eventObj) {
		// hide any currently-visible popups
			hideCurrentPopup();
			// stop event from bubbling up any farther
			eventObj.cancelBubble = true;
			// move popup div to current cursor position 
			// (add scrollTop to account for scrolling for IE)
			
			//obtain the element that triggered this event:
			var relTarg;
			var e;
			
			if (!e) var e = window.event;
			
			//use either method to get the element that triggered this event, depending on the browser we're running in:
			if (eventObj.relatedTarget) {
				relTarg = eventObj.relatedTarget;
			} else if (eventObj.fromElement) {
				relTarg = eventObj.fromElement;
			}
			
		    //obtain the X and Y positions of the element that triggered the event:	  	
			if (document.anchors(eventObj.name)) {
				//currentEventElement = getEventElement('Search for a Child to Sponsor');	    
				currentEventElement = getEventElement(document.anchors(eventObj.name).name);
			} else {
				currentEventElement = getEventElement(eventObj);
			}
		    
		    if(!currentEventElement) {
		  	  //alert("There is no currentEventElement!");
		      return;
		    }
		
		    //call functions to return X and Y values:
		    currentX = getObjectXValue(currentEventElement, blnMac);
		    currentY = getObjectYValue(currentEventElement, blnMac);
				
			//blnResize will be True the first time the popup is displayed:			
			if (blnResize) {			
				newXCoordinate = currentX + xOffset;
				newYCoordinate = currentY + yOffset;

				moveObject(targetObjectId, newXCoordinate, newYCoordinate);
			}
			
			// and make it visible
			if( changeObjectVisibility(targetObjectId, 'visible') ) {
			    // if we successfully showed the popup
			    // store its Id on a globally-accessible object
			    window.currentlyVisiblePopup = targetObjectId;
			    return true;
			} else {
			    // we couldn't show the popup, boo hoo!
			    return false;
			}			
	    } else {	// if (eventObj)
		// there was no event object, so we won't be able to position anything, so give up
			return false;
	    }
		
	} else {  // if (blnMac==true)
	
	  // must be another browser either on mac or pc:			  
	  if(eventObj) {
		
		// hide any currently-visible popups
		hideCurrentPopup();
		
		// stop event from bubbling up any farther
		eventObj.cancelBubble = true;
		
		// move popup div to current cursor position 				
		//obtain the element that triggered this event:
		var relTarg;
		var e;
		
		if (!e) var e = window.event;
		
		//use either method to get the element that triggered this event, depending on the browser we're running in:
		if (eventObj.relatedTarget) {
			relTarg = eventObj.relatedTarget;
		} else if (eventObj.fromElement) {
			relTarg = eventObj.fromElement;
		}
		
	    //obtain the X and Y positions of the element that triggered the event:
	    if (document.anchors(eventObj.name)) {
			//currentEventElement = getEventElement('Search for a Child to Sponsor');	    
			currentEventElement = getEventElement(document.anchors(eventObj.name).name);
		} else {
			currentEventElement = getEventElement(eventObj);
		}
	    
	    if(!currentEventElement) {
	  	  //alert("There is no currentEventElement!");
	      return;
	    }
	
	    //call functions to return X and Y values:
	    currentX = getObjectXValue(currentEventElement, blnMac);
	    currentY = getObjectYValue(currentEventElement, blnMac);	  	
		
		//MAS 3/22/05 added to move box so it won't overwrite the other text on page:
		//move to new visible position:
		if (blnResize) {
			newXCoordinate = currentX + xOffset;
			newYCoordinate = currentY + yOffset;
			
			//reposition the popup box now to the correct coordinates:
			moveObject(targetObjectId, newXCoordinate, newYCoordinate);	
		}	
			
		// and make it visible
		if( changeObjectVisibility(targetObjectId, 'visible') ) {
		    // if we successfully showed the popup
		    // store its Id on a globally-accessible object
		    window.currentlyVisiblePopup = targetObjectId;
		    return true;
		} else {
		    // we couldn't show the popup, boo hoo!
		    return false;
		}
	    } else { //main if (eventObj)
		// there was no event object, so we won't be able to position anything, so give up
			return false;
	  }
  }	// if (blnMac)
  
} // showPopup


//these next three functions are needed to make sure the mouse events don't cause the displayed DIV to disappear when selecting
//an element inside the DIV. They're called from the elements in the DIV that can be operated on:
function checkMouseEnter (element, evt) {
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}



function getObjectXValue(objElement,blnMac) {
  var posX, tempPosX;
  //calculate the X position of the element that triggered event:
  //formula changes based on which browser/platform user is on:
  if (blnMac==true) {  	
  	if (objElement.clientX) {
		posX = objElement.clientX;
	} else if (objElement.x) {
		posX = objElement.x;
	} else if (objElement.layerX) {
		posX = objElement.layerX;
	} else if (objElement.screenX) {
		posX = objElement.screenX;
	} else {
		posX = objElement.offsetLeft;
	}
  } else {
	tempPosX = objElement;
	posX = objElement.offsetLeft;
	while(tempPosX.offsetParent) {
	  tempPosX = tempPosX.offsetParent;
	  posX += tempPosX.offsetLeft;
	}
  }
//  alert("posX = " + posX);
  return posX;
}

function getObjectYValue(objElement,blnMac) {
  var posY, tempPosY;

  //calculate the Y position of the element that triggered event:
  //formula changes based on which browser/platform user is on:
  
  if (blnMac==true) {
  	if (objElement.clientY) {
		posY = objElement.clientY;
	} else if (objElement.x) {
		posY = objElement.x;
	} else if (objElement.layerY) {
		posY = objElement.layerY;
	} else if (objElement.screenY) {
		posY = objElement.screenY;
	} else {
		posY = objElement.offsetTop;
	}
  } else { 
	tempPosY = objElement;
	posY = objElement.offsetTop;
	while(tempPosY.offsetParent) {
	  tempPosY = tempPosY.offsetParent;
	  posY += tempPosY.offsetTop;
	}
  }
  return posY;
}

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
		changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
		window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup


// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
		window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
		window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
		getStyleObject('blankDiv').width = document.body.clientWidth - 20;
		getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function getElement(id, d) {
  if (!d) d = document;
    if (d.getElementById) {
        return d.getElementById(id);
    }
    if (d.layers && d.layers[id]) {
      return d.layers[id];
    }
    if (d.all && d.all[id]) {
        return d.all[id];
    }
  }

function getEventElement(obj) {
  if(document.layers) {
    return document.layers[obj];
  } else if(document.all && !document.getElementById) {
   	return document.all[obj];
  } else if(document.getElementById) {
  	 return document.getElementById(obj);
  } else {
   	return null;
  }
}
