var clients = 
{
    direction: 'left',
    cursor_offset: null,
    left_displacement: 0,
    speed_mod: 3,
    width: 942,
    scroll_method: 'margin-left',

    $container: '#clients',
    $container_in: '#clients_in', // #clients ul
    $elements: '#clients_in img', // #clients ul li
    element_width: 3705,
    element_template: '<img src="test.png" alt="" />',
    step: -1,

    init: function()
    {
        var that = this;

        var $clients = $('#clients');

        this.width = $clients.width();
        this.half_position = Math.round(this.width / 2);
        this.m = Math.round( (this.element_width / 2) - this.half_position );
        this.step = -1;

        this.clone_pos = {
            right: this.element_width - this.width
            //left: 
        };

        // enter
        $clients.mouseenter(function(e) 
        {
            that.speed_mod = 3;
            that.direction = that.getCurInfo(e, this).direction;
            that.stopAnimation();
            that.startAnimation();
        });


        // leave
        $clients.mouseleave(function(e) 
        {
            that.speed_mod = 1;
            that.stopAnimation();
            that.startAnimation();
        });
        
        // move
        $clients.mousemove(function(e) 
        {
            var newdirection = that.getCurInfo(e, this).direction;

            if (that.direction != newdirection)
            {
                that.direction = newdirection;
                that.stopAnimation();
                that.startAnimation();
            }

        });
        this.center = Math.round((this.element_width/2) - (this.width/2));

    },


    /**
     * Выполняет анимацию
     */
    startAnimation: function() 
    {
        var that = this;
        var width = 100;
        var speed = 2000;
        var sign = (that.direction == 'left') ? '-' : '+';

        var milisecs = Math.round(speed / width);
        var pixels = 1 * that.speed_mod;

        window.anim = setInterval(function ()
        {
            var left = parseInt($('#clients_in').css(clients.scroll_method));
            var left_displacement = (sign == '-') ? left - pixels : left + pixels;
            var c = 0;
            // признак нового шага
            var new_step = parseInt(left_displacement / that.element_width);
            if (new_step != that.step)
            {
                if (sign == '-')
                {
                    // right half
                    c = that.element_width;
                }
                else
                {
                    // left half
                    c = -that.element_width;
                }
                this.step = new_step;
            }

            //left_displacement += c;
            $('#clients_in').css(clients.scroll_method, left_displacement += c);
        }, 
        milisecs);
    },


    /**
     * Останавливает анимацию
    */
    stopAnimation: function()
    {
        clearInterval(window.anim);
    },


    /**
     * Получает информацию о текущем состоянии анимации
     */
    getCurInfo: function(e, o)
    {
        var cursor_offset = e.pageX - o.offsetLeft;
        var direction = (cursor_offset > clients.half_position) ? 'left' : 'right';
        return {
            'cursor_offset': cursor_offset,
            'direction': direction
        };
    }
    
}

function isInteger(s) {
    return (s % 1 == 0);
}


function get_dimensions()
{
     var dimensions = {width: 0, height: 0};
     if (document.documentElement) 
     {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     }
     else if (window.innerWidth && window.innerHeight)
     {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     }
     return dimensions;

}
