/**GuiWidget.js
  The GuiWidget is the base element of the gui library.  Based on a DIV, various extensions are made to handle
  size, position and events throughg a single interface.  Also included is the ability to generate a set of widgets
  from a specification file written in an XML language, eliminating the need for complex JavaScript coding in a given
  application
  
  Dependancies:
    x.js  -  can be obtained from cross-browser.com.  handles differences between browser implementations of html
    XMLParser-base,js - defines XMLParser object and helper functions.
**/


GuiWidget = function(newID, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newClass)
{
  if (arguments.length > 0)
  {
    this.widgetType = 'GuiWidget';
    this.init(newID, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newClass);
  }
};

GuiWidget_onLoad = function()
{
  GuiWidget.ToolTip = document.createElement("DIV");  //ToolTip is a static property of the GuiWidget class to ensure that only one is created.
  document.getElementsByTagName("body").item(0).appendChild(GuiWidget.ToolTip);            //child of the document object.
  xHide(GuiWidget.ToolTip);
};

GuiWidget.prototype.init = function(newID, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newClass)
{
  this.id = newID;
  this.parentElement = newParent;
  this.xPosition = parseInt(newXPosition);
  this.yPosition = parseInt(newYPosition);
  this.zIndex = parseInt(newZIndex);
  this.visibility = false;
  
  this.element = document.createElement("DIV");
  this.element.objectManagerId = OBJECT_MANAGER.registerObject(this.id, this);
  if (this.parentElement == null)
  {
    document.getElementsByTagName("body").item(0).appendChild(this.element); 
  }
  else
  {
  	this.parentElement.appendChild(this.element);
  }
  this.setClass(newClass);
  this.element.id = this.id;

  this.moveTo(newXPosition, newYPosition);
  xZIndex(this.element,newZIndex);
  this.resizeTo(newWidth,newHeight);
  this.setVisibility(newVisibility);
};

/*******************************************/

GuiWidget.prototype.setVisibility = function(newVisibility)
{
  this.visibility = newVisibility;
  return (newVisibility)?this.show():this.hide();
};

/*******************************************/

GuiWidget.prototype.show = function()
{
  this.visibility = true;
  if (this.element)
    this.element.style.visibility= 'inherit';
  return true;
};

/*******************************************/

GuiWidget.prototype.hide = function()
{
  this.visibility = false;
  this.element.style.visibility='hidden';
  xHide(this.element);
  return true;
};

/*******************************************/

GuiWidget.prototype.width = function()
{
  if (arguments.length > 0)
    return xResizeTo(this.element, parseInt(arguments[0]), xHeight(this.element));
  return xWidth(this.element);
};

/*******************************************/

GuiWidget.prototype.height = function()
{
  if (arguments.length > 0)
  {
    return xHeight(this.element,arguments[0]);
  }
  return xHeight(this.element);
};

/*******************************************/

GuiWidget.prototype.left = function()
{
  if (arguments.length > 0)
    return xLeft(this.element,arguments[0]);
  else
    return xLeft(this.element);
};

/*******************************************/

GuiWidget.prototype.top = function()
{
  if (arguments.length > 0)
    return xTop(this.element,arguments[0]);
  else
    return xTop(this.element);
};

/*******************************************/

GuiWidget.prototype.resizeTo = function (newWidth, newHeight)
{
  xResizeTo(this.element,newWidth, newHeight);
  if (this.resizeEvent)
    this.resizeEvent(newWidth, newHeight);
};

GuiWidget.prototype.moveTo = function (newXPosition, newYPosition)
{
  this.xPosition = parseInt(newXPosition);
  this.yPosition = parseInt(newYPosition);
  xMoveTo(this.element, this.xPosition, this.yPosition);
  if (this.moveEvent)
    this.moveEvent(newXPosition, newYPosition);
};

GuiWidget.prototype.setClass = function (newClass)
{
  this.element.setAttribute((xIE4Up?("className"):("class")),(((newClass == '')||(newClass == null))?(this.widgetType):(newClass)));
};

GuiWidget.prototype.innerHtml = function (newString)
{
  this.element.innerHTML = newString;
};

/****************************************************************/
/* Event Callback Functions                                     */
/* Calls the appropriate functions if they exist.               */
/****************************************************************/

GuiWidget.prototype.mouseOverCallback = function(e)
{
  if (this.mouseOver)
    this.mouseOver(e);
  if (this.layoutMouseOverEvent)
    this.layoutMouseOverEvent(e);
  if (this.mouseOverEvent)
    this.mouseOverEvent(e);
};

/*******************************************/

GuiWidget.prototype.clickCallback = function(e)
{
  if (this.click)
    this.click(e);
  if (this.layoutMouseClickEvent)
    this.layoutMouseClickEvent(e);
  if (this.clickEvent)
    this.clickEvent(e);
};

/*******************************************/

GuiWidget.prototype.mouseOutCallback = function(e)
{
  if (this.mouseOut)
    this.mouseOut(e);
  if (this.layoutMouseOutEvent)
    this.layoutMouseOutEvent(e);
  if (this.mouseOutEvent)
    this.mouseOutEvent(e);
};

/*******************************************/

GuiWidget.prototype.mouseDownCallback = function(e)
{
  if (this.mouseDown)
    this.mouseDown(e);
  if (this.layoutMouseDownEvent)
    this.layoutMouseDownEvent(e);
  if (this.mouseDownEvent)
    this.mouseDownEvent(e);
};

/*******************************************/

GuiWidget.prototype.mouseMoveCallback = function(e)
{
  xMoveTo(GuiWidget.ToolTip,e.clientX, e.clientY);
  if (this.mouseMove)
    this.mouseMove(e);
  if (this.layoutMouseMoveEvent)
    this.layoutMouseMoveEvent(e);
  if (this.mouseMoveEvent)
    this.mouseMoveEvent(e);
};

/*******************************************/

GuiWidget.prototype.mouseUpCallback = function(e)
{
  if (this.mouseUp)
    this.mouseUp(e);
  if (this.layoutMouseUpEvent)
    this.layoutMouseUpEvent(e);
  if (this.mouseUpEvent)
    this.mouseUpEvent(e);
};

GuiWidget.showTooltip = function(e, tooltipText)
{
  if (document.babelfish)
    tooltipText = document.babelfish.translateWord(tooltipText);
  xMoveTo(GuiWidget.ToolTip,e.pageX+GuiWidget.tooltipOffset[0], e.pageY+GuiWidget.tooltipOffset[1]);
  xZIndex(GuiWidget.ToolTip,255);
  GuiWidget.ToolTip.innerHTML = tooltipText;
  if (xIE4Up)
    GuiWidget.ToolTip.setAttribute("className", "GuiToolTip");
  else
    GuiWidget.ToolTip.setAttribute("class", "GuiToolTip");
  xShow(GuiWidget.ToolTip);
};

GuiWidget.hideTooltip = function()
{
  xHide(GuiWidget.ToolTip);
};
