/**
 * <code>dtContentSlider</code> ..
 *
 * @class dtContentSlider
 **/

/* ........................................................ [Control Options] */

/**
 * Points at the box(es) where this module's functionality should be added.
 * @config selector
 * @type string
 **/

/* ......................................................... [Plugin Options] */

/**
 * Display mode - 'horizontal', 'vertical', or 'fade'.
 * @config mode
 * @type string
 **/

/**
 * Wrap around.
 * @config infiniteLoop
 * @type boolean
 **/

/**
 * Slide transition duration in msec.
 * @config speed
 * @type int
 **/

/**
 * Advance automatically.
 * @config auto
 * @type boolean
 **/

/**
 * Transversal direction if auto mode is enabled - 'prev' or 'next'.
 * @config autoDirection
 * @type string
 **/

/**
 * Pause on mouse over (auto enabled).
 * @config autoHover
 * @type boolean
 **/

/**
 * Initial delay in msec befor the auto mode is started (if auto mode is enabled).
 * @config autoDelay
 * @type int
 **/

/**
 * Pause between transitions (if auto mode is enabled).
 * @config pause
 * @type int
 **/

/**
 * Ticker mode (continuous motion).
 * @config ticker
 * @type boolean
 **/

/**
 * Ticker speed (if ticker mode is enabled). Odd inverse behavior. 10000 = very slow, 50 very fast.
 * @config tickerSpeed
 * @type int
 **/

/**
 * Transversal direction if auto mode is enabled - 'prev' or 'next'.
 * @config tickerDirection
 * @type string
 **/

/**
 * Pause on mouse over (ticker enabled).
 * @config tickerHover
 * @type boolean
 **/

/*
 * Class name for the slider wrapper. [breaks with 3.0 - ingore]
 * --config wrapperClass
 * --type string
 */

/**
 * Index of the first slide to show (0 based).
 * @config startingSlide
 * @type int
 **/

/**
 * Number of slides to display at once.
 * @config displaySlideQty
 * @type int
 **/

/**
 * Number of slides to move at once.
 * @config moveSlideQty
 * @type int
 **/

/**
 * Start with a random slide.
 * @config randomStart
 * @type boolean
 **/

/*jslint strict:false*/
/*global DT:false*/

DT.CORE.register('dtContentSlider', function(io, $) {
    return {
        // check if the bxSlider plugin was loaded
        init: function() {
            if (typeof $().bxSlider !== 'function') {
                io.logError('bxSlider plugin not loaded!');
            }
        },
        initEach: function(options) {
            var slider,
				sliderOptions = $.extend({}, options),
				container = $(options.selector),
				prev = container.find('.prev'),
				next = container.find('.next'),
				pager = container.find('.paging-panel');

            container.show();
            
            // remove irrelevant properties
            delete sliderOptions.selector;

            // attach onAfterSlide callback function
            sliderOptions.onAfterSlide = function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject) {
                if (sliderOptions.infiniteLoop) {
                    return;
                }
                if (totalSlideQty - sliderOptions.displaySlideQty <= currentSlideNumber) {
                    next.addClass('disabled');
                } else {
                    next.removeClass('disabled');
                }
                if (!currentSlideNumber) {
                    prev.addClass('disabled');
                } else {
                    prev.removeClass('disabled');
                }
            };

            // disable default controls
            sliderOptions.controls = false;

            // disable default pager
            sliderOptions.pager = false;

            io.logDebug(sliderOptions);

            slider = container.find('ul').bxSlider(sliderOptions);

            // public method calls - these won't do anything if the buttons and/or pager doesn't exist
            prev.click(function(e) {
                if (!$(this).hasClass('disabled')) {
                    slider.goToPreviousSlide();
                    e.preventDefault();
                }
            });
            next.click(function(e) {
                if (!$(this).hasClass('disabled')) {
                    slider.goToNextSlide();
                    e.preventDefault();
                }
            });
            pager.find('a').click(function(e) {
                var a = $(this), parent = a.parent(),
					pagerIndex = parent.find('a').index(this);
                slider.goToSlide(pagerIndex);
                parent.find('a:eq(' + pagerIndex + ')').removeClass('active');
                a.addClass('active');
                e.preventDefault();
            });
            
        }
    };
});
