/* Found here: http://sedition.com/perl/javascript-fy.html */
function shuffleArray(myArray)
{
	var i = myArray.length;
	
	if (i == 0)
	{
		return false;
	}
	
	while (--i)
	{
		var j = Math.floor(Math.random() * (i + 1));
		
		var tempi = myArray[i];
		var tempj = myArray[j];
		
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
}


/* Really lame image preloader */
jQuery.preloadImages = function(images)
{
	for(var i = 0; i < images.length; i++)
	{
		jQuery('<img>').attr('src', images[i]);
	}
}


/* Subtitle animation */
jQuery.swapSubtitles = function(subtitleClass, back)
{
	back = (typeof(back) != 'undefined' ? back : false); // I want a default arg
	
	$('#subtitle h2').stop();
	
	if (!back)
	{
		$('#subtitle h2').fadeOut(50, function()
		{
			$('#subtitle h2').attr('class', subtitleClass);
			
			$('#subtitle h2').css('width', 0);
			$('#subtitle h2').animate({"opacity": 0 }, 0);
			
			$('#subtitle h2').animate({
				opacity: 1,
				width: "415px"
			}, 300, function (x, t, b, c, d) {return c*((t=t/d-1)*t*t + 1) + b;});
		});
	}
	else // pull the old one back in from the other direction.
	{
		var origClass = $('div#subtitle').attr('class');
		
		$('#subtitle h2').fadeOut(50, function()
		{
			$('#subtitle h2').attr('class', origClass);
			$('#subtitle h2').css('width', 500);
			
			$('#subtitle h2').animate({"opacity": 0}, 0);
			
			$('#subtitle h2').animate({
				opacity: 1,
				width: "415px"
			}, 300, function (x, t, b, c, d) {return c*((t=t/d-1)*t*t + 1) + b;});
		});
		
	}
};

/* Animate the nav button hover transitions. */
$(document).ready(function()
{
	$('#buttonNav li a').hover(function()
	{
		var fade = $('img', this);
		fade.fadeIn(250);
		
		$.swapSubtitles($(this).parent().attr('class'));
	}, function()
	{
		var fade = $('img', this);
		fade.fadeOut(250);
		
		/* The true here is for the "go back" function. */
		$.swapSubtitles($(this).parent().attr('class'), true);
	});
});

// rounded corners
//$(document).ready(function()
//{
//	$('#content #resources p').corner({
//	tl: { radius: 12 },
//	tr: { radius: 12 },
//	bl: { radius: 12 },
//	br: { radius: 12 },
//	antiAlias: true,
//	autoPad: true,
//	validTags: ["div"] });
//});

$(document).ready(function()
{
	var logoImages = ['/img/intro-logo-blue.gif', '/img/link-archive-down.gif', '/img/link-archive-fr-down.gif', '/img/link-archive-fr-over.gif', '/img/link-archive-fr.gif', '/img/link-archive-over.gif', '/img/link-archive.gif', '/img/link-contactUs-down.gif', '/img/link-contactUs-fr-down.gif', '/img/link-contactUs-fr-over.gif', '/img/link-contactUs-fr.gif', '/img/link-contactUs-over.gif', '/img/link-contactUs.gif', '/img/link-partners-down.gif', '/img/link-partners-fr-down.gif', '/img/link-partners-fr-over.gif', '/img/link-partners-fr.gif', '/img/link-partners-over.gif', '/img/link-partners.gif', '/img/link-signUp-down.gif', '/img/link-signUp-fr-down.gif', '/img/link-signUp-fr-over.gif', '/img/link-signUp-fr.gif', '/img/link-signUp-over.gif', '/img/link-signUp.gif', '/img/video-down.gif', '/img/video-link-fr-down.gif', '/img/video-link-fr-over.gif', '/img/video-link-fr.gif', '/img/video-link.gif', '/img/video-over.gif', '/img/merchandise-down.gif', '/img/merchandise-link-fr-down.gif', '/img/merchandise-link-fr-over.gif', '/img/merchandise-link-fr.gif', '/img/merchandise-link.gif', '/img/merchandise-over.gif', '/img/property-down.gif', '/img/property-link-fr-down.gif', '/img/property-link-fr-over.gif', '/img/property-link-fr.gif', '/img/property-link.gif', '/img/property-over.gif'];
	$.preloadImages(logoImages);
});

// set up mirimichi video embedding
//$(document).ready(function() {
	//$('#video a').colorbox({iframe:true, width:510, height:600, scrolling:false});
//});

// new-window links
$(document).ready(function() {
	var openInNewWindow = function() {
		window.open($(this).attr('href'), '')
		return false;
	};
	
	$('#communities a').click(openInNewWindow)
	$('a.external').click(openInNewWindow)
});

// form validation
$(document).ready(function() {
	$('form').validate({onkeyup: false}) // onkeyup makes us loose focus in Safari
})


