//<![CDATA[

//bannerName, pagingHolder, increment, autoTime, autoJump
function bannerAnimate(options) {
    var jumpToBanner = options.autoJump;
    var numElements = $(options.pagingHolder).size();
    var BannerWidth = numElements * options.increment;
    var currentElement = 1;
    var previousElement = 1;
    var currentPosition = 0;
    var isAnimating = false;
    var isLocked = false;
    var autoTimer;

    auto();

    if (jumpToBanner != "na") {
        currentElement = parseInt(jumpToBanner);
        diff = 1 - currentElement;
        amount = currentPosition + diff * options.increment;
        currentPosition = amount;
        $(options.bannerName).css("left", currentPosition)
    }

    $(options.pagingHolder + ':nth-child(' + jumpToBanner + ')').toggleClass('current');

    $(options.bannerName).css("width", BannerWidth + "px");

    $(options.bannerName).hover(
        function () {
            isLocked = true;
        },
        function () {
            isLocked = false;
        }
      );

        $(options.pagingHolder).hover(
        function () {
            isLocked = true;
        },
        function () {
            isLocked = false;
        }
      );

        $(options.previousArrow).click(function () {
            bannerBackDo();
            return false;
        });

        $(options.nextArrow).click(function () {
        bannerNextDo();
        return false;
    });

    function bannerBackDo() {
        if (isAnimating != true) {
            if (currentElement == 1) {
                previousElement = currentElement;
                currentElement = numElements;
                diff = previousElement - currentElement;
                amount = currentPosition + diff * options.increment;
                currentPosition = amount;
                move(amount, options.bannerName);
            } else {
                previousElement = currentElement;
                currentElement = currentElement - 1;
                diff = previousElement - currentElement;
                amount = currentPosition + diff * options.increment;
                currentPosition = amount;
                move(amount, options.bannerName);
            }
            updateClass(currentElement, previousElement);
        }
    }


    function bannerNextDo() {

        if (isAnimating != true) {
            if (currentElement == numElements) {
                previousElement = currentElement;
                currentElement = 1;
                diff = previousElement - currentElement;
                amount = currentPosition + diff * options.increment;
                currentPosition = amount;

                move(amount, options.bannerName);

            } else {
                previousElement = currentElement;
                currentElement = currentElement + 1;
                diff = previousElement - currentElement;
                amount = currentPosition + diff * options.increment;
                currentPosition = amount;

                move(amount, options.bannerName);
            }
            updateClass(currentElement, previousElement);
        }
    }



    $(options.pagingHolder + ' a').click(function (e) {
        e.preventDefault();  // to stop the div jump
        if (isAnimating != true) {

            window.clearTimeout(autoTimer);
            previousElement = currentElement;
            var itemlist = $(this).parent();

            var indexIs = $(options.pagingHolder).index(itemlist);

            currentElement = parseInt(indexIs + 1);
            diff = previousElement - currentElement;
            amount = currentPosition + diff * options.increment;
            currentPosition = amount;

            move(amount, options.bannerName);
            auto();
            updateClass(currentElement, previousElement);

            return false;
        }
    });

    function updateClass(on, off) {

        if (on != off) { // if button is clicked twoce we dont want to switch the class
            $(options.pagingHolder + ':nth-child(' + off + ')').toggleClass('current');
            $(options.pagingHolder + ':nth-child(' + on + ')').toggleClass('current');
        }
    };


    function auto() {
        if (options.autoTime != "none") {
            autoTimer = window.setTimeout(function () {
                if (!isLocked && !isAnimating) {

                    if (currentElement == numElements) {
                        previousElement = currentElement;
                        currentElement = 1;
                        diff = previousElement - currentElement;
                        amount = currentPosition + diff * options.increment;
                        currentPosition = amount;

                        move(amount, options.bannerName);

                    } else {

                        previousElement = currentElement;
                        currentElement = currentElement + 1;
                        diff = previousElement - currentElement;

                        var amount = currentPosition + diff * options.increment;

                        currentPosition = amount;

                        move(amount, options.bannerName);

                    }
                    updateClass(currentElement, previousElement);
                    auto();
                } else {
                    window.clearTimeout(autoTimer);
                    auto();
                }
            }, options.autoTime);
        } //end auto none if
    }


    /*  function move(amount, objectName){
    isAnimating = true;
    $("#"+objectName).parent().fadeOut('fast', function(){
    $("#"+objectName).css('left',amount);
    $("#"+objectName).parent().fadeIn(200);
    isAnimating= false;
    });
    };*/

    function move(amount, objectName) {
        isAnimating = true;
        /*$(".image-info-panel").slideUp(350, "easeOutCubic", function () {
            isAnim = false;
        });*/
            $(objectName).animate({ left: amount }, 500, 'easeInCubic', function () { isAnimating = false; });
        
    };

};






//]]>
  

