/**
 * Javascript ScrollerRSS
 * @author: Gerardo Orellana <orellg@mnsu.edu>
 * version 0.9
 * Last Modified: Thu Oct 1 17:37:51 2009
 * Path:  /util/smbl/build-scrollrss/scroller.js
 */

var xmlHttp;

function $()
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') {
            element = document.getElementById(element);
        }
        if (arguments.length == 1) {
            return element;
        }
        elements.push(element);
    }
    return elements;
}

function goScroller(options)
{
    if(!options) return die(' crop > cannot create crop: no options were found\n');
    this.config  = options;
    this.reference = 0;
    this.parser;
    this.current_opacity = 0.0;
    this.mouseUp = 0;
    
    // Retrieve **XML** document (generic function)
    this.getXMLHTTPObject = function()
    {
        xmlHttp=null;
        try {
            // branch for native XMLHttpRequest object
            xmlHttp=new XMLHttpRequest();
        } catch(e) {
            //Internet Explorer
            try {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e) {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    
    this.fadeScroll = function(){
        var myScroll = this;
        $(myScroll.config.sDiv).style.MozOpacity = myScroll.current_opacity;
        myScroll.current_opacity += 0.1;
        if (myScroll.current_opacity < 1.0)  setTimeout ( function() { myScroll.fadeScroll() }, 80 ); 
    }
    
    this.scrollRSS = function()
    {   
        var myScroll = this; // due closures
    
        if (myScroll.mouseUp == 1){
            setTimeout(function(){myScroll.scrollRSS()}, 100);
        } else {
            $(myScroll.config.sDiv).innerHTML = ''; // clear up div
            myScroll.current_opacity = 0.0; // set opacity to 0 so we get the 'fading' effect
    
            switch(myScroll.config.sDisplay)
            {
                case 'all':
                    $(myScroll.config.sDiv).innerHTML +='<p><a href="'+myScroll.parser[myScroll.reference].getAttribute("golink")+'" title="Goto news source.">'+myScroll.parser[myScroll.reference].getAttribute("gotitle")+'</a>' +
                                                        '<br /><span style="font-size:10px;">'+myScroll.parser[myScroll.reference].getAttribute("godate")+'</span>' + 
                                                        '<br /><br /><span style="font-size:11px;">'+myScroll.parser[myScroll.reference].getAttribute("godescription")+'</span></p>';
                    break;
                case 'title':
                    $(myScroll.config.sDiv).innerHTML +='<p><a href="'+myScroll.parser[myScroll.reference].getAttribute("golink")+'" title="Goto news source.">'+myScroll.parser[myScroll.reference].getAttribute("gotitle")+'</a>' +
                                                        '<br /><span style="font-size:10px;">'+myScroll.parser[myScroll.reference].getAttribute("godate")+'</span></p>'; 
                    break;
                case 'description':
                    $(myScroll.config.sDiv).innerHTML +='<p><a href="'+myScroll.parser[myScroll.reference].getAttribute("golink")+'" title="Goto news source.">'+myScroll.parser[myScroll.reference].getAttribute("godescription")+'</a></p>';
                    break;
                default: // only shows title instance
                    $(myScroll.config.sDiv).innerHTML +='<a href="'+myScroll.parser[myScroll.reference].getAttribute("golink")+'" title="Goto news source.">'+myScroll.parser[myScroll.reference].getAttribute("gotitle")+'</a>';
            }
            myScroll.reference = (myScroll.reference < myScroll.parser.length - 1) ? myScroll.reference + 1 : 0;
            setTimeout ( function() { myScroll.scrollRSS() }, myScroll.config.iDelay ); //update container every 60 second
            if(myScroll.config.bFader) myScroll.fadeScroll();
        }
    }
    
    this.fetchRSS = function()
    {
        var myScroll = this; // due closures
        xmlHttp = this.getXMLHTTPObject();
        if (xmlHttp === null){ alert('Browser does not support HTTP Request');  return }
        
        // display loading message prior getting XML
        $(myScroll.config.sDiv).innerHTML = 'Loading RSS feed';
        var url='/util/smbl/build-scrollrss/scroller.php' + '?src=' + this.config.sURL + '&ctime=' + this.config.iCacheTime  + '&sid=' + Math.random();
        xmlHttp.open('GET',url,true);
        xmlHttp.send(null);
        
        // Returns a cross-browser XMLHttpRequest object. **HTTP**
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    // The response is parsed as if it were a text/xml stream NOT as *text*.
                    var xml = xmlHttp.responseXML;
                    
                    if (xml.getElementsByTagName('item').length==0) {
                        $(myScroll.config.sDiv).innerHTML = '<p>Error fetching data from XML cache file</p>';
                        return;
                    }
                    
                    $(myScroll.config.sDiv).innerHTML = '';
                    myScroll.parser = xml.getElementsByTagName('item');
                    // has to be parsed as XML
                    for (var i=0; i<myScroll.parser.length; i++) {
                        myScroll.parser[i].setAttribute('gotitle', myScroll.parser[i].getElementsByTagName('title')[0].firstChild.nodeValue);
                        myScroll.parser[i].setAttribute('golink', myScroll.parser[i].getElementsByTagName('link')[0].firstChild.nodeValue);
                        // Firefox has a 4096 character limit for XML nodes
                        if(typeof(myScroll.parser[i].getElementsByTagName('description')[0].textContent) != "undefined")
                            myScroll.parser[i].setAttribute('godescription', myScroll.parser[i].getElementsByTagName('description')[0].textContent); 
                        else // IE
                            myScroll.parser[i].setAttribute('godescription', myScroll.parser[i].getElementsByTagName('description')[0].text); 
                        myScroll.parser[i].setAttribute('godate', myScroll.parser[i].getElementsByTagName('pubDate')[0].firstChild.nodeValue);
                    }
                    $(myScroll.config.sDiv).onmouseover = function(){
                        myScroll.mouseUp = 1; // set flag
                    };
                    $(myScroll.config.sDiv).onmouseout = function(){
                        myScroll.mouseUp = 0; // set flag
                    };
                    myScroll.scrollRSS();
                }
            }
        }
    }
    // not need
    this.init = function()
    {
        this.fetchRSS();
    }
}

