/*global window, rJS, document*/
/*jslint indent: 2, maxerr: 3 */
(function (window, rJS, document) {
  "use strict";

  rJS(window)

    .declareMethod('render', function (options) {
      var state_dict = {
          href: options.value || options.default || "",
          alt: options.description || options.key || ""
        };
      return this.changeState(state_dict);
    })

    .onStateChange(function () {
      var gadget = this,
          link = document.createElement('a'),
          uri = gadget.state.href.slice(0, 60);
      if (uri.length < gadget.state.href.length)
        uri += "...";
      link.href = gadget.state.href;
      link.title = gadget.state.alt;
      link.textContent = gadget.state.title || uri;
      while (gadget.element.firstChild) {
        gadget.element.removeChild(gadget.element.lastChild);
      }
      if (!gadget.state.href)
        return;
      gadget.element.append(link);
    })

    .declareMethod('getContent', function () {
      // An Link field never modifies a document
      return {};
    })

    .declareMethod('checkValidity', function () {
      return true;
    });

}(window, rJS, document));