$(document).ready(function() {
	
	var animation = true;
	
	// Show work details
	$('.title a').click(function() {
		var el = $(this).parent().parent().parent().find('.work');
		
		el.slideToggle(400, function() {
			$(this).toggleClass('selected');		
		});
		
		return false;
	});
	
	// Show next work image
	$('.thumb .next').click(function() {
		if(animation == true) {
			
			var activeElId = $(this).parent().find('.active').attr('id');
			var nextElId = $(this).parent().find('.active').next().attr('id');
			var nextNextElId = $(this).parent().find('.active').next().next().attr('id');
			
			// Disables animation
			animation = false;
			
			$(this).parent().find('.prev').show();
			
			// Hide nav arrow if last item
			if(typeof(nextNextElId) == 'undefined' || nextNextElId == '') {
				$(this).hide();
			}
			
			if(typeof(nextElId) != 'undefined') {
				$('#'+activeElId).animate({ left: '-=500' }, 600);
				
				$('#'+nextElId).animate({ left: '0' }, 600, function() {
					animation = true;
				});
				
				// Set active state to new element
				$('#'+activeElId).removeClass('active');
				$('#'+nextElId).addClass('active');
			}
		}
		
		return false;
	});
	
	// Show prev work image
	$('.thumb .prev').click(function() {
		if(animation == true) {
			
			var activeElId = $(this).parent().find('.active').attr('id');
			var nextElId = $(this).parent().find('.active').prev().attr('id');
			var nextNextElId = $(this).parent().find('.active').prev().prev().attr('id');
			
			// Disables animation
			animation = false;
			
			$(this).parent().find('.next').show();
			
			// Hide nav arrow if last item
			if(typeof(nextNextElId) == 'undefined' || nextNextElId == '') {
				$(this).hide();
			}
			
			if(typeof(nextElId) != 'undefined') {
				$('#'+activeElId).animate({ left: '+=500' }, 600);
				
				$('#'+nextElId).animate({ left: '0' }, 600, function() {
					animation = true;
				});
				
				// Set active state to new element
				$('#'+activeElId).removeClass('active');
				$('#'+nextElId).addClass('active');
			}
		}
		
		return false;
	});
	
	var pageUrl = location.href.toString();
	var pos = pageUrl.indexOf("#");
	var hashId = pageUrl.substring(pos);
	var el = $('#main-content').find(hashId);
	
	// If correct hash value exists on page, trigger click
	if(el.length > 0) {
		$(hashId+' .title a').trigger('click');
	}
	
});
