/* create the popup elements */
var popups = new Array();
/* window timer event placeholder */
var popup_hider;

function populate_popups() {
	var popup_ids = 'win2pdfprices win2pdfProprices win2pdfTSEprices win2pdfProTSEprices batch2pdfprices infixprices windows_servers format_support'.split(' ');
	for (pid in popup_ids) {
		var elem_id = popup_ids[pid];
		var popup_content = document.getElementById(elem_id);
		if (popup_content) {
			/* add mouse events to:
			 * 1. prevent the popup from closing when mousing over
			 * 2. re-schedule the closing of all popups when the
			 *    user mouses-out of the popup
			 */
			popup_content.onmouseover = cancel_popup_hide;
			popup_content.onmouseout = delayhide_popups;
			popups[elem_id + 'popup'] = popup_content;
		}
	}
}

function show_popup(popupname, e) {
	/* test to see if it exists first */
	if (popups[popupname] && e &&
			/* only show if not shown (cuts down on jumpiness) */
			! popups[popupname].className.match(/shown/)) {
		/* cancel the timeout that hides all popups */
		cancel_popup_hide();

		/* hide all other popups */
		hideall_popups(popupname);

		/* unhide the current popup */
		popups[popupname].className = 'popup shown';

		/* set the position of the current popup */
		position_popup(popupname, e);
		
	}
}

function cancel_popup_hide() {
	/* cancel the timeout that hides all popups */
	if (popup_hider) {
		popup_hider = window.clearTimeout(popup_hider);
	}
}

function hideall_popups(except_for) {
	/* hide all the other popups */
	for (popupname in popups) {
		if (! except_for || popupname != except_for) {
			hide_popup(popupname);
		}
	}
}

function delayhide_popups() {
	/* cancel any previous request */
	cancel_popup_hide();

	/* delay is in milliseconds */
	popup_hider = window.setTimeout('hideall_popups()', 250);
}

function hide_popup(popupname) {
	/* test to see if it exists first */
	if (popups[popupname]) {
		popups[popupname].className = 'popup'; /* (default is hidden) */
	}
}

function position_popup(popupname, e) {
	/* test to see if it exists first */
	var popup = popups[popupname];
	if (popup && e) {

		var mouse_x =
			e.x ? e.x :
			e.layerX ? e.layerX :
			e.clientX ? e.clientX :
			e.pageX ? e.pageX :
			e.screenX ? e.screenX :
			0;
		var mouse_y =
			e.y ? e.y :
			e.layerY ? e.layerY :
			e.clientY ? e.clientY :
			e.pageY ? e.pageY :
			e.screenY ? e.screenY :
			0;
		if (document.all) {
			mouse_x = e.x + getScrollPos()[0];
			mouse_y = e.y + getScrollPos()[1];
		}

		/* account for the size of the window */
		var cw = popup.clientWidth;
		var bcw = documentSize()[0];
		/* the arrow element -- for shifting it back and forth */
		var arrow = popup.getElementsByClassName ?
			// supported in sane browsers
			popup.getElementsByClassName('arrow')[0] :
			// fallback for IE
				popup.rows[0].firstChild;
		if (mouse_x + cw > bcw) {
			var shift_left = mouse_x + cw - bcw;

			// move the mouse click to a position where the popup
			// window will not draw itself outside the window
			mouse_x -= shift_left;

			/* extra trick : move the arrow */

			if (arrow) {
				arrow.style.backgroundPosition = shift_left + "px 0px";
			}

		} else {
			// arrow shift not needed -- put it back
			if (arrow) {
				arrow.style.backgroundPosition = '0px';
			}
		}

		/* refuse to position a popup at the (0,0) position */
		if (mouse_x && mouse_y) {
			popup.style.left = mouse_x - 20 + "px";
			popup.style.top = mouse_y + 10 + "px";
		}
	}
}

/***** begin browser compatibility functions (grrr!) *****/

function getScrollPos() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function documentSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}
