// Buttons support classes
// (c) SimX Corp. 2008

if( !button_loaded ) {

// --- Bitmap appearance ---
var BitmapButton = function( data_elem, img_obj, std_img_url, fcs_img_url, dwn_img_url, dsb_img_url ) 
{
    this.constructor.call( this, data_elem );
    this.img_obj = img_obj;
    this.std_img = new Image();  this.std_img.src = std_img_url;
    if( dwn_img_url && dwn_img_url.length ) { this.dwn_img = new Image(); this.dwn_img.src = dwn_img_url; } else this.dwn_img = null;
    if( fcs_img_url && fcs_img_url.length ) { this.fcs_img = new Image(); this.fcs_img.src = fcs_img_url; } else this.fcs_img = null;
    if( dsb_img_url && dsb_img_url.length ) { this.dsb_img = new Image(); this.dsb_img.src = dsb_img_url; } else this.dsb_img = null;
}
BitmapButton.prototype = new DataHolder;  // "inheritance"
BitmapButton.prototype.down = function() { if( this.dwn_img ) this.img_obj.src = this.dwn_img.src; }
BitmapButton.prototype.over = function() { if( this.fcs_img ) this.img_obj.src = this.fcs_img.src; }
BitmapButton.prototype.out  = function() { this.img_obj.src = this.std_img.src; }
BitmapButton.prototype.disable = function() {    
    if( this.dsb_img ) this.img_obj.src = this.dsb_img.src;
    this.img_obj.disable=true;
}
BitmapButton.prototype.enable = function() {
    this.img_obj.src = this.std_img.src;
    this.img_obj.disable=false;
}

// --- Text appearance ---
var TextButton = function( data_elem, obj ) {
    this.constructor.call( this, data_elem );
    this.obj = obj;
    this.classname_base = obj.className.substr(0,obj.className.length-2);
}
TextButton.prototype = new DataHolder;  // "inheritance"
TextButton.prototype.down = function() { this.obj.className=this.classname_base + '_d';}
TextButton.prototype.out  = function() { this.obj.className=this.classname_base + '_s'; }
TextButton.prototype.over = function() { this.obj.className=this.classname_base + '_f'; }
TextButton.prototype.disable = function() {    
    this.obj.className=this.classname_base + '_x';
    this.obj.disabled=true;
}
TextButton.prototype.enable = function() {
    this.obj.className=this.classname_base + '_s';
    this.obj.disabled=false;
}

// --- Push behaviour ---

var PushButton = function( button, two_st ) {
    if( !button ) return;
    this.button = button;
    this.two_st = two_st;
    var data = button.GetDataElem();
    if( data ) {
        var $this = this;
        updates[data.id] = function() { 
            $this.button.clear();
            $this.button.isDisabled() ? $this.button.disable() : $this.button.enable(); 
        }
    }
}
PushButton.prototype.click = function() {
    if( this.button.isDisabled() ) return false;
    this.button.set(true); 
    return true;
}
PushButton.prototype.over = function() { if( !this.button.isDisabled() ) this.button.over(); }
PushButton.prototype.out  = function() { if( !this.button.isDisabled() ) this.button.out();  }
PushButton.prototype.down = function() { if( !this.button.isDisabled() && !this.two_st ) this.button.down(); }

// --- Check behaviour ---

var CheckButton = function( button, two_st ) {
    if( !button ) return;
    this.button = button;
    this.two_st = two_st;
    var data = button.GetDataElem();
    if( data ) {
        var $this = this;
        updates[data.id] = function() { 
            if( $this.button.isDisabled() )
                $this.button.disable() 
            else
                $this.button.isOn() ? $this.button.down() : $this.button.enable(); 
        }
    }
}
CheckButton.prototype.click = function() {
    return this.button.isDisabled() ? false : true;
}
CheckButton.prototype.over = function() { if( !this.button.isDisabled() && !this.button.isOn() && !this.two_st ) this.button.over(); }
CheckButton.prototype.out  = function() { if( !this.button.isDisabled() && !this.button.isOn() && !this.two_st ) this.button.out();  }
CheckButton.prototype.down = function() { 
    if( !this.button.isDisabled() ) {
        if(this.button.isOn()) {
            this.button.out();
            this.button.clear(true);
        } else { 
            this.button.down();
            this.button.set(true);
        }
    }
}

var  button_loaded = true;

}

