/*
 * 	Name:  jQuery Menu Version
 * 	Author:  Chris Shelton
 *	Last Modified:  7-9-2007 12:45 PM
 *
*	The Menu plugin for jQuery is used to display dropdown menus that
*	are either horizontal or vertical.  This plugin requires the
*	dimensions plugins for jQuery.
*
*	Example JS:
*	$('ul.menu').menu({hideDelay: 500);
*
*	Example HTML (horizontal):
*	<ul class="menu">
*		<li><a href="#">toolbar 1</a>
*			<ul>
*				<li><a href="#">sub 1</a></li>
*				<li><a href="#">sub 2</a></li>
*				<li><a href="#">sub 3</a></li>
*			</ul>
*		</li>
*		<li><a href="#">toolbar 2</a>
*			<ul>
*				<li><a href="#">sub 1</a></li>
*				<li><a href="#">sub 2</a></li>
*				<li><a href="#">sub 3</a></li>
*			</ul>
*		</li>
*	</ul>
*
*	Example HTML (vertical):
*	<ul class="menu menu_vertical">
*		<li><a href="#">toolbar 1</a>
*			<ul>
*				<li><a href="#">sub 1</a></li>
*				<li><a href="#">sub 2</a></li>
*				<li><a href="#">sub 3</a></li>
*			</ul>
*		</li>
*		<li><a href="#">toolbar 2</a>
*			<ul>
*				<li><a href="#">sub 1</a></li>
*				<li><a href="#">sub 2</a></li>
*				<li><a href="#">sub 3</a></li>
*			</ul>
*		</li>
*	</ul>
*
*	Options:
*	hideDelay: the delay in milliseconds before the submenu will hide (default: 300)
*	submenuIndicator: determines whether a div is added to the inner part of the LI (default: false)
*	rightOnly: the rightonly option specifies to only display submenus to the right of the parent (default: false)
*	activePage: an override url to set the highlighted menu. If undefined the document.location.href will be used. (default: undefined)

*	pathCheck: this determines of the path of the domain items is checked against the current location (default: false)
*
*	CSS:
*	menu_root:
*	The top-level element of the list HTML.  This will be the UL element
*	that contains the menu.  It is add in the menu plugin.
*
*	menu_vertical:
*	A top-level element that is added to the UL element in the HTML.  The
*	menu plugin will check for the occurance of the class to control
*	positioning.
*
*	menu_toolbar_hover:
*	A class that is added to the first-level of the menu when the mouse
*	hovers over the LI element.  It is also added to the A element within the
*	LI element.
*
*	menu_item_hover:
*	A class that is added to elements below the first-level of the menu
*	when the mouse hovers over the LI element.  It is also added to the
*	A element within the LI element.
*
*	menu_active_domain:
*	A class added to the A element and all parent LI elements where the
*	href attribute of the A element matches the document.location.href.
*
*	menu_submenu_indicator:
*	A class added to a dynamically added SPAN element.  This element
*	is positioned on the right of the LI element and will only be created
*	for a LI element that has a submenu.
*
*	menu_submenu_indicator_hover:
*	A class added to a dynamically added SPAN element when the mouse
*	hovers over the LI element.  This element is positioned on the right of
* 	the LI element and will only be created for a LI element that has a
*	submenu.
*
*/
(function($){

	//Local variables
	var menu = [];

	//CSS Variables
	var cssRoot 	= 'menu_root';
	var cssVerticalMenu = 'menu_vertical';
	var cssToolbarHover	= 'menu_toolbar_hover';
	var cssItemHover	= 'menu_item_hover';
	var cssActiveDomain = 'menu_active_domain';
	var cssSubmenuIndicator = 'menu_submenu_indicator';
	var cssSubmenuIndicatorHover = 'menu_submenu_indicator_hover';

	//Bind events to the document for clean-up of menus
	$(document)
		// Bind a click event to hide all visible menus when the document is clicked
		.bind('click', function(){
			//alert($(menu).debug());
			$(menu).each(function(){
				$(this).find('ul:visible').each(function(){
					hide($(this).parent());
				});
			});
		})
		// Cleanup after ourself by nulling the $settings object
		.bind('unload', function() {
			$(menu).each(function() {
				this.settings = null;
			});
		});

	// Public methods
	$.fn.menu = function(inSettings) {
		var settings = $.extend({}, arguments.callee.defaults, inSettings);
		return this.each(function() {
			menu.push(this);
			$(this).addClass(cssRoot);
			this.$settings = $.extend({}, settings, {isVerticalMenu: $(this).is('.' + cssVerticalMenu)});
			$('> li', this).hover(hoverOver,hoverOut);
			if (this.$settings.submenuIndicator) {
				$('li[ul]', this).prepend($('<div class="' + cssSubmenuIndicator + '"></div>'));
			};

			var activeAnchor = findActiveAnchor(this, this.$settings.activePage, this.$settings.pageGuid);
			$(activeAnchor).parents('li').each(function(){
				if ($(this).parent().is('.' + cssRoot)) {
					$(this).addClass(cssActiveDomain);
				};
			});

//  COMMENT OUT
//			var activeDomainFound = false;
//			$(this).find('a[href]').each(function() {
//				if (document.location.href == this.href) {
//					$(this).parents('li').each(function() {
//						if ($(this).parent().is('.' + cssRoot)) {
//							$(this).addClass(cssActiveDomain);
//						};
//					});
//					activeDomainFound = true;
//				};
//			});
//			if (activeDomainFound ==false && this.$settings.pathCheck) {
//				var savedA;
//				$('> li', this).each(function() {
//					var a = $('> a', this)[0];
//					var docPath = document.location.href.substring(0, a.href.length);
//					if (a.href == docPath) {
//						savedA = a;
//					}
//				});
//				if (savedA) {
//					$(savedA).parents('li').each(function() {
//						if ($(this).parent().is('.' + cssRoot)) {
//							$(this).addClass(cssActiveDomain);
//						};
//					});
//				};
//			};
//  COMMENT OUT

		});
	};

// These are our default settings for this plugin
//        pathCheck: false    (parm removed)
	$.fn.menu.defaults = {
		hideDelay: 300,
		submenuIndicator: false,
		rightOnly: false,
		activePage: undefined,
		pageGuid: undefined
	};


	$.fn.menu.findActiveAnchor = function(menu, activePage, pageGuid) {
		return findActiveAnchor(menu, activePage, pageGuid);
	}

	// Private methods

	function findActiveAnchor(menu, activePage, pageGuid) {
		var activeAnchor = undefined;
//        $(menu).find('a[@href]').each(function() {
		$(menu).find('a[href]').each(function() {
			if(pageMatches(this.href, activePage, pageGuid)){
				if(activeAnchor == undefined) activeAnchor = this;
			};
		});
		return activeAnchor;
	}

	function pageMatches(href, activePage, pageGuid) {
		if(pageGuid == undefined && activePage == undefined){
			return document.location.href == href;
		}else if(pageGuid == undefined && activePage != undefined){
			return activePage == href;
		}else if(pageGuid != undefined){
			//have to do a search through the link for the pageGuid
			var regex = new RegExp('(&pageguid=' + pageGuid + ')');
			return regex.test(href) || document.location.href == href;
		}
	}

	function hoverOver() {
		if (this.$timer) {
			clearTimeout(this.$timer);
		};

		if ($('> ul', this).size() > 0) {
			if (!$('> ul', this).is(':visible')) {
				show(this);
			};
		}
		else {
			$(this).siblings().each(function(){
				hide($(this));
			});
			var c = $(this).parent().is('.' + cssRoot) ? cssToolbarHover : cssItemHover;
			$(this).addClass(c).find('> a').addClass(c);
		};
	};

	function hoverOut() {
		if (this.$timer) {
			clearTimeout(this.$timer);
		};

		if ($(this).is(':visible')) {
			var settings = $(this).parents('.' + cssRoot)[0].$settings;
			var li = this;
			this.$timer = setTimeout(function() {
				hide(li);
			}, settings.hideDelay);
		};
	};

	function show(li) {
		if (this.$timer) {
			clearTimeout(this.$timer);
		};

		var ul = $('> ul', li);
		var settings = $(ul).parents('.' + cssRoot)[0].$settings;
		$(ul).parent().siblings().each(function(){
			hide($(this));
		});

		$('> li', ul).hover(hoverOver,hoverOut);
		$('> .' + cssSubmenuIndicator, li).addClass(cssSubmenuIndicatorHover);

		var liPosition = $(li).position();
		var x = 0, y = 0;
		if ($(li).parent().is('.' + cssRoot) && !settings.isVerticalMenu) {
			x = liPosition.left;
			y = liPosition.top + $(li).outerHeight();
		} else {
			x = liPosition.left + $(li).outerWidth();
			if (!settings.rightOnly) {
 //                var liOffset = $(li).offsetLite();
                var liOffset = $(li).offset();


                if ($.browser.msie && $.browser.version < '7.0') {
                    $(ul).css({visibility: '', left: 0, top: 0}).show();
                } else {
				    $(ul).css({visibility: '', left: 0, top: 0}).slideDown("fast");
                }
				var menuOuterWidth = $(ul).outerWidth();
				if ((liOffset.left + x + menuOuterWidth) > $(window).width()) {
					x = -menuOuterWidth;
				};
			};
			y = liPosition.top - (parseInt($(ul).css('borderTopWidth')) || 0);
		};


		var c = $(li).parent().is('.' + cssRoot) ? cssToolbarHover : cssItemHover;
		$(li).addClass(c).find('> a').addClass(c);
        if ($.browser.msie && $.browser.version < '7.0') {
            $(ul).css({left: x + 'px', top: y + 'px'}).show();
		} else {
            $(ul).css({left: x + 'px', top: y + 'px'}).slideDown("fast");
        }
		if ($.browser.msie && $.browser.version < '7.0') {
			if ($('> iframe', ul).size() == 0) {
				$(ul).append('</iframe>').prepend('<iframe style="position: absolute; z-index: -1;" src="javascript:false">');
			};
			$('> iframe', ul).css({		opacity:		'0',
										left: 			'0px',
										top: 			'0px',
										width: 			$(ul).innerWidth() + 'px',
										height: 		$(ul).outerHeight() + 'px'});
		};

	};

	function hide(li) {
		var c = $(li).parent().is('.' + cssRoot) ? cssToolbarHover : cssItemHover;
		$(li).removeClass(c).find('> a').removeClass(c);
		var ul = $('ul:visible', li);
		if ($(ul).length > 0) {
            if ($.browser.msie && $.browser.version < '7.0') {
                $('ul:visible', ul).hide();
            } else {
			    $('ul:visible', ul).slideUp("fast");
            }
//			$('> li', ul).unbind('mouseover');
			$('> .' + cssSubmenuIndicator, li).removeClass(cssSubmenuIndicatorHover);
            if ($.browser.msie && $.browser.version < '7.0') {
                $(ul).hide();
            } else {
                $(ul).slideUp("fast");
            }
			$('> iframe', ul).remove();
		};
	};
})(jQuery);