(function($) {
	$(document).ready(function() {
		Nav = {
		
			def: null,
			url: null,
			
			update: function(el, section) {
				this.set(this.get(section));
			},
			
			
			reset: function(el, section) {
				if ($(el).parents('#navigation').length === 0) {
					this.set(this.def);
				};
			},
			
			
			init: function() {
				this.url = window.location.href;
				$('#level1Nav li').each(function() {
					if ((Nav.url.indexOf('sitemap') !== -1 ||
						Nav.url.indexOf('links') !== -1 ||
						Nav.url.indexOf('terms') !== -1) && this.id === 'about') {
		                $('a', this).eq(0).addClass('active');
					}
					else if (Nav.url.indexOf('contact') !== -1 && this.id === 'locations') {
						$('a', this).eq(0).addClass('active');
					}
					else if (Nav.url.indexOf('gallery') !== -1 && this.id === 'products') {
						$('a', this).eq(0).addClass('active');
					}
					else if (Nav.url.indexOf('privacy') !== -1 && this.id === 'about') {
						$('a', this).eq(0).addClass('active');
					}
					
					else if (Nav.url.indexOf(this.id) !== -1) {
						$('a', this).eq(0).addClass('active');
		            };
				});
		
				this.def = this.get(this.url);
				this.set(this.def);
			},
			
			
			get: function(s) {
				return $('.hidemenu').filter(function() {
					if (this.id.split('subNav-').pop() === 'about' &&
						(s.indexOf('sitemap') !== -1 || s.indexOf('careers') !== -1 ||
						 s.indexOf('links') !== -1 ||  s.indexOf('terms') !== -1)) {
							return true;
					} else if (this.id.split('subNav-').pop() === 'locations' && s.indexOf('contact') !== -1) {
						return true;
					} 
					else if (this.id.split('subNav-').pop() === 'products' && s.indexOf('gallery') !== -1) {
						return true;
					} 
					else if (this.id.split('subNav-').pop() === 'about' && s.indexOf('privacy') !== -1) {
						return true;
					} 
					else {
						return s.indexOf(this.id.split('subNav-').pop()) !== -1;
					}
				}).clone(true);
			},
			
			
			set: function(el) {
				return $('#menu').empty().append($(el))
					.children().removeClass('hidemenu');
			}
		
		};
		

		$('#level1Nav a').bind('mouseover', function() {
            Nav.update.call(Nav, this, $(this).parent().attr('id'));			
		});
		
		
		$('body').bind('mouseover', function(e) {
			Nav.reset.call(Nav, e.target);
			return false;			
		});
		
		
		Nav.init();	
	});
})(jQuery);
