﻿/**
 *
 * Edited by NLC
 * June 25, 2009
 * non~linear creations @ http://www.nonlinear.ca
 *
 * ----------------------------------------------------------------------------
 *
 * Multi-level Drop Down Menu (multi-ddm)
 * March 26, 2009
 * Corey Hart @ http://www.codenothing.com
 *
 * @timer: [Default 500] Time in milliseconds to hold menu's open on mouseouts
 * @parentMO: CSS class to add/remove from parent menu on mouseover/mouseouts
 * @childMO: CSS class to add/remove to ALL child menus
 * @levels: Array of CSS classes in order of appearance on drop downs
 * @parentTag: List type of the parent menu ('ul' or 'ol')
 * @childTag: List type of each menu level ('ul' or 'ol')
 * @tags: List type of each level in order ('ul' or 'ol' for each)
 * @numberOfLevels: [Default 5] Number of levels the menu has. Will default to 
 * 	length of levels array when childMO is null.
 */ 

;(function($){
	$.fn.dropDownMenu = function(options){
		var menus = new Array();
		var css;
		var tag;
		var internal;
		var timeout;
		var settings = $.extend({
			timer: 500,
			parentMO: null,
			childMO: null,
			levels: [],
			parentTag: 'ul',
			childTag: 'ul',
			tags: [],
			numberOfLevels: 1
		},options||{});

		// Set number of levels
		if (settings.tags.length > 0)
		{
			settings.numberOfLevels = settings.tags.length;
		}
		else if (settings.levels.length)
		{
			settings.numberOfLevels = settings.levels.length;
		}
		
		// Set css levels with childMO
		if (settings.childMO)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.levels[i] = settings.childMO;
			}
		}

		// Set tag levels with tag
		if (settings.tags.length < 1)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.tags[i] = settings.childTag;
			}
		}
		
		//Need this for google translate since it inserts a span tag
		var tagSelector = "a";
		if ($("#nav_main > ul > li > span").length > 0) 
			tagSelector = "span";
		
		// Run through each level
		menus[0] = $(this).children('li').children(tagSelector);
				
		//  ********
		//  NLC EDIT
		//  Variable to store previous hovered item		
			var prevItem;
			var menuPos;
			var browserWidth;
			var browserPadding = 20;
			var newLeft;
			var leftPos = 0;
			var siteWidth = $("#wrapper").width();
			var menuItemWidth = $("#multi-ddm ul").width();
			var defaultLeft = 0;
			var theLevelNum = 0;
		//  END NLC EDIT
		//  ************
		
		var i = 1;
		var len_i = settings.numberOfLevels + 2;
		for (i; i < len_i; i++)
		{
			// Tags/CSS
			css = (i==1) ? settings.parentMO : settings.levels[i-2];
			tag = (i==1) ? settings.parentTag : settings.tags[i-2];
						
			// level selector
			menus[i] = menus[i-1].parent().children(settings.tag).children('li').children(tagSelector);
			
			// Action
			menus[i-1].attr({rel: css+';'+tag}).mouseover(function()
			{
				if (timeout)
				{
					clearTimeout(timeout);
				}
				internal = $(this).attr("rel").split(";");
				
				//  ********
				//  NLC EDIT
				//  Check whether the pull out menu will overlap the browser window
				//  Adjust the position based on results
					theLevelNum = $(this).parents('li').length;
					
					if(jQuery.browser.msie)
					{
						browserWidth = document.documentElement.offsetWidth - browserPadding;
					}
					else
					{
						browserWidth = window.innerWidth - browserPadding;
					}
					
					if (browserWidth < siteWidth)
					{
						browserWidth = siteWidth;
					}
					
					menuPos = Math.floor($(this).offset().left);
					defaultLeft = (theLevelNum > 1) ? menuItemWidth : 0;
					
					if ($(this).parent().children(tagSelector).siblings(internal[1]).html() != null)
					{
						if (theLevelNum == 1)
						{
							if (($(this).parent().offset().left + (menuItemWidth)) > browserWidth)
							{
								leftPos = -(($(this).parent().offset().left + (menuItemWidth)) - browserWidth);
							}
							else
							{
								leftPos = 0;
							}
						}
						else
						{
							var numOfLevels = $(this).siblings('ul').length + 1;
														
							if (($(this).parents('li[id^=mn-]').find('ul:first').offset().left + (numOfLevels * menuItemWidth)) > browserWidth)
							{
								leftPos = -menuItemWidth;
							}
							else
							{
								leftPos = menuItemWidth;
							}
						}
					}
					else
					{
						newLeft = -((menuPos + menuItemWidth) - browserWidth);
						(menuPos + menuItemWidth > browserWidth) ? leftPos = newLeft : leftPos = defaultLeft;
					}
				
				//  Removes active state of current items sub menu		
					if (prevItem != undefined)
					{
						var prevParent = prevItem.parents('li[id^=mn-]');
						if (prevParent.attr('id') != $(this).parents('li[id^=mn-]').attr('id'))
						{
							prevParent.find(tagSelector).removeClass('child-hover');
							prevParent.find('ul').hide();
						}
					}

					$(this).parent().children('ul').children('li').children(tagSelector).removeClass(internal[0]);
					$(this).parent().siblings('li').children(tagSelector).removeClass(internal[0]).siblings(internal[1]).hide();
					
					var topPos = (theLevelNum == 1) ? 0 : -($(this).parent().children(tagSelector).outerHeight());					
					$(this).parent().children(tagSelector).addClass(internal[0]).siblings(internal[1]).css({left: leftPos + 'px', 'margin-top': topPos + 'px', 'z-index': theLevelNum * 10000}).show();
					
				//  Stores previous hovered item
					prevItem = $(this);
				//  END NLC EDIT
				//  ************
				
			}).mouseout(function()
			{
				internal = $(this).attr("rel").split(";");
				//if (internal[0] == settings.parentMO)
				if (internal[0] == settings.parentMO || internal[0] == settings.childMO) //NLC EDIT 1/12/2010 - Menu now hides when the cursor leaves the drop down
				{
					timeout = setTimeout(function(){closemenu();}, settings.timer);
				}
			});
		}

		// Allows user option to close menus by clicking outside the menu on the body
		$(document).click(function()
		{
			closemenu();
		});

		// Closes all open menus
		var closemenu = function()
		{
			//  ********
			//  NLC EDIT
			//  Removes active state of all children when menu is closed
				$('.child-hover').each(function()
				{
					$(this).removeClass('child-hover');
				});
			//  END NLC EDIT
			//  ************
			
			var i = -1;
			var len_i = menus.length;
			for (len_i; len_i > i; len_i--)
			{
				if (menus[len_i] && menus[len_i].attr("rel"))
				{
					internal = menus[len_i].attr("rel").split(";");
					menus[len_i].parent().children(internal[1]).hide().siblings(tagSelector).removeClass(internal[0]);
				}
			}
			$(tagSelector, menus[0]).removeClass(settings.parentMO);
			if (timeout)
			{
				clearTimeout(timeout);
			}
		}
	};
})(jQuery);


/*

;(function($){
	$.fn.dropDownMenu = function(options){
		var menus = new Array();
		var css;
		var tag;
		var internal;
		var timeout;
		var settings = $.extend({
			timer: 500,
			parentMO: null,
			childMO: null,
			levels: [],
			parentTag: 'ul',
			childTag: 'ul',
			tags: [],
			numberOfLevels: 1
		},options||{});

		// Set number of levels
		if (settings.tags.length > 0)
		{
			settings.numberOfLevels = settings.tags.length;
		}
		else if (settings.levels.length)
		{
			settings.numberOfLevels = settings.levels.length;
		}
		
		// Set css levels with childMO
		if (settings.childMO)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.levels[i] = settings.childMO;
			}
		}

		// Set tag levels with tag
		if (settings.tags.length < 1)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.tags[i] = settings.childTag;
			}
		}
		
		// Run through each level
		menus[0] = $(this).children('li').children('a');
		
		//  ********
		//  NLC EDIT
		//  Variable to store previous hovered item		
			var prevItem;
			var menuPos;
			var browserWidth;
			var browserPadding = 20;
			var newLeft;
			var leftPos = 0;
			var siteWidth = $("#wrapper").width();
			var menuItemWidth = $("#multi-ddm ul").width();
			var defaultLeft = 0;
			var theLevelNum = 0;
		//  END NLC EDIT
		//  ************
		
		var i = 1;
		var len_i = settings.numberOfLevels + 2;
		for (i; i < len_i; i++)
		{
			// Tags/CSS
			css = (i==1) ? settings.parentMO : settings.levels[i-2];
			tag = (i==1) ? settings.parentTag : settings.tags[i-2];

			// level selector
			menus[i] = menus[i-1].parent().children(settings.tag).children('li').children('a');

			// Action
			menus[i-1].attr({rel: css+';'+tag}).mouseover(function()
			{
				if (timeout)
				{
					clearTimeout(timeout);
				}
				internal = $(this).attr("rel").split(";");
				
				//  ********
				//  NLC EDIT
				//  Check whether the pull out menu will overlap the browser window
				//  Adjust the position based on results
					theLevelNum = $(this).parents('li').length;
					
					if(jQuery.browser.msie)
					{
						browserWidth = document.documentElement.offsetWidth - browserPadding;
					}
					else
					{
						browserWidth = window.innerWidth - browserPadding;
					}
					
					if (browserWidth < siteWidth)
					{
						browserWidth = siteWidth;
					}
					
					menuPos = Math.floor($(this).offset().left);
					defaultLeft = (theLevelNum > 1) ? menuItemWidth : 0;
					
					if ($(this).parent().children('a').siblings(internal[1]).html() != null)
					{
						if (theLevelNum == 1)
						{
							if (($(this).parent().offset().left + (menuItemWidth)) > browserWidth)
							{
								leftPos = -(($(this).parent().offset().left + (menuItemWidth)) - browserWidth);
							}
							else
							{
								leftPos = 0;
							}
						}
						else
						{
							var numOfLevels = $(this).siblings('ul').length + 1;
														
							if (($(this).parents('li[id^=mn-]').find('ul:first').offset().left + (numOfLevels * menuItemWidth)) > browserWidth)
							{
								leftPos = -menuItemWidth;
							}
							else
							{
								leftPos = menuItemWidth;
							}
						}
					}
					else
					{
						newLeft = -((menuPos + menuItemWidth) - browserWidth);
						(menuPos + menuItemWidth > browserWidth) ? leftPos = newLeft : leftPos = defaultLeft;
					}
				
				//  Removes active state of current items sub menu		
					if (prevItem != undefined)
					{
						var prevParent = prevItem.parents('li[id^=mn-]');
						if (prevParent.attr('id') != $(this).parents('li[id^=mn-]').attr('id'))
						{
							prevParent.find('a').removeClass('child-hover');
							prevParent.find('ul').hide();
						}
					}

					$(this).parent().children('ul').children('li').children('a').removeClass(internal[0]);
					$(this).parent().siblings('li').children('a').removeClass(internal[0]).siblings(internal[1]).hide();
					
					var topPos = (theLevelNum == 1) ? 0 : -($(this).parent().children('a').outerHeight());					
					$(this).parent().children('a').addClass(internal[0]).siblings(internal[1]).css({left: leftPos + 'px', 'margin-top': topPos + 'px', 'z-index': theLevelNum * 10000}).show();
					
				//  Stores previous hovered item
					prevItem = $(this);
				//  END NLC EDIT
				//  ************
				
			}).mouseout(function()
			{
				internal = $(this).attr("rel").split(";");
				//if (internal[0] == settings.parentMO)
				if (internal[0] == settings.parentMO || internal[0] == settings.childMO) //NLC EDIT 1/12/2010 - Menu now hides when the cursor leaves the drop down
				{
					timeout = setTimeout(function(){closemenu();}, settings.timer);
				}
			});
		}

		// Allows user option to close menus by clicking outside the menu on the body
		$(document).click(function()
		{
			closemenu();
		});

		// Closes all open menus
		var closemenu = function()
		{
			//  ********
			//  NLC EDIT
			//  Removes active state of all children when menu is closed
				$('.child-hover').each(function()
				{
					$(this).removeClass('child-hover');
				});
			//  END NLC EDIT
			//  ************
			
			var i = -1;
			var len_i = menus.length;
			for (len_i; len_i > i; len_i--)
			{
				if (menus[len_i] && menus[len_i].attr("rel"))
				{
					internal = menus[len_i].attr("rel").split(";");
					menus[len_i].parent().children(internal[1]).hide().siblings('a').removeClass(internal[0]);
				}
			}
			$('a', menus[0]).removeClass(settings.parentMO);
			if (timeout)
			{
				clearTimeout(timeout);
			}
		}
	};
})(jQuery);
*/


/*
;(function($){
	$.fn.dropDownMenu = function(options){
		var menus = new Array();
		var css;
		var tag;
		var internal;
		var timeout;
		var settings = $.extend({
			timer: 500,
			parentMO: null,
			childMO: null,
			levels: [],
			parentTag: 'ul',
			childTag: 'ul',
			tags: [],
			numberOfLevels: 1
		},options||{});

		// Set number of levels
		if (settings.tags.length > 0)
		{
			settings.numberOfLevels = settings.tags.length;
		}
		else if (settings.levels.length)
		{
			settings.numberOfLevels = settings.levels.length;
		}
		
		// Set css levels with childMO
		if (settings.childMO)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.levels[i] = settings.childMO;
			}
		}

		// Set tag levels with tag
		if (settings.tags.length < 1)
		{
			var i = 0;
			var len_i = settings.numberOfLevels;
			for (i; i < len_i; i++)
			{
				settings.tags[i] = settings.childTag;
			}
		}
		
		// Run through each level
		////menus[0] = $(this).children('li').children('a');
		menus[0] = $(this).children('li').children('span');
		
		//  ********
		//  NLC EDIT
		//  Variable to store previous hovered item		
			var prevItem;
			var menuPos;
			var browserWidth;
			var browserPadding = 20;
			var newLeft;
			var leftPos = 0;
			var siteWidth = $("#wrapper").width();
			var menuItemWidth = $("#multi-ddm ul").width();
			var defaultLeft = 0;
			var theLevelNum = 0;
		//  END NLC EDIT
		//  ************
		
		var i = 1;
		var len_i = settings.numberOfLevels + 2;
		for (i; i < len_i; i++)
		{
			// Tags/CSS
			css = (i==1) ? settings.parentMO : settings.levels[i-2];
			tag = (i==1) ? settings.parentTag : settings.tags[i-2];

			// level selector
			////menus[i] = menus[i-1].parent().children(settings.tag).children('li').children('a');
			menus[i] = menus[i-1].parent().children(settings.tag).children('li').children('span');

			// Action
			menus[i-1].attr({rel: css+';'+tag}).mouseover(function()
			{
				if (timeout)
				{
					clearTimeout(timeout);
				}
				internal = $(this).attr("rel").split(";");
				
				//  ********
				//  NLC EDIT
				//  Check whether the pull out menu will overlap the browser window
				//  Adjust the position based on results
					theLevelNum = $(this).parents('li').length;
					
					if(jQuery.browser.msie)
					{
						browserWidth = document.documentElement.offsetWidth - browserPadding;
					}
					else
					{
						browserWidth = window.innerWidth - browserPadding;
					}
					
					if (browserWidth < siteWidth)
					{
						browserWidth = siteWidth;
					}
					
					menuPos = Math.floor($(this).offset().left);
					defaultLeft = (theLevelNum > 1) ? menuItemWidth : 0;
					
					////if ($(this).parent().children('a').siblings(internal[1]).html() != null)
					if ($(this).parent().children('span').siblings(internal[1]).html() != null)
					{
						if (theLevelNum == 1)
						{
							if (($(this).parent().offset().left + (menuItemWidth)) > browserWidth)
							{
								leftPos = -(($(this).parent().offset().left + (menuItemWidth)) - browserWidth);
							}
							else
							{
								leftPos = 0;
							}
						}
						else
						{
							var numOfLevels = $(this).siblings('ul').length + 1;
														
							if (($(this).parents('li[id^=mn-]').find('ul:first').offset().left + (numOfLevels * menuItemWidth)) > browserWidth)
							{
								leftPos = -menuItemWidth;
							}
							else
							{
								leftPos = menuItemWidth;
							}
						}
					}
					else
					{
						newLeft = -((menuPos + menuItemWidth) - browserWidth);
						(menuPos + menuItemWidth > browserWidth) ? leftPos = newLeft : leftPos = defaultLeft;
					}
				
				//  Removes active state of current items sub menu		
					if (prevItem != undefined)
					{
						var prevParent = prevItem.parents('li[id^=mn-]');
						if (prevParent.attr('id') != $(this).parents('li[id^=mn-]').attr('id'))
						{
							////prevParent.find('a').removeClass('child-hover');
							prevParent.find('span').removeClass('child-hover');
							prevParent.find('ul').hide();
						}
					}

					////$(this).parent().children('ul').children('li').children('a').removeClass(internal[0]);
					////$(this).parent().siblings('li').children('a').removeClass(internal[0]).siblings(internal[1]).hide();
					$(this).parent().children('ul').children('li').children('span').removeClass(internal[0]);
					$(this).parent().siblings('li').children('span').removeClass(internal[0]).siblings(internal[1]).hide();
					
					////var topPos = (theLevelNum == 1) ? 0 : -($(this).parent().children('a').outerHeight());					
					////$(this).parent().children('a').addClass(internal[0]).siblings(internal[1]).css({left: leftPos + 'px', 'margin-top': topPos + 'px', 'z-index': theLevelNum * 10000}).show();
					var topPos = (theLevelNum == 1) ? 0 : -($(this).parent().children('span').outerHeight());					
					$(this).parent().children('span').addClass(internal[0]).siblings(internal[1]).css({left: leftPos + 'px', 'margin-top': topPos + 'px', 'z-index': theLevelNum * 10000}).show();
				
				//  Stores previous hovered item
					prevItem = $(this);
				//  END NLC EDIT
				//  ************
				
			}).mouseout(function()
			{
				internal = $(this).attr("rel").split(";");
				//if (internal[0] == settings.parentMO)
				if (internal[0] == settings.parentMO || internal[0] == settings.childMO) //NLC EDIT 1/12/2010 - Menu now hides when the cursor leaves the drop down
				{
					timeout = setTimeout(function(){closemenu();}, settings.timer);
				}
			});
		}

		// Allows user option to close menus by clicking outside the menu on the body
		$(document).click(function()
		{
			closemenu();
		});

		// Closes all open menus
		var closemenu = function()
		{
			//  ********
			//  NLC EDIT
			//  Removes active state of all children when menu is closed
				$('.child-hover').each(function()
				{
					$(this).removeClass('child-hover');
				});
			//  END NLC EDIT
			//  ************
			
			var i = -1;
			var len_i = menus.length;
			for (len_i; len_i > i; len_i--)
			{
				if (menus[len_i] && menus[len_i].attr("rel"))
				{
					internal = menus[len_i].attr("rel").split(";");
					////menus[len_i].parent().children(internal[1]).hide().siblings('a').removeClass(internal[0]);
					menus[len_i].parent().children(internal[1]).hide().siblings('span').removeClass(internal[0]);
				}
			}
			////$('a', menus[0]).removeClass(settings.parentMO);
			$('span', menus[0]).removeClass(settings.parentMO);
			if (timeout)
			{
				clearTimeout(timeout);
			}
		}
	};
})(jQuery);
*/


//Seach through all a tags
$("#nav_main > ul > li > a").each(function() {
		
    //Get the string of the link
    linkURL = $(this).attr("href");
		
		if(linkURL == undefined)
			return;
		
		var linkURLArray = linkURL.split("/");

    queryURL = document.location.pathname;
    var queryURLarray = queryURL.split("/");

    //If the link has a URL
    if (queryURL != "" && queryURLarray[2] != "search") {
        //And if the link is to the user Display page
        
        if (linkURLArray[2] != null)
					if (linkURLArray[2].match(queryURLarray[2]))
					{
							//Turn on the selected class
							$(this).attr("class", "selected");
					}
    }
});
//Seach through all a tags
$("#nav_main > ul > li > span").each(function() {
		

    //Get the string of the link
    linkURL = $(this).attr("href");
		
		if(linkURL == undefined)
			return;
		
		var linkURLArray = linkURL.split("/");

    queryURL = document.location.pathname;
    var queryURLarray = queryURL.split("/");

    //If the link has a URL
    if (queryURL != "" && queryURLarray[2] != "search") {
        //And if the link is to the user Display page
        
        if (linkURLArray[2] != null)
					if (linkURLArray[2].match(queryURLarray[2]))
					{
							//Turn on the selected class
							$(this).attr("class", "selected");
					}
    }
});

//Get rid of the 
$("#nav_main > ul > li > span").each(function(){
	$(this).attr("onmouseover", "");
});
