/**
 * @author bowling
 */

/* 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(which,id,ba)
{
    	// Show the overlay (disables rest of page)
	showOverlay();

	switch(which)
	{
		case 1:
			show_block(id,ba);
			centerElement($('dialogue-block'));
			$('dialogue-block').show();
			break;
		case 2:
			show_other(id);
			centerElement($('dialogue-other'));
			$('dialogue-other').show();
			break;
		case 3:
			show_another(id);
			centerLastElement($('dialogue-another'));
			$('dialogue-another').show();
			break;
		case 4:
			show_andanother(id);
			centerElement($('dialogue-andanother'));
			$('dialogue-andanother').show();
			break;
	}
}
 
// 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(which,keep)
{
	if(!keep) hideOverlay();
	
	switch(which)
	{
		case 1:
			$('dialogue-block').hide();
			var d = document.getElementById('overlaycontent');
	  		var olddiv = document.getElementById('dialogue-block');
	  		d.removeChild(olddiv);
			break;

		case 2:
			$('dialogue-other').hide();
			var d = document.getElementById('overlaycontent');
	  		var olddiv = document.getElementById('dialogue-other');
	  		d.removeChild(olddiv);
			break;

		case 3:
			$('dialogue-another').hide();
			var d = document.getElementById('overlaycontent');
	  		var olddiv = document.getElementById('dialogue-another');
	  		d.removeChild(olddiv);
			break;

		case 4:
			$('dialogue-andanother').hide();
			var d = document.getElementById('overlaycontent');
	  		var olddiv = document.getElementById('dialogue-andanother');
	  		d.removeChild(olddiv);
			break;
	}
}

function show_block(id,ba)
{
	var thediv = '<iframe frameborder="0" height="100%" width="100%" scrolling="no" src="block.php?ba=' + ba + '&id=' + id + '"></iframe>';

	var cont = document.getElementById("overlaycontent");
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id','dialogue-block');
	newdiv.style.display='none';
	newdiv.innerHTML = thediv;
	
	cont.appendChild(newdiv);
	
}

function show_other(id)
{
	var thediv;
	
	var cont = document.getElementById("overlaycontent");
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'dialogue-other');
	newdiv.style.display = 'none';
	newdiv.innerHTML = thediv;
	
	cont.appendChild(newdiv);
	
}

function show_another(id)
{
	var thediv;
	
	var cont = document.getElementById("overlaycontent");
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'dialogue-another');
	newdiv.style.display = 'none';
	newdiv.innerHTML = thediv;
	
	cont.appendChild(newdiv);
	
}

function show_andanother(id)
{
	var thediv;
	
	var cont = document.getElementById("overlaycontent");
	
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'dialogue-andanother');
	newdiv.style.display = 'none';
	newdiv.innerHTML = thediv;
	
	cont.appendChild(newdiv);
	
}
