/**
 * This plugin helps to show quick indicators after doing some action or it can also be used for showing loading icons
 * 
 * @param Hash    	options           	additional options 
 * @param string	options[className] 	class name to put/use for showing indicator
 * @param string 	options[text]		text to put, if appending/prepending
 * @param string	options[wrapTag]	tags to wrap the text with
 * @param bool		options[display]	false, after, before, append or prepend
 * @param bool		options[displayRemove] 	true will remove the previous added indicator, only works with dipslay
 * @param Mixed		options[fadeIn]		slow, fast or check the fadeIn function in jquery
 * @param Mixed		options[fadeOut]	slow, fast or check the fadeOut function in jquery
 * @version 1.0
 * @type  jQuery
 * 
 * @author Basit (basit@imegah.com || http://Basit.me)
 */

(function($) {
				
	$.fn.indicator = function(options){
				
		var defaults = {
			className: "loading",
			text:  "loading...",
			wrapTag: "span",
			display: false, // false or none for nothing
			displayRemove: true,
			fadeIn: "slow",
			fadeOut: false
		};
		
		var settings = $.extend(defaults, options);
		
		return this.each(function () {
			
			var $this = $(this);
			
			// create references to the settings
			var className = settings.className;
			var text = settings.text;
			var wrapTag = settings.wrapTag;
			
			var wrappedTag = "<" + wrapTag + " class='" + className + "'>" + text + "</" + wrapTag + ">"
			
			// Check if set "append, after, before, prepend", otherwise just show the "control" 
			if (settings.display == 'after') {
				
				// remove previously added class
				if (settings.displayRemove)
					$this.next("." + className).remove();
				
									
				$this.after(wrappedTag);
				
				var classObj = $this.next("." + className);
				
			} else if (settings.display == 'before') {
				
				// remove previously added class
				if (settings.displayRemove)
					$this.prev("." + className).remove();
					
				$this.before(wrappedTag);

				var classObj = $this.prev("." + className);
								
			} else if (settings.display == 'append') {
				
				// remove previously added class
				if (settings.displayRemove)
					$this.children("." + className).remove();
					
				$this.append(wrappedTag);
				
				var classObj = $this.children("." + className);
									
			} else if (settings.display == 'prepend') {
				
				// remove previously added class
				if (settings.displayRemove)
					$this.children("." + className).remove();
					
				$this.prepend(wrappedTag);
				
				var classObj = $this.children("." + className);

			} else {
				var classObj = $("." + className);
			}
					
			
			if (settings.fadeIn)	
				classObj.fadeIn(settings.fadeIn);
				
			if (settings.fadeOut)
				classObj.fadeOut(settings.fadeOut);	
						
		});
	};
})(jQuery);
