function widget(_layer, _x, _y, _w, _h, _load_position, _save_position) {
  this.loadPosition         = widget_loadPosition;
  this.savePosition         = widget_savePosition;
  this.setPosition          = widget_setPosition;
  this.setSize              = widget_setSize;
  this.setFocus             = widget_setFocus;

  this.load = (_load_position==null) ? true : _load_position; // true  = load from cookie
                                                              // false = load from call
  this.save = (_save_position==null) ? false: _save_position; // true  = cookie expire in 1 year
                                                              // false = cookie expire after session
  this.drag                 = false;

  this.domWidget = document.getElementById(_layer);

  if (typeof this.domWidget != "undefined") {

    for (i=0; i<this.domWidget.childNodes.length; i++){
      if (this.domWidget.childNodes[i].id=="title")    this.domTitle  = this.domWidget.childNodes[i];
      if (this.domWidget.childNodes[i].id=="content")  this.domContent= this.domWidget.childNodes[i];
      if (this.domWidget.childNodes[i].id=="resize")   this.domResize = this.domWidget.childNodes[i];
      if (this.domWidget.childNodes[i].id=="protect")  this.domProtect= this.domWidget.childNodes[i];
    }

    if (this.load) this.loadPosition();
    this.setPosition(_x, _y, this.load);
    this.setSize    (_w, _h, this.load);

    if (typeof document.oWidget != "undefined") {
      if (parseInt(document.oWidget.domWidget.style.zIndex) < parseInt(this.domWidget.style.zIndex)
      ||  parseInt(document.oWidget.domWidget.style.zIndex) == 0) {
        document.oWidget.domContent.className = "widget_content_disabled";
        document.oWidget      = this;
      }
      else
        this.domContent.className = "widget_content_disabled";
    }
    else
      document.oWidget        = this;


    this.domTitle.oWidget     = this;
    addEvent(this.domTitle,  'mousedown',  widget_drag, false);
    addEvent(this.domTitle,  'mouseup',    widget_drop, false);
//    this.domTitle.style.cursor='move';

    this.domContent.oWidget   = this;
    addEvent(this.domContent,'mousedown',  widget_drag, false);
    addEvent(this.domContent,'mouseup',    widget_drop, false);

    if (this.domResize!=null) {
      this.domResize.oWidget    = this;
      addEvent(this.domResize, 'mousedown',  widget_drag, false);
      addEvent(this.domResize, 'mouseup',    widget_drop, false);
//      this.domResize.style.cursor='se-resize';
    }
  }
  return this;
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_loadPosition() {
  var cookies = document.cookie.split(';');

  this.x = getCookie(this.domWidget.id+'_x');
  this.y = getCookie(this.domWidget.id+'_y');
  this.w = getCookie(this.domWidget.id+'_w');
  this.h = getCookie(this.domWidget.id+'_h');
  var zIndex = getCookie(this.domWidget.id+'_zIndex');
  this.domWidget.style.zIndex = (zIndex==null) ? 0 : zIndex;
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_savePosition() {
  if (this.save) {
    var now = new Date();
    expires = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);
  }
  else
    expires = false;

  setCookie (this.domWidget.id+'_x', this.x, expires);
  setCookie (this.domWidget.id+'_y', this.y, expires);
  setCookie (this.domWidget.id+'_w', this.w, expires);
  setCookie (this.domWidget.id+'_h', this.h, expires);
  setCookie (this.domWidget.id+'_zIndex', this.domWidget.style.zIndex, expires);
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_setPosition(_x, _y, _load_position) {
  this.x                    = (!_load_position || this.x==null) ? _x : this.x;
  this.y                    = (!_load_position || this.y==null) ? _y : this.y;

  this.domWidget.style.left    = this.x;
  this.domWidget.style.top     = this.y;
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_setSize(_w, _h, _load_position) {
  this.w                    = (!_load_position || this.w==null) ? _w : this.w;
  this.h                    = (!_load_position || this.h==null) ? _h : this.h;

  var w_min = 150;  if (this.w < w_min) this.w = w_min;
  var h_min =  31;  if (this.h < h_min) this.h = h_min;

  this.domWidget.style.width = this.w;
  this.domWidget.style.height= this.h;

  this.domProtect.style.left = 0;
  this.domProtect.style.top  = 0;
  this.domProtect.style.width= this.w;
  this.domProtect.style.height= this.h;

  this.domTitle.style.left   = 0;
  this.domTitle.style.top    = 0;
  this.domTitle.style.width  = this.w;
  this.domTitle.style.height = 20;

  if (this.domResize!=null) {
    this.domResize.style.left  = 0;
    this.domResize.style.top   = this.h - 11;
    this.domResize.style.width = this.w;
    this.domResize.style.height= 11;
  }

  this.domContent.style.left = 0;
  this.domContent.style.top  = parseInt(this.domTitle.style.height);
  this.domContent.style.width= this.w;
  this.domContent.style.height=this.h - parseInt(this.domTitle.style.height);
  if (this.domResize!=null)
    this.domContent.style.Height -= parseInt(this.domResize.style.height);
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_setFocus() {
  if (document.oWidget!=this)
    document.oWidget.domContent.className = "widget_content_disabled";

  this.domContent.className = "widget_content_enabled";

  this.domWidget.style.zIndex = parseInt(document.oWidget.domWidget.style.zIndex)+1;

  document.oWidget = this;
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_drag(obj) {
  var oClicked = getObjet(this,obj);
  while (typeof oClicked.oWidget == "undefined") oClicked = oClicked.parentNode;

  oWidget = oClicked.oWidget;

  if (isNS()) { // grab the x-y pos.s if browser is NS
    oWidget.mouseX = obj.clientX;
    oWidget.mouseY = obj.clientY;
    oWidget.mouseW = obj.clientX - oWidget.x;
    oWidget.mouseH = obj.clientY - oWidget.y;
  } else {      // grab the x-y pos.s if browser is IE
    oWidget.mouseX = event.clientX;
    oWidget.mouseY = event.clientY;
    oWidget.mouseW = event.clientX - oWidget.x;
    oWidget.mouseH = event.clientY - oWidget.y;
  }

  oWidget.drag     = true;
  oWidget.setFocus();

  if (oClicked.id=="title") {
    removeEvent(document, 'mousemove',  widget_resize,  true);
    addEvent   (document, 'mousemove',  widget_move,    true);
  }
  else
  if (oClicked.id=="resize") {
    removeEvent(document, 'mousemove',  widget_move,    true);
    addEvent   (document, 'mousemove',  widget_resize,  true);
  }
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_drop(obj) {
  var oClicked = getObjet(this,obj);
  while (typeof oClicked.oWidget == "undefined") oClicked = oClicked.parentNode;

  oWidget = oClicked.oWidget;
  oWidget.savePosition();
  oWidget.drag = false;

  removeEvent(document, 'mousemove',  widget_move,    true);
  removeEvent(document, 'mousemove',  widget_resize,  true);
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_move(obj) {
  var oClicked = getObjet(this,obj);
  while (typeof oClicked.oWidget == "undefined") oClicked = oClicked.parentNode;
  oWidget = oClicked.oWidget;

  if (!oWidget.drag)
    return false;
  else {
    if (isNS()) { // grab the x-y pos.s if browser is NS
      mouseX = obj.clientX;
      mouseY = obj.clientY;
    } else {      // grab the x-y pos.s if browser is IE
      mouseX = event.clientX;
      mouseY = event.clientY;
    }

    oWidget.setPosition(mouseX - oWidget.mouseW, mouseY - oWidget.mouseH, false);
  }
}
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
//
function widget_resize(obj) {
  var oClicked = getObjet(this,obj);
  while (typeof oClicked.oWidget == "undefined") oClicked = oClicked.parentNode;
  oWidget = oClicked.oWidget;

  if (!oWidget.drag)
    return false;
  else {
    if (isNS()) { // grab the x-y pos.s if browser is NS
      mouseX = obj.clientX;
      mouseY = obj.clientY;
    } else {      // grab the x-y pos.s if browser is IE
      mouseX = event.clientX;
      mouseY = event.clientY;
    }

    oWidget.setSize(mouseX - oWidget.x + 7, mouseY - oWidget.y + 4, false);
  }
}

