$(function() 
{
    /* Add hoverintent to any layered nav/popout elements */
    
    // For action links at top of pages
	$("ul#header-action-links li").hoverIntent({
		interval: 100, 
		over: drops_show, 
		timeout: 200, 
		out: topdrops_hide
	});
	$("ul#header-action-links li").addClass('with-js');
	
	// For sub navigation menus
	$("ul#header-navigation-menu li.has-sub-navigation").hoverIntent({
		interval: 100, 
		over: drops_show, 
		timeout: 200, 
		out: drops_hide
	});
	$("ul#header-navigation-menu li.has-sub-navigation").addClass('with-js');
	$("ul#header-navigation-menu li.has-sub-navigation").removeClass('has-sub-navigation');
	
	// For "super" sub navigation menus
    $("ul#header-navigation-menu li.has-super-sub-navigation").hoverIntent({
		interval: 100, 
		over: drops_show, 
		timeout: 200, 
		out: drops_hide
	});
	$("ul#header-navigation-menu li.has-super-sub-navigation").addClass('with-js');
	$("ul#header-navigation-menu li.has-super-sub-navigation").removeClass('has-super-sub-navigation');
	
	// For popout "pain points"
    $("div.popout-list ul li").hoverIntent({
		interval: 200, 
		over: drops_show, 
		timeout: 400, 
		out: drops_hide
	});
	$("div.popout-list ul li").addClass('with-js');
	
	/* Hook up sliding areas */
	
    $("div.tabbed-hero").codaSlider({
        tabsNavAdditionalClass: "hero-navigation",
        useCookies: true,
        cookieName: "IndustryPreference"
    });
    
    $("div.tabbed-content").codaSlider({
        tabsNavAdditionalClass: "tabbed-content-navigation",
        tabsBefore: true,
        useCookies: false
    });
    
    // Hook up any Lightbox links
    $("div.lightbox a:first").lightBox({
        imageBlank: '/images/lightbox/lightbox-blank.gif',
        imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
        fixedNavigation: true
    });
    
    $("div.lightbox a:last").lightBox({
        imageBlank: '/images/lightbox/lightbox-blank.gif',
        imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
        fixedNavigation: true
    });
    
     $("a.lightbox").lightBox({
        imageBlank: '/images/lightbox/lightbox-blank.gif',
        imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
        fixedNavigation: true
    });   
    
    /* Hook up the search boxes for autocomplete */
    
    $("div.header-action-dropdown input.search-suggest").autocomplete(searchwords).result(
        function(event, item) { location.href = "/Search.aspx?searchtext=" + item + "&searchmode=AnyWords"; }
    );
    
    $("div.form-container input.search-suggest").autocomplete(searchwords);
    
    /* Simple click events to print/bookmark the page */
    
    $("a.print").click(function(){
        if (!window.print) { return; }
        else { window.print(); return false; }
    });
    
    $("a.bookmark").click(function(){
        if (window.sidebar) { window.sidebar.addPanel(this.title, this.href, ""); }
        else if (window.external) { window.external.AddFavorite(this.href, this.title); }
        else if (window.opera) { this.attr("rel", "sidebar"); }
        else { return; }
    });
    
    /* Make homepage boxes equal heights */
    if(typeof document.body.style.maxHeight != "undefined") // Don't even bother in IE6
    {
        $(".equalise").equalHeights();
        $(".equalise").find(".module").css("position", "relative"); // To prepare for orange button positioning 
        
        $(".equalise").find(".rounded-content").css("height", function(index, value)
        {
            var pHeight = $(this).parent().height();
            var sHeight = $(this).siblings(".rounded-header").height();
            return val = pHeight - sHeight - 12; // Entire parent height minus preceding (header) div height minus margin/padding (10 + 2)
        }); // Make sure content div doesn't overflow, so calculate and fix height
        
        $(".equalise").find(".module-action-link").each(function()
        {
            $(this).wrap('<div class="module-action-link-wrapper" style="position:absolute;right:10px;bottom:10px;" />');
            $(this).closest(".module").append($(this).parent())
        }); // Wrap all the orange buttons in a div and move outside the content area and abs position 10px from bottom/right
        
        $(".equalise .rounded-content").find("shape").remove(); // Removes any unwanted PNG fix shapes from viewport
    }
});

/* Helper functions for hoverintent */

function drops_show(){ $(this).addClass('show'); $(this).removeClass('with-js'); }
function drops_hide(){ $(this).removeClass('show'); $(this).addClass('with-js'); }

function topdrops_hide()
{ 
    if (!$(".ac_results").is(":visible") || $(".ac_results").is(":hidden"))
    {
        $(this).removeClass('show'); 
        $(this).addClass('with-js'); 
    }
}

/* Function to perform height equally for homepage boxes */
(function(jQuery)
{
    jQuery.fn.equalHeights = function()
    {
        var maxheight = 0;
        jQuery(this).children().each(function()
        {
            maxheight = (jQuery(this).height() > maxheight) ? jQuery(this).height() : maxheight;
        });
        jQuery(this).children().css("height", maxheight);
    }
})(jQuery);