// begin module heroCarousel 
if(typeof(initModuleHeroCarousel) == 'undefined')
{

    var initModuleHeroCarousel = function($module, options, css){
        var options = options || {};
        var debug = function(msg){
            if(!options.debug)return;
            try {
                if($.browser.mozilla){
                    console.log(msg);
                }
            } catch(e) {
                alert(msg);
            }
        }
        
        var startImages = function(){
            $imgs = $module.find('ul.carousel li img.displayNone');
            $srcs = $module.find('ul.carousel li input:hidden');
            var idx = 0;
            var loadImage = function(){
                var $img = $imgs.eq(idx);
                var $src = $srcs.eq(idx);
                debug('loading ' + $src.attr('value'));
                $img.load(function(){
                    $(this).removeClass('displayNone').siblings('img').hide();
                    if($imgs.length > idx+1){
                        idx++; 
                        loadImage();
                    }
                }).attr('src', $src.attr('value'));
            };
            if($imgs.length) loadImage();
        };
        
        var calcHeight = function(){
            var heights = $(this).find('.carousel > li').map(function(){
                return $(this).height();
            }).sort();
            var height = heights[heights.length-1];
            // sanity check to prevent heights > max hero height of 780px
            if(height > 780) height = $(this).find('.carousel > li > img:not(.loader)').eq(0).attr('height');;
            $(this).find('.carousel > li').height(height);
        };
        
        var trackNext = function(){
            if(options.googleID) gaTrack(options.googleID, "clickNext");
        };
        var trackPrev = function(){
            if(options.googleID) gaTrack(options.googleID, "clickPrev");
        };
    
        //options = options || {};
        var toHover = null; // caption hover timeout
        
        // Controls the Caption Hover
        $module.find('.carousel li img').hover(function(){
        
            if($.browser.msie && $.browser.version <= 6){
                $(this).siblings('span').show().addClass('show');
                
                return false;
            }
            debug('on img');
            
            clearTimeout(toHover);
            var $siblings = $(this).siblings('span');
            if($siblings.eq(0).is(':visible')) return;
            toHover = setTimeout(function(){
                debug('fading in');
                
                $siblings.hide().addClass('show').fadeIn('fast');
                clearTimeout(toHover);
            }, 1000);
        }, function() {
            if($.browser.msie && $.browser.version <= 6){
                $(this).siblings('span').hide().removeClass('show');
                return false;
            }
            debug('off img');

            var $siblings = $(this).siblings('span');
            clearTimeout(toHover);
            toHover = setTimeout(function(){
                debug('fading out');
                
                $siblings.fadeOut('fast', function(){$(this).removeClass('show').hide();});
            }, 1000);
        });
        
        /* This prevents flicker when rolling over image from bottom. */
        $module.find('.carousel li span').hover(function(){
            if($.browser.msie && $.browser.version <= 6){
                $(this).show().addClass('show');
                return false;
            }
            clearTimeout(toHover);
            $(this).addClass('show');
        }, function() {
            if($.browser.msie && $.browser.version <= 6){
                $(this).hide().removeClass('show');
                return false;
            }
            debug('off span');
            $span = $(this);
            toHover = setTimeout(function(){
                debug('fading out (from span hoverOut())');

                $span.fadeOut('fast', function(){
                    $span.removeClass('show');
                });
            }, 1000);
        });
        var lis = $module.find('.carousel li').show();
        css = merge({ // backup css properties
            //left: $module.css('left'), // reading this 
            left: '50%',
            position: $module.css('position'),
            width: $module.width()
        }, css);
        
        // set background color on all carousel items that contains img elements and hidden elements
        $module.find('.carousel > li:has(img + :hidden)').css('background-color', '#f2f2f2');
        
        // force height of all li's to that of the tallest li
        $module.each(calcHeight);
        
        // queue the image loader for 1 second from now
        setTimeout(startImages, 1000);
        var hash = merge({
            btnPrev: $module.closest('.container').find('.productNav a.prev'),
            btnNext: $module.closest('.container').find('.productNav a.next'),
            btnGo: null,
            mouseWheel: false,
            auto: false,
            speed: 500,
            easing: null,
            vertical: false,
            circular: true,
            visible: 1,
            start: 0,
            scroll: 1,
            beforeStart: function(a){},
            afterEnd: function(a){}
        }, options);
        $(hash.btnNext).click(trackNext);
        $(hash.btnPrev).click(trackPrev);
        // initialize the carousel
        return $module.jCarouselLite(hash).css(css).each(calcHeight);
    };

}
// end module heroCarousel 
