// begin module comboCarousel 
if(typeof(initModuleComboCarousel) == 'undefined')
{

    var initModuleComboCarousel = function($module, options){

        options = options || {};
        var uniqueOptions = $.isArray(options);
        
        // index of selected carousel 
        var sIdx = $module.find('ul.tabs li:has(a.selected)').prevAll('li').length;
        var maxHeight = 0; //$module.height();
        
        var calcHeight = function(){
            var cont = $(this).closest('.module');
            var height = 0; // $(this).height();
            var ch = cont.height() - $(this).height();
            $(this).find('ul.carousel > li').each(function(){
                if($(this).height() > height) height = $(this).height();
            });
            //if((ch + height) > cont.height()){cont.height(ch + height);}
            if(height > maxHeight) maxHeight = height;
            
            return $(this);
        };
        
        var getHeights = function(){
            return $(this).height();
        };
        
        $module.find('ul.tabs li a').click(function(){
            // index of clicked tab
            var idx = $(this).closest('li').prevAll('li').length;
            // hide all but proper carousel
            $module.find('.carouselContainer').addClass('displayNone').eq(idx).removeClass('displayNone');
            // hide all carousel buttons but proper set
            $module.find('.carouselButtons').addClass('displayNone').eq(idx).removeClass('displayNone');
            if( (uniqueOptions && (options.length > idx ? options[idx].googleID : options[options.length-1].googleID) ) || (!uniqueOptions && options.googleID) ){
                gaTrack(uniqueOptions ? (options.length > idx ? options[idx].googleID : options[options.length-1].googleID) : options.googleID, "clickTab_" + idx);
            } 
            return false;
        });
        
        /** 
         * using jquery for fade on caption in place of: 
         * .ideaStarters .carousel li a:hover span.details, .ideaStarters .carousel li a.show span.details {
         */ 
        $module.find('.carousel li a').each(function(){
            var idx = $(this).closest('.carousel').prevAll('.carousel').length;
            var opts = uniqueOptions ? (options.length > idx ? options[idx] : options[options.length-1]) : options; 
            if(!opts.noHover) {
                $(this).hover(function(){
                    if($.browser.msie && $.browser.version <= 6){
                        $(this).find('span.details').show();
                    } else {
                        $(this).find('span.details').fadeIn('fast');
                    }
                }, function(){
                    if($.browser.msie && $.browser.version <= 6){
                        $(this).find('span.details').hide();
                    } else {
                        $(this).find('span.details').fadeOut('fast');
                    }
    
                });
            }
        });
        
        var hash = {};
        
        /**
         * 
         */
        $module.find('.carouselContainer').addClass('displayNone').each(function(){
            $el = $(this);

            // hide all but this carouselContainer so carousel will initialize properly
            $el.removeClass('displayNone').siblings('.carouselContainer').addClass('displayNone');
            
            var lis = $(this).find('li').show();
            //var vis = Math.floor($module.width() / lis.eq(0).width());
            var vis = Math.floor($(this).width() / lis.eq(0).width());
            var idx = $(this).prevAll('.carouselContainer').length;
            var hash = merge({
                btnPrev: $module.find('.carouselButtons:eq(' + idx + ') a.btnCarousel.prev'),
                btnNext: $module.find('.carouselButtons:eq(' + idx + ') a.btnCarousel.next'),
                btnGo: null,
                mouseWheel: false,
                auto: false,
                speed: 500,
                easing: null,
                vertical: false,
                circular: true,
                visible: vis || 1,
                start: 0,
                scroll: 1
            }, uniqueOptions ? (options.length > idx ? options[idx] : options[options.length-1]) : options);
            try {
                if(hash.googleID){
                    $(hash.btnPrev).click(function(){
                        gaTrack(hash.googleID, "clickPrev_" + idx);
                    });
                    $(hash.btnNext).click(function(){
                        gaTrack(hash.googleID, "clickNext_" + idx);
                    });
                }
            } catch (e) {
            
            }
            //console.log(vis + ' ' + lis.length);
            //console.log(hash);
            //$el.each(calcHeight);

            var heights = $el.find('ul.carousel > li').map(getHeights).sort();
            var tmp = heights[heights.length-1];
            if(tmp >= maxHeight) maxHeight = tmp;
            //console.log(tmp);

            if(lis.length <= hash.visible || lis.length == 0){
                with($(hash.btnPrev).click(function(){return false;})){
                    if(!hash.leaveButtons) hide();
                }
                with($(hash.btnNext).click(function(){return false;})){
                    if(!hash.leaveButtons) hide();
                }
                //console.log('skipping carousel vis:' + lis.eq(0).width());
            } else {
            }
            $(this).jCarouselLite(hash);
            
        });

        // hide all but first or selected carouselContainer
        $module.find('.carouselContainer').addClass('displayNone').eq(sIdx).removeClass('displayNone');
        
        //$module.height(maxHeight);
        $module.find('.carouselContainer').height(maxHeight).each(function(){
            $(this).find('.carousel > li').height(maxHeight);
        });

    };
    

}
// end module comboCarousel