
// image pre-loading
(function($) {
    var cache = [];
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery);

// setup default animate flag
var fpblockIntervalId;
// set if mouse is outside
var fpblockMouseOutside = true;

$(window).load(function ()
{
    // routine to size boxes correctly when image loads
    $('div.boxgrid img').each(function() {
        // size boxgrid
        $(this).parent().height($(this).height());
        // also make sure caption is hiddenticker
        $(this).next('div.cover').css('top', $(this).height());
    });
    // parent-height height fixer
    $('.parent-height').each(function() {
        // need to make sure that the height of this div is the height of its parent
        $(this).height($(this).parent().height());
    });
});

$(document).ready(function()
{
    // login setup
    $('input.login-email').val( 'Email Address' );
    $('input.login-password').val( 'xxxxxxxx' );

    // now null email address when clicked
    $('input.login-email').focus(function() {
        if($(this).val() == 'Email Address') {
            $(this).val('');
        }
    });
    $('input.login-email').blur(function() {
        if($(this).val() == '') {
            $(this).val('Email Address');
        }
    });

    // also null password when clicked
    $('input.login-password').focus(function() {
        if($(this).val() == 'xxxxxxxx') {
            $(this).val('');
        }
    });
    $('input.login-password').blur(function() {
        if($(this).val() == '') {
            $(this).val('xxxxxxxx');
        }
    });

    // fancybox gallery
    $('.fancybox-gallery').fancybox({
        'transitionIn'	: 'none',
        'transitionOut'	: 'none',
        'titlePosition' : 'over',
        'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
            if(title)
            {
                    return '<span id="fancybox-title-over">' + title + '</span>';
            }
        }
    });

    // need to store our menu original heights
    $('#ddmenu ul li ul').each(function() {
        // store the original height
        $(this).data('originalHeight', $(this).height());
    });

    // need to setup our dropdown menu for top level
    $('#ddmenu ul li').hover(
        function()
        {
            // must make sure all others are closed
            ddmenu_expand($('ul', this));
        },
        function()
        {
            ddmenu_hide($('ul', this));
        }
    );

    // sliding image captions
    // standard "full" caption from hidden to visibile
    $('.boxgrid.captionfull').hover(
        function(){
            $(".cover", this).stop().animate(
                {
                    top:    ($(this).children('img').height() - $(this).children('div.boxcaption').height())
                },
                {
                    queue:      false,
                    duration:   160
                }
            );
        },
        function() {
            $(".cover", this).stop().animate(
            {
                top:        $(this).children('img').height()
            },
            {
                queue:      false,
                duration:   160
            }
        );
    });

    // make sure that any block images are pre-loaded
    $('.block-image').each(function() {
        $.preLoadImages($(this).attr('pcms-image'));
    });

    // make first item active
    fpblockMakeActive($('.fpblock-nav li').first());
    // start frontpage animation
    fpblockStartAnimate();
    // register hover state
    $('.fpblock').hover(
        function() {
            fpblockMouseOutside = false;
            clearInterval(fpblockIntervalId);
            fpblockIntervalId = null;
        },
        function() {
            fpblockMouseOutside = true;
            setTimeout('fpblockStartAnimate()', 2500);
        }
    );

    // frontpage rollovers
    $('.fpblock-nav li').click(
        function() {
            // make this class active
            fpblockMakeActive($(this));
        }
    );
});

function fpblockStartAnimate()
{
    // only run animate if interval is null
    if(fpblockIntervalId == null && fpblockMouseOutside)
    {
        fpblockIntervalId = setInterval('fpblockAnimate()', 7500);
    }
}

// start fpblock animation
function fpblockAnimate()
{
    // activate the next label
    if($('.fpblock-nav li.active').next().html() == null)
    {
    	fpblockMakeActive($('.fpblock-nav li').first());
    }
    else
    {
    	fpblockMakeActive($('.fpblock-nav li.active').next());
    }
}

// fpblock make active function
function fpblockMakeActive(obj)
{
    // are WE active?
    if(!obj.hasClass('active'))
    {
        // update the navigation
        $('.fpblock-nav li').removeClass('active');
        obj.addClass('active');
        //  now animate the image
        $('.fpblock a.fpblock-image').stop().animate( { opacity: 0 }, 250, function() {
            // now update the image & link
            $('.fpblock a.fpblock-image').attr('href', obj.attr('pcms-link'));
            $('.fpblock a.fpblock-image img').attr('src', obj.attr('pcms-image'));
            $('.fpblock a.fpblock-image img').attr('alt', obj.attr('pcms-title'));
            $('.fpblock a.fpblock-image img').attr('title', obj.attr('pcms-title'));
            $('.fpblock a.fpblock-image').animate( { opacity: 1 }, 250);
        });
        // and the text
        $('.fpblock div.fpblock-text h2').html(obj.attr('pcms-title'));
        $('.fpblock div.fpblock-text p').html(obj.attr('pcms-subtitle'));
    }
}

// ddm function
function ddmenu_expand(obj)
{
    // set height to 0 and show
    obj.height(0);
    obj.show();
    // now animate out to original height
    obj.animate( { height: obj.data('originalHeight') }, { queue: false, duration: 200, easing: 'easeOutQuad'} );
}

function ddmenu_hide(obj)
{
    // and hide the menu
    obj.animate( {height: "hide"}, { queue: false, duration: 200 } );
}
