/*
*   Window resizing, fixed for FireFox
*/

Resizer = function(window, button, mainelement, onresize) {
  var self = this;
  this.window = window;
  this.button = button;
  this.mainElement = mainelement;
  this.lastPos = 0;
  this.onresize = onresize;
  this.minWidth = 75;
  this.minHeight = 75;
  
  this.mouseDown = function(event) {
    self._mouseDown(event);
  }

  this.mouseMove = function(event) {
    self._mouseMove(event);
  }

  this.mouseUp = function(event) {
    self._mouseUp(event);
  }

  if (myBrowser.needsAttachEvent) {
    this.button.attachEvent("onmousedown", this.mouseDown);
  }
  else {
    this.button.addEventListener("mousedown", this.mouseDown, true);
  }  
  
}

Resizer.prototype._mouseDown = function (event) {
    if (window.event) {
      event = window.event;
    }
    var x = event.clientX;// + this.window.screenX;
    var y = event.clientY;// + this.window.screenY;

    var doc = this.mainElement.ownerDocument;
    if (myBrowser.needsAttachEvent) {
      doc.attachEvent("onmousemove", this.mouseMove);
      doc.attachEvent("onmouseup", this.mouseUp);
      doc.attachEvent("onmouseout", this.mouseMove);
      doc.ondrag = function() {return false;};
      doc.onselectstart = function() {return false;};
    }
    else {  
      doc.addEventListener("mousemove", this.mouseMove, true);
      doc.addEventListener("mouseup", this.mouseUp, true);
      doc.addEventListener("mouseout", this.mouseMove, true);
    }  
    this.lastPos = {x:x, y:y};
    if (!myBrowser.needsAttachEvent) {
      event.stopPropagation();
      event.preventDefault();
    }  
    else {
      event.cancelBubble = true;
    }
    return false;
  }
  
Resizer.prototype._mouseMove = function (event) {
    if (window.event) {
      event = window.event;
    }
    var doc = this.mainElement.ownerDocument;
    var screenX = event.x;// + this.window.screenX;
    var screenY = event.y;// + this.window.screenY;

    var deltaX = 0;
	  var deltaY = 0;

    var iWidth = 0;
    var iHeight = 0;
    if (window.innerWidth) {
      iWidth = this.window.innerWidth;
      iHeight = this.window.innerHeight;
    }
    else {
      iWidth = this.window.document.documentElement.clientWidth;
      iHeight = this.window.document.documentElement.clientHeight;
    }
    
    if ( (iWidth + (screenX - this.lastPos.x)) >= this.minWidth ) {
        deltaX = screenX - this.lastPos.x;
        this.lastPos.x = screenX;
    }
    if ( (iHeight + (screenY - this.lastPos.y)) >= this.minHeight ) {
		  deltaY = screenY - this.lastPos.y;
		  this.lastPos.y = screenY;
	  }
	  addError(deltaX + " : " + deltaY);

    /*
    var style = doc.defaultView.getComputedStyle(this.mainElement, '');
    this.mainElement.style.width = parseInt(style.width) + deltaX;
    this.mainElement.style.height = parseInt(style.height) + deltaY;
    */
    this.mainElement.style.width = parseInt(this.mainElement.clientWidth) + deltaX + "px";
    this.mainElement.style.height = parseInt(this.mainElement.clientHeight) + deltaY + "px";
    this.window.resizeBy(deltaX, deltaY);
    if (this.onresize) {
      this.onresize();
    }  
    if (!myBrowser.needsAttachEvent) {
      event.stopPropagation();
      event.preventDefault();
    }
    else {
      event.cancelBubble = true;
    }
    return false;  
  }
  
Resizer.prototype._mouseUp = function (event) {
    if (window.event) {
      event = window.event;
    }
    var doc = this.mainElement.ownerDocument;
    /*
    var style = doc.defaultView.getComputedStyle(this.mainElement, '');
    this.window.resizeTo(parseInt(style.width), parseInt(style.height));
    */
    this.window.resizeTo(this.mainElement.clientWidth, this.mainElement.clientHeight);
    if (myBrowser.needsAttachEvent) {
      doc.detachEvent("onmousemove", this.mouseMove);
      doc.detachEvent("onmouseup", this.mouseUp);
      doc.detachEvent("onmouseout", this.mouseMove);
    }
    else {
      doc.removeEventListener("mousemove", this.mouseMove, true);
      doc.removeEventListener("mouseup", this.mouseUp, true);
      doc.removeEventListener("mouseout", this.mouseMove, true);
    }  
    if (!myBrowser.needsAttachEvent) {
      event.stopPropagation();
      event.preventDefault();
    }  
  }


