﻿
jQuery.fn.crawlLine = function(_options) {
    // defaults options
    var _options = jQuery.extend({
        speed: 3,
        crawElement: 'div',
        textElement: 'p',
        hoverClass: 'viewText'
    }, _options);

    return this.each(function() {
        var _THIS = jQuery(this);
        var _el = $(_options.crawElement, _THIS).css('position', 'relative');
        var _text = $(_options.textElement, _THIS);
        var _clone = _text.css('whiteSpace', 'nowrap').clone();
        var _elWidth = 0;
        var _k = 1;

        // set parametrs *******************************************************
        var _textWidth = 0;
        _text.each(function() {
            _textWidth += $(this).outerWidth(true);
        });
        var _duration = _textWidth * 50 / _options.speed;
        _el.append(_clone);
        _el.css('width', _textWidth * 2);

        var animate = function() {
            _el.animate({ left: -_textWidth }, { queue: false, duration: _duration * _k, easing: 'linear', complete: function() {
                _el.css('left', '0');
                _k = 1;
                animate();
            }
            })
        }
        animate();

        _THIS.hover(function() {
            /*_el.stop();*/
            _THIS.addClass(_options.hoverClass);
        }, function() {
            _THIS.removeClass(_options.hoverClass);
            /*_k = (_textWidth + parseInt(_el.css('left'))) / _textWidth;
            animate();*/
        });
    });
}











