/*
Function: showPopUp
	Changes the class of the target div to layeron, adds a drop shadow, and positions it appropriately (in relation to the text button for audio pop ups).

Parameters:
	target - The pop up text div ID
	type - "auto" or "manual"
	caller 	-	The el that called the function
	top	-	The top value for manually positioned overlays, defaults to 50
	left	-	The left value for manually positioned overlays, defaults to 50
	
Dependencies:
	<changeClassById>; <dropShadow>; <positionAuto>; <positionManual>; <wrapOrStuff>;

Returns:
	NA

Change Log:
	2006.07.07	ALP	- Initial version
	2006.08.25 	ALP - Revised to accomodate regular pop ups in addition to audio text pop ups.  Changed name to showPopUp
	2006.10.06	ALP	- Revised to code fork for IE vs FF browsers
	2006.10.31	ALP - Various small revisions to adjust placement in IE
	2006.11.08	ALP	- Added a check to see whether a scrollbar is present on the page before displaying the pop-up. If it is not present, the code prevents one from appearing while the pop-up is being positioned.
	2007.01.02	ALP - Changed the way a scrollbar is accounted for because the previosu solution caused the audio to stop playing when the text button was clicked.

Bugs:
	None known.
	
To do:
	None
*/
var contentScrollbar;
function showPopUp(target,type,caller,top,left) {
	var contentScrollbar = ($('content').offsetHeight < $('content').scrollHeight) ? true : false;
	changeClassById(target,'layeron clear');
	var newScrollBar = ($('content').offsetHeight < $('content').scrollHeight) ? true : false;
	myDS = new dropShadow(target).init();	
	if (type == "auto") {
		var fudgeFactor = 4; // Random number to get correct top value in IE
		var levels = 0;
		var callerTop = caller.offsetTop;
		var callerLeft = caller.offsetLeft;
		var height = caller.parentNode.offsetHeight;
		var width = caller.offsetWidth;
		var top=0;
		var left=0;
		var newNewCaller = caller;
		// if the browser is FF or Netscape
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture	
			top += caller.parentNode.offsetTop;
			left += caller.offsetLeft;
		// if the browser is IE
		} else { 
		var newCaller = caller;
			for( var top=0, left=0; newCaller.id != contentEl; newCaller = newCaller.offsetParent ) {
				if (newCaller == caller && caller.parentNode != caller.offsetParent) {
					top += caller.parentNode.offsetTop;
					left += newCaller.offsetLeft;
				} else {
					left += newCaller.offsetLeft;
					top += newCaller.offsetTop;
				}
			}
		}
		/*Depending on the length of the content, a scrollbar may briefly appear on pages that don't already have one when the pop-up is shown. This statement prevents the left positioning from being off by the width of the scrollbar when the audio play is floated right.*/
		if (!contentScrollbar && newScrollBar && caller.parentNode.className == "fr") {
			left += 15;
		}
		positionAuto(target,top,left,width,height);
		
	} else if (type == "manual") {
		var top = top || 50;
		var left = left || 50;
		/*Depending on the length of the content, a scrollbar may briefly appear on pages that don't already have one when the pop-up is shown. This statement prevents the left positioning from being off by the width of the scrollbar.*/
		if (!contentScrollbar && newScrollBar) {
			left += 15;
		}
		positionManual(target,top,left);
		checkOlay(target,top,left);
	}
}		
/*
Function: hidePopUp
	Hides a pop up div and removes the drop shadow divs

Parameters:
	target - The target div ID

Dependencies:
	<changeClassById>; <dropShadow>;

Returns:
	NA

Change Log:
	2006.07.07	ALP	- Initial version
	2006.08.25 	ALP - Changed name to hidePopUp

Bugs:
	None known

To do:
	None
*/
function hidePopUp (target) {
	changeClassById(target,'off'); 
	myDS = new dropShadow(target).remove();
	showEl(target);
}
/*
Function: positionAuto
	Positions the audio text appropriately on the page, taking into account the location of the audio player.

Parameters:
	target - The pop up text div ID
	top - The top position of the caller el
	left - The left position of the caller el
	width - The width of the caller el
	height - The height of the caller el's parent

Dependencies:
	None

Returns:
	NA

Change Log:
	2006.07.07	ALP	- Initial version

Bugs:
	None known

To do:
	None
*/
function positionAuto(target_arg,top,left,width,height) {
	var pagePadding = 10;// don't come within 10 pixels of the edge of the page
	var padding = 10; // distance between caller and the overlay
	var shadow = 4; // width of the shadow
	var target = $(target_arg);
	var olayHeight = target.getHeight() - shadow; //subtract the shadow so the bottom borders line up
	var olayWidth = target.offsetWidth + padding; //add padding to the width of the box
	var audioBottom = top + height;
	var pageWid = $(contentEl).offsetWidth - pagePadding;
	var scrollPos = $(contentEl).scrollTop;
	// If there is room above the player (taking into account scrolling)
	if ((audioBottom - scrollPos) > olayHeight) {
		// If there is room to the right of the player
		if (left+width+olayWidth<pageWid) {
			var newX = left + width + padding;
			var newY = audioBottom - olayHeight;
		// If there is room to the left of the player
		} else if (left+width+olayWidth>pageWid && left>olayWidth) {
			var newX = left - olayWidth + shadow;
			var newY = audioBottom - olayHeight;
		// If there is no room on either side of the player
		} else if (left+width+olayWidth>pageWid && left<olayWidth) {
			/*alert('left: ' + left + ' top: ' + top);*/
			/*alert("Builder: Reduce the width of the audio1 div so that it will fit to the right (preferred) or left of the player");*/
		}
	// If there is room below the player (taking into account scrolling)
	} else if ((audioBottom - scrollPos) < olayHeight) {
		// If there is room to the right of the player
		if (left+width+olayWidth<pageWid) {
			var newX = left + width + padding;
			var newY = audioBottom - height;
		// If there is room to the left of the player
		} else if (left+width+olayWidth>pageWid && left>olayWidth) {
			var newX = left - olayWidth + shadow;
			var newY = audioBottom - height;
		// If there is no room on either side of the player
		} else if (left+width+olayWidth>pageWid && left<olayWidth) {
			/* alert("Builder: Reduce the width of the audio1 div so that it will fit to the right (preferred) or left of the player"); */
		}
	}
	positionManual(target,newY,newX)
	checkOlay(target_arg,newY,newX);
}
/*
Function: positionManual
	Positions a pop up to the specified location on the page.

Parameters:
	target - The pop up text div ID
	top - The top position of the pop up
	left - The left position of the pop up

Dependencies:
	None

Returns:
	NA

Change Log:
	2006.08.24	ALP	- Initial version

Bugs:
	None known

To do:
	None
*/
function positionManual(target,top,left) {
	var target = $(target);
	target.style.position = "absolute";
	target.style.top = top+"px";
	target.style.left = left+"px";
}
