
// 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 hidden
		$(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() - 75) },{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'));
	});
	
	// frontpage animation
	fpblockStartAnimate();
	// register hover state
	$('.fpblock').hover(
		function() {
			fpblockMouseOutside = false;
			clearInterval(fpblockIntervalId);
			fpblockIntervalId = null;
		},
		function() {
			fpblockMouseOutside = true;
			setTimeout('fpblockStartAnimate()', 5000);
		}
	);
	
	// frontpage rollovers
	$('.fpblock-label').hover(
		function() {
			// make this class active
			fpblockMakeActive($(this));
		},
		function() {
			// do nothing
		}
	);
	// frontpage clickers
	$('.fpblock-label').click(function() {
		window.location.href = $(this).children('strong').children('a').attr('href');
	});
	
});

function fpblockStartAnimate()
{
	// only run animate if interval is null
	if(fpblockIntervalId == null && fpblockMouseOutside)
	{
		fpblockIntervalId = setInterval('fpblockAnimate()',5000);
	}
}

// start fpblock animation
function fpblockAnimate()
{
	// activate the next label
    if($('.fpblock-label.active').next().html() == null)
    {
    	fpblockMakeActive($('.fpblock-label').first());
    }
    else
    {
    	fpblockMakeActive($('.fpblock-label.active').next());
    }
}

// fpblock make active function
function fpblockMakeActive(obj)
{
	// are WE active?
	if(!obj.hasClass('active'))
	{
		// find an existing active class
		$('.fpblock-label').removeClass('active');
		obj.addClass('active');
		// set parent background image and link
		$('.fpblock-image-active').stop().animate( { opacity: 0 }, 500, function() {
			$('.fpblock-image-active').attr('src', obj.attr('pcms-image'));
			$('.fpblock-image-active').attr('alt', obj.children('strong').children('a').html());
			$('.fpblock-image-active').attr('title', obj.children('strong').children('a').html());
			$('.fpblock-image-active').animate( { opacity: 1 }, 500);
		});
		$('.fpblock').children('a').attr('href', obj.children('strong').children('a').attr('href'));
	}
}

// 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: 600, easing: 'easeOutQuad'} ); 
}

function ddmenu_hide(obj)
{
	// and hide the menu
	obj.animate( {height: "hide"}, { queue: false, duration: 600 } ); 
}
