/** 
 * slideshow.js
 * 
 * Uses the slides plugin for jQuery (http://slidesjs.com/) by Nathan Searles
 * 
 * TODO: make the caption stuff object oriented.
 */

$(function(){
    
    // attach captions data for each image.
    $('.slide').each(function (index) {
        var p = $(this).find('p.slide-description').first();
        $(this).data('caption', p.html());
    });
    
    // caption functions
    var CaptionShow = function (options) {
        $('#slide-caption').animate({
            bottom: '85px'
        }, 1000, 'easeInOutQuart');
    };
    
    var CaptionHide = function (options) {
        $('#slide-caption').animate({
            bottom: '0'
        }, 1000, 'easeInOutQuart');
    };
    
    var CaptionSet = function (content) {
        $('#slide-caption').find('p span.text').html(content);
    };
    
    var CaptionGet = function (index) {
        return $('.slide').eq(index).data('caption');
    };
    
    var CaptionClose = function () {
        var $caption = $('#slide-caption');
        
        $caption.animate({
            width: '25px'
        }, 200);
        $caption.data('status', 'hidden');
        
        $(this).addClass('btn-caption-close');
    };
    
    var CaptionOpen = function () {
        var $caption = $('#slide-caption');
            
        $caption.animate({
            width: '870px'
        }, 200);
        $caption.data('status', 'displayed');
        
        $(this).removeClass('btn-caption-close');
    };
    
    // toggle caption handling
    $('#btn-toggle-caption').toggle(CaptionClose, CaptionOpen);
    
    $('#slideshow').slides({
        container: 'slides_container',
        preload: true,
        preloadImage: '/img/loading.gif',
        play: 7000,
        pause: 2000,
        effect: 'fade',
        fadeSpeed: 1000,
        crossfade: true,
        animationStart: function (current) {
            var captionStatus = $('#slide-caption').data('status');
            
            if (captionStatus != 'hidden') {
                CaptionHide();
            }
        },
        animationComplete: function (current) {
            // change the caption content
            var caption = CaptionGet(current - 1);
            CaptionSet(caption);
            
            CaptionShow();
        },
        slidesLoaded: function() {
            var caption = CaptionGet(0);
            CaptionSet(caption);
            
            CaptionShow();
        }
    });
    
});

