/**
 * Sliding menu plugin
 * makes an animated div slide along the bottom of the menu and position directly below 
 * the item A tag, it adjusts its width accordingly.
 * 
 * @author Grant Ockwell
 */
(function($) {
	var animationTimer = null;
	
	/**
	 * Extend jquery so we can work with the .each of the passed element
	 */
	$.fn.extend({
		
		/**
		 * The guts of the plugin itself
		 */
		slideMenu: function(options) {
			
			/** Default Options */
			var defaults = {
				fadeInOnLoad: false,
				fadeOutIfNoSelected: true,
				slideId: 'menu-slide',
				parentDivId: 'menu',
				selectedClass: 'topnavSelected',
				slideToSpeed: 500,
				slideBackSpeed: 500,
				graceTiem: 200
			};
			
			// @todo: Finish implimenting all these options.
			var options = $.extend({}, defaults, options);
			
		
			/**
			 * The global onload method, handles a slider menu with no selected item wher b
			 */
			$(function() {
				if (options.fadeInOnLoad) {
					$('#menu-slide').hide();
				}

				if ($('a.topnavSelected').length > 0) {
					setTimeout("$('a.topnavSelected').mouseover()", 500);
				} else {
					$('#menu').mouseleave(function() {
						animationTimer = setTimeout("$('#menu-slide').fadeOut('slow')", 200);
					});
				}
				
				
			});
		
			return this.each(function() {
				$(this).find('A').mouseover(function() 
				{
					
					if (animationTimer) { 
						clearTimeout(animationTimer); 
					}
					
					$('#menu-slide').clearQueue();
					
					if ($('#menu-slide:hidden').length == 1) {
						$('#menu-slide').fadeIn();
					} 
					
					$('#menu-slide').animate({
						left: $(this).position().left,
						width: $(this).outerWidth()
					}, 500);
					
					
				}).mouseout(function() 
				{
					if ($('a.topnavSelected').length > 0) {
						animationTimer = setTimeout("$('#menu-slide').animate({left: $('a.topnavSelected').position().left, width: $('a.topnavSelected').outerWidth()}, 600)", 200);
					} 
				});
			});
		}
	})
})(jQuery);
