﻿/// <reference path="frameworks/jquery-1.7.1-vsdoc.js" />

(function () {
    'use strict';

    $.fn.bannerRotator = function (customConfig) {
        var defaultConfig = { delay: 12000 },
            config = $.extend({}, defaultConfig, customConfig);

        return this.each(function () {
            var $this = $(this),
                $items = $this.find('.items li'),
                $links = $this.find('.links li'),
                $prev = $this.find('.prev'),
                $next = $this.find('.next'),
                index = 0,
                timeout = 0;

            function itemFadeIn() {
                $items
                    .not(this) //ignore jslint
                    .hide();
            }

            function moveTo(newIndex) {
                if (index !== newIndex) {
                    index = newIndex;

                    $links
                        .removeClass('selected')
                        .eq(index)
                        .addClass('selected');

                    $items
                        .stop(true, true)
                        .removeClass('selected')
                        .eq(index)
                        .addClass('selected')
                        .fadeIn('slow', itemFadeIn);
                }
            }

            function move(prev) {
                var newIndex = index + (prev ? -1 : 1);

                if (newIndex < 0) {
                    newIndex = $items.length - 1;
                }

                if (newIndex >= $items.length) {
                    newIndex = 0;
                }

                moveTo(newIndex);
            }

            function autoAdvance() {
                move();

                timeout = setTimeout(autoAdvance, config.delay);
            }

            function stopAdvance() {
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = 0;
                }
            }

            function buttonClick(event) {
                stopAdvance();
                move(event.data.prev);
            }

            function buttonHover(event) {
                $(this) //ignore jslint
                    .stop(true, true)
                    .fadeTo('fast', event.data.fadeTo);
            }

            function buttonInit($button, prev) {
                if ($button.length) {
                    $button
                        .fadeTo(1, 0)
                        .on('mouseenter', { fadeTo: 1 }, buttonHover)
                        .on('mouseleave', { fadeTo: 0 }, buttonHover)
                        .on('click', { prev: prev }, buttonClick);
                }
            }

            function linkClick() {
                stopAdvance();
                moveTo($links.index(this)); //ignore jslint
            }

            if ($items.length > 1) {
                buttonInit($prev, true);
                buttonInit($next, false);

                $links
                    .on('click', linkClick)
                    .first()
                    .addClass('selected');

                $items
                    .hide()
                    .first()
                    .addClass('selected')
                    .show();

                timeout = setTimeout(autoAdvance, config.delay);
            }
        });
    };
}());

