// JavaScript for dynamic page contant management
// Author: John Ironmonger
// Created: Dec 5th, 2007.
// 20090917 Lots of updates 


// contentBox(contentURL, cachetime, divID, classID, refreshInterval, 
//   displayFunction)
function contentBox(contentURL, cacheTime, divID, classID, refreshInterval, 
    displayFunction) {
  // url used to fetch feed content
  this.contentURL = contentURL;
  this.refreshURI = "/dynamicContent/contentfetch.jsp?cacheTime=" + 
      cacheTime +"&urlContent=" + encodeURIComponent(contentURL);
  // Time to cache feed, in seconds. 0=no cache.
  this.cacheTime = cacheTime;
  // ID of content div to display information
  this.contentID = divID;
  // Pause between screen refreshes in seconds.
  this.refreshInterval = refreshInterval * 1000;
  // Pointer to function used to display the content in the tagged <div> 
  this.displayFunction = displayFunction;
  // Can be used to do initialization in external function calls.
  this.firstTime = true;
  // Counts of number of times the refresh display has been called.
  this.iterations = 0;
  // Boolean to indicate whether mouse is currently over content 
  //   (and pause it if it is)
  this.mouseoverBol = 0;
  this.ajaxobj = createAjaxObject();
  document.write('<div id="' + divID + '" class="' + classID + 
      '">Initializing Content Holder...<br/></div>');
  this.initialize();
}

// initialize() - Setup content management
// Setup contentBox object ready for use, then call refresh.
contentBox.prototype.initialize = function() {
  var instanceOfContent = this;
  document.getElementById(this.contentID).onmouseover = 
      function(){instanceOfContent.mouseoverBol = 1};
  document.getElementById(this.contentID).onmouseout = 
      function(){instanceOfContent.mouseoverBol = 0};
  document.getElementById(this.contentID).innerHTML += 
      "Content Holder Initialized...<br/>";
  this.refreshContent();
}


// refreshContent()- Update content after refresh delay.
// Will get fresh content and display
contentBox.prototype.refreshContent = function() {
  var instanceOfContent = this;
  // If mouse is currently over content, don't update but check back quickly
  if (this.mouseoverBol == 1)
  setTimeout(function(){instanceOfContent.refreshContent()}, 100);
  else {
    // alert (this.contentID + " Refreshing Content " + this.iterations)
    this.getAjaxContent();
    // Update container after refresh interval
    setTimeout(function(){instanceOfContent.refreshContent()}, 
        this.refreshInterval);
  }
}


// getAjaxcontent()- Makes asynchronous GET request to
// "contentfetch.jsp" with the supplied parameters
// Gets contents ready for use by the display function.
// contentfetch.jsp?cacheTime=<Seconds>&urlContent=<url to get feed>
contentBox.prototype.getAjaxContent = function() {
  var instanceOfContent = this;
  // var contentURI = "/dynamicContent/contentfetch.jsp?cacheTime=" + 
  //     this.cacheTime +"&urlContent=" + encodeURIComponent(this.contentURL);
  // alert (this.contentID + " Fetching Content " + this.iterations)
  this.ajaxobj.onreadystatechange = 
      function() {instanceOfContent.checkAjaxContentStatus()};
  this.ajaxobj.open("GET", this.refreshURI, true);
  this.ajaxobj.send(null);
}


// Check status of content request
contentBox.prototype.checkAjaxContentStatus = function() {
  // alert (this.contentID + " Checking Content Status" + this.iterations)
  // See if request of file completed
  if (this.ajaxobj.readyState == 4) {
    // See if request was successful
    if (this.ajaxobj.status > 200) {
        document.getElementById(this.contentID).innerHTML += 
            "<b>Error</b> fetching content feed! Status = " + 
             this.ajaxobj.status + "<br/>";
        return;
    }
    this.iterations++;
    this.displayFunction(this.contentID, this.ajaxobj, this.firstTime, 
        this.iterations);
    this.firstTime = false;
  }
}


// Function to support single fetch of a page at given url
// Page will be chached
// Process will wait till the content is ready before returning
// httprequest geturlOnce (contentURL, cacheTime)
function getURLOnce(contentURL, cacheTimeSeconds) {
  var ajaxobj = createAjaxObj();
  var contentURI = "/dynamicContent/contentfetch.jsp?cacheTime=" +
      cacheTimeSeconds + "&urlContent=" + encodeURIComponent(contentURL) +
      "&r=" + Math.random();
  ajaxobj.open("GET", contentURI, false);
  ajaxobj.send(null);
  return ajaxobj;
}


// displayTwitterRSSXML()- Displays RSS XML Content in div
// Specifically, selects and displays the 'title', 'link' and converts
// and displays the time for the first 10 items in the xml file
function displayTwitterRSSXML(divID, displayContentAjaxObj, firstTime,
    iterations) {
  // Get a copy of the data in DOM form
  var xmldata = displayContentAjaxObj.responseXML;
  // Check the DOM has Tag called 'item'
  if(xmldata.getElementsByTagName("item").length == 0) {
    document.getElementById(divID).innerHTML += 
        "<b>Error</b> Content feed not returning XML!<br/>" +
        displayContentAjaxObj.responseText;
    return;
  }
  var contentToDisplay = "";
  var feeditem = xmldata.getElementsByTagName("item");
  var itemShow = feeditem.length;
      if (itemShow > 10) itemShow = 10;
  var itemDate;
  var itemMinute;
  
  for (itemCount = 0; itemCount < itemShow; itemCount++) {
    itemDate = new Date (feeditem[itemCount].
        getElementsByTagName("pubDate")[0].firstChild.nodeValue);
    itemMinute = itemDate.getMinutes() + '';
    if (itemMinute.length == 1) itemMinute = '0' + itemMinute;

    contentToDisplay += '<b>' +
        itemDate.toDateString() + ' ' + itemDate.getHours() + ':' + 
        itemMinute + '</b><br/>';
    contentToDisplay += feeditem[itemCount].getElementsByTagName("title")[0].
        firstChild.nodeValue.replace("GGAFL: G", "G");
    contentToDisplay += ' <a href="' + feeditem[itemCount].
        getElementsByTagName("link")[0].firstChild.nodeValue +
        '" target="_blank">&#187;</a><br/><div class="Spacer5"></div>';
  }
  document.getElementById(divID).innerHTML = contentToDisplay;
}


// displayAFL-RSSXML()- Displays RSS XML Content in div
// Specifically, selects and displays the 'title', 'link' and converts
// and displays the time for the first 10 items in the xml file
function displayFoxAFLRSSXML(divID, displayContentAjaxObj, firstTime,
    iterations) {
  // Get a copy of the data in DOM form
  var xmldata = displayContentAjaxObj.responseXML;
  // Check the DOM has Tag called 'item'
  if(xmldata.getElementsByTagName("item").length == 0) {
    document.getElementById(divID).innerHTML += 
        "<b>Error</b> Content feed not returning XML!<br/>" +
        displayContentAjaxObj.responseText;
    return;
  }
  var contentToDisplay = "";
  var feeditem = xmldata.getElementsByTagName("item");
  var itemShow = feeditem.length;
      if (itemShow > 10) itemShow = 10;
  var itemDate;
  var itemMinute;
  
  for (itemCount = 0; itemCount < itemShow; itemCount++) {
    itemDate = new Date (feeditem[itemCount].
        getElementsByTagName("pubDate")[0].firstChild.nodeValue);
    itemMinute = itemDate.getMinutes() + '';
    if (itemMinute.length == 1) itemMinute = '0' + itemMinute;

    contentToDisplay += itemDate.toDateString() + ' ' + 
        itemDate.getHours() + ':' + itemMinute + '<br/>';
    contentToDisplay += ' <a href="' + feeditem[itemCount].
        getElementsByTagName("link")[0].firstChild.nodeValue +
        '" target="FOXAFL">' + feeditem[itemCount].
        getElementsByTagName("title")[0].firstChild.nodeValue + 
        '</a><br/><div class="Spacer5"></div>';
  }
  document.getElementById(divID).innerHTML = contentToDisplay;
}




