/*
	Nav - jQuery (No-frills) Dropdown Navigation | Copyright 2011 Justin Shearer | Dual licensed under the MIT and GPL licenses: http://www.opensource.org/licenses/mit-license.php & http://www.gnu.org/licenses/gpl.html
	
	Options:
	selectedClass (default='selected') This is applied to the top level list item of the selected item.
	ddClass (default='dd') This is object that is to drop down.
	ddWidth (default=175) This is the width of the dropdown.
	center (default=false) Setting this to true will center each dropdown on their parent element.
	delay (default=500)	This is the time that the dropdown will be visible after mousing out.
	
	Example usage in $(document).ready:
	$('#mainNav').nav({center:true});
	
	Example html structure:
	<nav>
		<ul>
			<li><a href="#"></a>
				<div class="dd">
					<ul>
						<li><a href="#"></a></li>
					</ul>
				</div>
			</li>
		</ul>
	</nav>
*/
(function($){
	$.fn.nav=function(options){
		var defaults = {
			selectedClass :		'selected',
			ddClass :			'dd',
			ddWidth :			175,
			center :			false,
			delay :				500
		},
		options = 			$.extend({}, defaults, options),
		selectedSelector =	'.'+options.selectedClass,
		ddSelector =		'.'+options.ddClass,
		ddOffset =			0,
		$links = 			$(this).children('ul').children('li'),
		$dd = 				$(ddSelector)
		
		$links.children('a').mouseenter(function(){
			$dd.clearQueue().hide();
			$links.removeClass(options.selectedClass);
			$(this).parent('li').addClass(options.selectedClass).find(ddSelector).show();
			if(options.center==true){
				ddOffset=-Math.floor((options.ddWidth-$(this).innerWidth())/2);
				$(selectedSelector).children(ddSelector).css('left',ddOffset);
			}
		});
		$dd.mouseenter(function(){
			$dd.clearQueue();
		});
		$links.mouseleave(function(){
			$dd.clearQueue().delay(options.delay).fadeOut('fast',function(){
				$(selectedSelector).removeClass(options.selectedClass);
			});
		});
	};
})(jQuery);
