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

/**
 * @fileoverview Pulls Blog feed and distributes info based 
 * on container name. In this case, content is formatted to display 
 * a condensed version of the blog content related to the kml blog entry.
 * Three variables are passed from the html page to this script:
 * c: the Blog label (category) from which to base content manipulation
 * start: the JSON element to begin with
 * end: the JSON emelent to end with
 *
 * @author thorl@google.com (Thor Lewis)
 *
 * @param {Object}  json is the JSON object pulled from the Blogger service.
 */
 
// Global variables
var container = null;
var counter = 1;
var absCounter = 1;
var newLoc = '';
var once  = 0;
var listingLnk = new RegExp('kml_listing.html');
var type = ''
var numLabels = 3;
var contentLength = 70;
var titleLength = 30;

/*
 * Determines the type of page and distributes
 * Blog data accordingly
 * @param {String} data feed data from blog
 * @param {String} container div to fill data in
 * @param {String} m iteration through blog data
*/
function listEntries(json) {
  c = (c != '') ? c : ''; // category passed from html
  type = getType();
  // go through the feed and print out the content
  if (type == 'showcase') {
    for (var i = 0 ; i < json.feed.entry.length ; i++) {
      var entry = json.feed.entry[i];  
      // first check for the label outreach
      // cycle through the entry list and look for the category
      if( entry.category ){
        for (var j = 0 ; j < entry.category.length ; j++) {
          
          var t = entry.category[j].term;
          if (t == 'outreach') {
            
            for (var k = 0 ; k < entry.category.length ; k++) {
              var v = entry.category[k].term;
              if ( v == c ) {
                if ( (counter >= start) && (counter <= end) ) {
                  printContainer(json, container, i);
                  absCounter++;
                  count++;
                }
                counter++;
              }
            }
          }
        }
      }
    }
  } else if(type == 'kml_archive') {
    for (var i = 0 ; i < json.feed.entry.length ; i++) {
      var entry = json.feed.entry[i];  
      var flag = 1;
      if( entry.category ){
        for (var k = 0 ; k < entry.category.length ; k++) {
          var v = entry.category[k].term;
          if ( v == 'e_showcase' ) {
            flag = 0;
          }
        }
        if(flag == 1) {
          if ( (counter >= start) && (counter <= end) ) {
            printContainer(json, container, i);
            absCounter++;
            count++;
          }
          counter++;
        }
      }
    }
  }
}


/*
 * Prints data container based on iteration through
 * the blog
 * @param {String} data feed data from blog
 * @param {Element} container div to fill data in
 * @param {Number} m iteration through blog data
*/
function printContainer(json, container, m) {
  var entry = json.feed.entry[m];
  var newTitle = '';
  
  if ( (once == 0) && (type == 'showcase') ) {
    if ( document.getElementById('listing_title') ) {
      newTitle = document.getElementById('listing_title');
      var titleSpan = document.createElement('span');
      titleSpan.style.color = '#666666';
      titleSpan.appendChild( document.createTextNode( capMe(c) ) );
      newTitle.appendChild(document.createTextNode( erthOutShow ) );
      newTitle.appendChild( titleSpan );
      once++;
      
      printBlurb();  
    }
                              
  }
  
  if ( ( absCounter % 2 ) && ( absCounter != 1 ) ) {
    var clear = document.createElement('div');
    clear.style.clear = 'both';
    clear.style.width = '97%';
    container.appendChild(clear);
  }
  
  var snippet = '';
  snippet = entry.content.$t;
  var thumb_url = getData(snippet, 'thumb');
  var content = getData(snippet, 'content');
  content = abbrev(content, contentLength);
  var formatted = format(content);
  formatted.id = 'kml_p';
  var title = entry.title.$t;
  var ab_title = abbrev(title, titleLength);
  
  var kml_link = '';
  for (l in entry.link) {
    if (entry.link[l].rel == 'related') {
      kml_link = entry.link[l].href;
    }
  }
  
  var open_title = getData(snippet, 'open_title');
  open_title = abbrev(open_title, titleLength);
  
  var arrMatch = kml_link.match("\.com/\.*\/(.*)\.(kmz|kml)");
  var filename = arrMatch[1];

  
  // create table
  var et = document.createElement('table');
  et.id = 'showcaseEntry';
  et.width = '48%';
  et.cellpadding = '0';
  et.cellspacing = '4';
  et.border = '0';
  
  var et_body = document.createElement('tbody');
  et.appendChild(et_body);
  
  var et_tr1 = document.createElement('tr');
  et_body.appendChild(et_tr1);
  
  var et_td1 = document.createElement('td');
  et_td1.width = '1%'
  et_tr1.appendChild(et_td1);
  
  var et_tr2 = document.createElement('tr');
  et_body.appendChild(et_tr2);
  
  var et_td2 = document.createElement('td');
  et_td2.style.background = '#f5fdf8';
  et_tr2.appendChild(et_td2);

  var lt = document.createElement('div');
  lt.id = 'label_title';
  lt.appendChild( document.createTextNode( labelsStr ) );
  et_td2.appendChild(lt);
    
  // print labels
  var m = 0;
  for(var lbl = 0 ; lbl < entry.category.length ; lbl++) {
    if( ( checkTags( entry.category[lbl].term ) ) && ( m < numLabels ) ) {
      var lblName = entry.category[lbl].term;
      var albl = document.createElement('a');
      var p1 = 'kml_listing.html#c';
      var p2 = '#s1#e10';
      newLoc = p1 + lblName + p2;
      
      albl.href = newLoc;
      albl.onclick = function() {
        location.href = this.href;
        location.reload(true);
      };
      var listingPgChk = listingLnk.exec(window.location);
      var mie = 'Microsoft Internet Explorer';
      if ( ( navigator.appName == mie) && ( listingPgChk == null ) ) {
        albl.onclick = '';
      }
      albl.id = 'label';
      var dlbl = document.createElement('div');
      dlbl.appendChild( document.createTextNode(lblName) );
      albl.appendChild(dlbl);
      et_td2.appendChild(albl);
      m++;
    }
  }
  
  //create and attach thumb
  var th_link = document.createElement('a');
  th_link.href = 'kml_entry.html#t' + title;
  th_link.id = 'th_link';
  var thumb = document.createElement('img');
  thumb.src = thumb_url;
  thumb.border = '0';
  thumb.width = '125';
  thumb.height = '95';
  thumb.align = 'texttop';
  thumb.id = 'gallery_thumb';
  th_link.appendChild(thumb);
  //attach it to the read more text as well
  var lrn_more = document.createTextNode( lrnMoreStr );
  var lrnMoreLnk = document.createElement('a');
  lrnMoreLnk.href = 'kml_entry.html#t' + title;
  lrnMoreLnk.appendChild(lrn_more);
  formatted.appendChild(lrnMoreLnk);
  
  // create and attach title  
  var kmlTitle = document.createElement('span');
  kmlTitle.appendChild(document.createTextNode(ab_title));
  kmlTitle.id = 'kmlTitle';
  
  //create and attach open link
  var kmlImg = document.createElement('img');
  kmlImg.border = '0'
  kmlImg.src = 'http://www.google.com/earth/gallery/images/kml_feed_small.png';
  var kmlImgLnk = document.createElement('a');
  kmlImgLnk.href = kml_link;
  kmlImgLnk.id = 'kml_icon';
  kmlImgLnk.appendChild(kmlImg);
  
  var oLink = document.createElement('a');
  oLink.id = 'open_kml';
  oLink.href = kml_link;
  oLink.onclick = function() { urchinTracker('/kml_downloads/'+filename); };
  oLink.appendChild(document.createTextNode(open_title));
  
  var dKmlLinks = document.createElement('div');
  dKmlLinks.id = 'kml_links';
  dKmlLinks.appendChild(kmlImgLnk);
  dKmlLinks.appendChild(oLink);
  
  // create and attach content
  var p = document.createElement('p');
  p.id = 'kml_p';
  p.appendChild(th_link);
  p.appendChild(kmlTitle);
  p.appendChild(formatted);
  p.appendChild(document.createElement('br'));
  p.appendChild(dKmlLinks);
  
  et_td1.appendChild(p);
  container.appendChild(et);
}
  
  
  
function printBlurb() {
  var blurb = document.getElementById('kml_intro_p');
  var blurbText = '';
  
  switch(c) { 
  
    case 'current affairs':
      blurbText = document.createTextNode(erthCatCurr);
      blurb.appendChild(blurbText);
      break;
      
    case 'education culture':
      blurbText = document.createTextNode(erthCatEdu);
      blurb.appendChild(blurbText);
      break;
      
    case 'environment science':
      blurbText = document.createTextNode(erthCatEnv);
      blurb.appendChild(blurbText);
      break;
      
    case 'global development':
      blurbText = document.createTextNode(erthCatGlob);
      blurb.appendChild(blurbText);
      break;
    
    case 'public health':
      blurbText = document.createTextNode(erthCatHlth);
      blurb.appendChild(blurbText);
      break;
    
    case 'social services':
      blurbText = document.createTextNode(erthCatSoc);
      blurb.appendChild(blurbText);
      break;
  }
  
}
  
