(function($){
	$.fn.extend({ 
		//	plugin name - menumaker
		menumaker: function(options) {

			//	Settings list and the default values
			var defaults = {
				menu_speed:			50,
				constrain_to:		"",
				offset_mode:		"left"
			};
			
			var options = $.extend(defaults, options);

			var opts	= options;

    		return this.each(function() {
				var o =options;
				
				//	Menu group object (ul.m0 element)
				var menu_group = $(this);				
				
				//Get all LI in the UL
				var items = $("ul.m0 li", menu_group);
				var main_nav_images = $("a.a0 img", menu_group);

				//	Initialize (sets up submenus ul.m1 to be repositioned on first use after document load or resize
				$(window).resize(function(){ InitReset( menu_group );});
				$(document).ready(function(){ InitReset( menu_group );});
			
				items.hover( function()
				{
					$(this).find("ul").first().fadeIn( opts.menu_speed );
					$( this ).addClass("menu-highlight");

					var image_hover	= $( this ).find("a.a0 img:first").attr("image_hover");
					if( image_hover )
					{
						$( this ).find("a.a0 img").attr("src", image_hover );
					}
				}, 
				function () {
					$(this).find("ul").first().fadeOut( opts.menu_speed );
					$( this ).removeClass("menu-highlight");

					var image_default	= $( this ).find("a.a0 img:first").attr("image_default");
					if( image_default )
					{
						$( this ).find("a.a0 img").attr("src", image_default );
					}
				}
				);


			//---------------------------------------------------------------------------------------------------------
			//	function:		RepositionMenu
			//	what it does:	Reposition the m1 submenu elements when the parent button is activated
			//	args:			menu_button_object:		- the a.a0 button object
			//---------------------------------------------------------------------------------------------------------
				function RepositionMenu( menu_button_object )
				{
					if(  opts.offset_mode == "override" )
					{
						return;
					}

					m1_pos_left		= GetLeftOffset( menu_button_object );
					m1_pos_top		= GetTopOffset( menu_button_object );

					//	Get menu object
					menu_object	= $(menu_button_object).parentsUntil("ul.m0").find("ul:first");

					//	Display it (can't change offset to hidden objects)
					$(menu_object).css("display","block");

					//	Change the offset position (double calls for now, can't figure out why IE and Chrome don't work when this is called only once)
					$(menu_object).offset({ top: m1_pos_top, left: m1_pos_left });
					$(menu_object).offset({ top: m1_pos_top, left: m1_pos_left });

					//	Unbind the event listeners for improved performance
					$(menu_button_object).unbind('mouseover mousemove focus ready');
				}

			//	handle all ul.m1 -offset calculations here
			//---------------------------------------------------------------------------------------------------------
			//	function:		GetLeftOffset
			//	what it does:	Determines if a contraining element is specified, and returns the offsert (in pixels) to make sure that a level 1 menu block
			//						does not exceed the right-most limit of that constraining element
			//	args:			object:		- the image object
			//---------------------------------------------------------------------------------------------------------
				function GetLeftOffset( object )
				{
					//	Calc default left position
//					var p			= $( object );
					var p			= $( object ).parent();
					var offset		= p.offset();
					var	menu_left	= offset.left;

					var	offset_left		= 0;
					var	menu_obj		= $( object ).parentsUntil("ul").find("ul.m1").first();
					var	menu_width		= menu_obj.width();
//					var	button_width	= $( object ).width();
					var	button_width	= $( object ).parent().width();

					//	Adjust for custom offset
					switch( opts.offset_mode )
					{
						case "override":
							return;
						break;

						case "center":
							offset_left		= ( button_width - menu_width ) / 2;
						break;

						case "right":
							offset_left		= button_width - menu_width;
						break;

						case "left":
						default:
							offset_left		= 0;
						break;
					}

					menu_left	+= offset_left;

					//	Calc constrain offset
					if( opts.constrain_to )
					{
						var	constrain_obj		= $("#"+opts.constrain_to);
						var constrain_offset	= constrain_obj.offset();
						var	constrain_left		= constrain_offset.left;
						var	constrain_width		= constrain_obj.width();
						var	constrain_right		= constrain_left + constrain_width;

						var	menu_obj		= $( object ).parentsUntil("ul").find("ul.m1").first();
						var	menu_width		= menu_obj.width();
						var	menu_right		= menu_left + menu_width;

						if( menu_right > constrain_right )
						{
							menu_left	+= (constrain_right - menu_right );
						}
						else if( menu_left < constrain_left )
						{
							menu_left	= constrain_left;
						}
					}

					return menu_left;
				}

			//---------------------------------------------------------------------------------------------------------
			//	function:		GetTopOffset
			//	what it does:	Determines if a contraining element is specified, and returns the offsert (in pixels) to make sure that a level 1 menu block
			//						does not exceed the right-most limit of that constraining element
			//	args:			object:		- the image object
			//---------------------------------------------------------------------------------------------------------
				function GetTopOffset( object )
				{
					//	Calc default top position
					var p			= $( object );
					var offset		= p.offset();
					var	menu_top	= offset.top + $( object ).innerHeight();

					return menu_top;
				}

			//---------------------------------------------------------------------------------------------------------
			//	function:		InitReset
			//	what it does:	Resets the positining initialization so that DHTML menus can be repositioned when the window is resized
			//---------------------------------------------------------------------------------------------------------
				function InitReset( menu_object )
				{
					var main_nav_images = $("a.a0 img", menu_object);

					if( main_nav_images.length )
					{
						main_nav_images.parent().bind('mouseover mousemove focus', function() {
							RepositionMenu( this );
						});
					}
					else
					{
						$("a.a0", menu_object).bind('mouseover mousemove focus ready', function() {
							RepositionMenu( this );
						});
					}
				}

			
			
			});


		}
	
	});	
})(jQuery);



