/*
 * DCMS Carousel (extension of theTeam UI YACarousel)
 *
 * Depends:
 *    ui.core.js
 *    theteam.core.js
 *    theteam.yacarousel.js
 */
(function($) {
    $.widget("tt.dcms_carousel", $.extend({}, $.tt.yacarousel.prototype, {
        /* we don't need custom hard-coded logic (events handlers are enough) */
    }));
    $.extend(true, $.tt.dcms_carousel, $.tt.yacarousel, {
        defaults:{
            interval: 5000,
            arrows: true,
            init: function(event, options){
                
                options.slides.hide();
                options.slides.first().show();
                // add a click event to the full content
                $(".tt-carousel-content").click(function(){
                    location.href = $('.tt-yacarousel-slide:visible').find("a").attr("href");
                    return false;
                });
                
                // reposition with absolute coordinate, to avoid IE positioning issues
                var $controls = options.controls,
                    $selectors = $controls.find("li"),
                    $left_arrow = $controls.find(".tt-yacarousel-left-arrow"),
                    $right_arrow = $controls.find(".tt-yacarousel-right-arrow");
                    
                var selectors_width = 23 * $selectors.length;
                var total_width = $controls.width();
                // position the selectors
                $controls.find("ul").css({
                    position:'absolute',
                    top:0,
                    left: Math.round( (total_width - selectors_width) / 2 )
                });
                // positions the arrows
                $left_arrow.css({
                    position:'absolute',
                    top:7,
                    left: Math.round( (total_width - selectors_width) / 2 ) - 10
                });
                $right_arrow.css({
                    position:'absolute',
                    top:7,
                    left: Math.round( (total_width - selectors_width) / 2 ) + selectors_width + 5
                });
            },
            activate: function(event, options){
                var self = $(this);
                var fade_events = {
                    fadeOut: function(slide, callback){
                        if(slide.length < 1) return callback();
                        slide.stop().animate({'opacity': 0}, 500, function(){
                            $(this).hide();
                        });
                        return callback();                        
                    },
                    fadeIn: function(slide, callback){
                        if(slide.length < 1) return callback();
                        slide.stop().css({'display':'block', 'opacity': 0}).animate({'opacity': 1}, 500);
                        return callback();
                    }
                };
                // stops all other animations, and bring them to the end
                options.slides.stop(true, true);
                if(options.type == 'init'){
                    options.new_slide.css('opacity', 1);
                    return self.yacarousel("complete");
                }
                    
                fade_events.fadeOut(options.old_slide, function(){
                    fade_events.fadeIn(options.new_slide, function(){
                        // at the end of the activation we need to call "complete"
                        self.yacarousel("complete");
                    });
                });
                return false;
            }
    }
    });
    

})(jQuery);
