var Carousel = {
    carousel: null,
    active_slide: null,
    next_slide: null,
    dots: null,
    images: {
        background: [],
        title: [],
        author: [],
        browse: []
    },
    carousel_timer: null,
    next_image_timeout: 10000,
    animation_transition: 700,
    running: false,
    init: function() {
        this.carousel = $('.carousel');
        this.getSlideNumbers();
        this.assignClickableDots();
        this.loadImages();
    },
    getSlideNumbers: function(next_slide) {
        var active_slide_id = this.carousel.find('.dots .active').parent().attr('class').substr(1);
        this.active_slide = this.carousel.find('.slide' + active_slide_id);
        if(next_slide) {
            this.next_slide = this.carousel.find('.slide' + next_slide.parent().attr('class').substr(1));
        }
    },
    assignClickableDots: function() {
        var me = this;
        this.dots = $('.carousel .dots a');
        this.dots
            .click(function(e) {
                e.preventDefault();
                me.switchSlides($(this));
                me.stopCycle();
            });
    },
    loadImages: function() {
        this.images.background = $('.carousel .promo .background img');
        this.images.title = grabCssImgUrlFor('.title');
        this.images.author = grabCssImgUrlFor('.author');
        this.images.browse = grabCssImgUrlFor('.browse_inside');
        
        function grabCssImgUrlFor(name) {
            var els = $('.carousel .promo ' + name);
            var img_array = [];
            els.each(function(){
                css_bgr = $(this).css('background-image');
                css_bgr = css_bgr.substring(4, css_bgr.length -1);
                var img = new Image();
                img.src = css_bgr;
                img_array.push(img);
            });
            return img_array;
        }
        
        $(window).load(function(){
            Carousel.startCycle(this.next_image_timeout);
        });
    },
    switchSlides: function(ref) {
        var new_slide_id = ref.parent().attr('class').substr(1);
        this.next_slide = this.carousel.find('.slide' + new_slide_id);
        this.getSlideNumbers(ref);
        this.dots.removeClass('active');
        ref.addClass('active');
        this.animate();
    },
    animate: function() {
        var me = this;
        if($.browser.msie) {
            this.animation_transition = 0;
        }
        this.active_slide.fadeOut(this.animation_transition);
        this.next_slide.fadeIn(this.animation_transition);
    },
    startCycle: function(time) {
        this.carousel_timer = setInterval("Carousel.runCycle()", this.next_image_timeout);
        this.running = true;
    },
    stopCycle: function() {
        clearInterval(this.carousel_timer);
        
        this.running = false;
    },
    runCycle: function() {
        var current_index = Carousel.dots.parent().index(Carousel.dots.parent().find('.active').parent());
        var next_index = current_index + 1;
        if (next_index == this.dots.length) {
            next_index = 0;
        }
        var current_slide_dot = $(Carousel.dots.parent().get(next_index)).find('a');
        this.switchSlides(current_slide_dot);
    }
};