﻿(function($) {
    /*
    * Defaults Object
    * @param speed - ticker speed between 0 - 1000, depends on the element size 
    */
    var defaults =
  {
      speed: 25
  }

    Ticker = function(input, options) {
        var base,
        $input,
        config,
        stripWidth = 0,
        containerWidth;

        var init = function() {
            // To avoid scope issues, use 'base' instead of 'this'
            base = this;

            //get current input element as jquery object
            $input = $(input);

            // Add a reverse reference to the DOM object
            $input.data("Ticker", base);

            // Extend config with defults and options passed from the user 
            config = $.extend({}, defaults, options);

            // Calc container width
            containerWidth = $input.parent().width();

            //Set overflow hidden to the contianer
            $input.parent().css("overflow", "hidden");

            //Calc Ul full width
            $input.find("li").each(function(i) {
                stripWidth += $(this, i).outerWidth() + 100;
            });

            //Set ul full width
            $input.width(stripWidth);

            //bind mouse events 
            bindEvents();

            //start the ticker
            animator($input.children(":first"));
        }

        bindEvents = function() {
            //Set mouseenter  
            $input.mouseenter(function() {
                //Stop current animation  
                $input.children().stop();
            });

            //Set mouseleave  
            $input.mouseleave(function() {
                //Resume animation  
                animator($input.children(":first"));
            });
        }

        //animator function
        animator = function(currentItem) {

            //work out new anim duration
            var distance = currentItem.outerWidth();
            duration = (distance + parseInt(currentItem.css("marginLeft"))) / (config.speed * 0.001);

            //animate the first child of the ticker
            currentItem.animate({ marginLeft: -distance }, duration, "linear", function() {

                //move current item to the end of the list
                currentItem.appendTo(currentItem.parent());

                //get the last element
                var lastElement = currentItem.parent().children(":last");

                //check if we need add extra space before the last element to prevent jamps
                if (lastElement.position().left + lastElement.outerWidth() < containerWidth) {
                    currentItem.css("marginLeft", containerWidth - lastElement.position().left);
                }
                else {
                    currentItem.css("marginLeft", 0);
                }

                //recurse
                animator(currentItem.parent().children(":first"));
            });
        };

        init();
    }

    $.fn.ticker = function(options) {

        return this.each(function() {
            var tickerInstance = new Ticker(this, options);
        });
    };
})(jQuery);


