var MyUploadLibrary = Class.create(UploadLibrary,
{
  // url must be an absolute path, container is either dom element or its id
  initialize: function($super, container, url)
  {
    $super(container, url);
    
    // we iverride constructor to implement pasting text to the last focused
    // input by clicking on the item
    $$('textarea', 'input').each(function(t) {
      t.observe('focus', this.onAnyInputFocus.bindAsEventListener(this, t));
    }, this);
  },

  onAnyInputFocus: function(event, i)
  {
    this.last_focused_input = i;
  },

  onItemMouseover: function($super, event, item)
  {
    // you can create onhover thumbnail preview
    $super(event, item);
  },

  onItemMouseout: function($super, event, item)
  {
    $super(event, item);
  },

  onError: function($super, message)
  {
    // you can override error reporting here
    $super(message);
  },

  onItemClick: function(event, item)
  {
    if (this.last_focused_input)
    {
      if (this.last_focused_input.tagName == 'TEXTAREA')
        this.insertText(this.last_focused_input, '\n\n[* '+item.file.name+' *] *** popisek\n\n');
      else
        this.last_focused_input.setValue(item.file.name);
      this.last_focused_input.focus();
      //this.last_focused_input.scrollTo();
    }
  },

  insertText: function(element, text)
  {
    if (document.selection)
    {
      element.focus();
      sel = document.selection.createRange();
      sel.text = text;
    }
    else if (!Object.isUndefined(element.selectionStart))
    {
      var startPos = element.selectionStart;
      var endPos = element.selectionEnd;
      element.value = element.value.substring(0, startPos) + text + element.value.substring(endPos);
      element.selectionStart = element.selectionEnd = startPos + text.length;
    } 
    else
      element.value += text;
  }
});
