/*
Title :: adidoCarousel
Author :: Thomas Stradling
Version :: 0.2

Changelog
*/
$.fn.adidoCarousel = function(options) {
    var opts = $.extend({}, $.fn.adidoCarousel.defaults, options);

    return this.each(function() {
        jQuery.easing.def = opts.anim;
        var $this = $(this);
        var lw = $this.children('li').eq(0).outerWidth();
        var lc = $this.children('li').size();
        var c = 1;
        $this.width(lc * lw);
        if (!$this.hasClass('aCarousel')) {
            $this.addClass('aCarousel');
        }
        if (opts.buildFW) {
            $.fn.adidoCarousel.buildFrameWork($this);
        }

        var carouselTimer = setInterval(function() {
            if (c < lc) {
                c += 1;
            } else {
                c = 1;
            }
            $.fn.adidoCarousel.moveCarousel($this, c, lw);
            $.fn.adidoCarousel.movePager(opts.pagerElement, c);
        }, opts.speed);
        if (opts.buttons) {
            $(opts.buttonNext).click(function(e) {
                e.preventDefault();
                if (!opts.clickContinue) {
                    clearInterval(carouselTimer);
                }
                if (c < lc) {
                    c += 1;
                } else {
                    c = 1;
                }
                $.fn.adidoCarousel.moveCarousel($this, c, lw);
                $.fn.adidoCarousel.movePager(opts.pagerElement, c);
            });
            $(opts.buttonPrev).click(function(e) {
                e.preventDefault();
                if (!opts.clickContinue) {
                    clearInterval(carouselTimer);
                }
                if (c > 1) {
                    c -= 1;
                } else {
                    c = lc;
                }
                $.fn.adidoCarousel.moveCarousel($this, c, lw);
                $.fn.adidoCarousel.movePager(opts.pagerElement, c);
            });
        }
        if (opts.pager) {
            // Build a pager system for the carousel
            var n = c + 1;
            $(opts.pagerElement).children('ul').children('li:nth-child(' + n + ')').addClass('acpNext');
            $(opts.pagerElement).children('ul').children('li:nth-child(' + c + ')').addClass('acpActive');
            $(opts.pagerElement).children('ul').children('li').click(function() {
                if (!opts.clickContinue) {
                    clearInterval(carouselTimer);
                }
                c = $(opts.pagerElement).children('ul').children('li').index(this);
                c += 1;
                $.fn.adidoCarousel.moveCarousel($this, c, lw);
                $.fn.adidoCarousel.movePager(opts.pagerElement, c);
            }).css('cursor', 'pointer');
            $(opts.pagerElement).children('ul').children('li').children('a').click(function(e) {
                e.preventDefault();
            });
        }
    });
};

$.fn.adidoCarousel.defaults = {
    anim: 'easeInOutExpo',
    buildFW: false,
    buttons: true,
    buttonNext: '.carouselNext',
    buttonPrev: '.carouselPrev',
    clickContinue: false,
    pager: false,
    pagerAuto: true,
    pagerElement: '.carouselPager',
    speed: 5000
};

$.fn.adidoCarousel.moveCarousel = function(e, c, w) {
    e.animate({
        left: -((c - 1) * w) + "px"
    }, 500);
}
$.fn.adidoCarousel.movePager = function(e, c) {
    $('.acpActive').removeClass('acpActive');
    $('.acpNext').removeClass('acpNext');
    var n = c + 1;
    $(e).children('ul').children('li:nth-child(' + n + ')').addClass('acpNext');
    $(e).children('ul').children('li:nth-child(' + c + ')').addClass('acpActive');
}
$.fn.adidoCarousel.buildFrameWork = function(e) {
   // var w = $('<div class="carouselContainer"><div class="carouselFrame"></div></div>');
   e.wrap('<div class="carouselFrame"></div>');
   e.parent().wrap('<div class="carouselContainer"></div>');
   var a = $('<a href="#prev" class="carouselPrev">Previous</a> <a href="#next" class="carouselNext">Next</a>');
   e.parent().parent().append(a);
}
