     
jQuery(function($) {
//Tooltips
	var tip;
	$(".tip_trigger").hover(function(){

		//Caching the tooltip and removing it from container; then appending it to the body
		tip = $(this).find('.tip').remove();
		$('body').append(tip);

		tip.fadeIn(500); //Show tooltip

	}, function() {

		tip.hide().remove(); //Hide and remove tooltip appended to the body
		$(this).append(tip); //Return the tooltip to its original position

	}).mousemove(function(e) {
	//console.log(e.pageX)
		  var mousex = e.pageX + 20; //Get X coodrinates
		  var mousey = e.pageY + 20; //Get Y coordinates
		  var tipWidth = tip.width(); //Find width of tooltip
		  var tipHeight = tip.height(); //Find height of tooltip

		 //Distance of element from the right edge of viewport
		  var tipVisX = $(window).width() - (mousex + tipWidth);
		  var tipVisY = $(window).height() - (mousey + tipHeight);

		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
			tip.css({  top: mousey, left: mousex });
		} else {
			tip.css({  top: mousey, left: mousex });
		}
	});
 
    // set nav columns to equal heights
    function setEqualHeight(columns) {
        var tallestcolumn = 0;
        columns.each(function() {
            currentHeight = $(this).height();
            if (currentHeight > tallestcolumn) { tallestcolumn  = currentHeight; }
        });
        
        columns.height(tallestcolumn + 40);
    }
    
    setEqualHeight($("ul.secondary-nav li > ul"));
 
    // begin mega menu   
    function addMega(){
        $('.secondary-nav').clone(false).appendTo('#site-sections-nav').hide().fadeIn(500);
        
    }
    function removeMega(){
        $('#site-sections-nav .secondary-nav').fadeOut(500, function(){
            $(this).remove();
        });        
    }
    var megaConfig = {    
        interval: 100,
        sensitivity: 7,
        over: addMega,
        timeout: 500,
        out: removeMega
        
    };
    
    $('#site-sections-nav').hoverIntent(megaConfig);
    
        
    $('span.button a').mousedown(function(){
        $(this).parent().animate({bottom : '+=2px'},50);
    }).mouseup(function(){
        $(this).parent().animate({bottom : '-=2px'},50);
    });
    
    // smooth scrolling
    $.localScroll({
        stop: true
    });


    // begin sub menu   
    function showSubMenu(){
        $(this).addClass('sfHover');
        $(this).children('ul.sub-menu').fadeIn();
    }
    
    function hideSubMenu(){
        $(this).removeClass('sfHover');
        $(this).children('ul.sub-menu').fadeOut();
   }
    
    var subMenuConfig = {    
        interval: 100,
        sensitivity: 7,
        over: showSubMenu,
        timeout: 500,
        out: hideSubMenu
    };
    
    // select only ul's that have sub-menu class
    $('li.menu-item').has('ul.sub-menu').hoverIntent(subMenuConfig);

    
});
