//**************This javascript is modified to communicate with the RAILS controller using XML**********
// Note that the AJAX framework file prototype.js is used to provide
// the AJAX functionality. prototype.js MUST be declared (included)
// before calling the window.onload function below.

// An array of feed urls which are used in multiple spots. For example, the
// same GCMD feed is used to populate several locations, with slightly
// different output formats. This array allows us to store the feed URL
// only once.
// Use an object to mimic the behavior of an associative array (they don't
// actually exist in javascript based on the resources I've located at the
// time of this writing).
var feed_urls = new Object();
feed_urls['ipy_news'] = "http://classic.ipy.org/start/index.php/forums/rss/4/";
feed_urls['gcmd']     = 'http://mercdev3.ornl.gov/daddi3/send/processRss?term2=a*&term2attribute=fullText&op2=+AND+&term4=&term4attribute=beginDate&op4=during&term5=&term5attribute=endDate&op5=&term6attribute=datasource&op6=+OR+&term6=gcmd';
//escaped version:
//feed_urls['gcmd'] = "http://mercdev3.ornl.gov/daddi3/send/processRss%3Fterm2=a*%26term2attribute=fullText%26op2=%2BAND%2B%26term4=%26term4attribute=beginDate%26op4=during%26term5=%26term5attribute=endDate%26op5=%26term6attribute=datasource%26op6=%2BOR%2B%26term6=gcmd";

// an array of feed information
// if you need to add a new feed, just add the required elements to this array
var rss_info = new Array();

// note that we're ignoring the first array element in order to
// make it more intuitive to map div id numbers to array indices

// ipydis news in the home page "related links" area
rss_info[1] = new Array();
rss_info[1][0] = "date_title";     // output template
rss_info[1][1] = 3;                // max number of items to show
rss_info[1][2] = 100;              // max number of characters for text fields
rss_info[1][3] = feed_urls['ipy_news']; // feed link

// content for the community events page
rss_info[2] = new Array();
rss_info[2][0] = "title_excerpt";  // output template
rss_info[2][1] = 4;                // max number of items to show
rss_info[2][2] = 200;              // max number of characters for text fields
rss_info[2][3] = "http://classic.ipy.org/start/index.php/forums/rss/17/"; // feed link

// GCMD content for the home page body
rss_info[3] = new Array();
rss_info[3][0] = "title_only";     // output template
rss_info[3][1] = 3;                // max number of items to show
rss_info[3][2] = 1;                // max number of characters for text fields
rss_info[3][3] = feed_urls['gcmd']; // feed link

// the "related links" content for data pages
rss_info[4] = new Array();
rss_info[4][0] = "title_short_excerpt";  // Different output template used to differentiate feeds 4 and 5 in cached files
rss_info[4][1] = 4;                // max number of items to show
rss_info[4][2] = 100;              // max number of characters for text fields
rss_info[4][3] = feed_urls['gcmd']; // feed link
		
// content for "recent data contributions" page
rss_info[5] = new Array();
rss_info[5][0] = "title_excerpt";  // output template
rss_info[5][1] = 4;                // max number of items to show
rss_info[5][2] = 500;              // max number of characters for text fields
rss_info[5][3] = feed_urls['gcmd']; // feed link

// stuff for testing
rss_info[6] = new Array();
rss_info[6][0] = "rss_dump";       // output template
rss_info[6][1] = 12;               // max number of items to show
rss_info[6][2] = 100;              // max number of characters for text fields
rss_info[6][3] = feed_urls['ipy_news']; // feed link

rss_info[7] = new Array();
rss_info[7][0] = "rss_dump";       // output template
rss_info[7][1] = 12;               // max number of items to show
rss_info[7][2] = 100;              // max number of characters for text fields
rss_info[7][3] = feed_urls['gcmd']; // feed link

window.onload = function(){
    for(j=0; j<rss_info.length-1; j++){
        div_ix = j + 1;
        div_id = 'RSSFEED_' + div_ix;
        if ( feed_div = $(div_id) ) {

            // feed div exists in this page; update the content
            // first create the serializer object
            var stringMaker = new XMLSerializer();
            var xmlDoc = document.implementation.createDocument('','',null);
            var rootNode    = xmlDoc.createElement('start');
            xmlDoc.appendChild(rootNode);
            rootNode.setAttribute("div_id", div_id);
            rootNode.setAttribute("template",rss_info[div_ix][0]);
            rootNode.setAttribute("max_no_items",rss_info[div_ix][1]);
            rootNode.setAttribute("max_no_chars",rss_info[div_ix][2]);
            var dummyNode   = xmlDoc.createTextNode(rss_info[div_ix][3]);
            rootNode.appendChild(dummyNode);

            // convert the xmlDocument object to a string
            var xmlString   = stringMaker.serializeToString(xmlDoc);

            new Ajax.Updater({success: div_id}, "/rails/common_apps/rss", {
                method: 'get',
                parameters: { 'var': xmlString }
            });
            // would prefer to use something like this, rather than all of
            // the XML steps above...max_no_items and max_no_chars were
            // not being parsed correctly when using the parameters arrangement
            // below, however (div id, template and url seemed ok, though...
            // go figure).
            //    parameters: { div_id: div_id, template: rss_info[div_ix][0],
            //                  max_no_items: rss_info[div_ix][1],
            //                  max_no_chars: rss_info[div_ix[2],
            //                  rss_url: rss_info[div_ix][3]}
        }
    }
}
