/* escapeOverlay's construction is a bit hard to grasp. It is needed to be able to
stop the event listener. See the prototype API docs for more information:
http://www.prototypejs.org/api/event
*/
var current_content_div;
var escapeOverlay = {
  fx: function(e) 
  {
    // To make script compatable with both MSIE and Firefox
    var kC  = (window.event) ? event.keyCode : e.keyCode;
    var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
    // If keypressed is escape
    if(kC==Esc) { closeDialogue(); }
	}
}

// Save in cache (to be able to stopObserving() it), see Prototype API docs for more info:
// http://www.prototypejs.org/api/event
escapeOverlay.bfx = escapeOverlay.fx.bindAsEventListener(escapeOverlay);

// loadPopup shows the overlay and dialogue box
function loadPopup(content_div)
{
  current_content_div = content_div;
  var y = document.body.scrollTop;
  if (y == 0) {
    y = document.documentElement.scrollTop;
  }
  document.getElementById(content_div).style.top = (y + 100) + "px";
  var maxPageHeight;
  if (document.body.scrollHeight) {
    maxPageHeight = document.body.scrollHeight;
  }   
  else if (screen.height) { // IE5
    maxPageHeight = screen.height; 
  }
  document.getElementById("overlay").style.height = maxPageHeight + "px";  

  // Show the overlay (disables rest of page)
	showOverlay();
	
	// Show dialogue and focus on newvalue
	$(content_div).show();
}
 
// Shows the overlay and starts the ESCAPE event listener
function showOverlay()
{
  $('overlay').show();
	Event.observe(document, 'keypress', escapeOverlay.bfx );
}

// Hides the overlay and stops the ESCAPE event listener
function hideOverlay()
{
	$('overlay').hide();
	
	Event.stopObserving(document, 'keypress', escapeOverlay.bfx );
}

// Closes the dialogue box, resets it and hides the overlay
function closeDialogue()
{
	hideOverlay();
	
	// Hide dialogue
	$(current_content_div).hide();
	
}

/* Event handler for onKeyPress for the newvalue field. Enables the use of the ENTER (RETURN)
key when adding a new entry in the dialogue box */
function enterKey(event, field)
{
	// If the event key pressed was a return (code 13)
	if (event.which == 13 || event.keyCode == 13)
		addEntry(field.value);
}
