/*
	ErrorTip 1.0
	
	@author Kevin Jantzer, Folium Partners, LLC
	@date 2011-01-25
	@revision 2011-02-14
	
	@todo - adjust position when too close to edge of screen
*/
(function( $ ){
			
	// +===============================+
	// | ErrorTip INIT				   |
	// +===============================+
	$.fn.errortip = function(msg,options) {
	
		// Extend Options/Settings
		var settings = {
	      'showArrow'		: true,		// whether or not we show a down arrow
	      'color'			: 'red',	// create more color styles in the CSS file
	      'fade'			: 120,		// set to 0/false for no fade
	      'maxWidth'		: 400,		// max width the error tip can be
	      'autoFadeOut'		: false,	// set to ms number (5000) for an auto fade out
	      'posX'			: 'right',	// position horizontal (right or left)
	      'offsetY'			: 0,		// fine tune the position of the error tip on the y axis
	      'offsetX'			: 0,		// fine tune the position of the error tip on the x axis
	      'returnTip'		: false		// alters chainability - 'true' returns the errotip object instead of jQuery selector.
	    };
	    
	    
	    
		if (options) { $.extend( settings, options ); }
		
		
		// ***TEMP*** only in here for MBF WE
		if($.browser.msie) settings.showArrow = false;
	
	
		// Methods - i.e. $('#someID').errortip('clear')
		if(msg == 'clear'){
			clear(this,settings);
		}else{
			return create(this,msg,settings);
		}
		
	};
	
	
	// +===============================+
	// | ErrorTip CREATE			   |
	// +===============================+
	create = function(e,msg,settings){
		// set vars
		var docHeight = $(window).height();
			offset = e.offset(),
			eWidth = e.width(),
			id = e.attr('id'),
			style = '',
			arrow = '',
			alreadyExists = $('[data-uniq_id="kjc_errortip_'+id+'"]').size();
		
		
		// if the errortip for this element already exists, let's remove it
		if(alreadyExists){
			
			// if we are displaying the same errortip msg
			if($('[data-uniq_id="kjc_errortip_'+id+'"] .kjc_errortip_msg').html() == msg){
				settings.fade = 0;
			}
			clear(e,settings);
		}
	
		// set Style attribute
		style = 'position: absolute; z-index:10000; display:none;';
		style += 'max-width:'+settings.maxWidth+'px;';
		style += 'bottom:'+(docHeight-offset.top-4+settings.offsetY)+'px;';
		
		if (settings.posX == 'left'){
			style += 'left:'+(offset.left+5+settings.offsetX)+'px;';
		}else{
			style += 'left:'+(offset.left+eWidth-3+settings.offsetX)+'px;';
		}
		
		// create Arrow <div> if settings set to true
		if(settings.showArrow){
			arrow = '<div class="kjc_arrow"></div>';
		}
		
		// create the DOM element, append to the body tag, then fade in the errortip
		var theTip = $('<div class="kjc_errortip '+settings.color+'" '+
			'data-uniq_id="kjc_errortip_'+id+'" '+
			'data-id="kjc_errortip" '+
			'style="'+style+'" ><span class="kjc_errortip_msg">'+msg+'</span>'+arrow+'</div>')
				.appendTo('body')
				.fadeIn(settings.fade);
		
		// Auto FadeOut
		if(settings.autoFadeOut){
			window.kjcErrortipAutoFadeOutTimer = setTimeout(function(){
				theTip.errortip('clear');
			},settings.autoFadeOut)
		}
		
		// What DOM element to return
		//		Defaults to returning jQuery selector object, but can be overridden to return errortip object
		if(settings.returnTip){
			return theTip;
		}else{
			return e;
		}
	}
	
	
	// +===============================+
	// | ErrorTip CLEAR				   |
	// +===============================+
	clear = function(e,settings){
		var id = e.attr('id');
		
		clearTimeout(window.kjcErrortipAutoFadeOutTimer);
		
		$('[data-uniq_id="kjc_errortip_'+id+'"]')
			.fadeOut(settings.fade,function(){
				$(this).remove();
			});
	}
	
	
	// ========================================================================
	// ========================================================================
	
	
	$.errortip = function(){
		// do nothing, needed for sub-functions below
	}
	
	/*
		ErrorTip Clear All - clears all 
	*/
	$.errortip.clear = function(){
		$('[data-id="kjc_errortip"]').remove();
	}
		
		
		
})( jQuery );
