var JB = {
    
    image_loader: new Image(),
    captions_open: false,
    info_loaded: false,

    set_blackout_fun: function(){
        $('body').append('<div class="blackout">Loading</div>');
        $('body').css('overflow','hidden');
        $('.blackout').css('opacity',0.1);
    },
    
    load_fun: function(url){
        $('.info').fadeTo('fast', 0, function(){$('body').removeClass('home'); }); 
        $('.captions').slideUp('slow');
        $('.banner').slideUp('slow', 
            function(){ 
                JB.set_blackout_fun();
                $('.blackout').fadeTo('slow',1);
                setTimeout(function(){
                        if($('.content-img').hasClass('thumbs')){
                            $('.content-img').removeClass('thumbs');
                            $('.content-img').html('<img src="" alt="">');
                        }
                        $.getJSON(url, null, JB.onload_fun);
                    },1000); 
            });
    },
    
    update_controls_fun: function(resource){
        // Update controls
        if(resource.prev_url){
            $('a[rel="prev"]').attr('href',resource.prev_url);
            $('a[rel="prev"]').removeClass('disabled');
        } else {
            $('a[rel="prev"]').addClass('disabled');
        }
        if(resource.next_url){
            $('a[rel="next"]').attr('href',resource.next_url);
            $('a[rel="next"]').removeClass('disabled');
        } else {
            $('a[rel="next"]').addClass('disabled');
        }
    },
    
    update_captions_fun: function(resource){
        // Append caption HTML if don't exist
        if(!$('.captions').length){
            $('body').append('<div class="captions">'+
                             '<div class="a">'+
                             '<dl class="clearfix">'+
                             '<dt>Title</dt><dd></dd>'+
                             '<dt>Client</dt><dd></dd>'+
                             '</dl>'+
                             '</div>'+
                             '<div class="b">'+
                             '<dl class="clearfix">'+
                             '<dt>Title</dt><dd></dd>'+
                             '<dt>Client</dt><dd></dd>'+
                             '</dl>'+
                             '</div>'+
                             '</div>');
        }
        $('.captions').css('display','none');
        $('.captions').css('opacity',0.85);
        // Update captions
        $.each(['a','b'], function(i, val){
                var caption = resource[val];
                if(null != caption){
                    $('.captions .'+val).show();
                    $('.captions .'+val+' dd:first').text(caption.title);
                    $('.captions .'+val+' dd:last').text(caption.client);
                } else {
                    $('.captions .'+val).hide();
                }
            });
    },
    
    onload_fun: function(resource){                        
        // Load the image
        JB.image_loader.onload = function(){            
            $('.content-img img').attr('src',this.src);                                           
            JB.update_controls_fun(resource);
            JB.update_captions_fun(resource);
            // Update page title
            if(resource.page_title){
                document.title = resource.page_title+' | '+JB.start_title;         
            } else {
                document.title = JB.start_title;
            }
            $('.blackout').fadeTo('slow', 0,
                    function(){ 
                        $('body').css('overflow','auto');
                        JB.init_mousemove_fun()
                        $('.banner').slideDown("slow");
                        $(this).remove();
                     }); 
        }
        JB.image_loader.src = resource.src;
    },
    
    mouse_move_fun : function(e) { 
        var x = e.clientX;
        var y = e.clientY;
        if(y > ($(window).height() - 100) && 
            y < ($(window).height()-5) && 
            x < ($(window).width()-20) && 
            x > 20){
            $('.captions').slideDown("slow");
            JB.captions_open = true;
        } else if(JB.captions_open){
            setTimeout(function() { 
                            $('.captions').slideUp("slow"); 
                            JB.captions_open = false; 
                            }, 
                        300 );
        }
    },
    
    init_mousemove_fun: function(){
        // Initialise mouse move captions
        if($('.content-img img').height() < $(window).height()){
            $('body').unbind('mousemove');
            $('.captions').slideDown("slow");
        } else {
            $('body').mousemove(JB.mouse_move_fun);
        }
    },
    
    load_info_fun: function(url){
        // Fade out and remove home page info
        if($('body').hasClass('home')){
            $('.info').fadeOut('fast', function(){
                $(this).remove();
                $('body').removeClass('home');
                setTimeout(function(){ JB.load_info_fun(url); }, 10);
            });
            return;
        }
        if(JB.info_loaded) {
            $('.info').fadeTo('slow',0.8);
            return;
        }

        // Create info panel
        $('body').append('<div class="info"></div>');
        $('.info').css('opacity',0);
        
        // Load HTML
        $.get(url, null, function(data, textStatus){            
            // Set info HTML
            $('.info').html(data);            
            $('.info').centerOf('window');          
            // Close button behaviour
            $('.info .close').click(function(){
                        $('.info').fadeTo('slow',0);
                        return false;
                    });
            // Fade in slow
            setTimeout(function() { $('.info').fadeTo('slow',0.8); }, 10);
            JB.info_loaded = true;
        });
    }
};

// Store current page title
JB.start_title = 'Jamie Bevan Photography';
