shell.carouselModule = {
    init: function () {
        if (!this.container) {
            this.container = $('.carousel:first');
        }
        if (!this.carousel) {
            this.carousel = this.container.find('> ul');
        }    
    },
    enable: function () {
        'use strict';
        
        var delay, height, that = this;
        
        this.init();
        
        if (this.container.length === 0) {
            return;
        }        
                                
        // Set the delay.
        delay = parseInt(this.container.attr('class').match(/delay_[0-9]*/g)[0].substring(6));
        
        if(!(delay >= 0)){
            delay = 500;
        }
        
        // Add pager.        
        this.container.append('<div class="pager"></div>');
 
        this.carousel.cycle({
            fx: 'scrollHorz',
            pause: 0,
            pager: this.container.find('.pager'),
            speed: delay,
            fit: 1                        
        });
        
        // Add swipe.
        if (typeof $.fn.touchwipe !== 'undefined') {
            this.carousel.touchwipe({
                wipeLeft: function () {
                    that.carousel.cycle('next');
                },
                wipeRight: function () {
                     that.carousel.cycle('prev');
                },
                preventDefaultEvents: false
            });
        }        
        
        // Add event -- pause on page click.
        this.container.find('.pager a').click(function(){
            that.carousel.cycle('pause');
        });
        
        this.carousel.find('img').load(function() {
            that.resize();
        });
    },
    resize: function () {
        var height = 0;
        
        this.init();
        
        // Iterate over slides.
        this.carousel.children().each(function() {
            'use strict';
            
            var h = $(this).height();
            height = h > height ? h : height;
            
            // Reset magic cycle width variable.
            $(this).get(0).cycleW = null;                
        });
        
        this.carousel.height(height);  
    },
    disable: function () {
        'use strict';
        
        this.init();
        
        if (this.container.length === 0) {
            return;
        }        
                
        // Destroy the cycle.
        this.carousel.cycle('destroy');
        
        // Remove pager.
        this.container.find('.pager').remove()
        
        // Clear all inline CSS.
        this.container.find('ul:first').attr('style', '');
        this.container.find('ul:first > li').attr('style', '');
    },
    resizeTimeout: null
};

$(document).ready(function () {
    'use strict';
    
    shell.carouselModule.enable();
});

$(window).resize( function(e) {
    'use strict';
    
    var doneResizing = function () {
        'use strict';
        
        shell.carouselModule.resize();
    };
    
    // Set timeout to avoid constant execution.
    clearTimeout(shell.carouselModule.resizeTimeout);
    shell.carouselModule.resizeTimeout = setTimeout(doneResizing, 10);
});

