/**
 *	jQuery Plugin Finder Costum Select
 */
(function(jQuery) {
	
	var pluginName = "messagebox";
	var lteIE7 = $.browser.msie && (parseInt($.browser.version) <= 7);
	var isIE6 = $.browser.msie && (parseInt($.browser.version) == 6);
	var methods = {
		/**
		 *	Remove
		 */
		'remove': function(immediate) {
			var $this = $(this);
			var eachFunc = function(i, e) {
				var $e = $(e);
				if (!immediate || lteIE7) {
					$e.fadeOut('fast', function() { $e.remove(); });
				}
				else {
					$e.remove();
				}
			}
			
			// reset
			$this.data(pluginName, null);
			$('.messagebox-matte').each(eachFunc);
			$('.messagebox-matte').unbind();
			$('.messagebox > a').unbind();
			$('.messagebox').each(eachFunc);
		},
		
		/**
		 *	Initialize
		 */
		'init':	function(options) {
			return this.each(function() {
				var $this = $(this);
				var data = $this.data(pluginName);
				var settings = {
					'title':		'Mitteilung',
					'message':		'',
					'buttonOk':		true,
					'buttonCancel':	true,
					'immediate':	false,
					'ok':			function(event) { methods['remove'].apply($this.context); },
					'cancel':		function(event) { methods['remove'].apply($this.context); }
				};
				
				// merge settings
				if (options) jQuery.extend(settings, options);
				
				// set data
				if (!data) {
					$this.data(pluginName, {
						'context':	this,
						'settings':	settings
					});
				}
				
				// set events
				$this.bind('ok.' + pluginName, settings.ok);
				$this.bind('cancel.' + pluginName, settings.cancel);
				
				// make
				var $body = $('body');
				var $window = $(window);
				var $doc = $(document);
				var $matte = $('<div class="messagebox-matte">&nbsp;</div>');
				var $messagebox = $('<div class="messagebox">' + 
									'<div class="title"><h2>' + settings.title + '</h2></div>' +
									'<p>' + settings.message + '</p>' + 
									'<div class="buttons">' +
									'<a href="#" class="ok">Ok</a>' +
									'<a href="#" class="cancel">Abbrechen</a>' +
									'</div>' +
									'</div>');
				if (!isIE6) $body.append($matte);
				$body.append($messagebox);
				
				// jquery setup html
				$matte.hide();
				$messagebox.hide();
				
				if (!settings.buttonOk) {
					$messagebox.find('.ok').hide();
				}
				else {
					$messagebox.find('.ok').click(function() {
						$this.trigger('ok.' + pluginName);
					});
				}
				
				if (!settings.buttonCancel) {
					$messagebox.find('.cancel').hide();
				}
				else {
					$messagebox.find('.cancel').click(function() {
						$this.trigger('cancel.' + pluginName);
					});
				}
				
				$matte.outerWidth($body.outerWidth() + 16);
				$matte.outerHeight($body.outerHeight() + 16);
				
				$messagebox.css({
					'left':		parseInt($doc.scrollLeft() + (($window.width() - $messagebox.width()) / 2)) + 'px',
					'top':		parseInt($doc.scrollTop() + (($window.height() - $messagebox.height()) / 2)) + 'px'
				});
				
				if (lteIE7) $matte.css('opacity', .7);
				
				// show
				if (!settings.immediate || !lteIE7) {
					$matte.fadeIn('fast');
					$messagebox.fadeIn('fast');
				}
				else {
					$matte.show();
					$messagebox.show();
				}
			});
		}
	};
	
	/**
	 *	jQuery Plugin Constructor
	 */
	jQuery.fn[pluginName] = function(method) {  
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} 
		else if (typeof(method) === 'object' || !method) {
			return methods.init.apply(this, arguments);
		}
	};

})(jQuery);
