/*
 * vegUI is copyright (c) 2005 Stefan Pratter
 *
 * This file is part of vegUI
 *
 * vegUI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * vegUI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with vegUI; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


/******************************************************************************
 * G L O B A L S
 *****************************************************************************/

VEGUIOBJ[VUI_BUTTON] = VegUIButton;

/******************************************************************************
 * V E G U I B U T T O N
 *****************************************************************************/
 
/***************************************************/
/** Constructor */

function VegUIButton(refName, Parent, Manager) {
  this.constructor = VegUINode;
  this.constructor(refName, Parent, Manager);
  
  this.type = VUI_BUTTON;
  
  this.flags |= VUI_HMOUSE_DOWN;
  this.T.micon = 'pointer';
  
  /* method links */
  this.build_button = vuibutton_build;
  this.set_button = vuibutton_set;
  this.hevent_button = vuibutton_hevent_button;
  this.disable_button = vuibutton_disable;  

  this.set = this.set_button;
  this.build = this.build_button;
  this.hevent = this.hevent_button;
  this.disable = this.disable_button;
}
VegUIButton.prototype = VegUINode;

/***************************************************/
/** VegUIButton.set_button(): Sets template
  * properties for button element 
  */

function vuibutton_set(x, y, w, h, img, img_mdown, c) {
  this.set_node(null, w, h, x, y);
  if(img) {
    this.States[VUI_MOUSE_UP].P.className = img;
    this.T.className = img;
  }
  if(img_mdown)
    this.States[VUI_MOUSE_DOWN].P.className = img_mdown;
  this.T.caption = c;
}

/***************************************************/
/** VegUIButton.build_button(): Build button
  * element 
  */

function vuibutton_build(toNode) {
  if(!this.build_node())
    return null;
  this.States[VUI_MOUSE_DOWN].Scripts.add(
    function(argArr) { 
      argArr[0].Manager.Base.ondragstart = function() { return false };
      argArr[0].Manager.Base.onselectstart = function() { return false };
    }
  );

  this.States[VUI_MOUSE_UP].Scripts.add(
    function(argArr) {
      argArr[0].Manager.Base.ondragstart = null;
      argArr[0].Manager.Base.onselectstart = null;
    }
  );
  if(this.T.caption)
    this.Base.appendChild(document.createTextNode(this.T.caption));

  this.dock(toNode);
  return 1;
}

/***************************************************/
/** VegUIButton.hevent_button(): handles mouse
  * events on button element */
  
function vuibutton_hevent_button(eventType, mEvent) {
  var _state = this.hevent_node(eventType, mEvent);
  
  if(eventType == VUI_MOUSE_DOWN)
    this.Manager.focusedElement = this;
  else if(eventType == VUI_MOUSE_UP && this.Manager.focusedElement == this) {
    this.Manager.focusedElement = null;
  }
    
  return _state;
}

/****************************************************/
/** VegUIButton.disable(): disables element */

function vuibutton_disable(b) {
  this.disable_node(b);
  this.Css.cursor = b ? 'default' : 'pointer';
}
