// begin module features
if(typeof(initModuleFeatures) == 'undefined')
{
var initModuleFeatures = function($module, options){
    // the options hash
    options = options || {};
    
    // this module
    var $module = $($module);

    var debug = function(msg){
        if(!options.debug) return;
        try {
            if($.browser.mozilla || $.browser.safari){
                console.log(msg);
            }
        } catch(e) {
            alert(e.message);
        }
    };
    
    // the carousels for the module
    var $carousels = $module.children('div').children('ul.carousel');
    
    // navigation element
    var $nav = $module.find('#featuresNav');    
    
    // active carousel index
    var aIdx = $nav.find('li.selected').prevAll('li').length;

    var calcHeights = function(){
        debug('calcHeights: module');
        var maxHeight = 0;
        $(this).children('div').children('ul.carousel').each(function(){
            debug('calcHeights: module.carousel');
            $(this).removeClass('displayNone').siblings('ul.carousel').addClass('displayNone');
            $(this).children('li').each(function(){
                debug('calcHeights: module.carousel.li.height() = ' + $(this).height());
                $(this).show().siblings('li').hide();
                if($(this).height() >= maxHeight){
                    maxHeight = $(this).height();
                }
            });
            if($(this).next('ul.carousel').length < 1){
                $(this).closest('div').height(maxHeight);
            }
        });
        debug('maxHeight: ' + maxHeight);
    };
    
    // calculate module height 
    if(!options.noCalcHeight){
        $module.each(calcHeights);
    }
    
    // loop each carousel
    $carousels.each(function(){
    
        // this carousel
        var $carousel = $(this);
        
        // index of this carousel among other carousels
        var cIdx = $carousel.prevAll('.carousel').length;
        
        // controls for this carousel
        var $carouselControls = $module.find('.carouselControls').eq(cIdx);
        
        // active item in this carousel
        var sIdx = $carouselControls.find('li.selected').prevAll('li').length;
        
        // toggle function to switch between carousel items
        var toggleFade = function(elToHide, elToShow, callback){
            elToHide = (elToHide.length ? elToHide : $('<p></p>'));
            elToShow = (elToShow.length ? elToShow : $('<p></p>'));
            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');
                
                // fade in proper carousel item
                elToShow.fadeIn('slow', callback || function(){}); 
            });
        };
        
        // show this carousel, hide others
        $carousel.removeClass('displayNone').siblings('.carousel').addClass('displayNone');
        
        // show the selected item in the carousel, hide the rest
        $carousel.children('li').eq(sIdx).show().siblings('li').hide();
        
        // 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.children('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');
            toggleFade(vis, hid);
            try {
                if(options.googleID){
                    gaTrack(options.googleID, "navClick" + (prev ? "Prev" : "Next") + "_carousel_" + cIdx);
                }
            } catch(e){}
            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;
            try {
                if(options.googleID){
                    gaTrack(options.googleID, "dotClick_" + dotIdx + "_carousel_" + cIdx);
                }
            } catch(e){}
            toggleFade($carousel.children('li:visible').eq(0), $carousel.find('li').eq(dotIdx));
            return false;
        });
        
    });
    
    // fire events on left nav click
    $nav.find('li a').click(function(){
    
        // index of selected nav item
        var idx = $(this).closest('li').prevAll('li').length;
        
        try {
            if(options.googleID){
                gaTrack(options.googleID, "clickNav_" + idx);
            }
        } catch(e) {}
        
        // set the selected class
        $(this).closest('ul').find('li').removeClass('selected').eq(idx).addClass('selected');
        
        // toggle vivible carousel 
        $carousels.addClass('displayNone').trigger('carouselHidden').eq(idx).removeClass('displayNone').trigger('carouselVisible'); //.trigger('goto', [0]) // this sets each carousel back to first element;
        
        // toggle carouselControls 
        $module.find('ul.carouselControls').addClass('displayNone').eq(idx).removeClass('displayNone');
        
        return false;
    });
    
    $carousels.addClass('displayNone').eq(aIdx).removeClass('displayNone');
    $module.find('.carouselControls').addClass('displayNone').eq(aIdx).removeClass('displayNone');

};

} // end module 