var messageBox;
function showMessageBox(title, message, type, buttons)
{
	messageBox = new MessageBox(title, message, type, buttons);
}

function MessageBox(title, message, type, buttons)
{
	this.title   = title;
	this.message = message;
	this.type    = type;
	this.buttons = buttons;
	
	this.messageboxPanel  = $('messagebox_panel');
	this.messageboxFader  = $('messagebox_fader');
	this.messageboxHolder = $('messagebox_holder');
	this.messageboxIframe = $('messagebox_iframe');
	this.messageboxText   = $('messagebox_text');
	this.messageboxTitle  = $('messagebox_title');
	this.messageboxIcon   = $('messagebox_icon');
	this.parentNode       = $(this.messageboxPanel.parentNode);
	
	if(document.loaded)
	{
		this.show()
	}
	else
	{
		Event.observe(window,'load', this.show.bindAsEventListener(this));
	}
} 

MessageBox.prototype.reset = function ()
{
	$$('#messagebox_buttons a').each(
		function(button)
		{
			$(button).hide();
		}
	);
	this.messageboxIcon.className = "";
}

MessageBox.prototype.show = function ()
{
	this.messageboxPanel.style.visibility = 'hidden';
	this.messageboxPanel.show();
	
	this.reset();
	this.fitHeight();	
	this.fitWidth();	
	this.center();	
	this.fillData();		
	this.playSound();
	
	this.messageboxPanel.style.visibility = 'visible';
	
	this.centerFxCache =  this.center.bindAsEventListener(this);
	Event.observe(window, 'scroll', this.centerFxCache);
	Event.observe(window, 'resize', this.centerFxCache);
}

MessageBox.prototype.playSound = function ()
{
	var sound = '/sounds/' + this.type + '.mp3';
	if(soundManager._didInit)
	{
		playSound(sound);
	}
	else
	{
		soundManager.onload = function() 
		{
			playSound(sound)
		}
	}
}

MessageBox.prototype.fillData = function ()
{
	this.messageboxTitle.innerHTML = this.title;
	this.messageboxText.innerHTML  = this.message;
	this.messageboxIcon.addClassName("messagebox_" + this.type);
		
	for(var i=0; i < this.buttons.length; i++)
	{
		var buttonName   = this.buttons[i];
		var buttonAction = null;
		if(this.buttons[i] instanceof Array)
		{
			buttonName   = this.buttons[i][0];
			buttonAction = this.buttons[i][1];
		}
		
		
		var button = $('messagebox_' + buttonName + '_bt');
		if(buttonAction != null)
		{
			button.href = buttonAction;
		}
		button.show();
	}
}
MessageBox.prototype.center = function ()
{
	var boundaryTop    = 0;	
	var boundaryBottom = this.messageboxPanel.getHeight() - this.messageboxHolder.getHeight();	
	var pageMiddle     = document.viewport.getScrollOffsets().top + (document.viewport.getHeight() / 2);
	var estPosition    = pageMiddle - this.messageboxPanel.cumulativeOffset().top - (this.messageboxHolder.getHeight() / 2);
	estPosition = Math.max(boundaryTop, estPosition);
	estPosition = Math.min(boundaryBottom, estPosition);
	
	this.messageboxHolder.style.top = estPosition + "px";		
}


MessageBox.prototype.fitHeight = function ()
{
	this.messageboxPanel.style.height = this.parentNode.getHeight() + "px";
	this.messageboxFader.style.height = this.parentNode.getHeight() + "px";
	if(this.messageboxIframe)
	{
		this.messageboxIframe.style.height = this.parentNode.getHeight() + "px";
	}
}

MessageBox.prototype.fitWidth = function ()
{
	this.messageboxPanel.style.width = this.parentNode.getWidth() + "px";
	this.messageboxFader.style.width = this.parentNode.getWidth() + "px";
	if(this.messageboxIframe)
	{
		this.messageboxIframe.style.width = this.parentNode.getWidth() + "px";
	}
}

MessageBox.prototype.close = function ()
{
	var panel = $('messagebox_panel');
	if(panel)
	{
		new Effect.Fade(panel, { duration:0.6 }); 
	}	
	soundManager.stop('messagebox');
	
	Event.stopObserving(window, 'scroll', this.centerFxCache);
	Event.stopObserving(window, 'resize', this.centerFxCache);
}

function playSound(sound)
{
	soundManager.destroySound('messagebox');
	soundManager.createSound('messagebox', sound);
	soundManager.play('messagebox');
}
			