﻿// Interval between transitions of splash images (milliseconds)
var splashInterval = 5000;

$(function () {

    // Disable top-level tab links for any item that has children
    $('#header-nav>li:has(ul)>a').bind('click', function () { return false; });

    // remove the vertical divider from the last menu item
    $("#header-nav>li:last").css({ 'background-image': 'none' });
    $("#header-nav #current").prev('li').css({ 'background-image': 'none' });


    var navConfig = {
        interval: 75,
        sensitivity: 5,
        over: function () {
            var li = $(this);
            li.addClass('tab')
              .prev('li').css({ 'background-image': 'none' });

            Cufon.refresh('#header-nav > li > a');
        },
        timeout: 150,
        out: function () {
            var li = $(this);
            li.removeClass('tab')
              .prev('li').css({ 'background-image': 'url(/content/img/site/bg-nav-divider.gif)' });

            Cufon.refresh('#header-nav > li > a');
        }
    };

    $('#header-nav>li').hoverIntent(navConfig);

    // Pre-load any splash images
    $.preloadImages = function () {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }

    for (var x = 0; x < splashImages.length; x++) {
        $.preloadImages('/content/img/lib/splash/' + splashImages[x] + '.jpg');
    }

    // Deal with any header images which need to rotate
    if (splashImages.length > 1) {
        window.setTimeout("SplashEffect(1, 'fadeout')", splashInterval);
    }

});

function SplashEffect(newOpacity, direction) {

    // Min opacity to fade to
    var minOpacity = 0.1;
    // Interval in milliseconds between changes in opacity
    var fadeInterval = 10;
    // Change in opacity after each interval - IE seems to be much slower at transitions than any other browser
    var opacityStep = ($.browser.msie) ? 0.02 : 0.01;

    $('#splash').css('opacity', newOpacity);

    if (direction == 'fadeout') {
        newOpacity -= opacityStep;
    } else {
        newOpacity += opacityStep;
    }

    if (newOpacity <= minOpacity) {
        currentSplashIndex++;
        if (currentSplashIndex >= splashImages.length) 
            currentSplashIndex = 0;
        $('#splash').css('background-image', 'url(/content/img/lib/splash/' + splashImages[currentSplashIndex] + '.jpg)');
        direction = 'fadein';
    }

    if (newOpacity < 1) {
        window.setTimeout("SplashEffect(" + newOpacity + ", '" + direction + "')", fadeInterval);
    } else {
        window.setTimeout("SplashEffect(1, 'fadeout')", splashInterval);
    }
}


