// button type constants
TBButton.TYPE_RADIO = 0;
TBButton.TYPE_CHECKBOX = 1;
TBButton.TYPE_COMMAND = 2;

// event listener constants
TBButton.EVENT_MOUSEOVER = 0;
TBButton.EVENT_MOUSEOUT = 1;
TBButton.EVENT_CLICK = 2;

function TBButton(strName, strImage, nType, strToolTip)
{
	this.strName = strName;
	this.strImage = strImage;
	this.nType = nType;
	this.strToolTip = strToolTip;

	this.strVariable = null;
	this.bSelected = false;

	this.onMouseOver = null;
	this.onMouseOut = null;
	this.onClick = null;
	
	this.GetID = TBButton_GetID;
	this.GetType = TBButton_GetType;
	this.SetToolTip = TBButton_SetToolTip;
	this.SetAssociatedVariable = TBButton_SetAssociatedVariable
	this.Show = TBButton_Show;
	this.AddEventListener = TBButton_AddEventListener;
	this.Select = TBButton_Select;
}

function TBButton_GetID()
{
	return this.strName;
}

function TBButton_GetType()
{
	return this.nType;
}

function TBButton_SetToolTip(strToolTip)
{
	this.strToolTip = strToolTip;
}

function TBButton_Show(oDoc)
{
	var imgTag = "<img id=\"" + this.strName + "\" name=\"" + this.strName + "\" alt=\"" + this.strToolTip + "\"src=\"" + this.strImage + "\" style=\"border: 4px; border-color: white; border-style: -MRF-BORDER-STYLE-;\" -MRF-EVENT-LISTENERS->";
	var events = "onmouseover=\"TBButton.MouseOver(this, " + this.nType + ");-MRF-MOUSE-OVER-\" onmouseout=\"TBButton.MouseOut(this, " + this.nType + ");-MRF-MOUSE-OUT-\" onclick=\"TBButton.Click(this, " + this.nType + ", '-MRF-ASSOCIATED-VARIABLE-');-MRF-CLICK-\"";
	
	// append custom event listeners
	if (this.onMouseOver == null)
		events = events.replace("-MRF-MOUSE-OVER-", "");
	else
		events = events.replace("-MRF-MOUSE-OVER-", this.onMouseOver);

	if (this.onMouseOut == null)
		events = events.replace("-MRF-MOUSE-OUT-", "");
	else
		events = events.replace("-MRF-MOUSE-OUT-", this.onMouseOut);

	if (this.onClick == null)
		events = events.replace("-MRF-CLICK-", "");
	else
		events = events.replace("-MRF-CLICK-", this.onClick);

	if (this.strVariable == null)
		events = events.replace("-MRF-ASSOCIATED-VARIABLE-", "null");
	else
		events = events.replace("-MRF-ASSOCIATED-VARIABLE-", this.strVariable);

	imgTag = imgTag.replace("-MRF-EVENT-LISTENERS-", events);

	if (this.bSelected)
		imgTag = imgTag.replace("-MRF-BORDER-STYLE-", "inset");
	else
		imgTag = imgTag.replace("-MRF-BORDER-STYLE-", "outset");

	// set the value of the associated varible
	if (this.nType == TBButton.TYPE_CHECKBOX)
	{
		if (this.strVariable == null)
		{
			alert("There is no variable associated with the button you want to select.");
			return false;
		}
			
		if (this.bSelected)
			eval(this.strVariable + " = true");
		else
			eval(this.strVariable + " = false");
	}
	
	oDoc.write(imgTag);
}

TBButton.MouseOver = function TBButton_MouseOver(oElement, nType)
{
/*
	if (nType != TBButton.TYPE_RADIO)
		return;
	
	oElement.style.borderStyle = 'inset';
*/
}

TBButton.MouseOut = function TBButton_MouseOut(oElement, nType)
{

	switch (nType)
	{
		case TBButton.TYPE_RADIO:
			if (MRFToolBar.SelectedButton == oElement.id)
				oElement.style.borderStyle = 'inset';
			else
				oElement.style.borderStyle = 'outset';
			break;

		case TBButton.TYPE_CHECKBOX:
			break;

		case TBButton.TYPE_COMMAND:
			oElement.style.borderStyle = 'outset';
			break;

		default:
			alert("Unknown button type.");
	}
}
/*
TBButton.ShowToolTip = function TBButton_ShowToolTip(strToolTip) {
	if (strToolTip == null || strToolTip == "")
		return;
	
    var src = event.srcElement;

    document.body.insertAdjacentHTML('beforeEnd', '<div id="zTip" style="visibility: visible; z-index: 10; background-color: #FFFFFF; position: absolute; font-Size: 8pt; border: 1 solid blue; color: blue; background-color: yellow">' + strToolTip + '</div>');

    with(document.all.zTip){
        style.posTop = document.body.scrollTop + event.clientY + 15;
        style.posLeft = document.body.scrollLeft + event.clientX - 15;
        visibility = 'visible';
    }
}

TBButton.HideToolTip = function TBButton_HideToolTip(){
	var src = event.srcElement;

	if (document.all.zTip != null)
	{
		with (document.all.zTip) {
			innerHTML = "";
			outerHTML = "";
		}
	}
}
*/
TBButton.Click = function TBButton_Click(oElement, nType, strVariableName)
{
	switch (nType)
	{
		case TBButton.TYPE_RADIO:
			if (MRFToolBar.SelectedButton != null)
				eval('document.all.' + MRFToolBar.SelectedButton + '.style.borderStyle = "outset"');
			
			oElement.style.borderStyle = 'inset';
			MRFToolBar.SelectedButton = oElement.id;
			break;
		
		case TBButton.TYPE_CHECKBOX:
			if (strVariableName == null)
			{
				alert("There is no variable associated with the button you clicked.");
				return false;
			}
			
			if (oElement.style.borderStyle == 'inset')
			{
				oElement.style.borderStyle = 'outset';
				eval(strVariableName + " = false");
			}
			else
			{
				oElement.style.borderStyle = 'inset';
				eval(strVariableName + " = true");
			}
			break;
		
		case TBButton.TYPE_COMMAND:
			oElement.style.borderStyle = 'inset';
			break;
		
		default:
			alert("Unknown button type.");
	}
}

function TBButton_AddEventListener(nEventType, strAction)
{
	switch (nEventType)
	{
		case TBButton.EVENT_MOUSEOVER:
			this.onMouseOver = strAction;
			break;
		
		case TBButton.EVENT_MOUSEOUT:
			this.onMouseOut = strAction;
			break;

		case TBButton.EVENT_CLICK:
			this.onClick = strAction;
			break;
		
		default:
			alert("Undefined event listener.");
	}
}

function TBButton_SetAssociatedVariable(strVariableName)
{
	this.strVariable = strVariableName;
}

TBButton.Select = function TBButton_Select()
{
	switch (this.GetType())
	{
		case TBButton.TYPE_RADIO:
			MRFToolBar.SelectedButton = this.GetID();
		
			this.bSelected = true;
			break;
		
		case TBButton.TYPE_CHECKBOX:
			if (this.strVariable == null)
			{
				alert("There is no variable associated with the button you want to select.");
				return false;
			}
			
			eval(this.strVariable + " = true");
			this.bSelected = true;
			break;

		default:
			alert("Unknown button type.");
	}
}
