﻿/************************************** Doggfight Ltd *******************************
* Created By:		Steve Doggett
* Creation Date:	30 October 2011
* Edited ----------------------------------------------------------------------------
*      By:              On:  
* Description -----------------------------------------------------------------------
*      
*
* Public Methods --------------------------------------------------------------------
*       Init
*       
* Private Functions -----------------------------------------------------------------
*       _init
*       _initButtons
*       
* Event Handlers --------------------------------------------------------------------
*   	_nextQuote
*
* Usage -----------------------------------------------------------------------------
*       var testimonials = new Testimonials().Init({
*		    selector: '.testimonialQuote',
*		    prevBtnSelector: '.prev a',
*		    nextBtnSelector: '.next a'
*		});
**************************************************************************************/

function Testimonials() {
/************************************** Variables ************************************/
    var _arrQuotes = null;
    var _currQuoteIdx = 0;
    var _settings = {
        selector: '',
        prevBtnSelector: '',
        nextBtnSelector: ''
    };

/********************************* Private Functions *********************************/
    function _init(params) {
        _settings = $.extend(_settings, params);

        _arrQuotes = $(_settings.selector);
        if (_arrQuotes.length == 0) {
            return;
        }

        $(_settings.selector + ':gt(0)').hide();

        if (_arrQuotes.length == 0) {
            alert('No tesimonials found on the page');
        }

        _initButtons();
    }

    function _initButtons() {
        $(_settings.nextBtnSelector).click(function () {
            _nextQuote(1);
            return false;
        });

        $(_settings.prevBtnSelector).click(function () {
            _nextQuote(-1);
            return false;
        });
    }

/********************************** Event Handlers **********************************/
    function _nextQuote(dir, nextClicked) {
        if((dir > 0 && $(_settings.nextBtnSelector).hasClass('on'))
            || (dir < 0 && $(_settings.prevBtnSelector).hasClass('on'))) {
            var newIdx = _currQuoteIdx + dir;

            if (newIdx > _arrQuotes.length - 1) {
                newIdx = 0;
            }
            if (newIdx < 0) {
                newIdx = _arrQuotes.length - 1;
            }

            $(_arrQuotes).eq(_currQuoteIdx).fadeOut('fast', function () {
                $(_arrQuotes).eq(newIdx).fadeIn('fast');
                _currQuoteIdx = newIdx;
            });


            if (newIdx == 0) {
                $(_settings.prevBtnSelector).removeClass('on');
                _initButtons(true, false);
            }
            else if (newIdx == _arrQuotes.length - 1) {
                $(_settings.nextBtnSelector).removeClass('on');
                _initButtons(false, true);
            }
            else {
                $(_settings.prevBtnSelector).addClass('on');
                $(_settings.nextBtnSelector).addClass('on');
                _initButtons(true, true);
            }
        }
    }

/********************************* Public Functions *********************************/
    var thisObj = this;

    this.Init = _init;

    return thisObj;
}


