﻿/*
修改时间：2010-8-13
修改人：清风
QQ:86987182
调用示例：$(document).ready(function () { var s = new Scroll(id, f, s);});
参数说明：id：要滚动的元素的id；f：0向左，1向上；s：速度
注意：必须先调用jquery1.4.2框架,多个滚动需要实例化多次
*/
function Scroll(id, f, s) {
    var p, c, m, n, i = 0;
    //根据条件设置滚动参数
    this.play = function (id, f, s) {
        n = f == 0 ? this.sLeft : this.sTop;
        this.CreatObject(id, f, s);
        m = setInterval(n, s);
    }
    //对要滚动的元素进行包裹
    this.CreatObject = function (id, f, s) {
        c = $("#" + id);

        if (f == 0) {
            //c.css("float", "left");
            c.wrap("<div></div>").wrap("<div></div>");
            c.after(c.clone());
            var cc = c.parent();
            cc.css({ width: c.width() * 2, height: c.height() });
            p = cc.parent();
            p.css({ width: c.width(), height: c.height(), "overflow": "hidden" }).hover(function () { clearInterval(m); }, function () { m = setInterval(n, s); });

        } else if (f == 1) {
            c.css("float", "left");
            c.wrap("<div></div>").wrap("<div></div>");
            c.after(c.clone());
            var cc = c.parent();
            cc.css({ width: c.width(), height: c.height()*2});
            //alert(cc.height());
            p = cc.parent();
            p.css({ width: c.width(), height: c.height(), "overflow": "hidden" }).hover(function () { clearInterval(m); }, function () { m = setInterval(n, s); });

        } else {
            alert("错误"); return;
        }

    }
    //向左滚动
    this.sLeft = function () {
        i++;
        p.scrollLeft(i);
        if (c.outerWidth() - p.scrollLeft() <= 0) {
            i = 0;
        }
    }
    //向上滚动
    this.sTop = function () {
        i++;
        p.scrollTop(i);
        if (c.outerHeight() - p.scrollTop() <= 0) {
            i = 0;
        }
    }
    //调用自身的滚动方法
    this.play(id, f, s);
}
