Scroll = function (scroller, scroller_bar, menu)
{
    this.canDrag = false;
    this.prepared = false;
    this.shift_x;
    this.delta = 0;
    this.scroller = scroller;
    this.scrollerBar = scroller_bar;
    this.menu = menu;
    this.scrollerStartShift;
    this.menuStartShift;
    this.scrollerTrackWidth = 560;
    this.menuTrackWidth;
    this.scrollerWidth;
    this.menuWidth = 600;
    this.step;
    this.dontmove = false;
    this.a = false;

    this.prepare = function()
    {
        if(get(this.scroller) && get(this.menu))
        {
            this.scroller = get(this.scroller);
            this.scrollerBar = get(this.scrollerBar);
            this.menuWidth = parseInt((''+this.scrollerBar.style.width).replace('px', ''));
            this.menu = get(this.menu);
            this.scrollerStartShift = parseInt(this.scroller.style.left);
            this.menuStartShift = parseInt(this.menu.style.marginLeft);
            this.menuTrackWidth = parseInt((''+this.menu.style.width).replace('px', '')) + this.menuStartShift;
            this.scrollerWidth = Math.round( (this.menuWidth * this.scrollerTrackWidth) / this.menuTrackWidth );
            this.scrollerWidth = (this.scrollerWidth < 16) ?  16 : this.scrollerWidth;
            this.scrollerWidth = (this.scrollerWidth > this.scrollerTrackWidth) ?  this.scrollerTrackWidth : this.scrollerWidth;
            this.scroller.style.paddingRight = this.scrollerWidth - 8 + "px";
            this.scrollerTrackWidth -= this.scrollerWidth;
            this.menuTrackWidth -= this.menuWidth;
            this.scrollerTrackWidth = parseInt((''+this.scroller.style.width).replace('px',''));
            this.delta = this.menuTrackWidth / this.scrollerTrackWidth;
            this.prepared = true;
        }
        return false;
    }

    this.fixForBrowsers = function(event)
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        if(event.stopPropagation) event.stopPropagation();
        else event.cancelBubble = true;
        if(event.preventDefault) event.preventDefault();
        else event.returnValue = false;
    }

    this.setStep = function()
    {
        step = Math.round(this.scrollerWidth / 3 * 2);
        //this.step = Math.round(this.menu.getElementsByTagName("td")['0'].offsetWidth * this.scrollerTrackWidth / this.menuTrackWidth);    
    }

    this.setPosition = function(newPosition)
    {
        if(newPosition <= this.scrollerTrackWidth + this.scrollerStartShift && newPosition >= this.scrollerStartShift)
        {
            this.scroller.style.left = newPosition + "px";
        }
        else
        {
            if(newPosition >= this.scrollerTrackWidth + this.scrollerStartShift)
            {        
                this.scroller.style.left = this.scrollerTrackWidth + this.scrollerStartShift + "px";
            }
            if(newPosition < this.scrollerStartShift)
            {
                this.scroller.style.left = this.scrollerStartShift + "px";
            }
        }
        this.menu.style.marginLeft = Math.round( (parseInt(this.scroller.style.left) - this.scrollerStartShift) * this.delta * (-1) ) + this.menuStartShift + "px";
        return false;
    }

    this.drag = function(event)
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        if (this.prepared)
        {
            this.canDrag = true;
            this.shift_x = event.clientX - parseInt(this.scroller.style.left);
            this.fixForBrowsers(event);
        }    
        return false;
    }

    this.movescroller = function(event)
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        if (this.prepared && !this.dontmove)
        {
            this.setStep();
            var clickX = event.layerX ? event.layerX : event.offsetX;
            var currentPosition = parseInt(this.scroller.style.left);               
            var i = (clickX > currentPosition) ? 1 : -1;
            var newPosition = 2*i*this.step + parseInt(this.scroller.style.left); 
            this.setPosition(newPosition);
            this.fixForBrowsers(event);
        }
        else
        {
            this.dontmove = false;
        }
        return false;
    }

    this.move = function(event)
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        if (this.prepared && this.canDrag)
        {
            this.setPosition(event.clientX-this.shift_x);
            this.fixForBrowsers(event);
        }
        return false;
    }

    this.drop = function()
    {
        this.canDrag=false; 
    }
    
    this.scrollerClickHandler = function()
    {
        this.dontmove=true;    
    }    

    this.handle = function(delta, event) 
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        var i = (delta < 0) ? 1 : -1;
        this.setStep()
        var currentPosition = parseInt(this.scroller.style.left);               
        var newPosition = i*this.step + currentPosition; 
        this.setPosition(newPosition);        
        this.fixForBrowsers(event);        
    }

    this.cancelWheelAction = function(event)
    {
        if (!event)
        {
            // For IE.
            event = window.event;
        }
        if (event.preventDefault)
        {
            event.preventDefault();
        }
        event.returnValue = false;
    }

    this.wheel = function(event)
    {
        var delta = 0;
        if (!event)
        {
            event = window.event;
        }
        if (event.wheelDelta) 
        {
            delta = event.wheelDelta/120;
            if (window.opera)
            {
                delta = delta;
            }
        } 
        else if (event.detail) 
        {
            delta = -event.detail/3;
        }
        if (delta)
        {
            this.handle(delta, event);
            this.cancelWheelAction(event);
            this.fixForBrowsers(event);
            return false;
        }
    }
}

function get(id)
{
    return document.getElementById(id);
}
function handleOnMouseUp(event)
{
    first.drop(event);
}
function handleOnMouseMove(event)
{
    first.move(event);
}

// first
function handleOnClickBarFirst(event) 
{
    first.movescroller(event);
}
function handleOnMouseDownFirst(event)
{
    first.drag(event);
}
function handleOnClickFirst(event) 
{
    first.scrollerClickHandler(event);
}
function handleOnMouseWheelFirst(event)
{
    first.wheel(event);
}
