﻿// JScript File

function onLoad(sender)
{
    var seconds = doClock();
    iTimeout = setTimeout(doClock, ((60000) - (seconds * 1000)));
        _onLoad();
    }    
    
///////////////////////////////////////////////////////////////////////////////////////////////////////

    var _RSS_READER_WORKING = 'Hoggett-Bowers Rss Reader is working .';
    var _PROB_RETRIEVING_XML_DATA = 'Problem retrieving XML data';        
    var _BROWSER_DOES_NOT_SUPPORT_XMLHTTP = 'Your browser does not support XMLHTTP.';
    var _DOT = '.';        
    
    var _oRssFeed = new Array();

    function _RssFeed() 
    {
        var _XmlHttp = null;
        var _Table = null;
        var _Doc = null;            
        var _M = 0;
        var _SleepTimer = 0;
        var _SleepInterval = 0;

        function _LoadRssFeed(_sURL, _Timer) 
        {
            _XmlHttp = null;
            _Doc = null;
            
            _SleepTimer = _Timer;
            
            // code for Mozilla, etc.
            if (window.XMLHttpRequest)
            {
                _XmlHttp = new XMLHttpRequest();
            }
            // code for IE
            else if (window.ActiveXObject)
            {
                _XmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            if (_XmlHttp != null)
            {
                try
                {
                    _XmlHttp.onreadystatechange = _State_Change;
                }
                catch(e)
                {
                    alert('e.message: ' + e.message );
                }

                try
                {
                    _XmlHttp.open('GET', _sURL, true);
                }
                catch(e)
                {
                    alert('e.message: ' + e.message );
                }              
              
                try
                {
                    _XmlHttp.send(null);
                }
                catch(e)
                {
                    alert('e.message: ' + e.message );
                }              
            }
            else
            {
                alert(_BROWSER_DOES_NOT_SUPPORT_XMLHTTP);
            }
        }
      
        function _State_Change() 
        {
            // if xmlhttp shows "loaded"
            if (_XmlHttp.readyState==4)
            {
                // if "OK"
                if (_XmlHttp.status==200)
                {
                    // ...some code here...
                    if (window.ActiveXObject)
                    {
                        _Doc = new ActiveXObject('Microsoft.XMLDOM');
                        _Doc.async = 'false';
                        _Doc.loadXML(_XmlHttp.responseText);

                    }
                    // code for Mozilla, Firefox, Opera, etc.
                    else
                    {
                        var parser = new DOMParser();
                        _Doc = parser.parseFromString(_XmlHttp.responseText, 'text/xml');
                    }
                }
                else
                {
                    window.status = _PROB_RETRIEVING_XML_DATA;
                }
            }
        }  

        function _Change()
        {
            try
            {
                _Table.firstChild.childNodes(0).firstChild.firstChild.innerHTML = _Doc.getElementsByTagName('item')(_GetM()).getElementsByTagName('title')(0).text;
                
                var _Attribute = document.createAttribute("_url");
                _Attribute.nodeValue = _Doc.getElementsByTagName('item')(_GetM()).getElementsByTagName('link')(0).text;
                _Table.firstChild.childNodes(0).firstChild.firstChild.setAttributeNode(_Attribute);
               
                _Attribute = document.createAttribute("class");
                _Attribute.nodeValue = 'subTitle';
                _Table.firstChild.childNodes(0).firstChild.firstChild.setAttributeNode(_Attribute);
               
                _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Doc.getElementsByTagName('item')(_GetM()).getElementsByTagName('description')(0).text;
                
                _Attribute = document.createAttribute("_url");
                _Attribute.nodeValue = _Doc.getElementsByTagName('item')(_GetM()).getElementsByTagName('link')(0).text;
                _Table.firstChild.childNodes(1).firstChild.firstChild.setAttributeNode(_Attribute);
                
                _Table.firstChild.childNodes(0).firstChild.firstChild.style.cursor = 'pointer';
                _Table.firstChild.childNodes(1).firstChild.firstChild.style.cursor = 'pointer';
                
                _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Table.firstChild.childNodes(1).firstChild.firstChild.innerText;
                
                if( _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.length > 80 )
                {
                    _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.substring(0, 80);
                    
                    var _iLastIndex = _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.lastIndexOf(' ');
                    
                    _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.substring(0, _iLastIndex);
                    
                    if( _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.lastIndexOf(',') == (_Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.length-1) )
                    {
                        _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.substring(0, _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML.length-2) + '...';
                    }
                    else
                    {
                        _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML = _Table.firstChild.childNodes(1).firstChild.firstChild.innerHTML + '...';
                    }
                }
                _IncrementM();
            }
            catch(e)
            {
                    _SetM(0);
            }
        }             

        function _SetTable(t)
        {
            _Table = t;
        }

        function _GetTimer()
        {
            try
            {
                return _SleepTimer;
            }
            catch(e)
            {
                return 0;
            }
        }

        function _GetM()
        {
            return _M;
        }

        function _SetM(param)
        {
            try
            {
                _M = param;
            }
            catch(e)
            {
                alert('e.message: ' + e.message );
            }
        }

        function _IncrementM()
        {
            return _M++;
        }

        function _GetSleepInterval()
        {
            return _SleepInterval;
        }
                      
        function _IncrementInterval()
        {
            return _SleepInterval++;
        }

        function _ResetSetSleepInterval()
        {
            _SleepInterval = 0;
        }            
      
        this.LoadRssFeed = _LoadRssFeed;
        this.Table = _Table;
        this.SetTable = _SetTable;
        this.Change = _Change;
        this.GetTimer = _GetTimer;
        this.GetM = _GetM;
        this.SetM = _SetM;
        this.IncrementM = _IncrementM;
        this.GetSleepInterval = _GetSleepInterval;
        this.IncrementInterval = _IncrementInterval;
        this.ResetSetSleepInterval = _ResetSetSleepInterval;            
    }        
    
    var onWake = 1000;
    var onWakeID = 0;
    function _onWake()
    {
        clearTimeout(onWakeID);
        onWakeID  = setTimeout('_onWake()', onWake);

        if( _oRssFeed.length > 0 )
        {
            window.status = _RSS_READER_WORKING;
        }

        for(i = 0; i < _oRssFeed.length; i++ )
        {
            try
            {
                if((_oRssFeed[i].GetSleepInterval() * 1000) == _oRssFeed[i].GetTimer())
                {
                    for(j = 0; j <= i; j++)
                    {
                        if( ( j % (i / 10) ) == 0 | i == 0 )
                        {
                            window.status = window.status + _DOT;            
                        }
                    }
                    _oRssFeed[i].Change();
                    _oRssFeed[i].ResetSetSleepInterval();
                }
                else
                {
                    _oRssFeed[i].IncrementInterval();
                }
            }
            catch(e)
            {
               alert('e.message: ' + e.message );
            }
        }
        setHeight(this);
    }
    
    function _CreateRssTable(row, col) 
    {
        var _Body = document.getElementsByTagName("body")[0];
        var _Table     = document.createElement("table");
        var _TableBody = document.createElement("tbody");

        for(var j = 0; j < row; j++) 
        {
            var _CurrentRow = document.createElement("tr");
            for(var i = 0; i < col; i++) 
            {
                var _CurrentCell = document.createElement("td");
                var _CurrentDiv = document.createElement("div");
                _CurrentCell.appendChild(_CurrentDiv);
                _CurrentRow.appendChild(_CurrentCell);
            }
            _TableBody.appendChild(_CurrentRow);
        }
        _Table.appendChild(_TableBody);
        return _Table;
    }            
    
    function _onLoad()
    {
        var j = 0;
        
        for(i = 0; i < document.body.getElementsByTagName('div').length; i++ )
        {
            try
            {
                if( document.body.getElementsByTagName('div').item(i).attributes('_type').value == 'hbrssfeed' )
                {
                    var _Url = document.body.getElementsByTagName('div').item(i).attributes('_url').value;
                    var _Timer = document.body.getElementsByTagName('div').item(i).attributes('_timer').value;
                    
                    _oRssFeed[j] = new _RssFeed();
                    
                    document.body.getElementsByTagName('div').item(i).appendChild(_CreateRssTable(2, 1));
                    
                    document.body.getElementsByTagName('div').item(i).firstChild.onclick = _onClick;
                    
                    _oRssFeed[j].SetTable(document.body.getElementsByTagName('div').item(i).firstChild);
                    _oRssFeed[j].LoadRssFeed('_ProcessRssFeed.aspx?RSS_FEED=' + _Url, _Timer);
                    
                    j++;
                }
            }
            catch(e)
            {}
        }
        
        for(i = 0; i < _oRssFeed.length; i++ )
        {
            _oRssFeed[i].Change();
        }
        _onWake();
    }        
    
    function _onClick()
    {
       try
       {
            var oLink = event.srcElement;
            window.open(oLink.parentNode.firstChild.attributes.item('_url').value);
       }
       catch(e)
       {
        
       }
    }
            
////////////////////////////////////////////////////////////////////////////////////////////////////            