
/** function to transform a list into a horizontal rollover menu */
jQuery.fn.hrmenu = function(settings)
{
	settings = jQuery.extend
	(
		{
			iframe: false, 	//if you want the menu to show over selects in ie6, need to set iframe to true
			timeout: 500,	//timeout for fading out menu
			effect: 'fade', 	//fade,slide,show
			submenuPadding: 10,
			minSubmenuWidth: 10
		},
		settings
	);
	
	/** if you want to use the iframe fix, you need to make sure there's an ie6 variable in javascript that's set to false, then overridden by a conditional comment in ie6
	
	<script type="text/javascript">
		var ie6 = false;
	</script>
	<!--[if lt IE 7]>
	<script type="text/javascript">
		var ie6 = true;
	</script>
	<![endif]-->
	
	*/
	
	/** check if we need to use the iframe fix */
	var show_iframe = false;
	if(settings.iframe == true && ie6 !== undefined && ie6 == true) show_iframe = true;

	jQuery.hrMnuObs = [];

	var tmp = [];

	$(this).children(':has("ul")').children(':has("li")').each
	(
		function()
		{
			$(this).css( {visibility:'hidden',display:'block'} );
			
			var maxwidth = settings.minSubmenuWidth;
			
			$(this).children().each(function(){
				if($(this).attr('offsetWidth') > maxwidth) maxwidth = $(this).attr('offsetWidth');
			});
			
			maxwidth += settings.submenuPadding;

			$(this).css( 'width', maxwidth + 'px' );
			$('a', $(this)).each(
				function()
				{
					$(this).css( 'width', maxwidth + 'px' );
				}
			);

			$(this).css( {display:'none',visibility:'','z-index': 999} );
		}
	);

	tmp = null;
	
	if(show_iframe)
	{
		$(' > li', $(this)).each(function(){
			if($('> ul', $(this)).length > 0)
			{
				$(this).append('<iframe class="iframe" frameborder="0" style="position:absolute; left:0px; background-color:white; width:100px; height:100px; display:none; "></iframe>');
								
				matchDimensions($('>ul', $(this)), $('.iframe', $(this)));
			}
		});
	}
	
	$(' > li', $(this)).bind('mouseover',
		function()
		{
			if($('> ul', $(this)).length > 0)
			{
				$(this).addClass('over');
				
				if (jQuery.inArray($(this).attr('id'),jQuery.hrMnuObs) == -1) jQuery.hrMnuObs.push($(this).attr('id'));
				
				for (var j=0; j<jQuery.hrMnuObs.length; j++) 
				{
					$('#' + jQuery.hrMnuObs[j]).css({'z-index': 999});
				}
				
				$(this).css({'z-index': 1000});
				
				if($(this).data('timeout')) clearTimeout($(this).data('timeout'));
				
				if (settings.effect == 'slide' ) 
				{
					$(' > ul:hidden', $(this)).slideDown('fast');
					if(show_iframe) $(' > .iframe:hidden', $(this)).slideDown('fast');
				}
				else if (settings.effect == 'show' ) 
				{
					$(' > ul:hidden', $(this)).show('slow');
					if(show_iframe) $(' > .iframe:hidden', $(this)).show('slow');
				}
				else 
				{ 
					$(' > ul:hidden', $(this)).fadeIn('slow');
					if(show_iframe) $(' > .iframe:hidden', $(this)).fadeIn('slow');
				}
				
				if(show_iframe) matchDimensions($('>ul', $(this)), $('.iframe', $(this)));
			}		}
	);

	$(' > li', $(this)).bind('mouseout',
		function()
		{
			$(this).removeClass('over');
			if($(this).data('timeout')) clearTimeout($(this).data('timeout'));
			var doit = (settings.effect == 'show') ? "hide('slow');" : (settings.effect == 'slide') ? "slideUp('fast');" : "fadeOut('fast');";
			$(this).data('timeout', setTimeout("$('>ul:visible', $('#"+$(this).attr('id')+"'))." + doit + "$('>.iframe:visible', $('#"+$(this).attr('id')+"'))." + doit, settings.timeout));
		}
	);
}

function pxToInt(val)
{
	return parseInt(val.replace('px', ''));
}

function matchDimensions(source, target)
{
	var width = $(source).width();
	width += pxToInt($(source).css('padding-left'));
	width += pxToInt($(source).css('padding-right'));
	width += pxToInt($(source).css('border-left-width'));
	width += pxToInt($(source).css('border-right-width'));
	
	var height = $(source).height();
	height += pxToInt($(source).css('padding-top'));
	height += pxToInt($(source).css('padding-bottom'));
	height += pxToInt($(source).css('border-top-width'));
	height += pxToInt($(source).css('border-bottom-width'));
					
	$(target).css(
		{
			'width': width+'px', 
			'height': height+'px',
			'top': $(source).css('top'),
			'left': $(source).css('left')
		}
	);
}
