jQuery(function($) {
	// =======================================================================
	// Sub-Navigation
	// =======================================================================
	$('.subnav li a').live('click', function() {
		var divs = $('.main .section');

		divs.hide();
		$(this).parent().parent().find('li').removeClass('active');
		$(this).parent().addClass('active');
		divs.eq($(this).parent().index()).show();

		return false;
	});
	
	$('.subnav li:first-child a').trigger('click');
	
	$('a#subnav_' + window.location.hash.substr(1)).trigger('click');
	
	// =======================================================================
	// Lightboxes
	// =======================================================================
	$('.news a').live('click', function() {
		if($(this).attr('href') == '#') {
			$('#lightbox').tmpl({
				content: $(this).parent().find('.detail').html()
			}).prependTo('body').show('fade');

			return false;
		}
	});	
	
	$('.lightbox .close').live('click', function() {
		$(this).closest('.lightbox').hide('fade', function() {
			$(this).remove();
		});
		
		return false;
	});
	
	$('.lightbox').live('click', function(e) {
		if(e.target == this) {
			$(this).find('.close').trigger('click');
		}
	});
	
	$(document).keydown(function(e) {
		if(e.which == 27) {
			$('.lightbox .close').trigger('click');
		}
	});
	
	// Gallery Images
	
	var galleryContainer = null;
	
	$('.gallery a').live('click', function() {
		galleryContainer = $(this).closest('ul').find('li');
		
		$('#lightbox_image').tmpl({
			image: $(this).attr('href'),
			index: $(this).parent().index(),
			total: galleryContainer.size()
		}).prependTo('body').show('fade');
		
		return false;
	});
	
	$('.lightbox .nav_prev').live('click', function() {
		var prevIndex = (parseInt($(this).parent().find('.val_index').val()) - 1);
		
		if(prevIndex < 0) {
			prevIndex = (parseInt($(this).parent().find('.val_total').val()) - 1);
		}
		
		$(this).parent().find('.image').html('<img src="' + galleryContainer.eq(prevIndex).find('a').attr('href') + '" alt="" />')
		$(this).parent().find('.val_index').val(prevIndex);
		
		return false;
	});
	
	$('.lightbox .nav_next').live('click', function() {
		var nextIndex = (parseInt($(this).parent().find('.val_index').val()) + 1);
		
		if(nextIndex >= parseInt($(this).parent().find('.val_total').val())) {
			nextIndex = 0;
		}
		
		$(this).parent().find('.image').html('<img src="' + galleryContainer.eq(nextIndex).find('a').attr('href') + '" alt="" />')
		$(this).parent().find('.val_index').val(nextIndex);
		
		return false;
	});
	
	// =======================================================================
	// List Bio Truncation
	// =======================================================================
	$('.list.wide .bio').condense({
		condensedLength: 500,
		moreText: '<a class="truncate" href="#">More [+]</a>',
		lessText: '<a class="truncate" href="#">Less [-]</a>',
		ellipsis: ' &hellip;',
		easing: 'easeOutExpo'
	});
	
	$('.list.narrow .bio').condense({
		condensedLength: 550,
		moreText: '<a class="truncate" href="#">More [+]</a>',
		lessText: '<a class="truncate" href="#">Less [-]</a>',
		ellipsis: ' &hellip;',
		easing: 'easeOutExpo'
	});
	
	$('.truncate').live('click', function() { return false; });
});
