var Marquee = {};
Marquee.List = [];
Marquee.add = function(id, interval, margin, mode, addHover, fulldiv, diff) {
    var $div = $("#" + id);
    var parentWidth = $div.parent().width();
    var parentHeight = $div.parent().height();

    var oneHtmlContent = $div.parent().html();
    fulldiv = (fulldiv && ((mode == 'up') || (mode == 'down')));

    if (addHover) {
        $div.parent().hover(function() {
            Marquee.setTimer(id, true);
        }, function() {
            Marquee.setTimer(id, false);
        });
    };

    $div.wrap('<div id="divparent_' + id + '"></div>');
    $("#divparent_" + id).css('width', '100%');

    var width = $div.css('float', 'right').width();
    var height = $div.height();

    $div.css('float', 'none');
    var count = Math.ceil(parentHeight / height);
    count += 3;

    if (fulldiv) {
        var i;
        for (i = 0; i < count; i++) {
            $div.parent().append(oneHtmlContent);
        };
    };

    var pr = "";
    var ct = 0;
    switch (mode) {
        case "up":
            pr = "margin-top";
            ct = parentHeight;
            break;
        case "down":
            pr = "margin-top";
            ct = -height;
            break;
        case "left":
            pr = "margin-right";
            ct = -width;
            break;
        case "right":
            pr = "margin-right";
            ct = parentWidth;
            break;
    };
    if (fulldiv) {
        $("#divparent_" + id + " :" + ((mode == 'up') ? "first" : "last")).css(pr, ct + 'px');
    } else {
        $div.css(pr, ct + 'px');
    };
    Marquee.List[id] = {
        width: width,
        height: height,
        parentWidth: parentWidth,
        parentHeight: parentHeight,
        current: ct,
        timer: null,
        interval: interval,
        margin: margin,
        property: pr,
        mode: mode,
        full: fulldiv,
        diff: diff
    };
    Marquee.setTimer(id, false);
}
Marquee.setTimer = function(id, cancel) {
    if (cancel) {
        window.clearInterval(Marquee.List[id].timer);
    } else {
        Marquee.List[id].timer = window.setInterval("Marquee.scroll('" + id + "')", Marquee.List[id].interval);
    };
}
Marquee.scroll = function(id) {
    if ((Marquee.List[id].mode == 'right') || (Marquee.List[id].mode == 'up')) {
        var lastValue = (Marquee.List[id].mode == 'right') ? (-Marquee.List[id].width) : (-Marquee.List[id].height);
        var currentValue = (Marquee.List[id].mode == 'right') ? Marquee.List[id].parentWidth : Marquee.List[id].parentHeight;
        if (Marquee.List[id].current < lastValue) {
            if (Marquee.List[id].full) {
                $("#divparent_" + id + " :first").css(Marquee.List[id].property, "").remove().appendTo("#divparent_" + id);
                Marquee.List[id].current = (Marquee.List[id].current) - (lastValue) - Marquee.List[id].diff;
            } else {
                Marquee.List[id].current = currentValue;
            };
        } else {
            Marquee.List[id].current -= Marquee.List[id].margin;
        };
    } else {
        var lastValue = (Marquee.List[id].mode == 'left') ? Marquee.List[id].parentWidth : Marquee.List[id].parentHeight;
        var currentValue = (Marquee.List[id].mode == 'left') ? (-Marquee.List[id].width) : (-Marquee.List[id].height);
        if (Marquee.List[id].current > lastValue) {
            if (Marquee.List[id].full) {
                $("#divparent_" + id + " :last").css(Marquee.List[id].property, "").remove().prependTo("#divparent_" + id);
                Marquee.List[id].current = (Marquee.List[id].current) - (lastValue) - Marquee.List[id].diff;
            } else {
                Marquee.List[id].current = currentValue;
            };
        } else {
            Marquee.List[id].current += Marquee.List[id].margin;
        };
    };
    if (Marquee.List[id].full) {
        $("#divparent_" + id + " :" + ((Marquee.List[id].mode == 'up') ? "first" : "last")).css(Marquee.List[id].property, (Marquee.List[id].current) + 'px');
    } else {
        $("#" + id).css(Marquee.List[id].property, (Marquee.List[id].current) + 'px');
    };
}

