/*	jMsg (Extension for jQuery)

	Copyright (c) 2009 by Marius Supner
	Licensed under MIT-license
	
	Version: 0.9 [Beta] / 2009-08-03
*/


(function($) {
		  
	$.jMsg = function() {};
		  
	$.jMsg.alert = function(userOptions) {
		
		function doAnimation() {
			// if ( ( $.browser.msie ) && ( $.browser.version.substr(0,1) >= 7 ) ) {
			if ( $.browser.msie ) {
				return false;
			} else {
				return true;
			}
		}
			
		function uniqueId() {
			var r = Math.floor(Math.random()*990) + 10; // Zahl zwischen 10 und 1000
			
			var newDate = new Date;
			var id = newDate.getTime() + '_' + r;
			
			return id;
		};
		
		function generateMsgBox(ui, title, text, opacity, btntext, rfunction, speed) { // ui means unique id
			if ( typeof rfunction != 'function' ) {
				rfunction = function() { /* do nothing */ }
			}
		
			var msgContainerId = 'jMessagesContainer_' + ui;
			var overlayId = 'jMessagesOverlay_' + ui;
			var msgboxId = 'jMessagesBox_' + ui;
			
			
			// container-element >> background
			$('<div class=\'jmContainer\' id=\''+msgContainerId+'\'></div>').prependTo('body');
			$('<div class=\'jmOverlay\' id=\''+overlayId+'\' style=\'opacity: ' + opacity + '; filter: alpha(opacity=' + (opacity * 100) + ');\'></div>').appendTo('#'+msgContainerId);
			
			// message-box (top: title // middle: message // bottom: ok-button)		
			var id_okBtn = 'btnOk_' + ui;
	
			$('<div class=\'jmMsgbox\' id=\'' + msgboxId + '\' style=\'display: none;\'></div>').appendTo('#'+msgContainerId);	
			$('<div class=\'jmMsgboxTop\' id=\'msgTop_' + ui + '\'><span class=\'jmMsgboxTitle\' id=\'msgTitle_' + ui + '\'>' + title + '</span></div>').appendTo('#'+msgboxId)
			$('<div class=\'jmMsgboxMiddle\' id=\'msgMiddle_' + ui + '\'><span class=\'jmMsgboxText\' id=\'msgTextId' + ui + '\'>' + text + '</span></div>').appendTo('#'+msgboxId)
			$('<div class=\'jmMsgboxBottom\' id=\'msgBottom_' + ui + '\'><a class=\'jmBtn\' id=\'' + id_okBtn + '\'><span>' + btntext + '</span></a></div>').appendTo('#'+msgboxId)


			// bind onclick-action on ok-button and execute recall function
			$('#'+id_okBtn).bind('click', function() {
				
				// hide message 
				// animation is disabled for ie browser greater than version 7
				// png fade is buggy ... 
				if ( doAnimation() ) {
					$('#'+overlayId).fadeOut(80);
					$('#'+msgboxId).fadeOut(speed, function() {
						$('#'+msgContainerId).remove();
						rfunction();
					});
				} else {
					$('#'+msgContainerId).remove();
					rfunction();
				}
				
			});
			
	
			// show message
			// animation is disabled for ie browser greater than version 7
			// png fade is buggy ... 
			if ( doAnimation() ) {
				$('#'+overlayId).fadeIn(80);
				$('#'+msgboxId).fadeIn(speed);
			} else {
				$('#'+overlayId).show();
				$('#'+msgboxId).show();
			}
		};
		

		
		
		// default - settings
		var defaults = { 
			boxId:			uniqueId(),
			boxTitle:		'Hinweis',
			boxText: 		'Just a simple dummy text ... :-)',
			overlayOpacity: 0.4,
			btnText:		'Ok',
			onOkClick:		function() { /* do nothing */ },
			effectSpeed:	400
		};
		
		
		// quick-call-mode: $.jMsg.alert('string here');
		// -> jMsg identifies string and sets boxText!
		if ( typeof(userOptions) == 'string' ) {
			userOptions = { boxText: userOptions };
		}
		
		
		var options = $.extend(defaults, userOptions);
		
		// generate and show jMessage
		generateMsgBox(options.boxId, options.boxTitle, options.boxText, options.overlayOpacity, options.btnText, options.onOkClick, options.effectSpeed);
			
	};
	
})(jQuery);









