/*!
 * TextBox Default Text Plugin
 * Version 1.0.0
 * Dependencies:
 * - requires jQuery
 *
 * Copyright © 2011 David Young & Cape Fear Webmasters, Inc.
 * http://www.cfwebmasters.com/
 * http://dcyoung.com/
 * Released under the GNU GPL Version 2 license.
 * 
 *************** Usage ****************************************
 *
 *	@param (string) location: top|right|bottom|left //where stickyNav will be located
 *	@param (int) showSpeed: 300 //number of milliseconds for show animation
 *	@param (int) hideSpeed: 300 //number of milliseconds for hide animation
 *	@param (int) scroll: 300 //number of pixels to scroll before stickyNav appears
 *
 *	Usage:
 *
 *	var settings = {
 *		'location':'top',
 *		'showSpeed':300,
 *		'hideSpeed':300,
 *		'scroll':300
 *	}
 *	jQuery('#StickyNav').cfwiStickyNav(settings)
 *
 **************************************************************
 */
if(typeof(jQuery)!=='undefined'&&window.jQuery){
	(function($){
		jQuery.fn.cfwiStickyNav=function(settings){
			var $win=$(window),
				m_settings={ 'location':'top', 'showSpeed':300, 'hideSpeed':300, 'scroll':300 }
				
			/* update default settings with provided values (if present) */
			if(typeof(settings)==='object'&&settings!==null){ $.extend(m_settings,settings); }
			
			/* initialize stickyNavs */
			return this.each(function(){
				var $nav=$(this).data({'showing':false}),
					offset={'top':$nav.outerHeight(false),'right':$nav.outerWidth(false),'bottom':$nav.outerHeight(false),'left':$nav.outerWidth(false)},
					css={ 'position':'fixed' },
					m_animate={show:{},hide:{}},
					OnScroll=function(){
						if($win.scrollTop() >= m_settings.scroll){
							if(!$nav.data().showing){
								$nav.data({'showing':true})
									.css(css)
									.animate(m_animate.show,m_settings.showSpeed);
							}
						}else{
							if($nav.data().showing){
								$nav.data({'showing':false})
									.animate(m_animate.hide,m_settings.hideSpeed);
							}
						}
					}
				
				/* update css, m_animate.show, m_animate.hide with correct location values */
				css[m_settings.location]='-'+offset[m_settings.location]+'px';
				m_animate.show[m_settings.location]=0;
				m_animate.hide[m_settings.location]='-'+offset[m_settings.location]+'px';
				
				/* initialize */
				$nav.css(css);
				$win.scroll(OnScroll);
			});
		}
	})(jQuery);
}
