/*
Title :: adidoCarousel
Author :: eokwusi
Version :: 0.3.3

   
*/

//
(function($) {
    //
    $.fn.adidoCarousel = function(options) {
        var opts = $.extend({}, $.fn.adidoCarousel.defaults, options);
        return this.each(function() {
            jQuery.easing.def = opts.anim;
            var $this = $(this);

            /* List count */var lc = $this.children('li').size();
            /* Current item index */var c = opts.startPosition;
            /* Offset */var os = -1 + opts.step;
            /* Rotation count */var r = lc - os;

            /* info array */
            var _info = new Array();
            _info['lc'] = lc;
            _info['c'] = c;
            _info['os'] = os;
            _info['r'] = r;

            if (opts.horizontal) {
                var d = 'left';
                var ls = $this.children('li').eq(0).outerWidth();
                $this.width(lc * ls);
            } else {
                var d = 'top';
                var ls = $this.children('li').eq(0).outerHeight();
                $this.height(lc * ls);
            }

            _info['ls'] = ls;

            if (!$this.hasClass('aCarousel')) {
                $this.addClass('aCarousel');
            }

            var carouselTimer = setInterval(function() {
                if (c < r) {
                    c += opts.step;
                } else {
                    c = opts.startPosition;
                }
                $.fn.adidoCarousel.moveCarousel($this, c, _info, opts);
            }, opts.speed);

            if (opts.buttons) {
                $(opts.buttonNext).click(function(e) {
                    e.preventDefault();
                    if (!opts.clickContinue) {
                        clearInterval(carouselTimer);
                    }
                    if (c < r) {
                        c += opts.step;
                    } else {
                        c = opts.startPosition;
                    }
                    $.fn.adidoCarousel.moveCarousel($this, c, _info, opts);
                });
                $(opts.buttonPrev).click(function(e) {
                    e.preventDefault();
                    if (!opts.clickContinue) {
                        clearInterval(carouselTimer);
                    }
                    if (c > 1) {
                        c -= opts.step;
                    } else {
                        c = r;
                    }
                    $.fn.adidoCarousel.moveCarousel($this, c, _info, opts);
                });
            }

            if (opts.pager) {
                // Build a pager system for the carousel

                // Assigns the selector to a variable allowing easier access / faster performance(?).
                var $pager = $(opts.pagerElement);

                // Creates an un-ordered list automatically, and populates it with a <li> item for each <li> in the carousel.
                if (opts.pagerAuto) {
                    $pager.append('<ul></ul>');
                    var i = 0;
                    for (i = 0; i < lc; i++) {
                        $pager.children('ul').append('<li></li>');
                    }
                }
                // Initialises the pager elements when first loaded.
                var n = c + 1;
                $pager.find('li:nth-child(' + n + ')').addClass('acpNext');
                $pager.find('li:nth-child(' + c + ')').addClass('acpActive');

                $pager.find('li').click(function() {
                    if (!opts.clickContinue) {
                        clearInterval(carouselTimer);
                    }
                    // Prevents jumping behaviour clicking on an item which is currently active.
                    if ($(this).hasClass('acpActive')) {
                        // Do Nothing
                    } else {
                        c = $pager.find('li').index(this);
                        c += 1;
                        $.fn.adidoCarousel.moveCarousel($this, c, _info, opts);
                    }
                }).css('cursor', 'pointer');

                // If custom pager element uses <a> tag as part of the mark up disables default click.
                $pager.find('a').click(function(e) {
                    e.preventDefault();
                });

                var _pagerArray = new Array();

                if (opts.pagerHover) {
                    $pager.find('li').hover(function() {
                        var pi = $pager.find('li').index(this);
                        _pagerArray[pi] = true;
                        if (!opts.clickContinue) {
                            clearInterval(carouselTimer);
                        }
                        if (opts.pagerAnim) {
                            $(this).addClass('acpHover', 500);
                        } else {
                            $(this).addClass('acpHover');
                        }
                        var cp = $pager.find('li').index(this);
                        cp += 1;

                        window.setTimeout(function() {
                            if (_pagerArray[pi]) {
                                c = cp;
                                $.fn.adidoCarousel.moveCarousel($this, c, _info, opts);
                            }
                        }, 2000);

                    }, function() {
                        var pi = $pager.find('li').index(this);
                        _pagerArray[pi] = false;
                        if (opts.pagerAnim) {
                            $(this).removeClass('acpHover', 500);
                        } else {
                            $(this).removeClass('acpHover');
                        }

                    });
                }

            }

            if (opts.imgLarge) {
                var src = $this.children('li:nth-child(' + c + ')').find('img').attr('src');
                if ($(opts.imgContainer).find('img').size() == 0) {
                    $(opts.imgContainer).append('<img />')
                }
                $(opts.imgContainer).find('img').attr('src', src);
            }

            if (opts.resizeMask) {
                var h = 0;
                $this.children('li').each(function() {
                    var hh = $(this).height();
                    if (hh > h) {
                        h = hh;
                    }
                });
                $this.parent().height(h);
            }

            if (opts.hoverPause) {
                $this.parent().hover(function() {
                    /*clearInterval(carouselTimer);*/
                    $this.addClass('hover');
                }, function() {
                    /*var carouselTimer = setInterval(function() {
                    if (c < r) {
                    c += opts.step;
                    } else {
                    c = opts.startPosition;
                    }
                    $.fn.adidoCarousel.moveCarousel($this, c, ls, opts);
                    }, opts.speed);*/
                    $this.removeClass('hover');
                });
            }

            /* Initial item setup */
            $this.children('li:nth-child(' + c + ')').addClass('acActive');

            switch (opts.mode) {
                case 'fade':
                    var fadeCSS = {
                        'left': '0',
                        'position': 'absolute',
                        'top': '0',
                        'z-index': '2'
                    };
                    $this.children('li').css(fadeCSS);
                    $this.children('li:nth-child(' + c + ')').css('z-index', '6');
                    break;
            }

            if (opts.dynamicResize) {
                var drh = $this.children('li:nth-child(' + c + ')').height();
                $this.parent().height(drh);
            }
        });
    };

    $.fn.adidoCarousel.defaults = {
        anim: 'easeInOutExpo',
        buttons: true,
        buttonNext: '.carouselNext',
        buttonPrev: '.carouselPrev',
        clickContinue: false,
        dynamicResize: false,
        horizontal: true,
        hoverPause: false,
        imgLarge: false,
        imgContainer: '.imgContainer',
        mode: 'slide',
        pager: false,
        pagerAuto: true,
        pagerElement: '.carouselPager',
        pagerHover: false,
        pagerAnim: false,
        resizeMask: false,
        speed: 5000,
        startPosition: 1,
        step: 1,
        wrap: false
    };

    $.fn.adidoCarousel.moveCarousel = function(e, c, _a, o) {
        e.children('.acActive').removeClass('acActive');
        e.children('li:nth-child(' + c + ')').addClass('acActive');
        switch (o.mode) {
            case 'slide':
                $.fn.adidoCarousel.animSlide(e, c, _a['ls'], o);
                break;

            case 'fade':
                var l = c - 1;
                if (l == 0) {
                    l = _a['r']
                }
                if (o.horizontal) {
                    var d = 'left';
                } else {
                    var d = 'top';
                }
                e.children('li').each(function() {
                    if ($(this).css('z-index') == 6) {
                        $(this).css('z-index', 4);
                    } else {
                        $(this).css('z-index', 2);
                    }
                });

                // e.children('li:nth-child(' + l + ')').css('z-index', 4);
                e.children('li:nth-child(' + c + ')').hide().css('z-index', 6).fadeIn();

                break;

            default:
                /* If value is over-ridden with an incorrect mode it defaults to using 'slide' animation method */
                $.fn.adidoCarousel.animSlide(e, c, _a['ls'], o);
        }
        if (o.pager) {
            $.fn.adidoCarousel.movePager(o, c);
        }
        if (o.imgLarge) {
            var src = e.children('li:nth-child(' + c + ')').find('img').attr('src');
            if ($(o.imgContainer).find('img').size() == 0) {
                $(o.imgContainer).append('<img />')
            }
            $(o.imgContainer).find('img').attr('src', src);
        }
        if (o.dynamicResize) {
            var h = e.children('li:nth-child(' + c + ')').height();
            e.parent().height(h);
        }

    }

    $.fn.adidoCarousel.animSlide = function(e, c, s, o) {
        if (o.horizontal) {
            e.animate({
                left: -((c - 1) * s) + "px"
            }, 500);
        } else {
            e.animate({
                top: -((c - 1) * s) + "px"
            }, 500);
        }
    }

    $.fn.adidoCarousel.movePager = function(o, c) {
        $e = $(o.pagerElement);
        $e.find('.acpActive').removeClass('acpActive');
        $e.find('.acpNext').removeClass('acpNext');
        var n = c + 1;
        if (o.pagerAnim) {
            $e.find('li:nth-child(' + n + ')').addClass('acpNext', 500);
            $e.find('li:nth-child(' + c + ')').addClass('acpActive', 500);
        } else {
            $e.find('li:nth-child(' + n + ')').addClass('acpNext');
            $e.find('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);
    }
    //
})(jQuery);
//
