// --------------------------------------------------------------------------
// Copyright Julian Nicholls 2011, All rights reserved.
// --------------------------------------------------------------------------

$(function() {
    $("#midnav li").hover( function() {
        var $this = $(this),
            href  = $this.attr( 'data-midref' );
    
        if( href === '#' )
            href = 'home';
            
        $("article").filter( ':visible' ).hide();
        $("article#" + href ).show(); 
        
/*        fadeOut( 300, function() {
            $(this).hide();
            $("article#" + href ).fadeIn( 300 );
        });
*/
    });
    
    var max = 0;
    
    $(".feature-block").each( function () {
        var h = $(this).height();
        
        if( h > max )
            max = h;
    });

    $(".feature-block").height( max );

    $("#galthumbs a").click( function() {
        var $this    = $(this),
            img      = new Image,
            src      = this.href,
            $chrome  = $("#picchrome"),
            $loading = $("#loading"),
            $tray    = $this.parent(),
            pos      = $tray.position();
            
        $("#cover").fadeTo( 100, 0.6 );
        $loading.css( {top: (pos.top + 200), left: (pos.left + 430)} ).show();

        img.src = src;
        
        img.onload = function() {
            var width  = img.width  + 12,
                height = img.height + 21;

            $loading.hide();
                
            $chrome.css( 
                { width: width, 
                  height: height, 
                  top: 50,
                  left: pos.left + ($tray.innerWidth() - width) / 2
                } )
                .html( '<img src="' + src + '" alt="' + src + '" />' )
                .fadeTo( 600, 1 );
        };

        return  false;            
    });
    
    $("#cover,#picchrome,#picchrome img").live( 'click', function() {
        $("#picchrome").fadeOut( 600 );
        $("#cover").hide();
    });
    
});
