//
// This javascript controls the main navigation menu in global_navigation.jsp
//
// at = active tab
// ht = hover tab
//
// Important! The events used below are abound to mouseenter and mouseleave to avoid 'event bubbling'. This should
// also stop the browser parsing the functions too often.
//
$(function(){
	
	// platform detect
	var userOSAgent = navigator.userAgent.toLowerCase();
	// Figure out what browser is being used
	jQuery.platform = {
		mac: /mac/.test(userOSAgent),
		osx: /mac os x/.test(userOSAgent),
		win: /win/.test(userOSAgent),
		linux: /linux/.test(userOSAgent)
	};
	if (jQuery.platform.mac && jQuery.browser.mozilla) {
		// tweak the position of the tabs
		$('#navigation').css('top','-42px');
	}
	
	// global vars
	var at_id = $('#navigation li.active').attr('id');
	var at_classes = $('#navigation li.active').attr('class');
	if (at_classes !== null){
		var regexPattern = /(colour[a-z0-9]+)/i;
		var at_colour = at_classes.match(regexPattern);
	} else {
		at_classes = 'colour00';
	}
	
	// mouseenter a tab
	$('#navigation li.tab').bind('mouseenter', function(){
		
		// add hover to tab
		$(this).addClass('hover');
		$(this).find('a:first-child').addClass('hover');
		
		var ht_id = $(this).attr('id');
		var ht_classes = $(this).attr('class');
		var regexPattern = /(colour[a-z0-9]+)/i;
		var ht_colour = ht_classes.match(regexPattern);
		
		// change subnav colour channel
		$('div#navColourBlock div').attr('class', ht_colour[0]);
		$(this).find('div.level2').css('visibility','visible');
		
		// no sub menu
		var noSubTab = $('li.hover').hasClass('colour00');
		if (noSubTab){
			$('div.level2').css('visibility','hidden');
		}
	});
	
	// mouseleave a tab
	$('#navigation li.tab').bind('mouseleave', function(){
		$('#navigation').find('.hover').removeClass('hover');
		$('div.level2').css('visibility','hidden');
	});
	
	// mouseleave navigation
	$('#navigation').bind('mouseleave', function(){
		// hide subnav
		$('div.level2').css('visibility','hidden');
		// restore active subnav
		if (at_id !== 'cat_more'){
			$('li#'+at_id).find('div.level2').css('visibility','visible');
		} else {
			
		}
		// remove hover from tab
		$('#navigation').find('.hover').removeClass('hover');
		// reset nav colour
		$('div#navColourBlock div').attr('class', at_colour[0]);
	});
	
	// First run
	
	// hide 2nd level navs
	$('div.level2').css('visibility','hidden');
	// set colour
	$('div#navColourBlock div').attr('class', at_colour[0]);
	// reveal nav
	$('li.'+at_colour[0]+' div.level2').css('visibility','visible');
	
	// tweaks
	$('div.level2 ul li:last-child a').find('span').css('border','none');
	
	// set up alternate dropdown
	$('#navigation li#cat_more div.level2').attr('id','cat_more');
	
});

//AC1 adding this in to switch on the font styles in the search box for teaser text
//$(function() {
 //var what = $("#what");
 ////if the searchbar doesn't exist simply exit this function.
 //if(what.length == 0) {
 // return false;
// }
 //if(what.parents('#masthead').hasClass('mastheadmini')) {
 // //do not apply this mechanism to minibar
 // return false;
// }
 //if(what.val().length == 0) {
 // what.addClass('inactiveWhat');
 // what.val('Type your search - e.g. hotels, plumbers, restaurants');
 //}
// what.focus(function() {
 //  var $this = $(this);
 //  if($this.hasClass('inactiveWhat')) {
 //   $this.val('').removeClass('inactiveWhat');
//   }
//  })
//  .blur(function() {
//   var $this = $(this);
//   if($this.val() == '') {
//    $this.val('Type your search - e.g. hotels, plumbers, restaurants').addClass('inactiveWhat');
  // }
//  });
//});
