/*
CMG Internet Extensions for main navigation.
Note that this script requires jquery files be included.
*/
if (typeof jQuery == 'function')
{
(function( $ ){
	$.fn.MainNavPlugin = function() {
		var $menu = this;
		
		var methods = {
			MenuHook: function(menu) {
				menu.data("fadedout", true);
				menu.data("mousein", false);

				menu.hover(
					// mouse in
					function() {
						menu.data("mousein", true);
						if(menu.data("fadedout") == true)
						    // Delay the dropdown menu before displaying. Sudden mouseovers will not trigger the menu.
							menu.delay(200).fadeIn("fast").data("fadedout", false);
					},

					// mouse out
					function(){
						menu.data("mousein", false);
						setTimeout(function() {
							if(menu.data("fadedout") == false && $("#MainNavTab_" + menu.attr("id").replace("MainNavItems_", "")).data("mousein") == false)
                                // Stop the animation of menu when mouseout occurs. Set the Stop() to delete the queue of 
                                // events and 'Jump to the end' of the current animation so that the opacity completes.
								menu.stop(true,true).fadeOut("fast").data("fadedout", true);
						}, 100);
					}
				);
			},
			
			HeaderTabHook: function(anchor) {
				anchor.data("mousein", false);
				
				anchor.hover(
					// mouse in
					function() {
						anchor.data("mousein", true);

						var target = $( "#MainNavItems_" + $(this).attr("id").replace("MainNavTab_", "") );
						
						$(".headerTabItems").each( function(index) {
							if ($(this).attr("id") != target.attr("id"))
								$(this).hide();	// Hide each fancy menu.
						});
						
						methods.SetPosition(anchor, target);
						if(target.data("fadedout") == true)
						    // Delay the dropdown menu before displaying. Sudden mouseovers will not trigger the menu.
							target.delay(200).fadeIn("fast").data("fadedout", false);
					},
					
					// mouse out
					function() {
						anchor.data("mousein", false);
						
						var target = $( "#MainNavItems_" + $(this).attr("id").replace("MainNavTab_", "") );
						setTimeout(function () {
							if(target.data("fadedout") == false && target.data("mousein") == false)
                                // Stop the animation of menu when mouseout occurs. Set the Stop() to delete the queue of 
                                // events and 'Jump to the end' of the current animation so that the opacity completes.
								target.stop(true,true).fadeOut("fast").data("fadedout", true);
						}, 100);
					}
				);
			},
			
			SetPosition: function(parent, menu){
				headerPos = parent.position();
				headerHeight = parent.height();
				
				// NOTE: You might want to simply do $("#menu1").offset(headerPos), but
				// that does not work if the element is not visible.
				menu.css("top", headerPos.top + headerHeight + "px");
				menu.css("left", (headerPos.left - 5) + "px");
				menu.css("width", menu.width() + "px");	// This fixes the width of the menu so that we can reposition.
				
				var windowWidth = $(window).width();
				var left = headerPos.left;
				var menuWidth = menu.outerWidth() + 5;	// Shadow is 5px
				if (left + menuWidth > windowWidth){
					var diff = windowWidth - left - menuWidth;
					menu.css("left", (headerPos.left + diff) + "px");
				}
			}
		};
		
		methods.MenuHook($menu);
		methods.HeaderTabHook($("#MainNavTab_" + $menu.attr("id").replace("MainNavItems_", "")));
		
		return this;
	};	
})( jQuery );
}

