$(document).ready(function (event) {
    
    // jQuery Lightbox
    $('a#rendering, #blueprint-images a:not(.pdf)').lightBox();
    $('#slideshow .photos a').lightBox();
    
    // Scrollable
    var slideshow = $('#slideshow #stage').scrollable({
        'size': 1,
        'items': 'ul.photos',
        'api': true
    });
    
    var slideshowNavigator = $('#slideshow #stage').navigator({
        'navi': 'ol.nav',
        'naviItem': 'li',
        'activeClass': 'current'
    });
    
    // Default Values
    $('#minimum_square_footage, #minimum_footprint_width, #minimum_footprint_depth').defaultValue('Min');
    $('#maximum_square_footage, #maximum_footprint_width, #maximum_footprint_depth').defaultValue('Max');
    
    var bindFavoritesEvent = function () {
        
        // Floor Plan "Add to Favorites" AJAX Submission
        $('form.add-to-favorites, form.remove-from-favorites').bind('submit', function (event) {
            var form = $(this),
                forms = $('form.add-to-favorites, form.remove-from-favorites'),
                url  = form.attr('action'),
                data = form.serialize();

            $.ajax({
                'url': url,
                'type': 'POST',
                'data': data,
                'dataType': 'html',
                'beforeSend': function (request) {
                    // TODO: Add in an AJAX indicator.
                },
                'success': function (data, status) {
                    if ($(data).find('#login-form').length > 0) {
                        alert('You need to be logged in to add a plan to your favorites!');
                        return false;
                    }
                    forms.replaceWith($(data));
                    bindFavoritesEvent();
                },
                'error': function (request, status, error) { alert('Sorry, but an error occurred.'); }
            });

            return false;
        });
        
    }
    
    bindFavoritesEvent();
    
});