function RDPosition(x, y)
{
  this.X = x;
  this.Y = y;
  this.Add = function(val)
  {
    var newPos = new RDPosition(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X += val.X;
      if(!isNaN(val.Y))
        newPos.Y += val.Y
    }
    return newPos;
  }
  this.Subtract = function(val)
  {
    var newPos = new RDPosition(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X -= val.X;
      if(!isNaN(val.Y))
        newPos.Y -= val.Y
    }
    return newPos;
  }
  
  this.Min = function(val)
  {
    var newPos = new RDPosition(this.X, this.Y)
    if(val == null)
      return newPos;
    
    if(!isNaN(val.X) && this.X > val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y > val.Y)
      newPos.Y = val.Y;
    
    return newPos;  
  }
  
  this.Max = function(val)
  {
    var newPos = new RDPosition(this.X, this.Y)
    if(val == null)
      return newPos;
    
    if(!isNaN(val.X) && this.X < val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y < val.Y)
      newPos.Y = val.Y;
    
    return newPos;  
  }  
  
  this.Bound = function(lower, upper)
  {
    var newPos = this.Max(lower);
    return newPos.Min(upper);
  }
  
  this.Check = function()
  {
    var newPos = new RDPosition(this.X, this.Y);
    if(isNaN(newPos.X))
      newPos.X = 0;
    if(isNaN(newPos.Y))
      newPos.Y = 0;
    return newPos;
  }
  
  this.Apply = function(element)
  {
    if(typeof(element) == "string")
      element = document.getElementById(element);
    if(element == null)
      return;
    if(!isNaN(this.X))
      element.style.left = this.X + 'px';
    if(!isNaN(this.Y))
      element.style.top = this.Y + 'px';  
  }
}

function RDHookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener)
  {
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}

function RDUnHookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.removeEventListener)
    element.removeEventListener(eventName, callback, false);
  else if(element.detachEvent)
    element.detachEvent("on" + eventName, callback);
}

function RDCancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}

function RDgetMousePos(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;
  var pos;
  if(isNaN(eventObj.layerX))
    pos = new RDPosition(eventObj.offsetX, eventObj.offsetY);
  else
    pos = new RDPosition(eventObj.layerX, eventObj.layerY);
  return correctOffset(pos, pointerOffset, true);
}

function RDgetEventTarget(e)
{
  e = e ? e : window.event;
  return e.target ? e.target : e.srcElement;
}

function RDabsoluteCursorPostion(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;
  
  if(isNaN(window.scrollX))
    return new RDPosition(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 
      eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
  else
    return new RDPosition(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
}

function RDdragObject(element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback, attachLater)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
      return;

  var cursorStartPos = null;
  var elementStartPos = null;
  var dragging = false;
  var listening = false;
  var disposed = false;

  function RDdragStart(eventObj)
  {
    if(dragging || !listening || disposed) return;
    dragging = true;

    if(startCallback != null)
      startCallback(eventObj, element);

    cursorStartPos = RDabsoluteCursorPostion(eventObj);

    elementStartPos = new RDPosition(parseInt(element.style.left), parseInt(element.style.top));

    elementStartPos = elementStartPos.Check();

    RDHookEvent(document, "mousemove", RDdragGo);
    RDHookEvent(document, "mouseup", RDdragStopHook);

    return RDCancelEvent(eventObj);
  }

  function RDdragGo(eventObj)
  {
    if(!dragging || disposed) return;

    var newPos = RDabsoluteCursorPostion(eventObj);
    newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
    newPos = newPos.Bound(lowerBound, upperBound)
    newPos.Apply(element);
    if(moveCallback != null)
      moveCallback(newPos, element);

    return RDCancelEvent(eventObj);
  }

  function RDdragStopHook(eventObj)
  {
    RDdragStop();
    return RDCancelEvent(eventObj);
  }

  function RDdragStop()
  {
    if(!dragging || disposed) return;
    RDUnHookEvent(document, "mousemove", RDdragGo);
    RDUnHookEvent(document, "mouseup", RDdragStopHook);
    cursorStartPos = null;
    elementStartPos = null;
    if(endCallback != null)
      endCallback(element);
    dragging = false;
  }

  this.Dispose = function()
  {
    if(disposed) return;
    this.StopListening(true);
    element = null;
    attachElement = null
    lowerBound = null;
    upperBound = null;
    startCallback = null;
    moveCallback = null
    endCallback = null;
    disposed = true;
  }
  
  this.GetLowerBound = function()
  { return lowerBound; }
  
  this.GetUpperBound = function()
  { return upperBound; }

  this.StartListening = function()
  {
    if(listening || disposed) return;
    listening = true;
    RDHookEvent(attachElement, "mousedown", RDdragStart);
  }

  this.StopListening = function(stopCurrentDragging)
  {
    if(!listening || disposed) return;
    RDUnHookEvent(attachElement, "mousedown", RDdragStart);
    listening = false;

    if(stopCurrentDragging && dragging)
      RDdragStop();
  }

  this.IsDragging = function(){ return dragging; }
  this.IsListening = function() { return listening; }
  this.IsDisposed = function() { return disposed; }

  if(typeof(attachElement) == "string")
    attachElement = document.getElementById(attachElement);
  if(attachElement == null)
    attachElement = element;

  if(!attachLater)
    this.StartListening();
}

function InitResizeableDiv(wrapperID, divWidth, divHeight, divmaxHeight) {
	
	arrResizeableDivs = document.getElementById(wrapperID).getElementsByTagName("DIV")

	for (var i=0; i<arrResizeableDivs.length; i++)
	{
	if(arrResizeableDivs[i].className == "ResizeableDiv")
		{
			var rt = new ResizeableDiv(arrResizeableDivs[i].id, divWidth, divHeight, divmaxHeight);
			rt.GetContainer().style.left = '0px';
			rt.GetContainer().style.top = '0px';
			rt.SetMaxWidth(divWidth);
			rt.SetMaxHeight(divmaxHeight);
			arrResizeableDivs[i].appendChild(rt.GetContainer());
			rt.StartListening();
		}
	}
}

function ResizeableDiv(originalID, divWidth, divHeight, divmaxHeight)
{
  var MINSIZE = 38;
  var EDGE_THICKNESS = 0;
  var EDGEDIFFSIZE = 2*EDGE_THICKNESS + 3;
  var EDGEDIFFPOS = EDGE_THICKNESS + 1;
  var TEXTDIFF = EDGE_THICKNESS;
  
  var _width = divWidth;
  var _height = divHeight;
  
  var _maxWidth = divWidth;
  var _maxHeight = divmaxHeight;
  
  var _minWidth = MINSIZE;
  var _minHeight = MINSIZE;

  var _container = document.createElement('DIV');
  _container.className = 'rtContainer';
  
  var _originalDiv = document.getElementById(originalID)

  var _textArea = document.createElement('DIV');
  _textArea.className = 'rtTextArea';
  _textArea.innerHTML = _originalDiv.innerHTML;
  _originalDiv.innerHTML = "";
//  _originalDiv.style.display='none';
  
  var _bottomEdge = document.createElement('DIV');
  _bottomEdge.className = 'rtBottomEdge';
 
  var _bottomHandle = document.createElement('DIV');
  _bottomHandle.className = 'rtBottomHandle';
  
  _bottomEdge.appendChild(_bottomHandle);

  _container.appendChild(_bottomEdge);
  _container.appendChild(_textArea);
    
  var _bottomHandleDrag = new RDdragObject(_bottomEdge, null, new RDPosition(0, 0), new RDPosition(3, 0), RDmoveStart, RDbottomHandleMove, RDmoveEnd, true);
  
  RDUpdateBounds();
  UpdateRDPositions();
  
  function RDmoveStart(eventObj, element)
  {
      document.body.style.cursor = 's-resize';
  }
  
  function RDmoveEnd(element)
  {
    UpdateRDPositions();
    document.body.style.cursor = 'auto'; 
  }

  function RDrightHandleMove(newPos, element)
  {   
    _width = newPos.X + EDGE_THICKNESS;
    UpdateRDPositions();
  }
  
  function RDbottomHandleMove(newPos, element)
  {
    _height = newPos.Y + EDGE_THICKNESS;
    UpdateRDPositions();
  }

  function RDcornerHandleMove(newPos, element)
  {
    _width = newPos.X + EDGE_THICKNESS;
    _height = newPos.Y + EDGE_THICKNESS;
    UpdateRDPositions();
  }

  function RDUpdateBounds()
  {
    _bottomHandleDrag.GetLowerBound().Y = _minHeight - EDGE_THICKNESS;
    _bottomHandleDrag.GetUpperBound().Y = _maxHeight - EDGE_THICKNESS;
  }
  
  function UpdateRDPositions()
  {
    if(_width < _minWidth)
      _width = _minWidth;
    if(_width > _maxWidth)
      _width = _maxWidth;
    
    if(_height < _minHeight)
      _height = _minHeight;
    if(_height > _maxHeight)
      _height = _maxHeight;
    
    _container.style.width = _width + 'px';  
    _container.style.height = _height + 'px';
    
    _textArea.style.width = (_width - TEXTDIFF) + 'px';
    _textArea.style.height = (_height - TEXTDIFF) + 'px';
    _bottomEdge.style.top = (_height - EDGEDIFFPOS) + 'px';
    _bottomEdge.style.width = (_width - TEXTDIFF + 1) + 'px';
    _bottomEdge.style.left = '0px';
    _bottomHandle.style.left = ((_width - MINSIZE) / 2) + 'px';
  }
  
  this.StartListening = function()
  {
    _bottomHandleDrag.StartListening();
  }
  
  this.StopListening = function()
  {
    _bottomHandleDrag.StopListening();
  }
  
  this.GetContainer = function()
  { return _container; }
  
  this.GetTextArea = function()
  { return _textArea; }
  
  this.GetText = function()
  { return _textArea.innerHTML; }
  
  this.GetMinWidth = function()
  { return _minWidth; }
  
  this.GetMaxWidth = function()
  { return _maxWidth; }
  
  this.GetCurrentWidth = function()
  { return _width; }
  
  this.GetMinHeight = function()
  { return _minHeight; }
  
  this.GetMaxHeight = function()
  { return _maxHeight; }
  
  this.GetCurrentHeight = function()
  { return _height; }
  
  this.SetMinWidth = function(value)
  {
    value = parseInt(value);
    if(isNaN(value) || value < MINSIZE)
      value = MINSIZE;
    _minWidth = value;
    UpdateRDPositions();
    RDUpdateBounds();
  }
  
  this.SetMaxWidth = function(value)
  {
    value = parseInt(value);
    if(isNaN(value) || value < MINSIZE)
      value = MINSIZE;
    _maxWidth = value;
    UpdateRDPositions();
    RDUpdateBounds();
  }
  
  this.SetCurrentWidth = function(value)
  {
    value = parseInt(value);
    if(isNaN(value))
      value = 0;
    _width = value;
    UpdateRDPositions();
  }
  
  this.SetMinHeight = function(value)
  {
    value = parseInt(value);
    if(isNaN(value) || value < MINSIZE)
      value = MINSIZE;
    
    _minHeight = value;
    
    UpdateRDPositions();
    RDUpdateBounds();
  }
  
  this.SetMaxHeight = function(value)
  {
    value = parseInt(value);
    if(isNaN(value) || value < MINSIZE)
      value = MINSIZE;
		_maxHeight = value;
		UpdateRDPositions();
		RDUpdateBounds();
  }
  this.SetCurrentHeight = function(value)
  {
    value = parseInt(value);
    if(isNaN(value))
      value = 0;
	  _height = value;
	 UpdateRDPositions();
  }
  this.SetText = function(value)
  { _textArea.innerHTML = value; }
}
