$j(document).ready(function(){

	
	if ($j('#navWrapper').length) {
		$j('#navWrapper').RYnavDropdown();
	};



// NAVIGATION #######################################################################################	
    // add "curent" class to selected top navigation (done via javascript so can cache rendering without varying by data)
    var selectedPath = window.location.pathname;
    $j('#navigation-primary > ul > li > a').each(function(){
		var navPath = $j(this).attr("href");
		navPath = navPath.substring(0,navPath.indexOf(".html")); //Remove extention
		
		if (navPath.lastIndexOf("/") > 0) {
		    navPath = navPath.substring(0,navPath.lastIndexOf("/")); //Only return first level path componant of tree
		}
		
		if (selectedPath.indexOf(navPath) == 0) {
		    $j(this).parent().addClass("current");
		    return false;
		}
    })
    


});


(function($j) {
	$j.fn.RYnavDropdown = function(options) {
		var opts = $j.extend({}, $j.fn.RYnavDropdown.defaults, options);		

		var browser = navigator.appVersion;
		if(browser.match('MSIE 6')){
			browser = 'ie6';
		};

		return this.each(function(i) {
			$navWrapper = $j(this);
			$navContainer = $j(this).find("#navigation-primary");
			$navList = $j(this).find("#navigation-primary > ul");
			$navContainer.after('<div id="wrapper-navigation-sub"><div id="navigation-sub" style="display: none;" class="navigation"></div></div>');
			$subContainer = $j(this).find('#navigation-sub');
			var content = [];
			var currentIndex = -1;
			var nextIndex;

			$navList.children('li').each(function(ii){
				if ($j(this).find('.inner').html() == null){
					content[ii] = '';
				} else {
					content[ii] = $j(this).find('.inner').html();
				}
				$subContainer.append('<div id="navigation-sub-' + ii + '" class="inner" style="display:none;">'+content[ii]+'</div>');
				
				if(content[ii].indexOf('<ul>') >= 0 || content[ii].indexOf('<UL>') >= 0){
					$j(this).addClass('active');
				} else {
					$j(this).addClass('inActive');
				};
			});
			// hover over buttons to show sub nav
			$navList.children('li.active').hoverIntent(function(e) {
				nextIndex = $navList.children('li').index(this);
				subChange();
				subShow();
			}, function(e) {
			});
			// hover off navigation area to hide sub nav
			$navWrapper.hoverIntent(function(e) {
			}, function(e) {
				subHide();
				currentIndex = -1;
			});
			
			// hover over buttons with no sub nav (inActive) to hide as well
			$navList.children('li.inActive').hoverIntent(function(e) {
				subHide();
				currentIndex = -1;
			}, function(e) {
			});
			
			// hover over search also closes nav
			$j('.quick-search').hoverIntent(function(e) {
				subHide();
				currentIndex = -1;
			}, function(e) {
			});
			// ## reused function
			function subShow(){		// show sub navigation
				$subContainer.animate({ height: 'show' }, {duration: opts.duration, easing: opts.easeInType});
			};
	
			function subHide(){				// hide sub navigation
				$subContainer.animate({ height: 'hide' }, {duration: opts.durationFast, easing: opts.easeInType});
				$subContainer.find('div.inner:eq('+currentIndex+')').animate({ delay: 0 }, opts.durationFast, function(){
					$j(this).css('display','none');
				})
				
			};
	
			function subChange(){	// swap visible content of sub nav
				if (currentIndex == -1 || currentIndex == nextIndex) {	// start state + if already current
				
					$subContainer.find('div.inner:eq('+nextIndex+')').css({'display':'block', 'opacity':1});

				} else {	
						// set default states
					$subContainer.find('div.inner:eq('+currentIndex+')').css({'display':'block', 'opacity':1});
					$subContainer.find('div.inner:eq('+nextIndex+')').css({'display':'block', 'opacity':0});
					
					// hide current
					$subContainer.find('div.inner:eq('+currentIndex+')')
						.animate({ opacity: 0}, 200, function(){
							$j(this).css('display','none');
						});
					
					// show new
					$subContainer.find('div.inner:eq('+nextIndex+')')
						.animate({ opacity: 0 }, 100).animate({ opacity: 1 }, 350);

				}
				currentIndex = nextIndex // set new index
			};
		});


		
	}; // end plugin RYnavDropdown


// end of closure
})(jQuery);

$j.fn.RYnavDropdown.defaults = {
	easeType: 'easeInOutCirc',	// examples at: http://www.robertpenner.com/easing/easing_demo.html
	durationFast: 400,
	duration: 800
};





