
/* 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 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 and the new entry field is empty
		if(kC==Esc && ($F('newvalue') == '' || $F('newvalue') == null) )
			closeDialogue();
		else if(kC==Esc && window.confirm('Are you sure you wish to close the dialogue box?') )
			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()
{
    	// Show the overlay (disables rest of page)
	showOverlay();
	
	// Show dialogue and focus on newvalue
	$('dialogue').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
	$('dialogue').hide();
	}


function enterKey(event, field)
{
	if (event.which == 13 || event.keyCode == 13)
		addEntry(field.value);
}
var count = 1;

function addEntry(message)
{
	// Close the dialogue
	closeDialogue();
	
	// If the value entered for the new entry is not empty
	if (message != '' && message != null)
	{		
		// Build a new LI, set its value and id and add it
		newLI= Builder.node('li', {id: count});
		newLI.innerHTML = message;
		count++;
		
		// Append the new LI to the entries UL
		$('entries').appendChild(newLI);
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var Scriptaculous = {
  Version: '1.8.2',
  require: function(libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
  },
  REQUIRED_PROTOTYPE: '1.6.0.3',
  load: function() {
    function convertVersionString(versionString) {
      var v = versionString.replace(/_.*|\./g, '');
      v = parseInt(v + '0'.times(4-v.length));
      return versionString.indexOf('_') > -1 ? v-1 : v;
    }

    if((typeof Prototype=='undefined') ||
       (typeof Element == 'undefined') ||
       (typeof Element.Methods=='undefined') ||
       (convertVersionString(Prototype.Version) <
        convertVersionString(Scriptaculous.REQUIRED_PROTOTYPE)))
       throw("script.aculo.us requires the Prototype JavaScript framework >= " +
        Scriptaculous.REQUIRED_PROTOTYPE);

    var js = /scriptaculous\.js(\?.*)?$/;
    $$('head script[src]').findAll(function(s) {
      return s.src.match(js);
    }).each(function(s) {
      var path = s.src.replace(js, ''),
      includes = s.src.match(/\?.*load=([a-z,]*)/);
      (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each(
       function(include) { Scriptaculous.require(path+include+'.js') });
    });
  }
};

Scriptaculous.load();
