JaradJohnson.Portfolio = {
    
    getWorkNode: function (work) {
        return node = $([
            '<li id="work-' + work.id + '">',
            '<a href="' + work.url + '">',
            '<img src="' + work.previewImageURL + '" alt="' + work.title + '" />',
            '</a>',
            '</li>'
        ].join(''));
    },
    
    showWorkDetail: function (url) {
        
        $.get(url, {'nocache': true}, function (workHtml, status) {
            var detailHeight,
                replace = false;
            
            if ($('body #work-detail').length > 0) {
                
                replace = true;
                
                workDetail = $('body #work-detail');
                workDetail.html(workHtml);
                
            } else {
                
                workDetail = $('<div id="work-detail" class="clear" />');
                $('#feature').append(workDetail);
                
                workDetail.css('opacity', 0).html(workHtml);
                
            }
            
            // Refresh Cufon text after the ajax call.
            Cufon.refresh('#work-detail h2');
            Cufon.refresh('#work-detail .tasks li');
            
            detailHeight = workDetail.height();
            
            if (replace) {
                
                workDetail.css('height', 'auto');
            
            } else {
                
                // Prepare the height for the display animation.
                
                workDetail.css('height', 0);

                // Hide the previous canvas element.
                $('#work-list-wrapper').fadeOut('fast');

                // Fluidly animate the display of the new canvas.
                workDetail.animate({
                    height: detailHeight,
                    opacity: 1
                }, { duration: 500 });
                
            }
            
            // Bind "Back to All Projects" link.
            workDetail.find('a.back').bind('click', function (event) {
                JaradJohnson.Portfolio.showWorkList();
                return false;
            });
            
            // Bind "View Next Project" link.
            workDetail.find('a.next').bind('click', function (event) {
                JaradJohnson.Portfolio.showWorkDetail($(event.target).attr('href'));
                return false;
            });
            
        });
        
    },
    
    showWorkList: function () {
        $('#work-list-wrapper').fadeIn('fast');
        $('#work-detail').animate({
            height: 0,
            opacity: 0
        }, {
            complete: function () { $('#work-detail').remove(); },
            duration: 300,
        });
        return false;
    },
    
    initialize: function () {
        
        $.getJSON('/portfolio/work_list.json', {}, function (workObjects) {
            
            // Empty the old objects from the work list.
            $('#work-list .items').empty();
            
            // Add each returned work to the work list.
            $.each(workObjects, function (i) {
                var work = this,
                    workNode = JaradJohnson.Portfolio.getWorkNode(work);
                    
                workNode.appendTo($('#work-list .items'));
                
                workNode.find('a').bind('click', function (event) {
                    JaradJohnson.Portfolio.showWorkDetail(work.url);
                    return false;
                });
                
            });
            
            // Remove the default click binding for previous, next pages.
            $('a.prevPage, a.nextPage').bind('click', function (event) {
                return false;
            }).removeClass('disabled');
            
            // Add scrollable action.
            $('#work-list').scrollable({ size: 4 });
            
        });
        
    }
    
}

$(document).ready(function (event) {
    
    JaradJohnson.Portfolio.initialize();
    
});