(function($)
{
	//
	// plugin definition
	//
	$.fn.messageBox = function(options) 
	{
		$body = $('body'); 

		// build options
		var opts = $.extend({}, $.fn.messageBox.defaults, options);
		
		// store data
		$body.data('messageBox.opts', opts);
	};
	
	
	//
	// define private functions 
	//
	function initBoxStack()
	{
		$body = $('body'); 
		$body.data('messageBox.boxStack',new Array());
	};
	
	function push($box)
	{
		$body = $('body'); 
		if(typeof $body.data('messageBox.boxStack') == 'undefined')
		{
			initBoxStack();
		}
		$box.attr('id', $box.attr('id') + '_' + $body.data('messageBox.boxStack').length);
		$body.data('messageBox.boxStack').push($box);
		return($body.data('messageBox.boxStack').length -1);
	};
	
	function pop()
	{
		$body = $('body'); 
		if(typeof $body.data('messageBox.boxStack') == 'undefined')
		{
			initBoxStack();
		}
		return $body.data('messageBox.boxStack').pop();
	};
	
	function postitionBox($box)
	{
		var $window	= $(window);
		var width	= $box.outerWidth();
		var height	= $box.outerHeight();
		var top		= (($window.height() - height)/2) + $window.scrollTop();
		var left	= ($window.width() - width)/2;
		$box.css('top',top);
		$box.css('left',left);
	};
	
	function messagePanel()
	{
		$body = $('body');
		$main = $('div.main');
		var stack = $body.data('messageBox.boxStack');
		if(stack.length > 0 && !$('#messageBox_panel')[0])
		{			
			$panel = $('<div id="messageBox_panel">&nbsp;</div>');
			$panel.css('top', $main.offset().top);
			$panel.css('left', $main.offset().left);
			$panel.css('width', $main.outerWidth());
			$panel.css('height', $main.outerHeight());
			$body.append($panel);
			$('select').css('visibility','hidden');
			$panel.fadeTo(0,$body.data('messageBox.opts').panel_opacity);
		}
		else if($('#messageBox_panel'))
		{
			$('select').css('visibility','visible');
			$('#messageBox_panel').remove();
		}
	};

	
		
	//
	// define and expose public functions
	// carefull : $(this) means $.fn.<pluginname>, not an HTML element here 
	//
	$.fn.messageBox.destroy	=	
		function() 
		{
		};
	  
	$.fn.messageBox.open =
		function(content)
		{
			$body = $('body'); 
			var options	= $body.data('messageBox.opts')
			
			// build basic box
			if(content instanceof jQuery)
			{
				var $box = $(options.wrap_outer_pre + options.wrap_outer_post);
				$box = $box.append($(options.wrap_inner_pre + options.wrap_inner_post).append(content));
			}
			else
			{
				var $box	= $(options.wrap_outer_pre + options.wrap_inner_pre + content + options.wrap_inner_post + options.wrap_outer_post);
			}
			
			// append buttons 
			for(var i in options.buttons)
			{
				button = options.buttons[i];
				if(typeof button.cssClass == 'undefined')
				{
					button.cssClass = '';
				}
				$button = $('<div class="link-button' + button.cssClass + '"><a href="javascript:void(0)"><span>' + button.caption + '</span></a></div>');
				$button.one('click',button.action);
				$box.append($button);
			}
			$('body').append($box);
			postitionBox($box);
			push($box);
//			$('button',$box).click(function(){$('div.link-button',$box).hide()})
			messagePanel();
		};
	
	$.fn.messageBox.closeLast =
		function()
		{
			var $box = pop();
			if($box)
			{
				$box.remove();
				messagePanel();
			}
		};
		
	$.fn.messageBox.close =
		function($box)
		{
			$body = $('body'); 

			var newStack = new Array();
			var oldStack = $body.data('messageBox.boxStack'); 
			for(var i in oldStack)
			{
				if( $box.attr('id') != oldStack[i].attr('id'))
				{
					newStack.push(oldStack[i]);
				}
				else
				{
					oldStack[i].remove();
				}
			}
			$body.data('messageBox.boxStack', newStack);
			messagePanel();
		};
	  
	 
	//
	// plugin defaults
	//
	$.fn.messageBox.defaults = 
	{
			panel_opacity	:	0.8,
			wrap_outer_pre	:	'<div id="messageBox" class="messageBox">',
			wrap_outer_post	:	'</div>',
			wrap_inner_pre	:	'<div class="messageBox_inner">',
			wrap_inner_post	:	'</div>',	
			buttons			:	new Array
								(
									{
										caption	: 'schließen',
										action	: function(){$.fn.messageBox.close($(this).parent('.messageBox'));}
									}
								)
	};
		
})(jQuery);


