/* popup.js - Create, display and manage dynamic popups
 * $Id: popup.js 2974 2010-02-03 11:35:04Z dave $
 *
 * Usage: show_popup(parent, id, position, width, content_url)
 * Where: 'parent' is the desired parent object of the popup (semantically
 *        speaking).  Currently unused.  'position' is the distance down the
 *        page the popup will display, in pixels.  'width' is the width of
 *        the popup in pixels.  'content_url' is a url to load into the popup.
 *        This will be done dynamically when the popup is displayed, using AHAH.
 *
 * Requires: ahah.js, base ini stylesheet, ie6 stylesheet
 *
 * Author: Dylan Muir <dylan@ini.phys.ethz.ch>
 * Created: 6th May, 2008
 */

function make_popup(id) {
	// Get the main content div
	var main_content_div = document.body;

	// Create the main popup div
	var popup_div = document.createElement('div');
	popup_div.setAttribute('id', id);
	popup_div.className = 'pop-over';

	// Create the box and border divs
	var box_div = document.createElement('div');
	box_div.className = 'box';
	popup_div.appendChild(box_div);
	
	var box_tl_div = document.createElement('div');
	box_tl_div.className = 'box-top-left';
	var box_t_div = document.createElement('div');
	box_t_div.className = 'box-top';
	var box_tr_div = document.createElement('div');
	box_tr_div.className = 'box-top-right';
	var box_l_div = document.createElement('div');
	box_l_div.className = 'box-left';
	var box_r_div = document.createElement('div');
	box_r_div.className = 'box-right';
	var box_bl_div = document.createElement('div');
	box_bl_div.className = 'box-bottom-left';
	var box_b_div = document.createElement('div');
	box_b_div.className = 'box-bottom';
	var box_br_div = document.createElement('div');
	box_br_div.className = 'box-bottom-right';
	
	// Create the content div
	var popup_content_div = document.createElement('div');
	popup_content_div.className = 'box-content';
	
	// Create the close div
	var popup_close_div = document.createElement('div');
	popup_close_div.className = 'box-close';
	popup_close_div.innerHTML = '<a href="Javascript:hide_popup(\'' + id + '\');">Close<\/a>';
	
	// Add borders and content to box div
	box_div.appendChild(box_tl_div);
	box_div.appendChild(box_t_div);
	box_div.appendChild(box_tr_div);
	box_div.appendChild(box_l_div);
	box_div.appendChild(popup_content_div);
	box_div.appendChild(popup_close_div);
	box_div.appendChild(box_r_div);
	box_div.appendChild(box_bl_div);
	box_div.appendChild(box_b_div);
	box_div.appendChild(box_br_div);

	// Add pop-up to content div
	main_content_div.appendChild(popup_div);
	
	// Return the popup div
	return popup_div;
}

function hide_popup(popup_id) {
	// Get the popup div
	var popup_div = document.getElementById(popup_id);
	
	// Hide the popup
	popup_div.style.display = 'none';
}

function vIE() {
	return (navigator.appName=='Microsoft Internet Explorer')
		? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1])
		: -1;
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {	
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}
			
function show_popup(parent, id, position, width, content_url) {
	var popup_div = document.getElementById(id);
	
	if (popup_div == null) {
		popup_div = make_popup(id);
	}

	// Show the popup
	popup_div.style.display = 'block';

	// Set the popup position	
	popup_div.style.top = position;
	
	// Set the popup position in IE6
	if (vIE() == 6) {
		pos = findPos(parent);
		popup_div.style.top = pos[1];	
	}
	
	// Set the popup width
	popup_div.childNodes[0].style.width = width;

	// Load content for the content div using AHAH
	popup_content_div = popup_div.childNodes[0].childNodes[4];
	ahah(content_url, popup_content_div);
}
