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

/**
 * @fileoverview Functions needed for kml_pull.js
 * and kml_entry_pull.js
 *
 * @author thorl@google.com (Thor Lewis)
 *
 */

/*
 * Capitalizes first character in a string
 * @param {String} cap_str string to be capitalized
  * @return {String} Returns the capitalized string
*/
function capMe(cap_str) {
  caps = cap_str.split(' ');
  var n_str = '';
  for (var n = 0 ; n < caps.length ; n++) {
    n_str = n_str + caps[n].substr(0,1).toUpperCase();
    n_str = n_str + caps[n].substr(1,caps[n].length) + ' ';
  }
  n_str = n_str.substr(0,n_str.length-1);
  return n_str;
}

/*
 * Abbreviates a string of text
 * @param {String} content is the string to be abbreviated
 * @param {Number} size is the max size the string can be
  * @return {String} Returns the abbreviated content
*/
function abbrev(content, size) {
  if ( content.length > size ) {
    content = content.substr(0,size);
    cs = content.split(' ');
    content='';
    for (var s = 0 ; s < cs.length-1 ; s++) {
      content = content + ' ' + cs[s];
    }
    content = content.replace(/^\s+|\s+$/g,'');
    content = content.replace(/,$/g,'');
    content = content + '... ';
  }
  return content;
}

/*
 * Returns the type of page to direct the feed and
 * assigns a proper value to the container element
  * @return {String} Returns the type of page to funnel content
*/
function getType() {
  var type1 = document.getElementById('showcase');
  var type2 = document.getElementById('kml_archive');
  var type = type1 ? 'showcase' : type2? 'kml_archive' : null;
  container = type1 ? type1 : type2? type2 : null;
  return type;
}

/*
 * Checks the term string to see if its an organizational label
 * @param {String} term to be checked
  * @return {Boolean} Returns true if no organizational labels
  *     are found
*/
function checkTags(term) {
  var check = 0;
  var ignore_cat = new Array(
    'education culture',
    'environment science',
    'global development',
    'current affairs',
    'public health',
    'social services'
  );
  for (l = 0 ; l < ignore_cat.length-1 ; l++) {
    if( term == ignore_cat[l]) {
      check = 1;
      break;
    } else {
      check = 0;
    }
  }
  return (check == 1) ? true : false; 
}

/*
 * Returns data between two tags returned from a JSON Blog object
 * @param {String} snippet is the string of raw content
 * @param {String} tag is the name of tag surrounding the desired content
 * @return {String} String of text within the tags
*/
function getData(snippet, tag) {
  var re = new RegExp('\u003C'+ tag + '\u003E(.*?)\u003C/' + tag + '\u003E');
  var mt = '';
  if(re.exec(snippet)) {
    var k = re.exec(snippet);
    var b=k[1]
    return b;
  } else { return mt; }
}

/*
 * Adds in line break elements where they are noted in html
 * @param {String} snippet is the string of raw content
 * @return {Element} Paragraph with line breaks elements
 *     in place of the string versions
*/
function formatPull(snippet) {
  var mt = '';
  var c = document.createElement('p');
  c.id = 'kml_p';
  if(snippet) {
    var snips = snippet.split('<br />');
    for (var sn = 0 ; sn < snips.length ; sn++) {
      c.appendChild(document.createTextNode(snips[sn]))
      c.appendChild(document.createElement('br'));
    }
  }
  return c;
}

/*
 * Removes line breaks where they are noted in plain text
 * @param {String} snippet is the string of raw content
 * @return {Element} Paragraph without line breaks
*/
function format(snippet) {
  var mt = '';
  var c = document.createElement('p');
  if(snippet) {
    var snips = snippet.split('<br />');
    for (var sn = 0 ; sn < snips.length ; sn++) {
      c.appendChild(document.createTextNode(snips[sn] + ' '));
    }
  }
  return c;
}
