if (!window.stash_widget) {
  window.stash_widget = (function() {
    var popup;
  
    function get_by_class_name(element_type, class_name, parent_node) {
      parent_node = parent_node || document.body;
      var search_expr = new RegExp('\\b' + class_name + '\\b');
      var elements = parent_node.getElementsByTagName(element_type);
      var result = [];
      for (var i = 0, i_length = elements.length; i < i_length; ++i) {
        if (search_expr.test(elements[i].className)) {
          result.push(elements[i]);
        }
      }
      return result;
    }
  
    function attach_event(obj, event_type, fun) {
      if (obj.addEventListener) {
        obj.addEventListener(event_type, fun, false);
        return true;
      } else if (obj.attachEvent) {
        return obj.attachEvent('on' + event_type, fun);
      }
    }
  
    function init_popup() {
      var templates = get_by_class_name('div', 'stash-popup-template');
      for (var i = 1, i_length = templates.length; i < i_length; ++i) {
        var template = templates[i];
        template.parentNode.removeChild(template);
      }
      popup = templates[0];
      popup.id = 'stash-popup';
      popup.className = 'stash-popup-hidden';
      document.body.appendChild(popup);
      var close_links = get_by_class_name('a', 'stash-popup-close', popup);
      for (var i = 0, i_length = close_links.length; i < i_length; ++i) {
        var close_link = close_links[i];
        attach_event(close_link, 'click', handle_close);
      }
      return popup;
    }
  
    function handle_open(e) {
      popup = popup || init_popup();
      if (stash_widget.position_popup) {
        stash_widget.position_popup();
      }
      popup.className = 'stash-popup-visible';
  
      if (e && e.preventDefault) {
        e.preventDefault();
      }
      return false;
    }
  
    function handle_close(e) {
      popup.className = 'stash-popup-hidden';
  
      if (e && e.preventDefault) {
        e.preventDefault();
      }
      return false;
    }
  
    function init() {
      var widgets = get_by_class_name('div', 'stash-widget');
      for (var i = 0, i_length = widgets.length; i < i_length; ++i) {
        var widget = widgets[i];
        var links = widget.getElementsByTagName('a');
        for (var j = 0, j_length = links.length; j < j_length; ++j) {
          var link = links[j];
          attach_event(link, 'click', handle_open);
        }
      }
    }
  
    attach_event(window, 'load', init);
  
    return {
      initialize : init,
      open_popup : function() {
          handle_open();
        },
      close_popup : function() {
          handle_close();
        }
    }
  })();

  (function() {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    if (msie > 0) {
      if (parseInt(ua.substring (msie+5, ua.indexOf (".", msie ))) >= 7) {
        stash_widget.position_popup = function() {
          document.getElementById('stash-popup').style.position = 'fixed';
        }
      }
    }
  })();
}

