// This script is part of the Comments Resizer plugin for WordPress. Stand-alone version is available on http://www.laptoptips.ca/javascripts/block-resizer/

Resizer = {

  minH : 70, // minimal height in pixels
  ttl : 'Drag to resize', // title for the resize handle

  Start : function(e) {
    var y = e ? e.clientY : event.clientY;
    this.s = this.elem.style.height ? parseInt(this.elem.style.height) - y : this.elem.clientHeight - y;
    this.res = true;
    document.onselectstart = function(){return false;};
    document.onmousemove = function(e){Resizer.Move(e)};
    if (e) document.onmouseout = function(e){Resizer._s(e)};
    else document.body.onmouseleave = function(){Resizer.Stop()};
  },
  
  _s : function(e) {
    if ( e.target.nodeName != 'HTML' ) return;
    this.Stop();
  },
  
  Stop : function() {
	this.res = false;
    document.onselectstart = null;
    document.onmousemove = null;
    document.onmouseout = null;
    document.body.onmouseleave = null;
  },

  Move : function(e) {
    if ( this.res ) {
      var mvto = e ? e.clientY : event.clientY;
      var h = this.s + mvto;
      if( h < this.minH ) return false;
      this.elem.style.height = h + 'px';
   }
  },

  Init : function(id) {
    var h, w;
    if ( this.elem && this.elem.id == id ) return;
    if ( ! ( this.elem = document.getElementById(id) ) ) return;
    this.res = false;
    w = this.elem.offsetWidth || this.elem.clientWidth;
    
    if ( h = document.getElementById('cmnt-resize-handle') ) h.parentNode.removeChild(h);
    h = document.createElement('div');
    h.id = 'cmnt-resize-handle';
    h.style.width = w + 'px';
    h.title = this.ttl;
    h.onmousedown = function(e){Resizer.Start(e)};
    this.elem.parentNode.insertBefore(h, this.elem.nextSibling);
    
    document.onmouseup = function(e){Resizer.Stop(e)};
    window.onunload = function(){Resizer.Exit()};
  },
  
  Exit : function() {
    document.onmouseup = null;
    window.onunload = null;
  }
}
if( document.getElementById('comment') ) Resizer.Init('comment');