var Scroll = function (f, g, a, l, b, c, e, d, i, h, j, k) {
    this.Direction = (f + "").toLowerCase();
    this.IsGoNext = g;
    this.IsScrollView = a;
    this.OuterBox = $("#" + l);
    this.InnerBox1 = $("#" + b);
    this.InnerBox2 = $("#" + c);
    this.BtnGoNext = $("#" + e);
    this.BtnGoBack = $("#" + d);
    this.Speed = h;
    this.Step = j;
    this.Wait = k;
    this.Size = this.Direction == "x" ? this.InnerBox1.width() : this.InnerBox1.height();
    this.OuterSize = this.Direction == "x" ? this.OuterBox.width() : this.OuterBox.height();
    this.Count = this.InnerBox1.find(i).length;
    this.ItemSize = this.Size / this.Count;
    this.BeWait = false;
    this.BeGoGo = false;
    this.Timeout_GoOn = {};
    this.Interval_GoNext = {};
    this.Interval_GoBack = {}
};
Scroll.prototype.ScrollStep = function (a) {
    if (a == 0);
    if (this.Direction == "x") if (isNaN(a)) return this.OuterBox.scrollLeft();
    else return this.OuterBox.scrollLeft(a);
    else if (isNaN(a)) return this.OuterBox.scrollTop(); 
  else return this.OuterBox.scrollTop(a) };
Scroll.prototype.GoNext = function () {
    var a = this.ScrollStep();
    if (this.IsScrollView) if (!this.BeWait && !this.BeGoGo && this.Wait > 0 && a == this.OuterSize) {
        this.ClearAction();
        this.ScrollStep(0);
        this.GoStart();
        this.BeWait = true
    } else {
        if ((a + this.Step) % this.ItemSize < this.Step) this.ScrollStep(parseInt((a + this.Step) / this.ItemSize) * this.ItemSize);
        else this.ScrollStep(a + this.Step);
        a = this.ScrollStep();
        a == this.Size && this.ScrollStep(0);
        this.BeWait = false
    } else if (!this.BeWait && a > 0 && this.Wait > 0 && !this.BeGoGo && a % this.ItemSize == 0) {
        this.ClearAction();
        this.GoStart(); this.BeWait = true
    } else if (a == this.Size) {
        this.ScrollStep(0);
        this.BeWait = false
    } else {
        if ((a + this.Step) % this.ItemSize < this.Step) this.ScrollStep(parseInt((a + this.Step) / this.ItemSize) * this.ItemSize);
        else this.ScrollStep(a + this.Step);
        this.BeWait = false
    } 
   };

Scroll.prototype.GoBack = function () {
    var a = this.ScrollStep();
    if (a == 0) {
        this.ScrollStep(this.Size);
        a = this.Size; this.BeWait = true
    } if (this.IsScrollView) if (!this.BeWait && this.Wait > 0 && !this.BeGoGo && a == this.OuterSize) {
        this.ClearAction();
        this.ScrollStep(0);
        this.GoStart();
        this.BeWait = true
    } else {
        if (a - this.Step < 0) this.ScrollStep(0);
        else if (a % this.ItemSize > 0 && a % this.ItemSize < this.Step) this.ScrollStep(parseInt(a / this.ItemSize) * this.ItemSize); 
        else this.ScrollStep(a - this.Step);
        a = this.ScrollStep(); a == 0 && this.ScrollStep(this.Size);
        this.BeWait = false
    } else if (!this.BeWait && this.Wait > 0 && !this.BeGoGo && a % this.ItemSize == 0) { this.ClearAction(); this.GoStart(); this.BeWait = true } else {
        if (a - this.Step < 0) this.ScrollStep(0); else if (a % this.ItemSize > 0 && a % this.ItemSize < this.Step) this.ScrollStep(parseInt(a / this.ItemSize) * this.ItemSize);
        else this.ScrollStep(a - this.Step);
        a = this.ScrollStep();
        a == 0 && this.ScrollStep(this.Size);
        this.BeWait = false
    } 
 };
Scroll.prototype.GoNextStart = function () {
    this.IsGoNext = true;
    var a = this;
    this.Interval_GoNext = setInterval(function () {
        a.GoNext()
    },
    this.Speed
    ) 
};
Scroll.prototype.GoBackStart = function () {
    this.IsGoNext = false;
    var a = this;
    this.Interval_GoBack = setInterval(function () {
        a.GoBack()
    },
 this.Speed)
  };
Scroll.prototype.GoStart = function () {
    var a = this; this.Timeout_GoOn = setTimeout(function () {
        if 
        (a.IsGoNext) a.GoNextStart();
        else
         a.GoBackStart()
    },
    this.Wait) 
    };
Scroll.prototype.ClearAction = function () {
    clearInterval(this.Interval_GoNext); clearInterval(this.Interval_GoBack);
    clearTimeout(this.Timeout_GoOn) 
};
Scroll.prototype.Init = function () {
    var a = this;
    this.InnerBox2.html(this.InnerBox1.html());
    this.OuterBox.hover(function () {
        clearTimeout(a.Timeout_GoOn)
    }, function () {
        if (a.BeWait) {
            a.ClearAction();
            a.GoStart()
        }
    });
    this.BtnGoNext.bind({ mousedown: function () {
        a.ClearAction();
        a.BeGoGo = true;
        a.GoNextStart();
    }, mouseup: function () { if (a.BeGoGo) a.BeGoGo = false }, mouseout: function () { if (a.BeGoGo) a.BeGoGo = false }
    });
    this.BtnGoBack.bind({ mousedown: function () {
        a.ClearAction();
        a.BeGoGo = true;
        a.GoBackStart();
    }, mouseup: function () {
        if (a.BeGoGo)
            a.BeGoGo = false;
    }, mouseout: function () {
        if (a.BeGoGo)
            a.BeGoGo = false;
    }
    })
};
function d_scroll_y() {
        var a = new Scroll("y",false,false, "div_news", "div_1", "div_2", "btn_next", "btn_back", "div.item", 20, 3, 2e3);
        a.Init();
        //a.GoStart()
    }
    function scroll_x() {
        var a = new Scroll("x", false, false, "imgs_c", "imgs_c1", "imgs_c2", "btn_next", "btn_back", "td.item", 10, 30, 3e3);
        a.Init();
        //a.GoStart()
    }
