Scroll = function (scroller, scroller_bar, menu)
{
    this.canDrag = false;
    this.prepared = false;

    this.shift_x;
    this.delta;

    this.scroller = scroller;
    this.scrollerBar = scroller_bar;
    this.menu = menu;

    this.scrollerStartShift;
    this.menuStartShift;

    this.scrollerTrackWidth = 560;
    this.menuTrackWidth;

    this.scrollerWidth;
    this.menuWidth = 500;

    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.scroller_bar);
            this.menu = get(this.menu);
            
            this.scrollerStartShift = parseInt(this.scroller.style.left);
            this.menuStartShift = parseInt(this.menu.style.marginLeft);
            
            this.menuTrackWidth = this.menu.offsetWidth + 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.delta = this.menuTrackWidth / this.scrollerTrackWidth;

            this.prepared = true;
        }
        return false;
    }

    this.fixForBrowsers = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if(event.stopPropagation) event.stopPropagation();
        else event.cancelBubble = true;
        if(event.preventDefault) event.preventDefault();
        else event.returnValue = false;
    }

    this.setStep = function()
    {
        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)
        {
            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)
        {
            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)
        {
            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)
        {
            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)
        {
            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;
        }
    }
}

ScrollWithBugs = function (scroller, scroller_bar, menu)
{
    this.canDrag = false;
    this.prepared = false;

    this.shift_x;
    this.delta;

    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.scroller_bar);
            this.menu = get(this.menu);
            
            this.scrollerStartShift = parseInt(this.scroller.style.left);
            this.menuStartShift = parseInt(this.menu.style.marginLeft);
            
            this.menuTrackWidth = this.menu.offsetWidth + 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.delta = this.menuTrackWidth / this.scrollerTrackWidth;

            this.prepared = true;
        }
        return false;
    }

    this.setStep = function()
    {
        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)
        {
            event = window.event;
        }
        if (this.prepared)
        {
            this.canDrag = true;
            this.shift_x = event.clientX - parseInt(this.scroller.style.left);
        }    
        return false;
    }

    this.movescroller = function(event)
    {
        if (!event)
        {
            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);
        }
        else
        {
            this.dontmove = false;
        }
        return false;
    }

    this.move = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if (this.prepared && this.canDrag)
        {
            this.setPosition(event.clientX-this.shift_x);
        }
        return false;
    }

    this.drop = function()
    {
        this.canDrag=false; 
    }
    
    this.scrollerClickHandler = function()
    {
        this.dontmove=true;    
    }    

    this.handle = function(delta, event) 
    {
        if (!event)
        {
            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.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);
            return false;
        }
    }
}

VerticalScroll = function (scroller, scroller_bar, menu)
{
    this.canDrag = false;
    this.prepared = false;

    this.shift_y;
    this.delta;

    this.scroller = scroller;
    this.scrollerBar = scroller_bar;
    this.menu = menu;

    this.scrollerStartShift;
    this.menuStartShift;

    this.scrollerTrackWidth = 200;
    this.menuTrackWidth;

    this.scrollerWidth;
    this.menuWidth = 240;

    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.scroller_bar);
            this.menu = get(this.menu);
            
            this.scrollerStartShift = parseInt(this.scroller.style.top);
            this.menuStartShift = parseInt(this.menu.style.marginTop);
            
            this.menuTrackWidth = this.menu.offsetHeight + 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.paddingBottom = this.scrollerWidth - 8 + "px";
            
            this.scrollerTrackWidth -= this.scrollerWidth;
            this.menuTrackWidth -= this.menuWidth;
            
            this.delta = this.menuTrackWidth / this.scrollerTrackWidth;

            this.prepared = true;
        }
        return false;
    }

    this.fixForBrowsers = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if(event.stopPropagation) event.stopPropagation();
        else event.cancelBubble = true;
        if(event.preventDefault) event.preventDefault();
        event.returnValue = false;
    }

    this.setStep = function()
    {
        this.step = Math.round(this.menu.getElementsByTagName("td")['0'].offsetHeight * this.scrollerTrackWidth / this.menuTrackWidth);    
    }

    this.setPosition = function(newPosition)
    {
        if(newPosition <= this.scrollerTrackWidth + this.scrollerStartShift && newPosition >= this.scrollerStartShift)
        {
            this.scroller.style.top = newPosition + "px";
        }
        else
        {
            if(newPosition >= this.scrollerTrackWidth + this.scrollerStartShift)
            {        
                this.scroller.style.top = this.scrollerTrackWidth + this.scrollerStartShift + "px";
            }
            if(newPosition <= this.scrollerStartShift)
            {
                this.scroller.style.top = this.scrollerStartShift + "px";
            }
        }
        this.menu.style.marginTop = Math.round( (parseInt(this.scroller.style.top) - this.scrollerStartShift) * this.delta * (-1) ) + this.menuStartShift + "px";
        return false;
    }

    this.drag = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if (this.prepared)
        {
            this.canDrag = true;
            this.shift_y = event.clientY - parseInt(this.scroller.style.top);
            this.fixForBrowsers(event);
        }    
        return false;
    }

    this.movescroller = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if (this.prepared && !this.dontmove)
        {
            this.setStep();
            var clickY = event.layerY ? event.layerY : event.offsetY;
            var currentPosition = parseInt(this.scroller.style.top);               
            var i = (clickY > currentPosition) ? 1 : -1;
            var newPosition = 2*i*this.step + parseInt(this.scroller.style.top); 
            this.setPosition(newPosition);
            this.fixForBrowsers(event);
        }
        else
        {
            this.dontmove = false;
        }
        return false;
    }

    this.move = function(event)
    {
        if (!event)
        {
            event = window.event;
        }
        if (this.prepared && this.canDrag)
        {
            this.setPosition(event.clientY-this.shift_y);
            this.fixForBrowsers(event);
        }
        return false;
    }

    this.drop = function()
    {
        this.canDrag=false; 
    }
    
    this.scrollerClickHandler = function()
    {
        this.dontmove=true;    
    }    

    this.handle = function(delta, event) 
    {        
        if (!event)
        {
            event = window.event;
        }
        var i = (delta < 0) ? 1 : -1;
        this.setStep()
        var currentPosition = parseInt(this.scroller.style.top);               
        var newPosition = i*this.step + currentPosition; 
        this.setPosition(newPosition);        
        this.fixForBrowsers(event);        
    }

    this.cancelWheelAction = function(event)
    {
        if (!event)
        {
            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 = event.wheelDelta/40;
            }
        } 
        else if (event.detail) 
        {
            delta = -event.detail;
        }
        if (delta)
        {
            this.handle(delta, event);
            this.fixForBrowsers(event);
            return false;
        }
    }
}

function get(id)
{
    return document.getElementById(id);
}
function handleOnMouseUp(event)
{
    first.drop(event);
    third.drop(event);
    thirdFixed.drop(event);
    vertical.drop(event);
}
function handleOnMouseMove(event)
{
    first.move(event);
    third.move(event);
    thirdFixed.move(event);
    vertical.move(event);
}

function handleOnClickBarFirst(event) 
{
    first.movescroller(event);
}
function handleOnMouseDownFirst(event)
{
    first.drag(event);
}
function handleOnClickFirst(event) 
{
    first.scrollerClickHandler(event);
}
function handleOnMouseWheelFirst(event)
{
    first.wheel(event);
}

function handleOnClickBarVertical(event) 
{
    vertical.movescroller(event);
}
function handleOnMouseDownVertical(event)
{
    vertical.drag(event);
}
function handleOnClickVertical(event) 
{
    vertical.scrollerClickHandler(event);
}
function handleOnMouseWheelVertical(event)
{
    vertical.wheel(event);
}


function handleOnMouseDownThird(event)
{
    third.drag(event);
}

function handleOnMouseDownThirdFixed(event)
{

    thirdFixed.drag(event);
}
var first;
var second;
var third;
var thirdFixed;
var vertical;

function init()
{
    first = new Scroll('scroller', 'scroller_bar', 'movemenu');
    second = new Scroll('scroller2', 'scroller_bar2', 'movemenu2');
    third = new ScrollWithBugs('scroller3', 'scroller_bar3', 'movemenu3');
    thirdFixed = new Scroll('scroller4', 'scroller_bar4', 'movemenu4');
    vertical = new VerticalScroll('scroller_v', 'scroller_bar_v', 'movemenu_v');

    first.prepare();
    second.prepare();    
    third.prepare();
    thirdFixed.prepare();
    vertical.prepare();
    document.onmousemove = handleOnMouseMove;
    window.onmouseup = handleOnMouseUp;
    get('scroller_bar').onclick = handleOnClickBarFirst;    
    get('scroller_bar_v').onclick = handleOnClickBarVertical;
    get('scroller').onmousedown = handleOnMouseDownFirst;
    get('scroller').onmouseup = handleOnMouseUp;
    get('scroller').onclick = handleOnClickFirst;    

    get('scroller_v').onmousedown = handleOnMouseDownVertical;
    get('scroller_v').onmouseup = handleOnMouseUp;
    get('scroller_v').onclick = handleOnClickVertical;        
    
    get('scroller3').onmousedown = handleOnMouseDownThird;
    get('scroller3').onmouseup = handleOnMouseUp;
    
    get('scroller4').onmousedown = handleOnMouseDownThirdFixed;
    get('scroller4').onmouseup = handleOnMouseUp;        

    if (get('withscript').addEventListener)
        get('withscript').addEventListener('DOMMouseScroll', handleOnMouseWheelFirst, false);
    get('withscript').onmousewheel = handleOnMouseWheelFirst;
    
    if (get('withscript_v').addEventListener)
        get('withscript_v').addEventListener('DOMMouseScroll', handleOnMouseWheelVertical, false);
    get('withscript_v').onmousewheel = handleOnMouseWheelVertical;
}
window.onload = init;

