/* Author: Dan Bryant <db@leadingtone.org>
 * description: a general message reporting function for jquery bassed web apps.
 *   adds a error message to a <span id="app_message">. The error message fades out after 5 seconds or on click if it is an error *
 * perams:
 * 		message: the error message
 * 		options: aconfig object
 * 	 		inject: 'app_message' :: where do you want to put the output?
 * 	 		append: true :: append the output inside the inject or prepend it if false
 * 	 		cls:'apperr' :: set the class of the div ...
 * 	 		fade_length: a time to fade out in miliseconds; 1000 = 1 second; defaults to 10 seconds
 * 			type: ( sticky || fade ) :: sticky *cough* sticks until you click on it, fade automagicly fades out. defaults to fade
 */
// plugin definition
jQuery.extend({
	report: function(message,options){

		// plugin defaults
		var defaults = {
		  	inject: 'app_messages'
			,append: true
			,cls:'apperr'
			,fade_length: 6000
			,type: 'fade'
		};

		var opts = jQuery.extend(defaults, options);

		// create the empty element and get the handle
		var dv = document.createElement("div");

		// add the requested class to the new div
		// if(document.all){ var ie = 'retarded'; dv.className = opts.cls; } // ie specific code
		// else { dv.setAttribute("class", opts.cls); } // real browsers
		jQuery(dv).addClass(opts.cls);

		// add the div into the target
		if(opts.append){ $('#' + opts.inject).append(dv); }
		else{ $('#' + opts.inject).prepend(dv); }

		// set the error type and inject the content in to the new div
		switch(opts.type){
			case 'sticky':
				// show the message
				close_message = '<span class="sticky-message-button">[x]</span>';
				$(dv).html(close_message + message);
				$(dv).bind( "click", function(){
					$(dv).fadeOut("slow",function(){
						jQuery(this).remove();
					});
				})
			break;

			case 'fade':
				$(dv).html(message);
				$(dv).fadeOut(opts.fade_length,function(){
					jQuery(this).remove();
				});
			break;
		}
	}
});

jQuery(document).ready(function() {
	jQuery('body').prepend('<div id="app_messages"></div>');
});
