(function() {
  if (FORK && 
      FORK.isHostMethod && 
      FORK.isHostMethod(FORK, "on")) {
      
    FORK.on(window, "load", function() {

      xhrAddTagLinks();
      
    });
    
  } // feature test
})();

function xhrAddTagLinks() {
  if (FORK.isHostMethod(FORK, "update") &&
      FORK.isHostMethod(FORK, "find") && 
      FORK.isHostMethod(FORK, "on") &&
      FORK.isHostMethod(FORK, "purgeElement") &&
      FORK.isHostMethod(FORK, "preventDefault") &&
      FORK.isHostMethod(document, "createElement")) {
    // find all "add tag" links

    // get form html once at page load
    function getFormHtml(baseLink) {
      var href = baseLink.href + "&output=slim";
      var xhr = FORK.xhr("GET", 
                         href,
                         { 
                           onComplete : getFormHtmlResults
                         });
    
    }
    
    function getFormHtmlResults(o) {
      if (o &&
          o.responseText &&
          document.body) {

        FORK.insertBottom(document.body, o.responseText, false);
        var tagAddForm = FORK.find("#tag_add_form")[0];
        if (tagAddForm) {
          FORK.addClass(tagAddForm, "off");
          FORK.on(tagAddForm, "submit", addFormOnSubmit);
          // add a hidden input to the form
          addSlimOutputToForm(tagAddForm);
        }        
      }
    }
    
    // set onclick to load the "add tag" form in page
    function addLinkOnClick(e) {
      var targ = FORK.getTarget(e);
      var box = targ.parentNode.parentNode;
      var tagAddForm = FORK.find("#tag_add_form", box)[0];

      var domMsg = FORK.find(".message_inline")[0];
      if (domMsg) {
        domMsg.parentNode.removeChild(domMsg);
      }

      // user clicked add tag, add the form below the clicked link
      if (!tagAddForm) {
        tagAddForm = FORK.find("#tag_add_form")[0];

        // set the hidden question id of the form based on the link's href
        var href = targ.href,
            matches;
        if (matches = /question_id=(\d*)/.exec(href)) {
          tagAddForm.elements["question_id"].value = matches[1];
        }
        
        box.appendChild(tagAddForm);
        FORK.removeClass(tagAddForm, "off");
      } else {
      // user clicked finished, remove the form
        FORK.addClass(tagAddForm, "off");
        document.body.appendChild(tagAddForm)
      }

      if (tagAddForm && 
          FORK.isHostMethod(tagAddForm, "reset")) {
        tagAddForm.reset();
      }
      
      FORK.preventDefault(e);
    }
    
    function addLinkResults(o, link) {
      if (o && 
          o.responseText && 
          link && 
          link.parentNode &&
          link.parentNode.parentNode) {
        
        var formObj = FORK.find("form", link.parentNode.parentNode)[0];
        if (!formObj) {
          // add the form
          FORK.insertAfter(link.parentNode, o.responseText, false);
        }
      }
    }

    // add param to tell the perl script not to output the page template
    function addSlimOutputToForm(targ) {
      var slimInput = '<input type="hidden" name="output" value="slim">';
      FORK.insertTop(targ, slimInput, false);
    }
    
    // override the "add tag" form to display results in page
    function addFormOnSubmit(e) {
      var targ = FORK.getTarget(e);
      // use XHR to post the form
      var xhr = FORK.xhr("POST", 
                         targ.action, 
                         {
                           form:targ,
                           onComplete : function(o) { addFormResults(o, targ); }
                         });      
      
      FORK.preventDefault(e);
    }

    function addFormResults(o, form) {
      if (o && 
          o.responseText &&
          form &&
          form.parentNode) {
        
        var formHtml,
            listHtml,
            parts = o.responseText.split(Heroes.sep);
        if (parts) {
          formHtml = parts[0] || "";
          listHtml = parts[1] || "";
        }

        // write form results
        var domMsg = FORK.find(".message_inline", form.parentNode)[0];
        if (domMsg) {
          FORK.replace(domMsg, formHtml, false);
        } else {
          FORK.insertAfter(form, formHtml, false);
        }
        
        // update tag list
        var tagList = FORK.find("ul", form.parentNode)[0];
        if (tagList) {
          FORK.replace(tagList, listHtml, false);
        }
      }
    }
    
    var domLink, 
        domLinks = FORK.find(".tags_add"),
        i = domLinks.length;
        
    if (i) {
      getFormHtml(domLinks[0]);
    }
    while(i--) {
      FORK.on(domLinks[i], "click", addLinkOnClick);
    }
 
  } // feature test
}

