YAHOO.namespace("YAHOO.ui");

YAHOO.ui.TwitterFeed = new function() {

  var TF = this;

  this.getXML = null;
  this.feedElem = null;
  this.posts = null;
  this.currentIndex = -1;
  this.fadeTime = 300;
  this.displayTime = 7000;

  this.fadeOutAnim = null;
  this.fadeInAnim = null;

  this.initialize = function(id, url) {
    TF.feedElem = document.getElementById(id);

    TF.fadeOutAnim = new YAHOO.util.Anim(  
      TF.feedElem,
      { opacity: {to: 0 } },
      TF.fadeTime / 1000,
      YAHOO.util.Easing.easeOut
    );

    TF.fadeInAnim = new YAHOO.util.Anim(  
      TF.feedElem,
      { opacity: {to: 1 } },
      TF.fadeTime / 1000,
      YAHOO.util.Easing.easeOut
    );

    var callback = 
    {
      success: this.render,
      failure: this.fail,
      argument: ""
    };

    this.getXML = YAHOO.util.Connect.asyncRequest('GET', url, callback); 
  };

  this.fail = function(o) {
    TF.feedElem.innerHTML = "When Twitter comes back up, be sure to <a href=\"http://twitter.com/ericdpratt\">follow me</a>! In the meantime, here's how to cope: <a href=\"http://tcrn.ch/bfullm\">http://tcrn.ch/bfullm</a>";
    TF.fadeIn();
  };

  this.getNextIndex = function() {
    if (TF.currentIndex < TF.posts.length - 1) {
      return TF.currentIndex + 1;
    }
    else {
      return 0;
    }
  };

  this.showNextPost = function() {
    TF.currentIndex = TF.getNextIndex();
    TF.fadeOut();
    setTimeout(TF.showNextPost_setText, TF.fadeTime + 10);
    setTimeout(TF.fadeIn, TF.fadeTime + 20);
    setTimeout(TF.showNextPost, TF.displayTime + TF.fadeTime + 20);
  };

  this.showNextPost_setText = function() {
    TF.feedElem.innerHTML = TF.posts[TF.currentIndex];
  };

  this.generateHyperlinks = function(post) {
    var linkStart = -1;
    var linkEnd = -1;

    if ((linkStart = post.indexOf("http://")) != -1) {
      var link = "";

      if ((linkEnd = post.indexOf(" ", linkStart)) != -1) {
        link = "";
      }
      else {
        linkEnd = post.length;
      }

      link = post.substring(linkStart, linkEnd);
      post = post.replace(link, "<a href=\"" + link + "\">" + link + "</a>");
    }

    return post;
  };

  this.fadeIn = function() {
    TF.fadeInAnim.animate();
  };

  this.fadeOut = function() {
    TF.fadeOutAnim.animate();
  };

  this.render = function(o) {
    var items = o.responseXML.getElementsByTagName("item");
    TF.posts = new Array();

    // put each post from the XML in the posts array
    for (var i = 0; i < items.length; i++) {
      var title = items[i].getElementsByTagName("title");      // get title tag
      var fullText = title[0].childNodes[0].nodeValue;         // get text from title tag
      var displayText = fullText.substring(12);                // remove "ericdpratt: " from the post
      displayText = TF.generateHyperlinks(displayText);        // wrap hyperlinks in anchor tags
      TF.posts[i] = displayText;                               // add to the array
    }

    TF.showNextPost();
  };

};

YAHOO.util.Event.addListener(window, "load", function() { YAHOO.ui.TwitterFeed.initialize("twitterFeed", "twitter.php"); });
