function add_load_event(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}
function get_abs_position(el) {
  var sLeft = 0, sTop = 0;
  var isDiv = /^div$/i.test(el.tagName);
  if (isDiv && el.scrollLeft) {
    sLeft = el.scrollLeft;
  }
  if (isDiv && el.scrollTop) {
    sTop = el.scrollTop;
  }
  var r = { x: el.offsetLeft - sLeft, y: el.offsetTop - sTop };
  if (el.offsetParent) {
    var tmp = get_abs_position(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};
function get_width_height(el) {
  return { width: el.offsetWidth, height: el.offsetHeight };
}
function has_class(node, className) {
  if (node.className == className) {
    return true;
  }
  var reg = new RegExp('(^| )'+ className +'($| )')
  if (reg.test(node.className)) {
    return true;
  }
  return false;
}
function remove_node(node) {
  if (typeof node == 'string') {
    node = $(node);
  }
  if (node && node.parentNode) {
    return node.parentNode.removeChild(node);
  }
  else {
    return false;
  }
}
function stop_event_bubble(event) {
  if (event.preventDefault) {
    event.preventDefault();
    event.stopPropagation();
  }
  else {
    event.returnValue = false;
    event.cancelBubble = true;
  }
}
function $(id) {
  return document.getElementById(id);
}
function append_resizable_bar() {

  textareas = document.getElementsByTagName('textarea');

  var textarea;
  for (var i = 0; textarea = textareas[i]; ++i) {
    if (has_class(textarea, 'resizable')){// && !has_class(textarea.nextSibling, 'snapbar')) {
      if (typeof get_width_height(textarea).width != 'undefined' && get_width_height(textarea).width != 0) {
        new textArea(textarea);
      }
    }
  }
}

function textArea(element) {
  var ta = this;
  this.element = element;
  this.parent = this.element.parentNode;
  this.dimensions = get_width_height(element);

  // Prepare wrapper
  this.wrapper = document.createElement('div');
  this.wrapper.className = 'resizable-textarea';
  this.parent.insertBefore(this.wrapper, this.element);

  // Add snapbar and measure it
  this.snapbar = document.createElement('div');
  this.snapbar.className = 'snapbar';
  this.wrapper.appendChild(this.snapbar);
  this.snapbar.dimensions = get_width_height(this.snapbar);
  this.snapbar.onmousedown = function (e) { ta.beginDrag(e); };

  // Set wrapper and textarea dimensions
  this.wrapper.style.height = this.dimensions.height + this.snapbar.dimensions.height + 1 +'px';
  this.element.style.marginBottom = '0px';
  //this.element.style.width = this.dimensions.height;//'100%';
  this.element.style.height = this.dimensions.height +'px';

  // Wrap textarea
  remove_node(this.element);
  this.wrapper.insertBefore(this.element, this.snapbar);

  // Measure difference between desired and actual textarea dimensions to account for padding/borders
  this.widthOffset = get_width_height(this.wrapper).width - this.dimensions.width;

  // Make the snapbar line up in various browsers
  this.snapbar.style.width = this.dimensions.width-2+'px';
  
  if (window.opera) {
    // Opera
    // this.snapbar.style.marginRight = '4px';
  }
  if (document.all && !window.opera) {
    // IE
    this.snapbar.style.width = this.dimensions.width-4+'px';
    this.snapbar.style.paddingLeft = '2px';
  }
  // Firefox
  // this.element.style.MozBoxSizing = 'border-box';
  this.snapbar.style.marginLeft = this.element.style.marginLeft;
  this.snapbar.style.fontSize = '1px';

  this.heightOffset = get_abs_position(this.snapbar).y - get_abs_position(this.element).y - this.dimensions.height;
}

textArea.prototype.beginDrag = function (event) {
  if (document.isDragging) {
    return;
  }
  document.isDragging = true;

  event = event || window.event;
  // Capture mouse
  var cp = this;
  this.oldMoveHandler = document.onmousemove;
  document.onmousemove = function(e) { cp.handleDrag(e); };
  this.oldUpHandler = document.onmouseup;
  document.onmouseup = function(e) { cp.endDrag(e); };

  // Store drag offset from snapbar top
  var pos = get_abs_position(this.snapbar);
  this.dragOffset = event.clientY - pos.y;

  // Make transparent
  this.element.style.opacity = 0.5;

  // Process
  this.handleDrag(event);
}

textArea.prototype.handleDrag = function (event) {
  event = event || window.event;
  // Get coordinates relative to text area
  var pos = get_abs_position(this.element);
  var y = event.clientY - pos.y;

  // Set new height
  var height = Math.max(32, y - this.dragOffset - this.heightOffset);
  this.wrapper.style.height = height + this.snapbar.dimensions.height + 1 + 'px';
  this.element.style.height = height + 'px';

  // Avoid text selection
  stop_event_bubble(event);
}

textArea.prototype.endDrag = function (event) {
  // Uncapture mouse
  document.onmousemove = this.oldMoveHandler;
  document.onmouseup = this.oldUpHandler;

  // Restore opacity
  this.element.style.opacity = 1.0;
  document.isDragging = false;
}

add_load_event(append_resizable_bar);