// Aug 1, 2008 - Brian Ballard - Created for Georgia Log Cabin
// Oct 4, 2008 - Brian Ballard - Added locale date/time formats & browser status line

// This script reads an RSS file on your server and displays it on your web page.

// Save this JavaScript file to your server and include it on your page, like this:
//   <script type="text/javascript" src="getHeadlines.js"></script>

// To actually show the headlines, add an onload call to your page's body tag, like this:
//   <body onload="getHeadlines();">
// Or call this script near the end of your page like this:
//   <script type="text/javascript">getHeadlines();</script>

// Include a div in your page where you want the headlines to appear, like this:
//   <div id="headlines">Loading headlines...</div>

// You can also specify a category on links, like this:
//   http://www.yourdomain.com/yourpath/yourpage.html?National
// ------------------------------------------

// Set this to your RSS file name
var headlinesFile = "/news/headlines2.xml";
//var headlinesFile = "/cgi/proxy.pl?http://feedproxy.google.com/PoliticalNewsHeadlinesFromGeorgiaLogCabinRepublicans";

// Set this to the div id where you want your headlines to show up.
var destinationDiv = "headlines";

// Translate or update these if you wish, othwerise leave them as is.
var msg1 = "Your browser does not support AJAX.";
var msg2 = "There was a problem getting the headlines file.";
var msg3 = "Posted";
var msg4 = "Showing headlines in";
var msg5 = "Show all";
var msg6 = "No headlines found.";
var msg7 = "in";
var msg8 = "Click to show only headlines in";
var msg9 = "Click to show all headlines";
var msg10 = "Click to read the full story";
var msg11 = "Available categories";
var dayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); // short day names
//var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); //long day names
var monNames = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); // short month names
//var monNames = new Array("January","February","March","April","May","June","July","August","Septempber","October","November","December"); //long month names

// ------------------------------------------
// You shouldn't have to change anything else

var thisDate = new Date();
var tz = thisDate.getTimezoneOffset();
var newsItems = new Array();
var xmlHttp;

// Get the RSS file
function getHeadlines() {

  try { // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) { // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        document.getElementById(destinationDiv).innerHTML = msg1;
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange = onResponse;
  xmlHttp.open("GET", headlinesFile, true);
  xmlHttp.send(null);
}

// Handle the file return
function onResponse() {
  if ( xmlHttp.readyState == 4 ) {
    if ( xmlHttp.status == 200 ) {
      parseRss();
      displayHeadlines();
    } else {
      document.getElementById(destinationDiv).innerHTML = msg2;
    }
  }
}


// Item object prototype
function itemObj(t,d,l,p,c1,c2) {
  this.title = t;
  this.description = d;
  this.link = l;
  this.pubDate = p;
  this.cat1 = c1;
  this.cat2 = c2;
}


// Fills item objects with item data from the RSS XML file
function parseRss() {

  var theseItems = xmlHttp.responseXML.documentElement.getElementsByTagName("item");
  var lang = navigator.language ? navigator.language.toLowerCase() : navigator.userLanguage.toLowerCase();

  for ( i = 0; i < theseItems.length; i++ ) {

    try {
      theseTitles = theseItems[i].getElementsByTagName("title");
      try {
        thisTitle = theseTitles[0].firstChild.nodeValue;
      } catch(e) { thisTitle = ""; }
    } catch(e) { thisTitle = ""; }

    try {
      theseDescriptions = theseItems[i].getElementsByTagName("description");
      try {
        thisDescription = theseDescriptions[0].firstChild.nodeValue;
      } catch(e) { thisDescription = ""; }
    } catch(e) { thisDescription = ""; }

    try {
      theseLinks = theseItems[i].getElementsByTagName("link");
      try {
        thisLink = theseLinks[0].firstChild.nodeValue;
      } catch(e) { thisLink = ""; }
    } catch(e) { thisLink = ""; }

    try {
      thesePubDates = theseItems[i].getElementsByTagName("pubDate");
      try {
        thisPubDate = thesePubDates[0].firstChild.nodeValue;
      } catch(e) { thisPubDate = ""; }
    } catch(e) { thisPubDate = ""; }

    // Put GMT pubDate into locale format, e.g. "Tue, Mar 23, 2008, 2:46pm" or "Tue, 23 Mar 2008, 14:46"
    if ( thisPubDate ) {
      thisDate.setTime(Date.parse(thisPubDate)-tz);
      dow = dayNames[thisDate.getDay()];
      dd = thisDate.getDate();
      mon = monNames[thisDate.getMonth()];
      yyyy = thisDate.getFullYear();
      hh = thisDate.getHours();
      mm = thisDate.getMinutes();
      if ( mm < 10 ) { mm = "0" + mm; }

      // Choose a date format based on the broswer language
      if (lang == "en" || lang == "en-us" || lang == "undefined"  || lang == "") {
        // US date/time formats
        if ( hh > 11 ) { mm += "pm"; } else { mm += "am"; }
        if ( hh > 12 ) { hh -= 12; }
        if ( hh == 0 ) { hh = "12"; }
//      Long US date/time format, e.g. Tue, Mar 23, 2008, 2:46pm
        //thisPubDate = dow + ", " + mon + " " + dd + ", " + yyyy + ", " + hh + ":" + mm;
//      Short US date format, e.g. Tue, Mar 23
        thisPubDate = dow + ", " + mon + " " + dd;
      } else {
        // Euro date/time formats
        if ( hh == 0 ) { hh = "00"; }
//      Long Euro date/time format, e.g. Tue, 23 Mar 2008, 14:46
        //thisPubDate = dow + ", " + dd + " " + mon + " " + yyyy + ", " + hh + ":" + mm;
//      Short Euro date format, e.g. Tue, 23 Mar
        thisPubDate = dow + ", " + dd + " " + mon;
      }
    }

    try {
      theseCategories = theseItems[i].getElementsByTagName("category");

      try {
        thisCat1 = theseCategories[0].firstChild.nodeValue;
      } catch(e) { thisCat1 = ""; }

      try {
        thisCat2 = theseCategories[1].firstChild.nodeValue;
      } catch(e) { thisCat2 = ""; }

    } catch(e) { thisCat1 = ""; thisCat2 = ""; }

    newsItems[i] = new itemObj(thisTitle,thisDescription,thisLink,thisPubDate,thisCat1,thisCat2);
  }

}


// Step through items array, printing out items in an optional matching category
function displayHeadlines(cat) {
  var txt = "";
  var txt2 = "";
  var txt3 = "";
  var catCounts = new Array();

  // Check for category on URL like page.html?National
  if ( !cat && window.location.search ) {
    // Remove the ? if the category is from an URL query string
    cat = window.location.search.substring(1);
  }

  if ( cat && (cat != "none") ) {
    // Indicate which category is being displayed and link to all categories
    txt += "<p>" + msg4 + " <b>" + cat + "</b> (<a href=\"javascript:displayHeadlines('none');\" title=\"" + msg9 + "\" onmouseover=\"self.status='" + msg9 + "';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"pageTracker._trackEvent('dhtml','latest',pickText(this.innerText,this.textContent));\">" + msg5 + "</a>)</p>";
  }

  for ( var i = 0; i < newsItems.length; i++ ) {
    if ( !cat || cat == "none" || cat.toLowerCase() == newsItems[i].cat1.toLowerCase() || cat.toLowerCase() == newsItems[i].cat2.toLowerCase() ) {
      // add each item to HTML text
      txt2 += "<p><a href=\"" + newsItems[i].link + "\" title=\"" + msg10 + "\" onclick=\"pageTracker._trackEvent('out','latest',pickText(this.innerText,this.textContent));\"><b>" + newsItems[i].title + "</b></a><br /><b>" + msg3 + ":</b> " + newsItems[i].pubDate;
      if ( newsItems[i].cat1 ) { txt2 += " " + msg7 + " <a href=\"javascript:displayHeadlines('" + newsItems[i].cat1 + "');window.scrollTo(0,0);\" title=\"" + msg8 + " " + newsItems[i].cat1 + "\" onmouseover=\"self.status='" + msg8 + " " + newsItems[i].cat1 + "';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"pageTracker._trackEvent('dhtml','latest',pickText(this.innerText,this.textContent));\">" + newsItems[i].cat1 + "</a>"; }
      if ( newsItems[i].cat2 ) { txt2 += " &amp; <a href=\"javascript:displayHeadlines('" + newsItems[i].cat2 + "');window.scrollTo(0,0);\" title=\"" + msg8 + " " + newsItems[i].cat2 + "\" onmouseover=\"self.status='" + msg8 + " " + newsItems[i].cat2 + "';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"pageTracker._trackEvent('dhtml','latest',pickText(this.innerText,this.textContent));\">" + newsItems[i].cat2 + "</a>"; }
      txt2 += "<br />" + newsItems[i].description + "</p>";
    }
	
	if (newsItems[i].cat1) {
	  if ( catCounts[newsItems[i].cat1] ) {
	    catCounts[newsItems[i].cat1] += 1;
	  } else {
	    catCounts[newsItems[i].cat1] = 1;
	  }
	}
	
	if ( newsItems[i].cat2 ) {
	  if ( catCounts[newsItems[i].cat2] ) {
	    catCounts[newsItems[i].cat2] += 1;
		} else {
		  catCounts[newsItems[i].cat2] = 1;
		}
	}
  }

  if ( txt2 == "" ) { txt2 = msg6; }
  txt += txt2;

  for (var j in catCounts) {
	txt3 += "<a href=\"javascript:displayHeadlines('" + j + "');window.scrollTo(0,0);\" title=\"" + msg8 + " " + j + "\" onmouseover=\"self.status='" + msg8 + " " + j + "';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"pageTracker._trackEvent('dhtml','latest',pickText(this.innerText,this.textContent));\">" + j + "</a>&nbsp;(" + catCounts[j] + "), ";
  }
  txt3 += "<a href=\"javascript:displayHeadlines('none');window.scrollTo(0,0);\" title=\"" + msg9 + "\" onmouseover=\"self.status='" + msg9 + "';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"pageTracker._trackEvent('dhtml','latest',pickText(this.innerText,this.textContent));\">" + msg5 + "</a>";
  
  txt = txt + "<p><b>" + msg11 + "</b>: " + txt3 + "</p>";
  
  document.getElementById(destinationDiv).innerHTML = txt;
}

