/*
	Message Box
	
	this will create a message box directly after or into a specified element
	
	@author Kevin Jantzer, Folium Parters, LLC
	@date 2010-10-27
	
	@dependencies
		- jQuery
		- MessageBox CSS
		- scrollTo plugin (http://plugins.jquery.com/project/ScrollTo) - if auto scroll is desired
		
	
	..::Change Log ::..
	
	@rev 2011-01-10 => added "id" option
*/
function messageBox(options){
	
	// default settings	
	var settings = {
		'method'			: 'init',
		'src'				: '',
		'attachment'		: 'after',
		'color'				: '',
		'msg'				: 'Alert message goes here',
		'fadeOutDelay'		: 4000,
		'autoScroll'		: true,
		'autoScrollSpeed' 	: 500,
		'slideDownSpeed'	: 500,
		'fadeInSpeed'		: 500,
		'width'				: '100%',
		'position'			: 'center',
		'margin'			: 0,
		'id'				: ''							// 'my_message_box' - add an ID if you want this message to only show one at a time
	};
	
	// if options are detected, merge them with the default settings
	if ( options ) { 
		jQuery.extend( settings, options );
	}	
	
	// we must have a src to continue
	if(settings.src != ''){
	
		switch(settings.method){
			
			/*
				Default: Initialize the message box
				
				we will create the message box with the options
				given (or default to the original settings).
			*/
			case "init":
			
				var showMessage = true;
				
				// if the settings ID is set, lets check to see if this message already exists on the page.
				if(settings.id != ''){
					
					var existingMessageSize = jQuery('[data-id="kjc_message_box_'+settings.id+'"]').size();
					
					// if this message does already exist, do not show the new message
					if(existingMessageSize != 0){
						showMessage = false;
					}
				}
				
				if(showMessage){
							
					// if the color passed to us is not styled in the CSS, lets default to yellow
					/*if( !(settings.color in {'yellow':'','green':'','red':''}) ){
						settings.color="yellow";
					}*/
					
					// if the attachment option is not after or append, default back to after
					if( !(settings.attachment in {'after':'','append':''}) ){
						settings.attachment="after";
					}
					
					// set the message box width
					if(settings.width == '100%') settings.width = '';
					else settings.width = 'width: '+settings.width+'; ';
					
					// set the message box position
					if(settings.position == 'center') settings.position = 'margin: 0 auto; ';
					else if(settings.position == 'left') settings.position = 'float: left; ';
					else if(settings.position == 'right') settings.position = 'float: right; ';
					else settings.position = 'margin: 0 auto; ';
					
					if(settings.id != '') settings.id = 'data-id="kjc_message_box_'+settings.id+'"';
					
					// create the HTML message
					var element =  '<div class="message_box" '+settings.id+' style="margin: '+settings.margin+';">'+
										'<div class="message_content '+settings.color+'" '+
												'style="'+settings.position+settings.width+'">'+
											'<div class="close" onclick="messageBox({src:this,method:\'destroy\'})">x</div>'+
											'<p>'+settings.msg+'</p>'+
										'</div>'+
									'</div>';
					
					// create the DOM element
					var jQuerycreated_message = jQuery(element);
					
					// insert the message after, or append to the DOM
					if(settings.attachment == 'after'){
						jQuerycreated_message.insertAfter(jQuery(settings.src));
					}
					if(settings.attachment == 'append'){
						jQuerycreated_message.appendTo(jQuery(settings.src));
					}
					
					
					
					// slide down and fade in message
					jQuerycreated_message
									.slideDown(settings.slideDownSpeed)
									.find('.message_content')
									.fadeTo(settings.fadeInSpeed,1);
					
					// if a fadeout delay is detected, then we set an automatic fade out and removal.
					if(settings.fadeOutDelay > 0){
						setTimeout(function(){
							jQuerycreated_message
										.fadeTo(500,0)
										.slideUp(500,function(){
											jQuery(this).remove()});
						},settings.fadeOutDelay);	
					}
					
					
					if(settings.autoScroll){
						var message_top = jQuerycreated_message.offset().top,
							document_top = jQuery(document).scrollTop();
							
						if(message_top<document_top){
							jQuery.scrollTo(jQuerycreated_message,settings.autoScrollSpeed); // requires jQuery plugin: scrollTo
						}
					}
				}
				break;
			
			
			/*
				Destroy
				
				this method is used by the fn itself. this method
				is attached to the close button of the created 
				message.
			*/
			case "destroy":
				jQuery(settings.src).parent()
					.fadeTo(500,0)
					.parent()
					.slideUp(500,function(){
						jQuery(this).remove()});
				break;
			
			default:
				// do nothing
			
		}

	}

}
