/*
  JScript library    - obj_tButton.js
  Author             - Brendon Matthews

History
  Oct 2000		BM				Started.
*/

// ====================== tButton Class ====================== //


function tButton(id, imgdefault, imgover, imgdown, imgdisabled) {

	// Old Access Methods
	this.set_enabled          = tButton_setEnabled;
	this.get_enabled          = tButton_getEnabled;
	
	// Access Methods
	this.setEnabled           = tButton_setEnabled;
	this.getEnabled           = tButton_getEnabled;
	
	// Initialization Method
	this.init                 = tButton_init;

	// Fields
	this.element_id           = id;
	this.element              = null;
	this.img_default          = imgdefault;
	this.img_down             = imgdown;
	this.img_over             = imgover;
	this.img_disabled         = imgdisabled;
	
}

function tButton_init(disabled) {
	if (document.all) this.element = eval('document.all.'+this.element_id);
	else this.element = document.getElementById(this.element_id);
	if (!this.element) {
		alert('Control not found '+this.element_id);
		return;
	}
	tButton_setupButton2(this.element, disabled, this.img_default, this.img_over, this.img_down, this.img_disabled);
}

function tButton_getEnabled() {
	if (!this.element) {
		throw "Control not initialized: "+this.element_id;
	}
	return this.element.getEnabled();
}

function tButton_setEnabled(enabled) {
	if (!this.element) {
		throw "Control not initialized: "+this.element_id;
	}
	this.element.setEnabled(enabled);
}

// ---------------------- Static Methods ---------------------- //

function tButton_setupButton(element_id, disabled, imgdefault, imgover, imgdown, imgdisabled) {
	var el = null;
	if (document.all) el = eval('document.all.'+element_id);
	else el = document.getElementById(element_id);
	if (el) tButton_setupButton2(el,disabled,imgdefault,imgover,imgdown,imgdisabled);
}

function tButton_setupButton2(el, disabled, imgdefault, imgover, imgdown, imgdisabled) {
	if (!imgdefault || !imgover) return;
	el.m_enabled = (disabled != true);
	el.m_imageDefault = imgdefault;
	el.m_imageOver = imgover;
	el.m_imageDown = imgdown;
	el.m_imageDisabled = imgdisabled;
	if (el.onmouseover) el.m_onmouseover = el.onmouseover;
	if (el.onmouseout) el.m_onmouseout = el.onmouseout;
	if (el.onclick) el.m_onclick = el.onclick;
	el.onmouseover = tButton_mouseover;
	el.onmouseout = tButton_mouseout;
	el.onmousedown = tButton_mousedown;
	el.onmouseup = tButton_mouseup;
	el.onclick = tButton_click;
	el.getEnabled = tButton_getEnabled2;
	el.setEnabled = tButton_setEnabled2;
	BSP_preloadImages(imgdefault, imgover, imgdown, imgdisabled);
}

function tButton_mousedown() {
	if (!this.m_enabled) return;
	if (this.m_imageDown) this.src = this.m_imageDown;
}

function tButton_mouseup() {
	if (!this.m_enabled) return;
	this.src = this.m_imageOver;
}

function tButton_mouseover() {
	if (this.m_enabled) this.src = this.m_imageOver;
	if (this.m_onmouseover) return this.m_onmouseover();
	else return true;
}

function tButton_mouseout() {
	if (this.m_enabled) this.src = this.m_imageDefault;
	if (this.m_onmouseout) return this.m_onmouseout();
	else return true;
}

function tButton_getEnabled2() {
	return this.m_enabled;
}

function tButton_setEnabled2(enabled) {
	this.m_enabled = (enabled == true);
	if (this.m_imageDisabled) {
		if (this.m_enabled) this.src = this.m_imageDefault;
		else this.src = this.m_imageDisabled;
	}
}

function tButton_click() {
	if (!this.m_enabled) return false;
	else if (this.m_onclick) return this.m_onclick();
	else return true;
}
