/*
 *
 * @copyright (c) DomainTools, LLC
 * The following code may NOT be copied or distributed and is for reference only.
 *
 *
 * @package	DT Messaging
 * @author	Koa Metter
 * @version	1.5
 *
 *
 * @param array		messages		An array of one or multiple messages - inserted with format [["subject", "description"]]
 * 
 * @param object
 *		@param string	className		Set custom class name (default: "dt-message")
 *		@param boolean	showMultiple	Show multiple message boxes (default: false)
 *		@param boolean	stack			Stack messages of identical 'type' into same message box (default: false)
 *		@param string	type			Type of message - "success", "warning", "error" (default: "warning")
 *
 * @return mixed
 *
 * @example
 * $("#div").dtMessage(
 *		[
 *			[
 *				"Message",
 *				"Description goes here..."
 *			],
 *			[
 *				"Another message",
 *				"Another message description goes here..."
 *			],
 *		],
 *		{
 *			stack: true,
 *			type: "error"
 *		}
 * );
 *
 */

(function($) {
	
	$.fn.dtMessage = function(messages, options) {
		
		/******************/
		/* Error Handling */
		/******************/
		
		// report error if $(this) is undefined
		if (typeof $(this).attr("class") === "undefined" && typeof $(this).attr("id") === "undefined") {
			//console.log("DT Messages Error :: Unable to display page message. The container '"+$(this).selector+"' does not exist.");
			return false;
		}
		
		// report non-array
		if (!$.isArray(messages)) {
			//console.log("DT Messages Error :: Message is not an array.");
			return false;
		}
		
		/**********************/
		/* Defaults & Globals */
		/**********************/
		
		// options defaults
		var opts = $.extend({
			className:		"dt-message",
			showMultiple:	false,
			stack:			false,
			type:			"warning"
		}, options);
		
		// if opts.type is not one of the following, default to "warning"
		if (opts.type != "warning" && opts.type != "error" && opts.type != "success") {
			opts.type = "warning";
		}
		
		// get literal value of $(this)
		var selector = $(this).selector;
		
		// create unique class name
		var className = $(this).attr("id").length > 0
				? $(this).attr("id") + "-" + opts.className
				: $(this).attr("class") + "-" + opts.className;
		
		/*******************/
		/* Execute Plugin! */
		/*******************/
				
		// run for each call
		return this.each(function() {
			
			// empty previous errors
			if (!opts.showMultiple) $(this).empty();
			
			// create page message
			$(this).append(drawBox(selector));
			
			// make sure message is display:block
			$(this).show();
			
		});
		
		
		/********************/
		/* Plugin Functions */
		/********************/
		
		/*
		 * Creates/inserts page message box
		 * @return mixed 
		 */
		function drawBox (selector) {
				
			var content = "";
			
			for (i in messages) {
				if (typeof i != typeof function(){}) {
					content		+= '<div class="summary">' + messages[i][0] + '</div>'
								+  '<div class="messaging-description">' + messages[i][1] + '</div>';
				}
			};
			
			// stack message
			var stack =  $(selector + " .message-" +opts.type+ "-stack .content ." +opts.type+ "-lg").selector;
			
			if (opts.stack && $(stack).length > 0) {

				$(stack).append(content);
			
			// write new message box
			} else {
			
				var messageBox	=	'<div class="' + className + ' message-' + opts.type + '-stack">'
										+ '<div class="img top-stretcher ' + opts.type + '-top j-round"></div>'
										+ '<div class="round-box messaging ' + opts.type + '-box j-round hasCorners">'
											+ '<div class="content">'
												+ '<div class="body img ' + opts.type + '-lg">' + content +'</div>'
											+ '</div>'
										+ '</div>'
									+ '</div>';
				return messageBox;
			}
			
		}
		
	}
	
})(jQuery);