// Handles the navigation show and hide

// This is used to overcome issue in IE w.r.t setting cookies from Javascript.
var nav_maps = {
	'/articles/':'nav_articles',
	'/quizzes/':'nav_articles',
	'/':'nav_articles',
	'/blogs/':'nav_blogs',
	'/forums/':'nav_forums',
	'/groups/':'nav_groups',
	'/training/':'nav_training',
	'/exercise-summary/':'nav_training',
	'/self-assessment/':'nav_training',
	'/training-plans/':'nav_training',
	'/weight-tracker/':'nav_training',
	'/pace-tools/':'nav_training',
	'/events-calendar/':'nav_events',
	'/charity-challenges/':'nav_events',
	'/activity-listings/':'nav_events',
	'/maps/':'nav_maps',
	'/photos-and-videos/':'nav_photos',
	'/photos/':'nav_photos',
	'/videos/':'nav_photos',
	'/shop/':'nav_shop',
	'/find-my-nearest/':'fmn',
	'/about-us/':'nav_aboutus',
	'/site-map/':'nav_aboutus',
	'/site-tours/':'nav_aboutus',
	'/advertise/':'nav_aboutus',
	'/contact-us/':'nav_aboutus',
	'/help/':'nav_aboutus',
	'/daily-news/':'nav_dailynews',
	'/competitions/':'nav_dailynews',
	'/newsletter/':'nav_dailynews',
	'/terms-of-use/':'nav_terms',
	'/shop-terms-of-use/':'nav_terms',
	'/privacy-policy/':'nav_terms',
	'/mybuzz/':'nav_mybuzz',
    '/profile/':'nav_mybuzz',
	'/users/':'nav_mybuzz',
	'/messages/':'nav_mybuzz',
	'/premium/':'nav_mybuzz',
	'/friend-finder/':'nav_mybuzz',
	'/scrapbook/':'nav_mybuzz'
}

var search_nav_maps = {
	'/search/47/': '/articles/',
	'/search/50/': '/recipes/',
	'/search/4/': '/mybuzz/',
	'/search/55/': '/blogs/',
	'/search/63/': '/groups/',
	'/search/193/': '/forums/',
	'/search/74/': '/photos/',
	'/search/88/': '/videos/',
	'/search/121/': '/maps/',
	'/search/118/': '/maps/',
	'/search/126/': '/articles/',
	'/search/95/': '/events-calendar/',
	'/search/187/': '/shop/'
}

var cls = null;
var cur_parent_path = null;

var navs = '.nav_articles, .nav_forums, .nav_blogs, .nav_groups, .nav_training, .nav_events, .nav_maps, .nav_photos, .nav_shop, .fmn, .nav_aboutus, .nav_dailynews, .nav_terms, .nav_mybuzz';

jQuery(document).ready(function($){
	var split_navs = navs.split(',');
	
	cur_parent_path = window.location.pathname.match(/\/.*?\//);
	
	try{
		cls = nav_maps[cur_parent_path];
	} catch(e){
		cls = nav_articles;
	}
	
	setBySearchPath();
	$('.'+cls).attr('class',cls+' active');
	
	$(navs).hover(function(){
		// clearTimeout($(this).data('timeoutId'));
		
		// in case we hover over an active class
		hv_cls = $(this).attr('class').replace(/active/g,'');
				
		$(navs).each(function(){
			cls = $(this).attr('class').replace(/active/g,'');
			$(this).attr('class',cls);
		});
			
		// set this active
		$(this).attr('class','active '+hv_cls);
		
	})
	
	$('#topnav').mouseleave(function(){
		setTimeout("revert()", 750);
	});
});

function revert() {
	$(navs).each(function(){
		cls = $(this).attr('class').replace(/active/g,'');
		$(this).attr('class',cls);
	});
	
	setBySearchPath();
	if (!cur_parent_path) cur_parent_path = '/';
	if (nav_maps[cur_parent_path]==undefined) cur_parent_path = '/';
	
	$('.'+nav_maps[cur_parent_path]).attr('class',nav_maps[cur_parent_path]+' active');
}

function setBySearchPath() {
	if (cur_parent_path) {
		if (cur_parent_path[0] == '/search/') {
			var search_cls = window.location.pathname.match(/\/search\/[0-9]+\//);
		}
		
		if (search_cls) {
			cls = nav_maps[search_nav_maps[search_cls]];
			cur_parent_path = search_nav_maps[search_cls];
		}
	}
}

