/*
   -------------------------
   JS Script: parseYPipes.js
   By: Eduardo P. Carneiro
   Date: April 05, 2007
   -------------------------
*/

/* This script will parse a JSON object returned from
   Yahoo! Pipes (http://pipes.yahoo.com/) and spit out the
   content inside an 'li' or 'p' tag.

   The need for this script came from a desire to display
   blog entries (from an xml) on to a Cisco page. AJAX was
   ruled out because http://blogs.cisco.com/ is hosted by
   an external company (livingdot.com) and AJAX cannot
   retrieve data from external sources due to security
   issues (Mozilla based browsers only).

   The user can customize how the content will be displayed
   on a page by passing params to an object. A page call will
   look like this:

   &lt;script type="text/javascript" src="http://newsroom.cisco.com/Newsroom/scripts/parseYPipes.js"&gt;&lt;/script&gt;
   &lt;script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?_id=xObEeazD2xG_zlCA0kqv4w&_render=json&_callback=ws_results"&gt;&lt;/script&gt;
   <div id="DCBlog"></div>
   &lt;script type="text/javascript"&gt;
     var myObj = new xParser();
     myObj.show('DCBlog',4,null,45);
   &lt;/script&gt;

   The params to pass to myObj.show are:
   1- container id...probably a div (mandatory)
   2- number of entries to display (default is 3)
   3- 'p' for content inside <p> tags (default is 'li')
   4- max length for an entry
   5- Visual Sciences tracking
   * Use null if empty param
*/

var pipes;
function xParser () {
  this.boxID = null;
  this.num = 3;
  this.tag = 'li';
  this.length = null;
  this.metric = null;
  this.pipesObj = pipes;
  this.show = parsePipes;
}

function parsePipes (pbox, pnum, ptag, plen, pstat) {
  // Perform some validation and assign params
  // to default vars
  if (pbox == null || pbox == '') {
    return;
  } else {
    this.boxID = pbox;
  }

  if (pnum > 0 && pnum < this.pipesObj.count) {
   this.num = pnum;
  } else if (pnum >= this.pipesObj.count) {
   this.num = this.pipesObj.count;
  }

  if (ptag != null && ptag.toLowerCase() == 'p') {
    this.tag = ptag;
  }

  if (plen > 0) {
    this.length = plen;
  }

  // Get the element of that id
  var Bcontainer = getContainer(this.boxID);
  var Blist;
  var Blink;
  var Btxt;


  // Create an 'li' or 'p' tag, then an 'a' tag
  // insert the values into it and append it to the
  // container object
		
		consumerList = document.createElement('ul');
		Bcontainer.appendChild(consumerList);


  for (var Bcnt = 0; Bcnt < this.num; Bcnt++) {
    Blist = document.createElement(this.tag);
    Blink = document.createElement('a');
    Blink.href = this.pipesObj.value.items[Bcnt].link;
    Btxt = document.createTextNode(trimTitle(this.pipesObj.value.items[Bcnt].title, this.length));
    Blink.appendChild(Btxt);
    Blist.appendChild(Blink);
    consumerList.appendChild(Blist);
  }
		
}

function ws_results(obj) {
  pipes = obj;
}

function getContainer (id) {
  return document.getElementById(id);
}

function trimTitle (title, x) {

  if (x == null || title.length <= x) {
    return title;
  }

  t=title.substring(0,x);

  var reg = /\s.{2,3}$/;
  if (reg.test(t)) {
    t=t.replace(reg, '...');
  } else {
    t=t.replace(/\s.$/, '');
    t=t.replace(/\s\S+$/, '...');
  }
  return t;
}

