﻿/*
应用说明:<script type="text/javascript" src="marquee.js""></script> 
2011-4-20 Jason Edit

创建实例
		//参数直接赋值法
		new Marquee("marquee")
		new Marquee("marquee","top")
		......
		new Marquee("marquee",0,1,760,52)
		new Marquee("marquee","top",1,760,52,50,5000)
		......
		new Marquee("marquee",0,1,760,104,50,5000,3000,52)
		new Marquee("marquee",null,null,760,104,null,5000,null,-1)
 
		//参数动态赋值法
		var Marquee1 = new Marquee("marquee")	*此参数必选
		Marquee1.Direction = "top";	或者	Marquee1.Direction = 0;
		Marquee1.Step = 1;
		Marquee1.Width = 760;
		Marquee1.Height = 52;
		Marquee1.Timer = 50;
		Marquee1.DelayTime = 5000;
		Marquee1.WaitTime = 3000;
		Marquee1.ScrollStep = 52;
		Marquee1.Start();
 
参数说明
		ID　　　　　 "marquee"　容器ID　　　　　　(必选)
		Direction　　(0)　　　　滚动方向　　　　　(可选,默认为0向上滚动) 值:-1,0,1,2,3,4(0上 1下 2左 3右 -1上下交替 4左右交替)
		Step　　　　 (1)　　　　滚动的步长　　　　(可选,默认值为2,数值越大,滚动越快)
		Width　　　　(760)　　　容器可视宽度　　　(可选,默认值为容器初始设置的宽度)
		Height　　　 (52)　　　 容器可视高度　　　(可选,默认值为容器初始设置的高度)
		Timer　　　　(50)　　　 定时器　　　　　　(可选,默认值为30,数值越小,滚动的速度越快,1000=1秒,建议不小于20)
		DelayTime　　(5000)　　 间歇停顿延迟时间　(可选,默认为0不停顿,1000=1秒)
		WaitTime　　 (3000)　　 开始时的等待时间　(可选,默认或0为不等待,1000=1秒)
		ScrollStep　 (52)　　　 间歇滚动间距　　　(可选,默认为翻屏宽/高度,该数值与延迟均为0则为鼠标悬停控制,-1禁止鼠标控制)
*/


function Marquee() {
    this.CopyRight = "通用不间断滚动JS封装类";
    var a = this;
    a.ID = document.getElementById(arguments[0]);
    if (!a.ID) {
        alert('您要设置的"' + arguments[0] + '"初始化错误\r\n请检查标签ID设置是否正确!');
        return a.ID = -1
    } a.BakStep = a.Bound = a.Direction = a.Width = a.Height = a.DelayTime = a.WaitTime = 0;
    a.a = a.b = a.c = a.d = a.e = a.f = a.g = a.h = a.i = a.j = a.m = 0;
    a.Timer = 30;
    a.Step = a.k = 1;
    a.n = {
        altertop: -1, alterup: -1, top: 0, up: 0, bottom: 1, down: 1, left: 2, right: 3, alterleft: 4, slide: 5
    };
    if (typeof arguments[1] == "number" || typeof arguments[1] == "string") {
        a.Direction = arguments[1]
    } if (typeof arguments[2] == "number") {
        a.Step = arguments[2]
    } if (typeof arguments[3] == "number") {
        a.Width = arguments[3]
    } if (typeof arguments[4] == "number") {
        a.Height = arguments[4]
    } if (typeof arguments[5] == "number") {
        a.Timer = arguments[5]
    } if (typeof arguments[6] == "number") {
        a.DelayTime = arguments[6]
    } if (typeof arguments[7] == "number") {
        a.WaitTime = arguments[7]
    } if (typeof arguments[8] == "number") {
        a.ScrollStep = arguments[8]
    } a.ID.style.overflow = a.ID.style.overflowX = a.ID.style.overflowY = "hidden";
    a.ID.noWrap = true;
    a.IsNotOpera = (navigator.userAgent.toLowerCase().indexOf("opera") == -1);
    a.IsIE = (document.all) ? 1 : 0;
    if (arguments.length >= 7) {
        a.Start()
    }
} Marquee.prototype.Start = function () {
    if (this.ID == -1) {
        return
    } var d = this;
    if (d.WaitTime < 800) {
        d.WaitTime = 800
    } if (d.Timer < 20) {
        d.Timer = 20
    } if (d.Width == 0) {
        d.Width = parseInt(d.ID.style.width)
    } if (d.Height == 0) {
        d.Height = parseInt(d.ID.style.height)
    } d.b = Math.round(d.Width / 2);
    d.c = Math.round(d.Height / 2);
    d.BakStep = d.Step;
    d.ID.style.width = d.Width + "px";
    d.ID.style.height = d.Height + "px";
    if (typeof d.Direction == "string") {
        d.Direction = d.n[d.Direction.toString().toLowerCase()]
    } if (typeof d.ScrollStep != "number") {
        d.ScrollStep = d.Direction > 1 ? d.Width : d.Height
    } var c = d.Direction > 1 ? "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;display:inline;'><tr><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td></tr></table>" : "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;'><tr><td>MSCLASS_TEMP_HTML</td></tr><tr><td>MSCLASS_TEMP_HTML</td></tr></table>";
    d.tempHTML = d.ID.innerHTML;
    if (d.Direction == 0 || d.Direction == 1) {
        d.ID.innerHTML = c.replace(/MSCLASS_TEMP_HTML/g, d.ID.innerHTML)
    } else {
        if (d.Direction == 2 || d.Direction == 3) {
            (d.ScrollStep == 0 && d.DelayTime == 0) ? d.ID.innerHTML += d.ID.innerHTML : d.ID.innerHTML = c.replace(/MSCLASS_TEMP_HTML/g, d.ID.innerHTML)
        }
    } if (d.Step < 1) {
        d.k = d.Step;
        d.Step = 1
    } d.d = d.Direction > 1 ? "scrollLeft" : "scrollTop";
    var b = d.Direction > 1 ? d.Width : d.Height;
    var a = d.Direction > 1 ? "scrollWidth" : "scrollHeight";
    if (d.Direction == 4 || d.Direction == -1) {
        d.Direction = d.Direction > 1 ? 2 : 0;
        d.e = 1
    } d.h = function () {
        d.k < 1 ? d.Step = Math.ceil((d.ScrollStep - d.g) * d.k) : 0;
        d.Scroll()
    };
    d.Continue = function () {
        if (d.j == 1) {
            d.f = setTimeout(d.Continue, d.DelayTime)
        } else {
            clearInterval(d.m);
            d.g = d.i = 0;
            d.m = setInterval(d.h, d.Timer)
        } d.Bound = 0;
        if (d.e == 1) {
            d.UnBound()
        }
    };
    d.Run = function (e) {
        if (d.i == 1) {
            if (arguments[0]) {
                d.Direction = arguments[0]
            } if (typeof arguments[0] == "string") {
                d.Direction = d.n[arguments[0].toString().toLowerCase()]
            } if (d.Direction < 0 || d.Direction > 3) {
                alert("参数设置错误!");
                return false
            } clearTimeout(d.f);
            d.f = setTimeout(d.Continue, 0)
        }
    };
    d.OnBound = d.UnBound = d.Terminate = function () {

    };
    d.Pause = function () {
        d.i = 1;
        clearInterval(d.m);
        if (d.e == 1) {
            if (d.ID[d.d] == 0 || d.ID[d.d] >= d.ID[a] - b) {
                d.Bound = 1;
                d.OnBound();
                d.Direction > 1 ? d.Direction = d.Direction == 2 ? 3 : 2 : d.Direction = 1 - d.Direction
            }
        } d.f = setTimeout(d.Continue, d.DelayTime)
    };
    d.Begin = function () {
        d.ClientScroll = d.ID[a] / 2;
        if (d.ID[a] <= b + d.Step) {
            d.ID.innerHTML = d.tempHTML;
            delete (d.tempHTML);
            return
        } delete (d.tempHTML);
        if (d.WaitTime >= 60000) {
            d.i = 1;
            if (d.WaitTime == 100000) {
                d.DelayTime = 3600000
            } return
        } setTimeout(function () {
            if (d.ScrollStep >= 0 && d.l) {
                d.l()
            } d.m = setInterval(d.h, d.Timer)
        }, d.WaitTime)
    };
    setTimeout(d.Begin, 800);
    d.Scroll = function () {
        d.g += d.Step;
        if (d.g >= d.ScrollStep && d.DelayTime > 0) {
            d.Direction % 2 ? d.ID[d.d] -= d.ScrollStep + d.Step - d.g : d.ID[d.d] += d.ScrollStep + d.Step - d.g;
            d.Pause();
            return
        } switch (d.Direction) {
            case 0: case 2: if (d.ID[d.d] >= d.ClientScroll && d.e == 0) {
                    d.ID[d.d] -= d.ClientScroll
                } d.ID[d.d] += d.Step;
                break;
            case 1: case 3: if (d.ID[d.d] <= 0 && d.e == 0) {
                    d.ID[d.d] += d.ClientScroll
                } d.ID[d.d] -= d.Step;
                break
        }
    }
};
Marquee.prototype.l = function () {
    var f = this;
    var b = OC = null;
    var a = EventXY = OldXY = NewXY = 0;
    function c(g) {
        var g = g || window.event;
        if (f.DelayTime != 0 || (g.which && g.which != 1) || (g.button && g.button != 1)) {
            return false
        } if (a == 0) {
            f.i = a = 1;
            clearInterval(f.m);
            g.stopPropagation ? g.stopPropagation() : g.cancelBuble = true;
            g.preventDefault ? g.preventDefault() : g.returnValue = false;
            OldXY = f.ID[f.d];
            EventXY = f.Direction > 1 ? window.event ? g.clientX : g.pageX : window.event ? g.clientY : g.pageY;
            b = g.srcElement || g.target;
            OC = b.style.cursor;
            b.style.cursor = "move";
            if (f.IsIE) {
                b.setCapture();
                document.attachEvent("onmousemove", d);
                document.attachEvent("onmouseup", e)
            } else {
                document.addEventListener("mousemove", d, false);
                document.addEventListener("mouseup", e, false)
            }
        }
    } function d(h) {
        var h = h || window.event;
        NewXY = f.Direction > 1 ? window.event ? h.clientX : h.pageX : window.event ? h.clientY : h.pageY;
        var g = EventXY - NewXY + OldXY;
        if (g <= 0 || g >= f.ClientScroll) {
            g <= 0 ? f.ID[f.d] += f.ClientScroll : f.ID[f.d] = f.ClientScroll - g;
            EventXY = f.Direction > 1 ? window.event ? h.clientX : h.pageX : window.event ? h.clientY : h.pageY;
            OldXY = f.ID[f.d];
            g = EventXY - NewXY + OldXY
        } f.ID[f.d] = g
    } function e(g) {
        if (f.IsIE) {
            b.releaseCapture();
            document.detachEvent("onmousemove", d);
            document.detachEvent("onmouseup", e)
        } else {
            document.removeEventListener("mousemove", d, false);
            document.removeEventListener("mouseup", e, false)
        } f.Continue();
        a = f.j = 0;
        b.style.cursor = OC;
        if (f.j == 0 || f.ScrollStep == 0) {
            clearInterval(f.m);
            f.m = setInterval(f.h, f.Timer)
        }
    } if (f.e == 0) {
        f.IsIE ? f.ID.attachEvent("onmousedown", c) : f.ID.addEventListener("mousedown", c, false)
    } if (f.ScrollStep == 0) {
        f.ID.onmousemove = function (g) {
            if (f.Direction > 1) {
                var g = g || window.event;
                if (window.event) {
                    if (f.IsNotOpera) {
                        f.a = g.srcElement.id == f.ID.id ? g.offsetX - f.ID[f.d] : g.srcElement.offsetLeft - f.ID[f.d] + g.offsetX
                    } else {
                        f.ScrollStep = null;
                        return
                    }
                } else {
                    f.a = g.layerX - f.ID[f.d]
                } f.Direction = f.a > f.b ? 3 : 2;
                f.AbsCenter = Math.abs(f.b - f.a);
                f.Step = Math.round(f.AbsCenter * (f.BakStep * 2) / f.b)
            }
        };
        f.ID.onmouseout = function () {
            if (f.Step == 0) {
                f.Step = 1
            }
        }
    } else {
        f.ID.onmouseover = function () {
            f.j = 1;
            clearInterval(f.m)
        };
        f.ID.onmouseout = function () {
            f.j = 0;
            if (f.i == 0) {
                clearInterval(f.m);
                f.m = setInterval(f.h, f.Timer)
            }
        }
    }
};
