//Declare return variables for the script

var bodyReturnHeight;
var GMSNewsFeed = { };

//Set the top of the holder <div> to the bottom of the page

$('#rss_reader').find('div.holder').css('top', $(window).height() + 'px');

function launchNewsFeed () {
    
    $('a#recent_news_btn').css('background-image', 'url(\'/_images/home/rss_feed_loader.gif\')');
    $('a#recent_news_btn').html('');
    
    RSXResourceLoader.resourcesFinishedLoading      =   function () {
        
        $.ajax({
            
            url: '/_scripts/exe/news_feed.php?act=call' ,
            dataType: 'json' ,
            success: function (data) {
                
                if(data.length) {
                    
                    //Scroll the window to the top
                    //Prevents <body> from clipping the top of the news feed
                    
                    scroll(0, 0);
                    
                    //Create the news feed
                    
                    $(document.body).append('<div id="rss_reader">' +
                                                '<div class="holder">' +
                                                    '<div class="left_column">' +
                                                        '<ul class="news_feed"></ul>' +
                                                    '</div>' +
                                                    '<div class="right_column">' +
                                                        '<div class="header">' +
                                                            '<div class="title"></div>' +
                                                            '<div class="close">X</div>' +
                                                        '</div>' +
                                                        '<article>' +
                                                            '<div id="article_container"></div>' +
                                                        '</article>' +
                                                    '</div>' +
                                                '</div>' +
                                            '</div>');
                    
                    //Set the return height for the body
                    
                    bodyReturnHeight    =   $(document.body).height();
                    
                    //Set the width and height of the RSS reader
                    
                    $('#rss_reader').css('width', $(window).width() + 'px');
                    $('#rss_reader').css('height', $(window).height() + 'px');
                    
                    //Set the width and height of the holder <div>
                    
                    $('#rss_reader').find('div.holder').css('width', $(window).width() + 'px');
                    $('#rss_reader').find('div.holder').css('height', $(window).height() + 'px');
                    
                    //Set the height of the news feed list
                    
                    $('#rss_reader').find('ul.news_feed').css('height', $(window).height() + 'px');
                    
                    //Set the body height and overflow properties
                    
                    $(document.body).css('overflow', 'hidden');
                    $(document.body).css('height', $(window).height() + 'px');
                    
                    //Set a timeout function to set the top and opacity properties
                    
                    setTimeout(function () { $('#rss_reader').css('opacity', '1'); $('#rss_reader').find('div').css('top', '0px'); }, 20);
                    
                    $('#rss_reader').find('div.close').click(closeNewsFeed);
                    
                    //Handle each article in the data set returned
                    
                    for(article in data) {
                        
                        //Create the list element to hold the article
                        //Append the element to the news feed
                        
                        $('#rss_reader').find('ul.news_feed').append($('<li>' +
                                                                        '<h3>' + data[article]['title'] + '</h3>' +
                                                                        '<p>' + data[article]['description'] + '</p>' +
                                                                       '</li>'));
                        
                    }
                    
                    //
                    
                    $($('#rss_reader').find('ul.news_feed').children('li')[0]).attr('class', 'active');
                    
                }
                else {
                    
                    //Show the alert window with the error
                    
                    alert       =   new RSXAlertWindow();
                    alert.setTitleAndMessage('News Error', 'We encountered an error when retrieving the news articles. Please try again later');
                    alert.show();
                    
                }
                
                //Closes the news feed window
                
                $('#rss_reader').find('div.close').click(function() {
                    
                    //Set opacity to 0 and set holder top position to window height
                    
                    $('#rss_reader').css('opacity', '0');
                    $('#rss_reader').find('div.holder').css('top', $(window).height() + 'px');
                    
                    //Set timeout to remove the RSS reader
                    
                    setTimeout(function () { $('#rss_reader').css('width', '0px'); $('#rss_reader').css('height', '0px'); $(document.body).css('height', bodyReturnHeight + 'px'); $(document.body).css('overflow', 'visible'); }, 1000);
                    
                });
                
                //Reset the recent news button
                
                $('a#recent_news_btn').css('background-image', 'none');
                $('a#recent_news_btn').html('Recent News');
                
            }
            
        });
        
        //Set the function to nothing
        
        this.resourcesFinishedLoading   =   function () { };
        
    };
    RSXResourceLoader.loadStylesheet(['/_styles/std/home/news_feed.css']);
    
}



//Closes the news feed window

function closeNewsFeed () {
             
    //Set opacity to 0 and set holder top position to window height
    
    $('#rss_reader').css('opacity', '0');
    $('#rss_reader').find('div.holder').css('top', $(window).height() + 'px');
    
    //Set timeout to remove the RSS reader
    
    setTimeout(function () { $('#rss_reader').css('width', '0px'); $('#rss_reader').css('height', '0px'); $(document.body).css('height', bodyReturnHeight + 'px'); $(document.body).css('overflow', 'visible'); }, 1000);
    
}


//Set the "recent news" button to show the news feed upon click

$("#recent_news_btn").click( launchNewsFeed );
