//
// 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.
//
jQuery(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
		jQuery('#navigation').css('top','-42px');
	}

	// global vars
	var at_id = jQuery('#navigation li.active').attr('id');
	var at_classes = jQuery('#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
	jQuery('#navigation li.tab').bind('mouseenter', function(){

		// add hover to tab
		jQuery(this).addClass('hover');
		jQuery(this).find('a:first-child').addClass('hover');

		var ht_id = $(this).attr('id');
		var ht_classes = jQuery(this).attr('class');
		var regexPattern = /(colour[a-z0-9]+)/i;
		var ht_colour = ht_classes.match(regexPattern);

		// change subnav colour channel
		jQuery('div#navColourBlock div').attr('class', ht_colour[0]);
		jQuery(this).find('div.level2').css('visibility','visible');

		// no sub menu
		var noSubTab = jQuery('li.hover').hasClass('colour00');
		if (noSubTab){
			jQuery('div.level2').css('visibility','hidden');
		}
	});

	// mouseleave a tab
	jQuery('#navigation li.tab').bind('mouseleave', function(){
		jQuery('#navigation').find('.hover').removeClass('hover');
		jQuery('div.level2').css('visibility','hidden');
	});

	// mouseleave navigation
	jQuery('#navigation').bind('mouseleave', function(){
		// hide subnav
		jQuery('div.level2').css('visibility','hidden');
		// restore active subnav
		if (at_id !== 'cat_more'){
			jQuery('li#'+at_id).find('div.level2').css('visibility','visible');
		} else {

		}
		// remove hover from tab
		jQuery('#navigation').find('.hover').removeClass('hover');
		// reset nav colour
		jQuery('div#navColourBlock div').attr('class', at_colour[0]);
	});

	// First run

	// hide 2nd level navs
	jQuery('div.level2').css('visibility','hidden');
	// set colour
	jQuery('div#navColourBlock div').attr('class', at_colour[0]);
	// reveal nav
	jQuery('li.'+at_colour[0]+' div.level2').css('visibility','visible');

	// tweaks
	jQuery('div.level2 ul li:last-child a').find('span').css('border','none');

	// set up alternate dropdown
	jQuery('#navigation li#cat_more div.level2').attr('id','cat_more');

});

$(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.val() == 'Type your search - e.g. hotels, plumbers, restaurants') {
		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');
			}
		});
});
