// Copyright 2007 Google Inc.
// All Rights Reserved.

/**
 * @fileoverview Pulls data from Enterprise Side News Blog
 *               Fills side table with three news items
 *               based on Blog labels
 *
 *               Thanks to Champika
 *       
 * @author thorl@google.com (Thor Lewis)
 */
 
// initialize globals 
var counter = 0; // counter for containers to fill
var topC = null;
var midC = null;
var botC = null;
var badTags;

/*
 * Parse Blog feed for labels and fill news table with news
 * @param {} raw data feed data from blog
*/
function listEntries(json) {
  topC = document.getElementById('topNewsItem');
  midC = document.getElementById('midNewsItem');
  botC = document.getElementById('botNewsItem');
  var a = (topC)? 1 : (midC)? 1 : (botC)? 1 : 0;
  if (a == 1){ // no nulls detected
    var container = topC; // first container to fill
    var type = getType();
    // iterate through the data items in feed
	if (type) {
      for (var i = 0 ; i < json.feed.entry.length; i++) {
		if(counter > 2) { // only allow three
		  break;
        }
		for (var j = 0 ; j < json.feed.entry[i].category.length ; j++){ //determine which label it has
		  if ( type == json.feed.entry[i].category[j].term ) {
            checkType(type, json, container, i);
          }
        }
      }
    }
  }
}

/*
 * Prints data in news table
 * @param {String} data feed data from blog
 * @param {String} container div to fill data in
 * @param {String} m iteration through blog data
*/
function printContainer(json, container, m) {
  var json = json;
  var entry = json.feed.entry[m];
  var txt = entry.title.$t;
  var link = document.createElement('a');
  link.href = entry.link[1].href;
  link.appendChild(document.createTextNode(txt));
  
  var snippet = entry.content.$t;
  if (entry.content.$t) {
    snippet = entry.content.$t;
    if (snippet.indexOf('\n\n') != -1) {
      snippet = snippet.split('\n\n');
      snippet = snippet[1].replace(/\n/g,'');
      if (snippet.charAt(snippet.length-5) == '.') {
        snippet = snippet.substring(0,snippet.length-4);
      }
    }
  }
  snippet = snippet.replace(/<[^>]*>/g,"");
  
  container.appendChild(link);
  container.appendChild(document.createElement('br'));
  container.appendChild(document.createTextNode(snippet));
}

/*
 * Checks the type of news and prints it out
 * @param {String} type what label the news applies
 * @param {String} data feed data from blog
 * @param {String} container div to fill data in
 * @param {String} m iteration through blog data
*/
function checkType(type, json, container, m) {
  var json = json;
  container = counter == 0 ? topC : counter == 1 ? midC : botC;
  printContainer(json, container, m);
  counter++;
}

/*
 * Returns the type of News in which to print
*/
function getType() {
  var type1 = document.getElementById('mainNewsTbl');
  var type2 = document.getElementById('gsaNewsTbl');
  var type3 = document.getElementById('miniNewsTbl');
  var type = (type1) ? "Main" : (type2)? "GSA" : (type3)? "Mini" : null;
  return type;
}