/*** 
    Please use jQuery instead of $ in global applications
    in order to remove conflicts with other non-jQuery
    JavaScript AJAX libraries
***/
    
jQuery(function() {
	jQuery('#mainNav > .tab').each(function() {
		var tolerance = 150;
		var tab = jQuery(this);
		tab.tolerantHover(function() {
			jQuery(this).addClass('highlight');
		},function() {
			jQuery(this).children('ul').addClass('hidden');
			jQuery(this).removeClass('highlight');
		}, tolerance);
		
		var down_arrows = tab.find('> a > .arrow');
		var right_arrows = tab.find('.menu > li > .arrow');
		down_arrows.add(right_arrows).tolerantHover(function() {
			jQuery(this).addClass('highlight');
		},function() {
			jQuery(this).removeClass('highlight');
		}, tolerance);
		
		down_arrows.click(function(event) {
			event.preventDefault();
			tab.addClass('highlight');
			tab.children('ul').toggleClass('hidden');
			tab.find('.submenu').addClass('hidden');
		});
		
		tab.find('.menu').each(function() {
			var menu_items = jQuery(this).children('li'); 
			menu_items.each(function() {
				var arrow = jQuery(this).find('.arrow');
				var submenu = jQuery(this).find('.submenu');
				arrow.click(function(event) {
					event.preventDefault();
					if(submenu.hasClass('hidden')) {
						menu_items.find('.submenu').addClass('hidden');
						submenu.removeClass('hidden');
					}
					else {
						menu_items.find('.submenu').addClass('hidden');
					}
				});
			});
		});
		
		tab.children('ul').tolerantHover(function() {
			tab.children('a').addClass('nofocus');
		}, function() {
			tab.children('a').removeClass('nofocus');
		}, tolerance);
		
		tab.find('li:has(.submenu)').tolerantHover(function() {
			jQuery(this).addClass('highlight');
		},function() {
			jQuery(this).removeClass('highlight');
		}, tolerance)
	});
});

jQuery.fn.tolerantHover = function(onHover,offHover,tolerance) {
	jQuery(this).each(function() {
		var hover = false, self = jQuery(this);
		self.hover(function() {
			hover = true;
			onHover.apply(this);
		}, function(){
			hover = false;
			setTimeout(function() {
				if(!hover) {
					offHover.apply(self);
				};
			}, tolerance);
		});
	});
	return this;
}

//############ ROS - focus/blur for search field ########################
function focusForm(me) {
	var theElements = document.getElementsByTagName("input");
	for (var i=0; i<theElements.length; i++) {
		if(theElements[i].className == 'searchText'){
		
			theElements[i].onfocus = function() {
			if(this.value == me)
			{this.value = ''}
			}
	
			theElements[i].onblur = function() {
			if(this.value.replace(' ','') == '')
			{this.value = me}
			}
		}
	}
}
//############ ROS - vaildation for search field ########################
function validateForm(e,m,n,me)
{
	var theForm = document.getElementById(n);
	if(theForm.Ntt.value == me || theForm.Ntt.value.replace(' ','') == "") {
		alert(m);
		theForm.Ntt.focus();
    	if (e && e.preventDefault) {
    		e.preventDefault(); // DOM style
        }
        return false; // IE style
	}
    else
        return true;
}

// ############ SEARCH - INPUT EMPTY VALIDATION ########################
function validateForm(e)
{
	var theForm = document.getElementById("search_form");
	if(theForm.Ntt.value == theForm.Ntt.defaultValue || theForm.Ntt.value.replace(' ','') == "") {
		alert("Please enter a search term");
		theForm.Ntt.focus();
    	if (e && e.preventDefault) {
    		e.preventDefault(); // DOM style
        }
        return false; // IE style
	}
    else
        return true;
}