$(function() {

	// Animation: Sweep (Square Grid)

	function animateSweep1()
	{
		var square = $(this).closest('div.square');

		if ( square.is(':last-child') ) 
		{
			$('div.sweep div.square:first div.focus').fadeOut(400, animateSweep2);
		} else {
			square.next('div.square').find('div.focus').fadeIn(200, animateSweep1);
		}
	}

	function animateSweep2()
	{
		$(this).closest('div.square').next('div.square').find('div.focus').fadeOut(400, animateSweep2);
	}

	// Animation: Diagonal Sweep (Square Grid)

	var dsQueue = $('div.sweep div.square');

	var dsVisCols = 5;
	var dsNumRows = Math.floor(dsQueue.length / dsVisCols);
	var dsNumCols = dsVisCols + dsNumRows;

	var dsSweep = 1;
	var dsSweeps = new Array();

	for ( var column = 1; column < dsNumCols; column++ )
	{
		dsSweeps[column] = '';

		for ( var row = 1; row <= dsNumRows; row++ )
		{
			var rowIndex = column - ( row - 1 );
			var index = ( ( row - 1 ) * dsVisCols ) + rowIndex;

			if ( rowIndex > 0 && rowIndex <= dsVisCols ) 
			{
				dsSweeps[column] += dsSweeps[column].length > 0 ? ',:eq(' + (index - 1) + ')' : ':eq(' + (index - 1) + ')';
			}
		}
	}

	function animateDiagonalSweep()
	{
		dsQueue.filter(dsSweeps[dsSweep]).find('div.focus').fadeIn(800, function(){
			$(this).fadeOut(2000);

			if ( dsSweep < dsSweeps.length )
			{
				dsSweep++;

				animateDiagonalSweep();
			}
		});
	}

	// Filter menu

	$('div.project_filter ul.filter').bind('mouseenter', function () {
		$(this).addClass('open').find('div.filter-container:hidden').slideDown('fast');
	});

	$('div.project_filter ul.filter').bind('mouseleave', function () {
		$('div.project_filter ul.filter').removeClass('open').find('div.filter-container').hide();
	});

	// Window load event

	$(window).load(function () {
		dsSweep = 1;
		animateDiagonalSweep();

		$('div.project_filter div.callout').animate({
			opacity: 1,
			left: '0px'
		}, 2000, 'easeOutQuad');

		if ( typeof(categoryImage) !== 'undefined' && categoryImage > 1 )
		{
			$('div.scrollable a.graphic').eq(categoryImage-1).click();
			$('div.scrollable:eq(0)').scrollable().click(categoryImage-1);
		}
	});

});
