jQuery.easing.def = "easeInOutExpo";

var isIE = ($.browser.msie);

$(function(){
    
    var delay = 850; // default animation duration
    
    // binds a helper function to the onfocus of the .job elements
    $('.job section')
        .bind('focus', function(){
            $('html, body').animate({scrollTop: $(this).offset().top - 30}, delay);
            $('.job section.focus').removeClass('focus');
            $(this).addClass('focus');
        })
        .css({'position': 'relative'});
    $('.job section:first').addClass('focus');
    
    // hides the non-image contents of figure's
    $('.job section figure').find(':not(img)').hide();
    
    // add CSS styling to the gallery and its elements
    $('.job section').css({'overflow': 'hidden'});
    $('.job section figure').css({'float': 'left'});
    
    $('.job section').each(function(){
        var w = $(this).width();
        var n = $(this).find('figure').length;
        
        // wrapps the figure elements into a helper div
        $(this).find('figure').wrapAll('<div class="wrapper"></div>');

        // sets the .wrapper width appropriatedly so it
        // accomodates all the figures inside of it
        $(this).find('.wrapper').css({
            'width': w * n,
            'position': 'relative',
        });
        
        // builds the image slider if there's more than 1 image in the gallery
        if(n > 1){
            for(i = 1; i <= n; i++){
                link = $('<a href="#" class="click inset kerning">'+i+'</a>');
                if(i == 1){ 
                    link.addClass('current'); 
                }
                
                $(this).parent().find('.pagination').append(link, " ");
            }
        
        }
        
        // image slider actions
        $(this).parent().find('.pagination a').click(function(){
            $(this).closest('.job section').focus();
            if( !$(this).hasClass('current') ){                
                var offset = $(this).index();

                // scrolling in reverse direction
                $(this).closest('.job section').find('.wrapper').animate({
                    'left': - w * offset,
                }, delay);

                // toggles the class of current
                $(this)
                    .siblings('.current').removeClass('current').end()
                    .addClass('current');
            }
            return false;
        });
    });
    
    // builds the project slider if there's more than 1 project
    if($('.job section').length > 1){
        // project slider actions
        $('.job section button').click(function(){
            var direction = $(this).attr('value');
            $(this).closest('.job section')[direction]().focus();
        });
        
    }
    
    // image slider keyboard navigation
    $(window).keyup(function(key){
        if(key.keyCode == 37){          // left
            
            // if the first image is the current, go to the last
            if( $('.job section.focus .pagination a.current').is(':first-child') ){
                $('.job section.focus .pagination a:last').click();
            } else {
                // otherwise, go to the previous
                $('.job section.focus .pagination a.current').prev('a').click();
            }
            
        } else if(key.keyCode == 39){   // right
            
            // if the last image is the current, go to the first
            if( $('.job section.focus .pagination a.current').next().is(':not(a)') ){
                $('.job section.focus .pagination a:first').click();
            } else {
                // otherwise, go to the next
                $('.job section.focus .pagination a.current').next('a').click();
            }
            
        }
    });
    
    // project slider keyboard navigation
    $(window).keydown(function(key){
        
        if(key.keyCode == 40) {         // down
            if( $(window).scrollTop() < $('.job section:first').offset().top - 30 ){
                $('.job section:first').focus();
            } else {
                if( !$('.job section.focus').is(':last-child') ){
                    $('.job section.focus button.next').click();
                } else {
                    $('.job section:first').focus();
                }
            }
            return false;
        } else if(key.keyCode == 38){   // up
            if( !$('.job section.focus').is(':first-child') ){
                $('.job section.focus button.prev').click();
            } else {
                $('.job section:last').focus();
            }
            return false;
        }
        
    });
});
