jQuery(document).ready(function($){
    
    // Progressive enhancment. 
    //Remove all alt and title attributes for aesthetic purposes. 
    $('img').removeAttr('alt').removeAttr('title');
    $('a').removeAttr('title');
    
    // Resize text to fill thumbnail overlay
    $('.title-overlay').textfill({ maxFontSize: 36 });
    
    // Disable the non-active thumbnails on mobile devices
    $('.home .gallery-thumb').not(':even').css('cursor', 'default').removeAttr('href');
    
    $('.home.mobile .title-overlay-wrapper').not(':even').hide();
    
    // Fade in the link names on the home page, but don't try to mouse over the non-active thumbnails
    $('.not-mobile .gallery-thumb').not('.home.not-mobile .gallery-thumb:odd').hover(
        function () {
            $(this).find('.title-overlay-wrapper').not(':animated').css('visibility', 'visible').hide().fadeIn();
        },
        function () {
            $(this).find('.title-overlay-wrapper').fadeOut();
        }
    );
    

	/*
	 * Keep track of the keypresses, so that we know if someone is trying to
	 * open a link in a new window (so then don't fade the content out.
	 */
        var keyState = 'up';        
		$(document).keydown(function(e) {
			keyState = 'down';
		});
		
		$(document).keyup(function(e) {
			keyState = 'up';
		});	

	
	/*
	 * Fade in the content after page load
	 */
        $('.not-mobile #content').hide();
		$(window).bind('unload load',function(){
			$('.not-mobile #content').fadeIn(1000);
		});
		
		
		
	/*
	 * Fade out content when a link is clicked
	 */
		$('a').not('[target="_blank"], [href^="mailto:"]').click(function(){
			if(keyState == 'up') {
				$('#content').fadeOut(1000);
			}
		});

    
});

/**
* Resize inner element to fit the outer element
* @author Some modifications by Alexander Sandstorm
* @author Code based on earlier works by Russ Painter (WebDesign@GeekyMonkey.com)
* @version 0.2
*/
(function($) {
$.fn.textfill = function(options) {
	
    options = jQuery.extend({
        maxFontSize: null,
        minFontSize: 8,
        step: 1
    }, options);

    return this.each(function() {

        var innerElements = $(this).children(':visible'),
            fontSize = options.maxFontSize || innerElements.css("font-size"), // use current font-size by default
            maxHeight = $(this).height(),
            maxWidth = $(this).width(),
            innerHeight,
            innerWidth;

        do {

            innerElements.css('font-size', fontSize);

            // use the combined height of all children, eg. multiple <p> elements.
            innerHeight = $.map(innerElements, function(e) {
                return $(e).outerHeight();
            }).reduce(function(p, c) {
                return p + c;
            }, 0);

            innerWidth = innerElements.outerWidth(); // assumes that all inner elements have the same width
            fontSize = fontSize - options.step;

        } while ((innerHeight > maxHeight || innerWidth > maxWidth) && fontSize > options.minFontSize);

    });

};

})(jQuery);

