/*************************************************************
 * This script is developed by Arturs Sosins aka ar2rsawseen, http://code-snippets.co.cc
 * Fee free to distribute and modify code, but keep reference to its creator
 *
 * Marquee class can be used to create horizontal or vertical marquee elements in websites
 * This class rotates all provided html elements, so marquee will never be empty
 * Marquee div element can be customized using css styles
 * There are also customizations for rotating speed and separator, etc
 *
 * For more information, examples and online documentation visit:
 * http://code-snippets.co.cc/JS-classes/Javascript-marquee
 **************************************************************/
var marquee = function (f) {
        this.elem = document.getElementById(f);
        var g = this;
        this.children = [];
        this.visible = [];
        this.conf = {
            step: 1,
            interval: 10,
            stop_on_hover: true,
            separator: "",
            backwards: false
        };
        this.construct = function () {
            this.elem.style.overflow = "hidden";
            var a = k(this.elem, "position");
            if (a != "absolute" && a != "fixed") {
                this.elem.style.position = "relative"
            }
            while (this.elem.childNodes[0]) {
                var b = this.elem.childNodes[0];
                this.elem.removeChild(b);
                if (b.nodeType == 1) {
                    var c = document.createElement("div");
                    c.style.position = "absolute";
                    c.style.whiteSpace = "pre";
                    c.appendChild(b);
                    this.children.push(c);
                    if (this.conf.separator != "") {
                        c = document.createElement("div");
                        c.innerHTML = this.conf.separator;
                        c.style.position = "absolute";
                        c.style.whiteSpace = "pre";
                        this.children.push(c)
                    }
                }
            }
            var d = 0;
            for (var i = 0; true; i++) {
                this.cur = i % this.children.length;
                var b = this.children[this.cur].cloneNode(true);
                this.elem.appendChild(b);
                var e = this.visible.push(b);
                this.visible[e - 1].style[this.pos] = d + "px";
                if (d > this.elem[this.size]) {
                    break
                }
                d += this.visible[e - 1][this.size]
            };
            if (this.conf.backwards) {
                this.children.reverse()
            }
            if (this.conf.stop_on_hover) {
                j(g.elem, "mouseover", function () {
                    if (g.timer) {
                        clearTimeout(g.timer)
                    }
                });
                j(g.elem, "mouseout", function () {
                    g.timer = setTimeout(h, g.conf.interval)
                })
            }
            h()
        };
        var h = function () {
                var a;
                var b;
                for (var i in g.visible) {
                    if (!a) {
                        a = g.visible[i]
                    }
                    b = g.visible[i];
                    if (!g.conf.backwards) {
                        g.visible[i].style[g.pos] = (parseInt(g.visible[i].style[g.pos]) - g.conf.step) + "px"
                    } else {
                        g.visible[i].style[g.pos] = (parseInt(g.visible[i].style[g.pos]) + g.conf.step) + "px"
                    }
                }
                if (!g.conf.backwards) {
                    if (parseInt(a.style[g.pos]) + a[g.size] < 0) {
                        g.visible.shift();
                        g.elem.removeChild(a)
                    }
                    if (parseInt(b.style[g.pos]) <= g.elem[g.size]) {
                        g.cur = (g.cur + 1) % g.children.length;
                        var c = g.children[g.cur].cloneNode(true);
                        g.elem.appendChild(c);
                        c.style[g.pos] = (parseInt(b.style[g.pos]) + b[g.size]) + "px";
                        g.visible.push(c)
                    }
                } else {
                    if (parseInt(a.style[g.pos]) >= 0) {
                        g.cur = (g.cur + 1) % g.children.length;
                        var c = g.children[g.cur].cloneNode(true);
                        g.elem.appendChild(c);
                        c.style[g.pos] = (parseInt(a.style[g.pos]) - c[g.size]) + "px";
                        g.visible.unshift(c)
                    }
                    if (parseInt(b.style[g.pos]) > g.elem[g.size]) {
                        g.visible.pop();
                        g.elem.removeChild(b)
                    }
                }
                g.timer = setTimeout(h, g.conf.interval)
            };
        var j = function (a, b, c) {
                if (a.addEventListener) {
                    a.addEventListener(b, c, false)
                } else {
                    a.attachEvent('on' + b, c)
                }
            };
        var k = function (a, b) {
                if (document.defaultView) {
                    return document.defaultView.getComputedStyle(a, null).getPropertyValue(b)
                } else {
                    return a.currentStyle[b]
                }
            };
        this.horizontal = function (a) {
            if (a) {
                for (var b in a) {
                    this.conf[b] = a[b]
                }
            }
            this.pos = "left";
            this.size = "offsetWidth";
            this.apos = "top";
            this.construct()
        };
        this.vertical = function (a) {
            if (a) {
                for (var b in a) {
                    this.conf[b] = a[b]
                }
            }
            this.pos = "top";
            this.size = "offsetHeight";
            this.apos = "left";
            this.construct()
        }
    }
