if (typeof(client) == "undefined")
  var client = new Client();

var btn;

var UPEXT = "_up.gif";
var OVEREXT = "_over.gif";
var DOWNEXT = "_down.gif";
var SELEXT = "_sel.gif";
var DISABLESRC = "_disable.gif"
var IMAGEDIR = "images/";
  
function Button (layerId,id,src,w,h,alt,style,border,hs,vs,align,valign) {

  //properties
  this.self = null;
  this.image = null;
  
  this.enabled = true;
  this.selectable = false;
  this.handCursor = true;
  this.href = "#?";
  this.target = ""
  this.html = "";
  this.xhtml = "";
  this.downEvent = null;
  this.downListener = null;
  this.downInterval;
  this.downWait = 0;
  this.downRepeat = 0;
  this.upEvent = null;
  this.upParams = null;
  this.upListener = null;
  
  this.upImage = null;
  this.downImage = null;
  this.overImage = null;
  this.selImage = null;
  this.disableImage = null;
  
  //methods
  this.construct = Button_construct;
  this.preload = Button_preload;
  this.setImageDir = Button_setImageDir;
  this.setUpExtension = Button_setUpExtension;
  this.setDownExtension = Button_setDownExtension;
  this.setOverExtension = Button_setOverExtension;
  this.setSelectable = Button_setSelectable;
  this.useHandCursor = Button_useHandCursor;
  this.addHyperLink = Button_addHyperLink;
  this.addMouseDownEvent = Button_addMouseDownEvent;
  this.addMouseUpEvent = Button_addMouseUpEvent;
  this.init = Button_init;
  this.disable = Button_disable;
  this.enable = Button_enable;
  this.printHtml = Button_printHtml;
  this.printXHtml = Button_printXHtml;
  this.mouseUp = Button_mouseUp;
  this.startRepeat = Button_startRepeat;
  this.repeatDownEvent = Button_repeatDownEvent;
  this.mouseDown = Button_mouseDown;
  this.rollOver = Button_rollOver;
  this.rollOut = Button_rollOut;
  this.disableMouseDown = Button_disableMouseDown;
  this.disableMouseOver = Button_disableMouseOver;
  
  if (arguments.length > 0)
    this.construct(layerId,id,src,w,h,alt,style,border,hs,vs,align,valign);
}

function Button_construct (layerId,id,src,w,h,alt,style,border,hs,vs,align,valign) {

  this.layer = layerId;
  this.name = id;
  this.src = src;
  this.width = w;
  this.height = h;
  this.border = (border == "undefined") ? 0 : border;
  this.hspace = hs;
  this.vspace = vs;
  this.alt = alt;
  this.style = style;
  this.align = align;
  this.valign = valign;
  
  this.preload();
}

function Button_preload () {
  
  this.upImage = new Image(this.width,this.height);
  this.upImage.src = IMAGEDIR + this.src + UPEXT;
  this.downImage = new Image(this.width,this.height);
  this.downImage.src = IMAGEDIR + this.src + DOWNEXT;
  this.overImage = new Image(this.width,this.height);
  this.overImage.src = IMAGEDIR + this.src + OVEREXT;
  this.selImage = new Image(this.width,this.height);
  this.disableImage = new Image(this.width,this.height);
  this.disableImage.src = IMAGEDIR + this.src + DISABLESRC;
}

function Button_setImageDir (dir) {

  IMAGEDIR = dir;
}

function Button_setUpExtension (upExt) {

  UPEXT = upExt;
}

function Button_setDownExtension (downExt) {

  DOWNEXT = downExt;
}

function Button_setOverExtension (overExt) {
  
  OVEREXT = overExt;
}

function Button_setSelectable (bool) {
  
  this.selectable = bool;
  if (this.selectable)
    this.selImage.src = IMAGEDIR + this.src + SELEXT;
}

function Button_useHandCursor (bool) {
  
  this.handCursor = bool;
  if (!this.handCursor)
    if (this.self)
      client.setLayerStyleProperty(this.self,"cursor","default");
    else
      this.style += ";cursor:default";
}

function Button_addHyperLink (href,target) {
  
  this.href = href;
  this.target = target;
}

function Button_addMouseDownEvent (func,listener,wait,repeat) {
  
  this.downEvent = func;
  this.downListener = listener;
  this.downWait = (wait != "undefined") ? wait : 0;
  this.downRepeat = (repeat != "undefined") ? repeat : 0;
}

function Button_addMouseUpEvent (func,listener,params) {

  this.upEvent = func;
  this.upParams = params;
  this.upListener = listener;
}

function Button_disableMouseDown () {

  this.downImage = null;
}

function Button_disableMouseOver () {

  this.overImage = null;
}

function Button_init () {

  this.self.image = client.getImageReference(this.layer,this.name);
  this.self.object = this;
  if (this.downImage)
    this.self.onmousedown = this.mouseDown;
  this.self.onmouseup = this.mouseUp;
  this.self.onmouseover = this.rollOver;;
  this.self.onmouseout = this.rollOut;
}

function Button_disable () {

  this.enabled = false;
  this.useHandCursor(false);
  if (this.disableImage.complete)
    this.self.image.src = this.disableImage.src;
    
}

function Button_enable () {

  this.enabled = true;
  this.useHandCursor(true);
  if (this.self.image.src == this.disableImage.src)
    this.self.image.src = this.upImage.src;
}

function Button_printHtml () {
  
  if (this.html == "") {
    this.html += "<a href=\""+ this.href +"\"";
    if (this.target > "")
      this.html += " target=\""+ this.target +"\"";
    this.html += ">";
    this.html += "<img src=\""+ this.upImage.src +"\"";
    if (this.width > 0)
      this.html += " width=\""+ this.width +"\"";
    if (this.height > 0)
      this.html += " height=\""+ this.height +"\"";
    this.html += " name=\""+ this.name +"\"";
    if (this.alt > "")
      this.html += " alt=\""+ this.alt +"\"";
    if (this.style > "")
      this.html += " style=\""+ this.style +"\"";
    if (this.border >= 0)
      this.html += " border=\""+ this.border +"\"";
    if (this.hspace > 0)
      this.html += " hspace=\""+ this.hspace +"\"";
    if (this.vspace > 0)
      this.html += " vspace=\""+ this.vspace +"\"";
    if (this.align > "")
      this.html += " align=\""+ this.align +"\"";
    this.html += " /></a>";
  }
  document.write(this.html);
  this.self = client.getLinkReference(this.layer);
}

function Button_printXHtml (str) {
  
  if (typeof(str) == "undefined")
    str = "";
  if (this.xhtml == "") {
    this.xhtml += "<a href=\""+ this.href +"\"";
    if (this.target > "")
      this.xhtml += " onclick=\"this.target = '"+ this.target +"'\"";
    this.xhtml += ">";
    this.xhtml += "<img src=\""+ this.upImage.src +"\"";
    if (this.width > 0)
      this.xhtml += " width=\""+ this.width +"\"";
    if (this.height > 0)
      this.xhtml += " height=\""+ this.height +"\"";
    this.xhtml += " id=\""+ this.name +"\"";
    this.xhtml += " alt=\""+ this.alt +"\"";
    if (this.style > "")
      this.xhtml += " style=\""+ this.style +"\"";
    this.xhtml += " /></a>";
  }
  document.write(this.xhtml+str);
  this.self = client.getLinkReference(this.layer);
}

function Button_startRepeat () {

  self.clearInterval(this.downInterval);
  btn = this;
  this.downInterval = self.setInterval("btn.repeatDownEvent()",this.downRepeat);
}

function Button_repeatDownEvent () {

  this.downListener[this.downEvent]();
}

function Button_mouseDown (e) {
  
  if (this.object.enabled && client.leftMouseClick(e)) {
    img = this.image;
    img.src = this.object.downImage.src;
    if (this.object.downEvent != null)
      this.object.downListener[this.object.downEvent]();
    btn = this.object;
    if ((this.object.downWait > 0) && (this.object.downRepeat > 0))
      this.object.downInterval = self.setInterval("btn.startRepeat()",this.object.downWait);
  }
  client.eventPassThrough(e);
  
  return true;
}

function Button_mouseUp (e) {

  //if ((e == -1) || (this.object.enabled && client.leftMouseClick(e))) {
  if (this.object.enabled) {
    self.clearInterval(this.object.downInterval);
    img = this.image;
    if (this.object.selectable)
      img.src = this.object.selImage.src;
    else if (this.object.overImage != null)
      img.src = this.object.overImage.src;
    else
      img.src = this.object.upImage.src;
    if (this.object.alt)
      window.status = this.object.alt;
    if (this.object.upEvent != null)
      this.object.upListener[this.object.upEvent](this.object.upParams);
  }
  this.blur();
  client.eventPassThrough(e);
    
  return true;
}

function Button_rollOver (e) {

  if (this.object.enabled) {
    img = this.image;
    if (this.object.overImage)
      img.src = this.object.overImage.src;
    if (this.object.alt)
      window.status = this.object.alt;
    else
      window.status = window.defaultStatus;
  }
    
  return true;
}

function Button_rollOut (e) {

  if (this.object.enabled) {
    self.clearInterval(this.object.downInterval);
    img = this.image;
    if ((img.src == this.object.selImage.src) && (this.object.selectable))
      img.src = this.object.selImage.src;
    else
      img.src = this.object.upImage.src;
    window.status = window.defaultStatus;
  }
  
  return true;
}