var dialog = null;
var dialogMessage = null;
var overlay = null;
var messageOverlay = null;
var countdownEnabled = false;
var dialogEnabled = false;
var timerPE;

function DialogOverlay(content, container) {

	// Manage arguments and assign defaults, 
	if (typeof container == 'undefined' ) { container = document.body; }
	if (null === (this.container = $(container))) { throw("container is not valid"); }

	// Assign instance variables
	this.content = content;
	
	this.overlay = $('overlay');
	this.dialog = $('dialog');
	
	if (this.overlay === null || this.dialog === null)
	{
		this.overlay = new Element('div', { 'id': 'overlay', 'class': 'overlay' }).hide();
		this.dialog = new Element('div', { 'id': 'dialog', 'class': 'dialog' }).hide();
	}
	else
	{
		this.overlay.hide();
		this.dialog.hide();
	}

	// Hide the overlay when clicked. Ignore clicks on the dialog.
	Event.observe(this.overlay, 'click', this.hide.bindAsEventListener(this));
	Event.observe(this.dialog, 'click',  function(event) { Event.stop(event); });
	
	// Insert the elements into the DOM
	this.dialog.insert(this.content);
	this.container.insert(this.overlay);
	this.container.insert(this.dialog);

	// Content may have been hidden if it is embedded in the page
	content.show();
	this.dialog.hide();
}

DialogOverlay.prototype.show = function() {
	new Effect.Appear(this.overlay, { duration: 0.5,  to: 0.8 });
	this.dialog.show();
	return this;
};

DialogOverlay.prototype.hide = function(event) {
	this.dialog.hide();
	this.overlay.hide();
	return this;
};

DialogOverlay.prototype.deleteOverlay = function(event) {
	this.dialog.hide();
	this.overlay.hide();
	delete this;
};	

function MessageOverlay(content, container) {
	// Manage arguments and assign defaults, 
	if (typeof container == 'undefined' ) { container = document.body; }
	if (null === (this.container = $(container))) { throw("container is not valid"); }

	// Assign instance variables
	this.content = content;
	
	this.messageOverlay = $('messageOverlay');
	this.dialogMessage = $('dialogMessage');
	
	if (this.messageOverlay === null || this.dialogMessage === null)
	{
		this.messageOverlay = new Element('div', { 'id': 'messageOverlay', 'class': 'overlay' }).hide();
		this.dialogMessage = new Element('div', { 'id': 'dialogMessage', 'class': 'dialog' }).hide();
	}
	else
	{
		this.messageOverlay.hide();
		this.dialogMessage.hide();
	}

	// Hide the overlay when clicked. Ignore clicks on the dialog.
	Event.observe(this.messageOverlay, 'click', this.hide.bindAsEventListener(this));
	Event.observe(this.dialogMessage, 'click',  function(event) { Event.stop(event); });
	
	// Insert the elements into the DOM
	this.dialogMessage.insert(this.content);
	this.container.insert(this.messageOverlay);
	this.container.insert(this.dialogMessage);

	// Content may have been hidden if it is embedded in the page
	content.show();
	this.dialogMessage.hide();
}

MessageOverlay.prototype.show = function() {
	new Effect.Appear(this.messageOverlay, { duration: 0.5,  to: 0.8 });
	this.dialogMessage.show();
	return this;
};

MessageOverlay.prototype.hide = function(event) {
	this.dialogMessage.hide();
	this.messageOverlay.hide();
	return this;
};

MessageOverlay.prototype.deleteOverlay = function(event) {
	this.dialogMessage.hide();
	this.messageOverlay.hide();
	delete this;
};	

function toMinuteAndSecond( x ) {
	var minutes = Math.floor(x/60);
	var seconds = x - (minutes * 60);
	
	if (seconds < 10)
	{
		return minutes + ":0" + seconds;
	}
	else
	{
		return minutes + ":" + seconds;
	}
	
}

function generateSystemMessage()
{
	dialogMessage = $('message_dialog');
	messageOverlay = new MessageOverlay(dialogMessage);		
  messageOverlay.show();
}

function generateTimeout(totalTime, displayWarningTime)
{
	dialog = $('session_dialog');
	overlay = new DialogOverlay(dialog);		
	var countdownEnabled = false;
	var time = displayWarningTime;
	var timer = $('session_timer');
	timer.value = totalTime - displayWarningTime;
	
	timerPE = new PeriodicalExecuter(
	function(pe) {
		dialogEnabled = false;
		overlay.show();
		pe.stop();
		
		new PeriodicalExecuter(
				function(pe) {
					countdownEnabled = true;
					var timer = $('session_timer');
					timeValue = parseInt(timer.value, 10);
					var timeOutput = $('session_time_left');

					if (timeValue <= 0)
					{
						countDownEnabled = false;
						timer.value = displayWarningTime;
            window.location.reload();
						pe.stop();
					}
					else
					{
						timer.value = timeValue - 1;
					}

				  timeOutput.innerHTML = toMinuteAndSecond(timer.value);					
		 }, 1);
	}, time);
}

