﻿//Author:   狼Robot
//Contact:  robot@k2046.cn
//Date:     2008-04-09
//options接受的参数
// container    :   string/dom,要实现滚动的元素.
// auto         :   boolean,表示是否自动开始滚动.
// button       :   {},按钮
// config       :   {},设置
//
// 参数说明     ==========================================
// button的内容 :
// next         :   string/dom,下一条记录的按钮.
// previous     :   string/dom,上一条记录的按钮.

// config的内容
// delay        :   number,每一步的滚动延时.
// amount       :   number,每一步的距离
// line         :   number,行高,要求必须能整除amount.
// timeout      :   number,每次滚动停顿的时间.
//K2046.Scroller = {
//    Attach: function(options) {
//        options = options || {};
//        if (!options.container) {
//            return;
//        }
//        options.config = options.config || {};
//        options.container = K2046.$(options.container);
//        options.config.delay = options.config.delay || 10;
//        options.config.line = options.config.line || 20;
//        options.config.amount = options.config.amount || 1;
//        options.config.timeout = options.config.timeout || 1000;
//        if (options.button) {
//            if (options.button.previous) {
//                K2046.Events.Attach(options.button.previous, "onclick", function() {
//                });
//            }
//            if (options.button.next) {

//            }
//        }
//    }
//    , Previous: function() {

//    }

//    , Next: function() {
//    }
//    , Start: function() {
//    }

//    , Stop: function() {
//    }

//    , Scroll: function() {
//    }
//}

//function(options) {
//    options = options || {};
//    if (!options.container) {
//        return;
//    }
//    options.config = options.config || {};
//    this.Container = K2046.$(options.container);
//    this.Delay = options.config.delay || 10;
//    this.Line = options.config.line || 20;
//    this.Amount = options.config.amount || 1;
//    this.Timeout = options.config.timeout || 1000;
//}

function Scroller(content, btnPrevious, btnNext, autoStart) {
    this.Delay = 10;
    this.LineHeight = 25;
    this.Amount = 1; //注意:LineHeight一定要能整除Amount.
    this.Direction = "up";
    this.Timeout = 1500;
    this.ScrollContent = this.$(content);
    this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;
    //this.ScrollContent.scrollTop = 0;
    if (btnNext) {
        this.NextButton = this.$(btnNext);
        this.NextButton.onclick = this.GetFunction(this, "Next");
        this.NextButton.onmouseover = this.GetFunction(this, "Stop");
        this.NextButton.onmouseout = this.GetFunction(this, "Start");
        this.NextButton.style.cursor = "pointer";
    }
    if (btnPrevious) {
        this.PreviousButton = this.$(btnPrevious);
        this.PreviousButton.onclick = this.GetFunction(this, "Previous");
        this.PreviousButton.onmouseover = this.GetFunction(this, "Stop");
        this.PreviousButton.onmouseout = this.GetFunction(this, "Start");
        this.PreviousButton.style.cursor = "pointer";
    }
    this.ScrollContent.onmouseover = this.GetFunction(this, "Stop");
    this.ScrollContent.onmouseout = this.GetFunction(this, "Start");
    if (autoStart) {
        this.Start();
    }
}

Scroller.prototype.$ = function(element) {
    return document.getElementById(element);
};

Scroller.prototype.Previous = function() {
    clearTimeout(this.AutoScrollTimer);
    clearTimeout(this.ScrollTimer);
    this.Scroll("up");
};

Scroller.prototype.Next = function() {
    clearTimeout(this.AutoScrollTimer);
    clearTimeout(this.ScrollTimer);
    this.Scroll("down");
};

Scroller.prototype.Start = function() {
    clearTimeout(this.AutoScrollTimer);
    this.AutoScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Timeout);
};

Scroller.prototype.Stop = function() {
    clearTimeout(this.ScrollTimer);
    clearTimeout(this.AutoScrollTimer);
};

Scroller.prototype.AutoScroll = function() {
    if (this.Direction == "up") {
        if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
            this.ScrollContent.scrollTop = 0;
        }
        this.ScrollContent.scrollTop += this.Amount;
    }
    else {
        if (parseInt(this.ScrollContent.scrollTop) <= 0) {
            this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
        }
        this.ScrollContent.scrollTop -= this.Amount;
    }
    if (parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0) {
        this.ScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Delay);
    }
    else {
        this.AutoScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Timeout);
    }
};

Scroller.prototype.Scroll = function(direction) {
    if (direction == "up") {
        if (this.ScrollContent.scrollTop == 0) {
            this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
        }
        this.ScrollContent.scrollTop -= this.Amount;
    }
    else {
        this.ScrollContent.scrollTop += this.Amount;
    }
    if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
        this.ScrollContent.scrollTop = 0;
    }
    if (parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0) {
        this.ScrollTimer = setTimeout(this.GetFunction(this, "Scroll", direction), this.Delay);
    }
};

Scroller.prototype.GetFunction = function(variable, method, param) {
    return function() {
        variable[method](param);
    }
};