﻿
function scrollnews() {
    var htext = document.getElementById("new_list").getElementsByTagName("dl");
    var text_holder = document.getElementById("newscommend");
    var oFrag = document.createDocumentFragment();
    oFrag.innerHTML = "";
    for (var i = 0; i < htext.length; i++) {
        oFrag.innerHTML += " <div class=\"index_news_titleimg\" ><dl>" + htext[i].innerHTML + "</dl></div>";
        if ((i > 0 && i % 2 == 1) || (i == htext.length - 1 && i % 2 == 0)) {
            oFrag.innerHTML += "<br/>";
        }
    }
    text_holder.innerHTML = oFrag.innerHTML;
    return htext.length;
}
function scrollIndex() {
    var htext = document.getElementById("tsVhl").getElementsByTagName("tr");
    var text_holder = document.getElementById("tranlistcommend");
    var oFrag = document.createDocumentFragment();
    oFrag.innerHTML = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"index_cargotable\">";
    for (var i = 0; i < htext.length; i++) {
        var trclass = i % 2 == 0 ? "index_cargoalt" : i == htext.length ? "indexlast" : "";
        oFrag.innerHTML += "<tr class=" + trclass + ">" + htext[i].innerHTML + "</tr>";
    }
    oFrag.innerHTML += "</table>";
    text_holder.innerHTML = oFrag.innerHTML;
    return htext.length;
}


function ScrollText(content, btnPrevious, btnNext, autoStart, timeout, isSmoothScroll, icount) {
    this.ScrollContent = this.$(content);
    this.Speed = 10;
    this.Timeout = timeout;
    this.stopscroll = false;
    this.isSmoothScroll = isSmoothScroll;
    this.LineHeight = this.ScrollContent.offsetHeight;
    this.icount = icount * 2;

    this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;
    if (autoStart) {
        this.Start();
    }
}
ScrollText.prototype = {
    $: function(element) {
        return document.getElementById(element);
    },
    ii: 0,
    Previous: function() {
        this.stopscroll = true;
        this.Scroll("up");
    },
    Next: function() {
        this.stopscroll = true;
        this.Scroll("down");
    },
    Start: function() {
        if (this.isSmoothScroll) {
            this.AutoScrollTimer = setInterval(this.GetFunction(this, "SmoothScroll"), this.Timeout);
        } else {
            this.AutoScrollTimer = setInterval(this.GetFunction(this, "AutoScroll"), this.Timeout);
        }
    },
    Stop: function() {
        clearTimeout(this.AutoScrollTimer);
        this.DelayTimerStop = 0;
    },
    MouseOver: function() {
        this.stopscroll = true;
    },
    MouseOut: function() {
        this.stopscroll = false;
    },
    AutoScroll: function() {
        this.ii++;
        if (this.ii > 10000) this.ii = 0;
        if (this.stopscroll) {
            return;
        }


        this.LineHeight = Math.floor(parseInt(this.ScrollContent.scrollHeight) / this.icount, 0);
        this.ScrollContent.scrollTop++;
        if (parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0) {
            if (this.ScrollContent.scrollTop + this.LineHeight >= this.ScrollContent.scrollHeight) {
                this.ScrollContent.scrollTop = 0;
            } else {
                this.ScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Speed);
            }
        } else {
            if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
                this.ScrollContent.scrollTop = 0;
            }
            clearTimeout(this.ScrollTimer);
        }
        // this.$("c1").innerHTML = this.ii + ":LineHeight：" + this.LineHeight + ":scrollTop" + this.ScrollContent.scrollTop + ":" + this.ScrollContent.scrollHeight;
    },
    SmoothScroll: function() {
        if (this.stopscroll) {
            return;
        }
        this.ScrollContent.scrollTop++;
        if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
            this.ScrollContent.scrollTop = 0;
        }
    },
    Scroll: function(direction) {
        if (direction == "up") {
            this.ScrollContent.scrollTop--;
        } else {
            this.ScrollContent.scrollTop++;
        }
        if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
            this.ScrollContent.scrollTop = 0;
        } else if (parseInt(this.ScrollContent.scrollTop) <= 0) {
            this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
        }

        if (parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0) {
            this.ScrollTimer = setTimeout(this.GetFunction(this, "Scroll", direction), this.Speed);
        }
    },
    GetFunction: function(variable, method, param) {
        return function() {
            variable[method](param);
        }
    }
}
