(function($, window, document, undefined) {

	$(function() {
		$('#slideshow').slideShow();

		$('#projects .filters a').click(function(e) {
			var regex = new RegExp($(this).attr('rel'), 'i'),
				duration = 150;

			$(this).toggleClass('active').siblings().removeClass('active');
			$(this).siblings().tinyTip('destroy');

			if($(this).hasClass('active')) {
				$(this).tinyTip({ content: 'Click again to remove filter', side: 'top' }).tinyTip('show');

				$('#projects .project').css('display', function() {
					return regex.test($(this).attr('rel')) ? 'block' : 'none';
				})
			}
			else {
				$(this).tinyTip('destroy');
				$('#projects .project:hidden').show();
			}
			
			updateProjects();

			e.preventDefault();
			return false;
		})
		updateProjects();
		

		$('#slideshow .nav li').tinyTip({ side: 'bottom' });

		$('#about .side li a').click(function(e) {
			var id = '#slide-' + $(this).attr('href').replace('#', '');

			$(id).show().siblings('.slide').hide();

			$(this).parents('ul').find('a').removeClass('active');
			$(this).addClass('active');

			e.preventDefault();
		})
		
		function decorateGrid(elems, cols) {
			$(elems).removeClass('first last').each(function(i) {
				var mod = (i % cols);

				$(this).addClass( mod === 0 ? 'first' : mod === 2 ? 'last' : '');
			});
		}
		
		function updateProjects() {
			decorateGrid( $('#projects .project:visible'), 3 );
		}
		
	});

}(jQuery, this, this.document))
