﻿/*
    内容滚动，支持水平滚动和垂直滚动，按钮按下不放则不断滚动
*/

var Scroll = function(direction,isAutoGo, isGoNext, isScrollView, id, InnerBoxId1, InnerBoxId2, btnGoNext, btnGoBack, expr, speed, step, wait) {
    //是否自动滚动
    this.IsAutoGo =false | isAutoGo;
    //滚动方向(x为横向或y纵向)
    this.Direction = (direction + "").toLowerCase();
    //滚动方向（1为向前，2为退后）
    this.IsGoNext = isGoNext;
    //是否滚动完可视区域包含的项
    this.IsScrollView = isScrollView;
    //外部框
    this.OuterBox = $("#" + id);
    //内容框1
    this.InnerBox1 = $("#" + InnerBoxId1);
    //用于填充复制内容框2
    this.InnerBox2 = $("#" + InnerBoxId2);
    //向前按钮
    this.BtnGoNext = $("#" + btnGoNext);
    //后台按钮
    this.BtnGoBack = $("#" + btnGoBack);
    //速度：次/毫秒
    this.Speed = speed;
    //每次移动的距离
    this.Step = step;
    //移动一个后停顿的时间
    this.Wait = wait;
    //内容尺寸，direction为x则为宽度，否则为高度
    this.Size = Math.ceil(this.Direction == "x" ? this.InnerBox1.width() : this.InnerBox1.height());
    //外容器尺寸，direction为x则为宽度，否则为高度
    this.OuterSize = Math.ceil(this.Direction == "x" ? this.OuterBox.width() : this.OuterBox.height());
    //个数(expr为用于查询的表达式)
    this.Count = this.InnerBox1.find(expr).length;
    //一个的尺寸
    this.ItemSize = Math.ceil(this.Size / this.Count);
    //是否处于等待状态
    this.BeWait = false;
    //是否处于持续移动状态（鼠标按下时）
    this.BeGoGo = false;

    this.Timeout_GoOn = new Object();
    this.Interval_GoNext = new Object();
    this.Interval_GoBack = new Object();
}

//滚动
Scroll.prototype.ScrollStep = function(newScrollSize) {
    if (newScrollSize == 0) { }
    if (this.Direction == "x") {
        if (isNaN(newScrollSize)) {
            return this.OuterBox.scrollLeft();
        } else {
            return this.OuterBox.scrollLeft(newScrollSize);
        }
    } else {
        if (isNaN(newScrollSize)) {
            return this.OuterBox.scrollTop();
        } else {
            return this.OuterBox.scrollTop(newScrollSize);
        }
    }
}

//进一步
Scroll.prototype.GoNext = function() {
    var scrollSize = this.ScrollStep();

    if (this.IsScrollView) {
        if (!this.BeWait && !this.BeGoGo && this.Wait > 0 && scrollSize == this.OuterSize) {
            this.ClearAction();
            this.ScrollStep(0);
            if (this.IsAutoGo) this.GoStart();
            this.BeWait = true;
            return;
        } else {
            //如果此次滚动刚好超出一整个则滚到到一整个
            if ((scrollSize + this.Step) % this.ItemSize < this.Step) {
                this.ScrollStep(parseInt((scrollSize + this.Step) / this.ItemSize) * this.ItemSize);
            } //否则正常滚动
            else {
                this.ScrollStep(scrollSize + this.Step);
            }

            scrollSize = this.ScrollStep();
            if (scrollSize == this.Size) {
                this.ScrollStep(0);
            }

            this.BeWait = false;
        }
    } else {
        //当滚动完一整个后且停留时间大于0时，停留一段时间
        if (!this.BeWait && scrollSize > 0 && this.Wait > 0 && !this.BeGoGo && scrollSize % this.ItemSize == 0) {
            this.ClearAction();
            if (this.IsAutoGo) this.GoStart();
            this.BeWait = true;
            return;
        } //当内容滚动的坐标值等于高度时，重置与滚动条的坐标值
        else if (scrollSize == this.Size) {
            this.ScrollStep(0);
            this.BeWait = false;
        } //设置当前横向滚动条的坐标值
        else {
            //如果此次滚动刚好超出一整个则滚到到一整个
            if ((scrollSize + this.Step) % this.ItemSize < this.Step) {
                this.ScrollStep(parseInt((scrollSize + this.Step) / this.ItemSize) * this.ItemSize);
            } //否则正常滚动
            else {
                this.ScrollStep(scrollSize + this.Step);
            }
            this.BeWait = false;
        }
    }
    var T = this;
    this.Interval_GoNext = setTimeout(function() { T.GoNext(); }, this.Speed);

}

//退一步
Scroll.prototype.GoBack = function() {
    var scrollSize = this.ScrollStep();

    //如果滚动距离为0则设置滚动距离为内容框的高度
    if (scrollSize == 0) {
        this.ScrollStep(this.Size);
        scrollSize = this.Size;
        this.BeWait = true;
    }


    //当须要滚动完整个尺寸才停留，且已滚到到此时，停留一段时间
    if (this.IsScrollView) {
        if (!this.BeWait && this.Wait > 0 && !this.BeGoGo && scrollSize == this.OuterSize) {
            this.ClearAction();
            this.ScrollStep(0);
            if (this.IsAutoGo) this.GoStart();
            this.BeWait = true;
            return;
        } else {
            //如果滚动距离小于0则设置滚动距离为0
            if (scrollSize - this.Step < 0) {
                this.ScrollStep(0);
            } //如果滚动距离小于0则设置滚动距离为0 
            else if (scrollSize % this.ItemSize > 0 && scrollSize % this.ItemSize < this.Step) {
                this.ScrollStep(parseInt(scrollSize / this.ItemSize) * this.ItemSize);
            } //否则正常滚动
            else {
                this.ScrollStep(scrollSize - this.Step);
            }

            scrollSize = this.ScrollStep();
            if (scrollSize == 0) {
                this.ScrollStep(this.Size);
            }
            this.BeWait = false;
        }
    }
    else {
        //当滚动完一整个后且停留时间大于0时，停留一段时间
        if (!this.BeWait && this.Wait > 0 && !this.BeGoGo && (scrollSize % this.ItemSize == 0)) {
            this.ClearAction();
            if (this.IsAutoGo) this.GoStart();
            this.BeWait = true;
            return;
        }
        else {
            //如果滚动距离小于0则设置滚动距离为0
            if (scrollSize - this.Step < 0) {
                this.ScrollStep(0);
            } //如果滚动距离小于0则设置滚动距离为0 
            else if (scrollSize % this.ItemSize > 0 && scrollSize % this.ItemSize < this.Step) {
                this.ScrollStep(parseInt(scrollSize / this.ItemSize) * this.ItemSize);
            } //否则正常滚动
            else {
                this.ScrollStep(scrollSize - this.Step);
            }

            scrollSize = this.ScrollStep();
            if (scrollSize == 0) {
                this.ScrollStep(this.Size);
            }
            this.BeWait = false;
        }
    }

    var T = this;
    this.Interval_GoBack = setTimeout(function() { T.GoBack() }, this.Speed);
}

//开始进
Scroll.prototype.GoNextStart = function() {
    this.IsGoNext = true;
    var T = this;
    T.GoNext();
}

//开始退
Scroll.prototype.GoBackStart = function() {
    this.IsGoNext = false;
    var T = this;
    T.GoBack()
}

//开始
Scroll.prototype.Go = function() {
    var T = this;
    if (T.IsGoNext) {
        T.GoNextStart();
    } else {
        T.GoBackStart();
    }
}

//开始
Scroll.prototype.GoStart = function() {
    var T = this;

    this.Timeout_GoOn = setTimeout(function() {
        T.Go();
    }, this.Wait);
}

//清除定时
Scroll.prototype.ClearAction = function() {             
    clearTimeout(this.Interval_GoNext);
    clearTimeout(this.Interval_GoBack);
    clearTimeout(this.Timeout_GoOn);
}

//初始化
Scroll.prototype.Init = function() {
    var T = this;
    if (this.Size < this.OuterSize) return;
    
    this.InnerBox2.html(this.InnerBox1.html());
    if (this.IsAutoGo) {
        this.OuterBox.hover(
        function() {
            T.ClearAction();
        },
        function() {
            T.ClearAction();
            T.Go();
        });
    }


    this.BtnGoNext.bind({
        mousedown: function() {
            T.ClearAction();
            T.BeGoGo = true;
            T.GoNextStart();
        },
        mouseup: function() {
            if (T.BeGoGo) {
                T.BeGoGo = false;
            }
        },
        mouseout: function() {
            if (T.BeGoGo) {
                T.BeGoGo = false;
            }
        }
    });


    this.BtnGoBack.bind({
        mousedown: function() {
            T.ClearAction();
            T.BeGoGo = true;
            T.GoBackStart();
        },
        mouseup: function() {
            if (T.BeGoGo) {
                T.BeGoGo = false;
            }
        },
        mouseout: function() {
            if (T.BeGoGo) {
                T.BeGoGo = false;
            }
        }
    });


    if (this.IsAutoGo) {
        this.GoStart();
    }
}



