// begin module fadeCarousel
if(typeof(initModuleFadeCarousel) == 'undefined')
{
    var inFadeCarouselClick = false;
    var initModuleFadeCarousel = function($module, options){
        var options = options || {};
        var $carousel = $module.find('.carousel');
        var $carouselControls = $module.find('.carouselControls');
        var sIdx = $carouselControls.find('li.selected').prevAll('li').length;
        
        var calcHeight = function(){
            var maxHeight = 290;
            $(this).find('li').each(function(){
                $(this).show().siblings('li').hide();
                if(maxHeight < $(this).height()){
                    maxHeight = $(this).height();
                }
            });
            $(this).closest('div').height(maxHeight);
        };
    
        var toggleFade = function(elToHide, elToShow, callback){
        
            if(inFadeCarouselClick) return false;
            inFadeCarouselClick = true;
    
            elToHide = (elToHide.length ? elToHide : $('<p></p>'));
            elToShow = (elToShow.length ? elToShow : $('<p></p>'));
            // pre-hide the element to be faded in 
            //if(elToShow.is(':visible')) elToShow.hide();
            
            // fade out visible carousel item
            elToHide.fadeOut('slow', function(){
            
                // select proper dot 
                $carouselControls.find('li:has(a.dot)').removeClass('selected').eq(elToShow.prevAll('li').length).addClass('selected');
                elToShow.fadeIn('slow', function(){
                    if(!$(this).is(':visible')){
                        $(this).show();
                    }
                    $(this).siblings('li').hide();
                    inFadeCarouselClick = false;
                    if(callback) callback();
                });
                // fade in proper carousel item
            });
        };
    
        // attach click handlers to next/prev buttons
        $carouselControls.find('a.btnCarousel').not('.dot').click(function(){
            var prev = $(this).hasClass('prev'); // which button clicked
            var vis = $carousel.find('li:visible').eq(0); // currently visible item in the carousel
            var hid = prev ? ( (vis.prev('li').length > 0) ? vis.prev('li') : vis.siblings('li:last-child') ) : (vis.next('li').length > 0) ? vis.next('li') : vis.siblings('li:first-child');
            var cb = options.googleID ? function(){
                gaTrack(options.googleID, "click" + (prev ? "Prev" : "Next"));
            } : null;
            toggleFade(vis, hid, cb);
            return false;
        });
        
        // show appropriate carousel item based on which dot button clicked 
        $carouselControls.find('a.btnCarousel.dot').click(function(){
            var dotIdx = $(this).closest('li').prevAll('li:has(a.dot)').length;
            var cb = options.googleID ? function(){
                gaTrack(options.googleID, "clickDot_" + dotIdx);
            } : null;
            toggleFade($carousel.find('li:visible').eq(0), $carousel.find('li').eq(dotIdx), cb);
            return false;
        });
        
        if(!options.noCalcHeight){
            $carousel.each(calcHeight);
        }
        
        // listen for image load complete events and resize slides to accomodate
        $carousel.find('li > img').load(function(){
            if(!options.noCalcHeight){
                $carousel.each(calcHeight).find('li').eq(sIdx).show().siblings('li').hide();
            }
        });
        
        $carousel.find('li').eq(sIdx).show().siblings('li').hide();
    
    }

}