var Marquee = {};
Marquee.List = [];
Marquee.add = function(id, interval, margin, mode, addHover) {
    var $div = $("#" + id);
    var parentWidth = $div.parent().width();
    var parentHeight = $div.parent().height();

    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', '9999px');

    var width = $div.css('float', 'right').width();
    var height = $div.height();

    $div.css('float', 'none');

    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;
    };

    $div.css(pr, parentWidth + 'px');
    Marquee.List[id] = {
        width: width,
        height: height,
        parentWidth: parentWidth,
        parentHeight: parentHeight,
        current: ct,
        timer: null,
        interval: interval,
        margin: margin,
        property: pr,
        mode: mode
    };
    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) {
            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) {
            Marquee.List[id].current = currentValue;
        } else {
            Marquee.List[id].current += Marquee.List[id].margin;
        };
    };

    $("#" + id).css(Marquee.List[id].property, (Marquee.List[id].current) + 'px');
}
