(function() {

  var Dom = YAHOO.util.Dom,
    Widget = YAHOO.widget;


  YAHOO.widget.ActivityTree = function(id, oConfig) {
    YAHOO.widget.ActivityTree.superclass.constructor.call(this, id, oConfig);
  };

  YAHOO.extend(YAHOO.widget.ActivityTree, YAHOO.widget.TreeView, {
    buildTreeFromMarkup: function (id) {
      var build = function (parent, markup) {
        var el, node, child, text;
        for (el = Dom.getFirstChild(markup); el; el = Dom.getNextSibling(el)) {
          if (el.nodeType == 1) {
            switch (el.tagName.toUpperCase()) {
              case 'LI':
                for (child = el.firstChild; child; child = child.nextSibling) {
                  if (child.nodeType == 3) {
                    text = YAHOO.lang.trim(child.nodeValue);
                    if (text.length) {
                      node = new Widget.TextNode(text, parent, false);
                    }
                  }
                  else {
                    switch (child.tagName.toUpperCase()) {
                      case 'UL':
                      case 'OL':
                        build(node, child);
                        break;
                      case 'A':
                        if (child.name) {
                          node = new Widget.ActivityNode({
                            label:child.innerHTML,
                            href: child.href,
                            target:child.target,
                            title:child.title || child.alt,
                            style: child.className,
                            activityId: child.id,
                            activityType: child.name
                          }, parent, false);
                        }
                        else {
                          node = new Widget.TextNode({
                            label:child.innerHTML,
                            href: child.href,
                            target:child.target,
                            title:child.title ||child.alt,
                            style: child.className
                          },parent,false);
                        }
                        break;
                      default:
                        node = new Widget.HTMLNode(child.parentNode.innerHTML, parent, false, true);
                        break;
                    }
                  }
                }
                break;
              case 'UL':
              case 'OL':
                build(node, el);
                break;
            }
          }
        }

      };
      var markup = Dom.getChildrenBy(Dom.get(id), function (el) {
        var tag = el.tagName.toUpperCase();
        return  tag == 'UL' || tag == 'OL';
      });
      if (markup.length) {
        build(this.root, markup[0]);
      }
      else {
      }
    }
  });

  YAHOO.widget.ActivityNode = function(oData, oParent, expanded) {
    YAHOO.widget.ActivityNode.superclass.constructor.call(this, oData, oParent, expanded);
  };

  YAHOO.extend(YAHOO.widget.ActivityNode, YAHOO.widget.TextNode, {

    activityId: -1,
    activityType: null,

    getContentHtml: function() {
      var sb = [];

      sb[sb.length] = YAHOO.widget.ActivityNode.superclass.getContentHtml.call(this);

      if (this.activityId && this.activityId != -1) {

        sb[sb.length] = '</td>';
        sb[sb.length] = '<td class="ygtvcontent-icons" align=right>';

        // Edit link
        sb[sb.length] = '<a href="';
        sb[sb.length] = '/servlets/quia.activities.common.ActivityEditor?AE_rand=';
        sb[sb.length] = Math.floor(Math.random()*1000000);
        sb[sb.length] = '&AE_action=2&AE_activityId=';
        sb[sb.length] = this.activityId;
        sb[sb.length] = '">';
        sb[sb.length] = '<img src="/img/web/instructor-zone/action-icon/edit.gif" alt="Edit" title="" ';
        sb[sb.length] = 'onmouseover="writetxtnowrap(\'Edit this ';
        sb[sb.length] = this.activityType;
        sb[sb.length] = '\')" onmouseout="writetxt(0)">';
        sb[sb.length] = '</a>';
        sb[sb.length] = '&nbsp;&nbsp;';

        // Hide link
        // TODO: make this an ajax request
        sb[sb.length] = '<a href="';
        sb[sb.length] = '/servlets/quia.web.QuiaWebManager?tagModuleType=15501&rand=';
        sb[sb.length] = Math.floor(Math.random()*1000000);
        sb[sb.length] = '&tagProfileActionFromList=NOT_VISIBLE&tagActivityId='
        sb[sb.length] = this.activityId;
        sb[sb.length] = '">';
        sb[sb.length] = '<img src="/img/web/instructor-zone/action-icon/hide_from_profile.gif" alt="Hide" title="" ';
        sb[sb.length] = 'onmouseover="writetxtnowrap(\'Hide this ';
        sb[sb.length] = this.activityType;
        sb[sb.length] = ' from my profile\')" onmouseout="writetxt(0)">';
        sb[sb.length] = '</a>';

        sb[sb.length] = '&nbsp;&nbsp;';
      }

      return sb.join("");
    },

    toString: function() {
      return "ActivityNode (" + this.index + ") " + this.label;
    }
  });

})();