
(function () {

    if (typeof DOMParser == "undefined") {
       DOMParser = function () {};
       DOMParser.prototype.parseFromString = function (str, contentType) {
          if (typeof ActiveXObject != "undefined") {
            
             var d = new ActiveXObject("MSXML.DomDocument");
             d.loadXML(str);
             return d;
          } else if (typeof XMLHttpRequest != "undefined") {
            
             var req = new XMLHttpRequest;
             req.open("GET", "data:" + (contentType || "application/xml") +
                             ";charset=utf-8," + encodeURIComponent(str), false);
             if (req.overrideMimeType) {
                req.overrideMimeType(contentType);
             }
             req.send(null);
             return req.responseXML;
          }
       }
    }

    MVD.RSSReader = function (url) {
        this.url = url;
        this.text = null;
        this.xml = null;
        this.onLoad = null;
        this.onError = null;
    }

    MVD.RSSReader.prototype = {
        get : function () {
            var that = this;
            MVD.Ajax.get(this.url, function (txt) {
                that.text = txt;
                var parser = new DOMParser();
                that.xml = parser.parseFromString(txt, "text/xml"); // getXMLParser(txt);
                if (that.onLoad) {
                    that.onLoad();
                }
            }, this.onError);
        }
    }

    MVD.MarqueeRssH = function (elemId, url, width, height) {
        this.id = elemId;
        this.elem = document.getElementById(elemId);        
        this.url = url;
        this.scroll = 0;
        this.items = [];
        this.currentItem = 0;
        this.width = width;
        this.height = height;
        this.lines = [];
        this.PAUSE = 100;
        this.state = 2;
        this.timer = 0;
    }

    MVD.MarqueeRssH.prototype = {
        _appendItem : function (item) {
            function getInfo(name) {
                return item.getElementsByTagName(name)[0].firstChild.nodeValue;
            }                                           
            this.items.push({ title: getInfo("title"), link: getInfo("link") });            
        },
    
        init : function () {               
            this._config();
            var reader = new MVD.RSSReader(this.url);
            var that = this;
            reader.onLoad = function () {
                var items = this.xml.getElementsByTagName("item");
                for (var i=0,l = items.length; i<l; i++) {
                    that._appendItem(items[i]);
                }        
                that._changeLine(1, 0);
                that.currentItem = 0;                
                that.start();                
            }
            reader.get();
        },
          
        _appendLine : function() {
            var c = document.createElement("div");                
            c.style.height = this.height + 'px';
            c.style.width = this.width + 'px';
            c.style.overflow = 'hidden';
            var that = this;
            c.onmouseout = function () {
                that.start();
            }
            c.onmouseover = function () {
                that.stop();
            }
            
            this.elem.appendChild(c); 
            this.lines.push(c);
            return c;
        },
        
        _config : function () {    
            /* Estilizar elemento principal */
            this.elem.style.height = this.height + 'px';
            this.elem.style.width = this.width + 'px';
            this.elem.style.overflow = 'hidden';
            var l = this._appendLine();
            l.style.marginTop = this.height + 'px';   
            l = this._appendLine();                           
        },
        
        _changeLine : function (lineId, itemId) {            
            this.lines[lineId].innerHTML = '<a href="' + this.items[itemId].link + '" target="_blank">' + this.items[itemId].title + '</a>';
        },  
        
        _step : function () {
            switch(this.state) {
            
            case 1: // pausado 
                this.pause --;
                if (this.pause == 0) {                                        
                    this.state = 2;
                }                                        
                break;                               
            case 2: // subiendo 2 
                
                var dif = (this.height + this.scroll) / 5;
                if (dif < 1) {
                    dif = 1;
                } 
                // console.log(dif);
                this.scroll -= dif;
                if (this.scroll <= (- this.height)) {
                    this.scroll = this.height;
                    this.lines[0].innerHTML = this.lines[1].innerHTML;
                    this.currentItem ++;
                    if (this.currentItem >= this.items.length) {
                        this.currentItem = 0;
                    }
                    this._changeLine(1, this.currentItem);                
                    this.scroll = 0;                
                    this.state = 1;
                    this.pause = this.PAUSE;
                }
                this.lines[0].style.marginTop = '' + this.scroll + 'px';            
                break;        
            }
        },
                
        start : function () {                          
            if (!this.timer) {
                var that = this;                       
                this.timer = setInterval(function () {
                    that._step();
                }, 30);
            }
        },
        stop : function () {
            clearInterval(this.timer);
            this.timer = 0;        
        }                     
    }

}) ();
