// Javascript to parse JSON feeds from Google News and Industry News

function build_industry_news(json) {
  var container1 = document.getElementById('industry_container');

  var list1 = document.createElement('div');

  for (var i = 0 ; i < json.feed.entry.length; i++) {
    var item = json.feed.entry[i];
    var item_title = item.title.$t;
    var item_link = item.link[0].href;
    var item_snippet = item.content.$t; 
    var post = document.createElement('p');


    var link = document.createElement('a');
    link.appendChild(document.createTextNode(item_title));
    link.href = item_link;
    post.appendChild(link);
    post.appendChild(document.createElement('br'));
    post.appendChild(document.createTextNode(item_snippet));
    list1.appendChild(post);
  }

  container1.appendChild(list1);

}

function formatDate(d,style) {
  d_arr = d.split("T");
  date = d_arr[0];
  date = date.split("-");
  var month_names = ["","January", "February", "March","April", "May", "June","July", "August", "September","October", "November", "December"];
  m_month = stripLeading(date[1]);
  m_day = stripLeading(date[2]);
  m_year = date[0];
  
  time = d_arr[1];
  time = time.split(":");
  m_hour = time[0];
  m_min = time[1];
  if (m_hour >= 12) {
    if (m_hour != 12 ) m_hour = m_hour-12;
    m_ampm = "PM";
  } else {
    m_ampm = "AM";
  }
  
  switch (style) {
    case 1:
      date = m_month+"/"+m_day+"/"+m_year+" "+m_hour+":"+m_min+" "+m_ampm;
      break;
    case 2:
      date = month_names[m_month]+" "+m_day+", "+m_year;
      break;
    default:
      date = m_month+"/"+m_day+"/"+m_year+" "+m_hour+":"+m_min+" "+m_ampm;
  }
  return date;
}

function stripLeading(d) {
  match = d.match(/^(0*)(\d*)/);
  if (match) {
    d = match[2];
  }
  return d;
}

function formatSnippet(t,wc) {
  var snippet = '';
  
  if (t.match(/<br \/><br \/>/)) {
    var snippet_arr = t.split('<br /><br />');
    for (var i = 1; i < snippet_arr.length; i++){
      snippet = snippet + snippet_arr[i];
    }
    if (snippet.charAt(snippet.length-5) == '.') {
      snippet = snippet.substring(0,snippet.length-4);
    }
  }
  
  snippet = snippet.replace(/<[^>]*>/g,"");
  var snippet_short = '';
  var words = snippet.split(" ");
  if (words.length < wc) {
    wc = words.length;
  }
  
  for (var i = 0; i <= wc; i++) {
    snippet_short = snippet_short + " " + words[i];
  }
  
  if (!snippet_short.match(/[.!?](\s*)?$/)) {
    snippet_short = snippet_short + "...";
  }
  
  return snippet_short;
}
function build_news(json) {
  var container = document.getElementById('news_container');
  var list = document.createElement('div');
  for (var i = 0 ; i < json.feed.entry.length; i++) {
    var item = json.feed.entry[i];
    var item_date = formatDate(item.published.$t,2);
    var item_title = item.title.$t;
    var item_link = item.link[0].href;
    var item_snippet = formatSnippet(item.content.$t,40);
    var post = document.createElement('p');
    var link = document.createElement('a');

    link.appendChild(document.createTextNode(item_title));
    link.href = item_link;
    post.appendChild(link);
    post.appendChild(document.createElement('br'));
    list.appendChild(post);
    var timestamp = document.createElement('span');
    timestamp.style.color = "#000";
    timestamp.appendChild(document.createTextNode(item_date));
    post.appendChild(timestamp);
    post.appendChild(document.createElement('br'));


  }

  var moreLink = document.createElement('a');
  moreLink.appendChild(document.createTextNode("More news from Google"));
  moreLink.id = "moreposts";
  moreLink.href = "http://googlepress.blogspot.com/index.html";
  
  container.appendChild(list);
  container.appendChild(moreLink);
  container.appendChild(document.createElement('br'));
  var pressEM = document.createElement('em');
  pressEM.appendChild(document.createTextNode("* press release")); 
  container.appendChild(document.createElement('br'));
  container.appendChild(pressEM);
}
