
oss = {
    set : function (elementId, scrollLeft, scrollTop, visibleWidth, visibleHeight, maxSpeed) {
        if (e=getElement(elementId))
        {
            e.oss = {
                id : elementId,
                scrollLeft : scrollLeft,
                scrollTop : scrollTop,
                visibleWidth : visibleWidth,
                visibleHeight : visibleHeight,
                currentTop : 0,
                currentLeft : 0,
                leftMax : 0,
                topMax : 0,
                offsetWidth : visibleWidth,
                offsetHeight : visibleHeight,
                direction : false,
                upStepsMax : 30,
                downStepsMax : 80,
                stepIndex : 0,
                speed : 0.0,
                maxSpeed : (typeof(maxSpeed)=='undefined') ? 2 : maxSpeed,
                upStep : 0,
                downStep : 0,
                scrollTimer : null,
                scrollTime : 1,
                reqStop : false
            }
            with(e.oss)
            {
                upStep = maxSpeed/upStepsMax;
                downStep = maxSpeed/downStepsMax;
            }
            e.style.position = 'relative';
            if (e.oss.scrollLeft>e.oss.offsetWidth)
            {
                e.scrollLeft = e.oss.scrollLeft - e.oss.offsetWidth;
                e.oss.posLeft = e.oss.offsetWidth + (e.scrollLeft - e.oss.offsetWidth);
            }
            //this.scrollObject.scrollTop = 100;
            e.oss.maxLeft = e.scrollWidth - e.oss.offsetWidth;
            e.oss.maxTop = e.scrollHeight - e.oss.offsetHeight;
        }
        else
            return false;
    },
    run : function (elementId, direction) {
        if (e=getElement(elementId))
        {
            if (!e.oss.reqStop && e.oss.speed>0 && direction==e.oss.direction)
                return false;
            else
            {
                e.oss.reqStop = false;
                if (typeof(direction)!='undefined')
                    e.oss.direction = direction;
                clearTimeout(e.oss.scrollTimer);
                e.oss.scrollTimer = setTimeout('oss.scroll(\''+elementId+'\');', e.oss.scrollTime);
            }
        }
        else
            return false;
    },
    scroll : function (elementId) {
        if (e=getElement(elementId))
        {
            with (e.oss)
            {
                if ((direction=='left' && currentLeft>=maxLeft)
                        || (direction=='right' && currentLeft<=0)
                        || (direction=='down' && currentTop>=maxTop)
                        || (direction=='up' && currentTop<=0))
                {
                    speed = 0;
                    return false;
                }
            }
            if (!e.oss.reqStop && e.oss.speed==0)
            {
                e.oss.speed = e.oss.upStep;
                e.oss.scrollTimer = setTimeout('oss.scroll(\''+elementId+'\');', e.oss.scrollTime);
            }
            else if (!e.oss.reqStop && e.oss.speed>0)
            {
                e.oss.speed += ((e.oss.speed+e.oss.upStep)<=e.oss.maxSpeed) ? (e.oss.upStep) : 0;
                e.oss.scrollTimer = setTimeout('oss.scroll(\''+elementId+'\');', e.oss.scrollTime);
            }
            else if (e.oss.reqStop && e.oss.speed<=0)
            {
                e.oss.reqStop = false;
                e.oss.speed = 0;
                //return false;
            }
            else if (e.oss.reqStop && e.oss.speed>0)
            {
                e.oss.speed -= e.oss.downStep;
                e.oss.scrollTimer = setTimeout('oss.scroll(\''+elementId+'\');', e.oss.scrollTime);
            }
            //getElement('info').innerHTML = e.oss.direction+' | '+e.oss.speed+' stop:'+((e.oss.reqStop)?'y':'n');
            with (e)
            {
                oss.stepIndex++;
                switch (oss.direction)
                {
                    case 'down':
                        oss.currentTop = oss.posTop + oss.speed;
                        scrollTop = Math.floor(oss.currentTop);
                        break;
                    case 'up':
                        oss.currentTop = oss.currentTop - oss.speed;
                        scrollTop = Math.floor(oss.currentTop);
                        break;
                    case 'left':
                        oss.currentLeft = oss.currentLeft + oss.speed;
                        scrollLeft = Math.floor(oss.currentLeft);
                        break;
                    case 'right':
                        oss.currentLeft = oss.currentLeft - oss.speed;
                        scrollLeft = Math.floor(oss.currentLeft);
                        break;
                }
            }
        }
        else
            return false;
    },
    stop : function (elementId) {
        if (e=getElement(elementId))
        {
            with (e.oss)
            {
                //if (speed && scrollTimer)
                    //clearTimeout(scrollTimer);
                reqStop = true;
            }
        }
        else
            return false;
    }
}

function scrolls(elementId, scrollLeft, scrollTop, visibleWidth, visibleHeight, speed) {
    
    this.elementId = elementId;
    this.visibleWidth = visibleWidth;
    this.visibleHeight = visibleHeight;
    this.scrollLeft = scrollLeft;
    this.scrollTop = scrollTop;
    this.posTop = 0;
    this.posLeft = 0;
    this.posLeftMax = 0;
    this.posTopMax = 0;
    if (typeof(speed)!='undefined' && speed=='slow')
    {
        this.scrollStepSpeed1 = 0.1;
        this.scrollStepSpeed2 = 0.4;
        this.scrollStepSpeed3 = 0.6;
        this.scrollStepSpeed4 = 0.8;
        this.scrollStepSpeed5 = 1;
    }
    else
    {
        this.scrollStepSpeed1 = 0.2;
        this.scrollStepSpeed2 = 0.8;
        this.scrollStepSpeed3 = 1.2;
        this.scrollStepSpeed4 = 1.6;
        this.scrollStepSpeed5 = 2;
    }
    this.scrollTimerSpeed = 1;
    this.scrollTimerMinSpeed = 10;
    this.stepIndex = 0;
    this.stepOutIndex = 0;
    this.run = false;
    this.direction = false;
    this.haltReq = false;
    
    this.timer = null;
    
    if (typeof(sObject)=='undefined')
    {
        this.sObjectIndex = 0;
        sObject = new Array();
        sObject[this.sObjectIndex] = this;
    }
    else
    {
        this.sObjectIndex = sObject.length;
        sObject[this.sObjectIndex] = this;
    }
    //selfObject = this;
    
    this.goScroll = function (direction) {
        if (this.scrollObject==null || typeof(this.scrollObject)=='undefined')
            this.scrollObject = this.getScrollObject(this.elementId);
        if (this.scrollObject && (!this.run || direction!=this.direction))
        {
            if (this.run)
            {
                clearTimeout(this.timer);
            }
            this.run = true;
            this.direction = direction;
            this.stepIndex = 0;
//            setTimeout(function(){ sObject[0].scroll(direction); }, this.scrollTimerSpeed);
            setTimeout('sObject['+this.sObjectIndex+'].scroll(\''+direction+'\');', this.scrollTimerSpeed);
            this.haltReq = false;
        }
    }

    this.haltScroll = function (direction) {
        if (this.scrollObject==null || typeof(this.scrollObject)=='undefined')
            this.scrollObject = this.getScrollObject(this.elementId);
        if (this.scrollObject && this.run)
        {
            this.stepOutIndex = (this.stepIndex > 40) ? 40 : this.stepIndex;
            this.haltReq = true;
            this.stepIndex = 0;
            //setTimeout(function(){ self.scroll(direction); }, this.scrollTimerSpeed);
        }
    }
    
    this.scroll = function (direction) {
        if (this.scrollObject)
        {
            this.stepIndex++;
            if (this.haltReq)
            {
                this.stepOutIndex--;
                if (this.stepOutIndex>34 && this.stepIndex>34)
                    step = this.scrollStepSpeed5;
                else if (this.stepOutIndex>26 && this.stepIndex>26)
                    step = this.scrollStepSpeed4;
                else if (this.stepOutIndex>18)
                    step = this.scrollStepSpeed3;
                else if (this.stepOutIndex>10)
                    step = this.scrollStepSpeed2;
                else
                    step = this.scrollStepSpeed1;
                if (this.stepOutIndex==0)
                    this.run = false;
            }
            else
            {
                if (this.stepIndex>34)
                    step = this.scrollStepSpeed5;
                else if (this.stepIndex>26)
                    step = this.scrollStepSpeed4;
                else if (this.stepIndex>18)
                    step = this.scrollStepSpeed3;
                else if (this.stepIndex>10)
                    step = this.scrollStepSpeed2;
                else
                    step = this.scrollStepSpeed1;
            }
            with (this.scrollObject)
            {
                switch (direction)
                {
                    case 'down':
                        if (this.posTop>=this.maxTop)
                            this.run = false
                        else if (this.run)
                        {
                            this.posTop = this.posTop + step;
                            scrollTop = Math.floor(this.posTop);
                        }
                        break;
                    case 'up':
                        if (this.posTop<=0)
                            this.run = false
                        else if (this.run)
                        {
                            this.posTop = this.posTop - step;
                            scrollTop = Math.floor(this.posTop);
                        }
                        break;
                    case 'left':
                        if (this.posLeft>=this.maxLeft)
                            this.run = false
                        else if (this.run)
                        {
                            this.posLeft = this.posLeft + step;
                            scrollLeft = Math.floor(this.posLeft);
                            //style.left = Math.floor(this.posLeft).toString()+'px';
                        }
                        break;
                    case 'right':
                        if (this.posLeft<=0)
                            this.run = false
                        else if (this.run)
                        {
                            this.posLeft = this.posLeft - step;
                            scrollLeft = Math.floor(this.posLeft);
                            //style.left = Math.floor(this.posLeft)+'px';
                        }
                        break;
                }
                //getElement('out').innerHTML = "#"+this.stepIndex+'/'+step+' '+((this.run)?'scrolling':'stopped')+' left:'+this.maxLeft;//Math.floor(this.posLeft)+'/'+this.scrollSpeed;
            }
            if (this.run)
                this.timer = setTimeout('sObject['+this.sObjectIndex+'].scroll(\''+direction+'\');', this.scrollTimerSpeed);
//                this.timer = setTimeout(function(){ sObject[0].scroll(direction); }, this.scrollTimerSpeed);
        }
    }

    this.getScrollObject = function (name) {
        if (document.getElementById)
            return document.getElementById(name)
        else if (document.all)
            return document.all.name
        return false;
    }

    if (this.scrollObject = this.getScrollObject(this.elementId))
    {
        this.offsetWidth = this.visibleWidth;//getElementWidth(this.elementId);
        this.offsetHeight = this.visibleHeight;
        this.scrollObject.style.position = 'relative';
        if (this.scrollLeft > this.offsetWidth)
        {
            this.scrollObject.scrollLeft = this.scrollLeft - this.offsetWidth;
            this.posLeft = this.offsetWidth + (this.scrollObject.scrollLeft - this.offsetWidth);
        }
        //this.scrollObject.scrollTop = 100;
        this.maxLeft = this.scrollObject.scrollWidth - this.offsetWidth;
        this.maxTop = this.scrollObject.scrollHeight - this.offsetHeight;
        //getElement('out').innerHTML = typeof(this.scrollObject.pageXOffset)+' '+this.maxLeft;
    }
    
}

function getElementWidth(Elem) {
	if (typeof(ns4)!='undefined') {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (typeof(op5)!='undefined') {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function getElementHeight(Elem) {
	if (typeof(ns4)!='undefined') {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (typeof(op5)!='undefined') { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

