﻿// begin module carousel
/**
* Setup a carousel.
* 
* takes the jQuery object of the div surrounding the carousel.
*/
if (typeof (initModuleCarousel) == 'undefined') {


    var initModuleCarousel = function ($module, options, css) {

        var debug = function (msg) {
            if (!options.debug) return;
            if (!$.browser.mozilla) return alert(msg);
            try {
                console.log(msg);
            } catch (e) {
                alert(msg);
            }
        };

        options = merge({
            vertical: false,
            googleID: 'carousel'
        }, options || {});

        var getTallest = function ($carousel) {
            var heights = [];
            $carousel.find('li').each(function () {
                heights.push($(this).height());
            });
            return parseInt(heights.sort(function (a, b) {
                return a < b ? 1 : -1;
            })[0]);
        };

        var isIE = function () {
            return $.browser.msie;
        };

        return $module.each(function () {

            var lis = $module.find('.carousel li');
            var vis = options.vertical ? Math.floor($module.height() / lis.eq(0).height()) : Math.floor($module.width() / lis.eq(0).width());
            //console.log($module.attr('id') + ' ' + vis);

            var hash = merge({
                btnPrev: $module.closest('.module').find('a.btnCarousel.prev'),
                btnNext: $module.closest('.module').find('a.btnCarousel.next'),

                btnGo: null,
                mouseWheel: false,
                auto: false,

                speed: 500,
                easing: null,

                vertical: false,
                circular: true,
                visible: vis || 1,
                start: 0,
                scroll: 1,

                beforeStart: function (a) {
                    if (hash.circular) return;
                    //debug('beforeStart() > calling checkButtons');
                    //checkButtons();
                },
                afterEnd: function (a) {
                    // tracking code here - log new image url.
                    if (hash.circular) return;
                    debug('afterEnd() > calling checkButtons');
                    checkButtons();
                },

                prevDisabledClass: 'disabledUp',
                nextDisabledClass: 'disabledDown'

            }, options);

            try {
                if (options.googleID) {
                    $(hash.btnPrev).click(function () {
                        gaTrack(options.googleID, "clickPrev");
                    });
                    $(hash.btnNext).click(function () {
                        gaTrack(options.googleID, "clickNext");
                    });
                }
            } catch (e) {

            }

            var opts = {
                total: lis.length,
                cur: hash.start || 1,
                window: hash.visible,
                prevDisabledClass: hash.prevDisabledClass,
                nextDisabledClass: hash.nextDisabledClass
            };

            var checkButtons = function () {
                debug('in checkButtons(): opts.cur:' + opts.cur + ' hash.scroll:' + hash.scroll + ' opts.total:' + opts.total);
                if (opts.cur <= hash.scroll) {
                    debug('btnPrev: adding class ' + opts.prevDisabledClass);
                    $(hash.btnPrev).addClass(opts.prevDisabledClass);
                } else {
                    debug('btnPrev: removing class ' + opts.prevDisabledClass);
                    $(hash.btnPrev).removeClass(opts.prevDisabledClass);
                }

                if (opts.cur + opts.window >= opts.total) {
                    debug('btnNext: adding class ' + opts.nextDisabledClass);
                    $(hash.btnNext).addClass(opts.nextDisabledClass);
                } else {
                    debug('btnNext: removing class ' + opts.nextDisabledClass);
                    $(hash.btnNext).removeClass(opts.nextDisabledClass);
                }
            };

            $(hash.btnPrev).click(function () {
                if (hash.circular) return false;
                opts.cur = parseInt(opts.cur) - parseInt(hash.scroll);
                debug('DECREMENTING: opts.cur -= hash.scroll : ' + opts.cur);
                if (opts.cur < 1) opts.cur = 1;
                return false;
            });
            $(hash.btnNext).click(function () {
                if (hash.circular) return false;
                if (opts.cur + opts.window < opts.total) {
                    opts.cur = parseInt(opts.cur) + parseInt(hash.scroll);
                    debug('INCREMENTING: opts.cur += hash.scroll : ' + opts.cur);
                }
                if (opts.cur > opts.total) opts.cur = opts.total;
                return false;
            });

            lis.show(); // show all LI elements for correct carousel calculation
            checkButtons(); // disabled/enable appropriate buttons

            var height = getTallest($(this).find('.carousel'));
            debug('getTallest() = ' + height);
            if (lis.length <= vis) {
                with ($(hash.btnPrev).click(function () { return false; })) {
                    if (!hash.leaveButtons) hide();
                }
                with ($(hash.btnNext).click(function () { return false; })) {
                    if (!hash.leaveButtons) hide();
                }
                $module.find('li').height(height).css({ display: 'block' });
                return $module;
            }

            return $module.jCarouselLite(hash).css(merge({}, css || {})).find('li').height(height).end().each(function () {
                if (isIE() && hash.iebug) {
                    var total = $(this).find('li').length;
                    $module.height((hash.visible * height) + ((total / hash.visible) / 2 * height));
                }
            });
        });
    }

}
// end module carousel
