/**
 * @class
 * <p>A simple AJAX updating widget which hijacks a link and changes its behavior to:</p>
 *
 * <ol>
 * <li>Submit via <code>XMLHttpRequest</code></li>
 * <li>Use HTTP <code>POST</code> instead of <code>GET</code></li>
 * <li>Append <code>?xhr</code> to the submission URL</li>
 * </ol>
 *
 * <p>While the submission is "in the air", it disables further clicks on the link, and
 * replaces it with custom text (e.g., "Loading..."). Once the submission returns successfully,
 * it replaces the text of the link with the <code>responseText</code> received from the
 * <code>XMLHttpRequest</code>.</p>
 *
 * @constructor
 * @param {String} id The <code>id</code> of the link to hijack.
 * @param {String} loadingText The HTML and text to display while the submission is pending.
 * Optional; will be '<span class="ajax-link-loading">Loading...</span>' if not supplied.
 * @param {String} finishedText The HTML and text to display once the submission completes.
 * Optional; will be 'Action was successful' if not supplied.
 */
WOL.widget.AjaxLink = function(id, loadingText, finishedText) {
   /**
    * The link which will be hijacked.
    * @type HTMLAnchorElement
    */
   this.link = document.getElementById(id);
   
   if(typeof(loadingText) == 'undefined') {
      this.loadingText = '<span class="ajax-link-loading">Loading...</span>';
   } else {
      this.loadingText = loadingText;
   }
   
   if(typeof(finishedText) == 'undefined') {
      this.finishedText = 'Action was successful';
   } else {
      this.finishedText = finishedText;
   }
   
   /**
    * If a submission is pending, this will be the connection object. Otherwise, will be
    * <code>null</code>.
    * @type asyncRequest
    */
   this.conn = null;
   
   Evt.addListener(this.link, 'click', this.submitLink, this);
};

/**
 * Click handler which actually sends the AJAX submission. If a submission is already pending,
 * does nothing.
 * 
 * @param {Event} e The click event which triggered the submission.
 * @param {AjaxLink} instance The instance of <code>AjaxLink</code> in use.
 */
WOL.widget.AjaxLink.prototype.submitLink = function(e, instance) {
   Evt.preventDefault(e);
   if(instance.conn) {
      return;
   };
   var submissionURL = instance.link.href + '?xhr';
   instance.link.innerHTML = instance.loadingText;
   var callbackObj = { success: instance.handleResponse,
                       failure: instance.responseFailed,
                       scope: instance
                     };
   instance.conn = Connect.asyncRequest('POST', submissionURL, callbackObj);
};

/**
 * <p>Handles a successful response from an AJAX submission. Expects the response to
 * be a simple string which will be inserted as the new value of the <code>href</code>
 * attribute of the link which triggered the AJAX submission.
 *
 * @param {Response} o The response object from the <code>XMLHttpRequest</code>.
 */
WOL.widget.AjaxLink.prototype.handleResponse = function(o) {
   this.link.href = o.responseText;
   this.link.innerHTML = this.finishedText;
   this.conn = null;
};

/**
 * Handles a failed response from an AJAX submission.
 * @param {Response} o The response object from the <code>XMLHttpRequest</code>.
 */
WOL.widget.AjaxLink.prototype.responseFailed = function(o) {
   this.link.innerHTML = 'Request failed';
   this.conn = null;
};