
/****************************************************************************************/
/* BUTTON OBJECT ************************************************************************/
/*``````````````````````````````````````````````````````````````````````````````````````*/

function Button() {
	this.init();
}

Button.prototype.init = function() {
	this.title = "";
	this.href = "#";
	this.execute = "";
	this.form = "";
	this.alt = "";
	this.active = true;
	this.marginTop = 0;
	this.marginRight = 0;
	this.marginBottom = 0;
	this.marginLeft = 0;
	this.align = "left";
	this.tabIndex = -1;
	this.lid = "";
	this.customMetric = "";
}

// Serialize for debugging by calling: alert(myBtn);
Button.prototype.toString = function() {
	buttonString = "Title: \t \t " + this.title + "\t\n";
	buttonString += "Href: \t \t " + this.href + "\t\n";
	buttonString += "Execute: \t \t " + this.execute + "\t\n";
	buttonString += "Form: \t \t " + this.form + "\t\n";
	buttonString += "Alt: \t \t " + this.alt + "\t\n";
	buttonString += "Active: \t \t " + this.active + "\t\n";
	buttonString += "Margin: \t \t " + this.marginTop + ", " + this.marginRight + ", " + this.marginBottom + ", " + this.marginLeft + "\t\n";
	buttonString += "Align: \t \t " + this.align + "\t\n";
	buttonString += "Tab Index: \t " + this.tabIndex + "\t\n";
	return buttonString;
}

Button.prototype.setTitle = function( val ) {
	this.title = val;
	if ( this.alt == "" ) this.alt = val;
}
Button.prototype.setHref = function( val ) {
	this.href = val;
}
Button.prototype.setExecute = function( val ) {
	this.execute = val;
}
Button.prototype.setForm= function( val ) {
	this.form = val;
}
Button.prototype.setAlt = function( val ) {
	this.alt = val;
}
Button.prototype.setMarginTop = function( val ) {
	this.marginTop = val;
}
Button.prototype.setMarginRight = function( val ) {
	this.marginRight = val;
}
Button.prototype.setMarginBottom = function( val ) {
	this.marginBottom = val;
}
Button.prototype.setMarginLeft = function( val ) {
	this.marginLeft = val;
}
Button.prototype.setMargin = function( top, right, bottom, left ) {
	this.setMarginTop( top );
	this.setMarginRight( right );
	this.setMarginBottom( bottom );
	this.setMarginLeft( left );	
}
Button.prototype.setAlign = function( dir ) {
	this.align = dir;
}
Button.prototype.setTabIndex = function( val ) {
	this.tabIndex = val;
}
Button.prototype.disable = function() {
	this.active = false;
}
Button.prototype.setHitboxLID = function( val ) {
	this.lid = val;
}
Button.prototype.setHitboxCustomMetric = function( func ) {
	this.customMetric = func;
}


// Serialize for display onscreen
Button.prototype.generate = function(id) {

	var writeTable = false;
	var out = '';

	if ( this.active ) {
		focusEvents = ' onFocus="rollOver(this,\'' + this.alt + '\',true); return true;" onBlur="rollOver(this,\'\',false); return true;"';
		mouseEvents = ' onMouseOver="rollOver(this,\'' + this.alt + '\',true); return true;"';
		mouseEvents += ' onMouseOut="rollOver(this,\'\',false); return true;"';
		mouseEvents += ' onMouseDown="pushButton(this,\'-on\',true); ';
		if ( this.customMetric != "" ) {
			mouseEvents += this.customMetric + ';"';
		} else {
			mouseEvents += '"';
		}
		mouseEvents += ' onMouseUp="pushButton(this,\'-on\',false); ';
		if ( this.lid != "" ) {
			mouseEvents += '';
			elementName = ' name="&lid=' + this.lid + '"';
		} else {
			elementName = '';
		}
		keyEvents = ' onKeyUp="if ( isActionKey() ) { ';
		if ( this.execute != "" ) {
			mouseEvents += this.execute + '; ';
			keyEvents += this.execute + '; ';
		}
		if ( this.form != "" ) {
			mouseEvents += 'document.' + this.form + '.submit()' + '; ';
			keyEvents += 'document.' + this.form + '.submit()' + '; ';
		} else {		
			if ( this.href != "" && this.href != "#" ) {
				mouseEvents += 'document.location=\'' + this.href + '\'' + '; ';
				keyEvents += 'document.location=\'' + this.href + '\'' + '; ';
			}
		}
		mouseEvents += 'return true;"';
		keyEvents += 'return true; }"';
	} else {
		mouseEvents = ' onMouseOver="changeCursor(this,false);" class="disabled"';
	}
	mouseEvents += ' alt="' + this.alt + '"';
	out += '<div' + elementName + focusEvents + mouseEvents + keyEvents + ' style="float:' + this.align + '; margin:' + this.marginTop + 'px ' + this.marginRight + 'px ' + this.marginBottom + 'px ' + this.marginLeft + 'px;" tabindex="' + this.tabIndex + '">';

	if ( isMacIE5 ) writeTable = true;

	if ( writeTable ) out += '<table cellpadding="0" cellspacing="0" border="0"><tr><td>';

	out += '<div class="btnTop1">' + spacer + '</div><div ';
	out += 'class="btnTop2">' + spacer + '</div><div ';
	out += 'class="btnTop3">' + spacer + '</div><div '; 
	out += 'class="btnText"><nobr>' + this.title + '</nobr></div><div ';
	out += 'class="btnBtm1">' + spacer + '</div><div '; 
	out += 'class="btnBtm2">' + spacer + '</div><div '; 
	out += 'class="btnBtm3">' + spacer + '</div>';

	if ( writeTable ) out += '</td></tr></table>';
	out += '</div>';
	document.write(out);
}

function pushButton( layer, type, state ) {
	var extOver = "-over";
	var extOn = "-on";

	if ( isMacIE5 ) {
		layer = layer.childNodes[0].childNodes[0].childNodes[0].childNodes[0];
	}
	
	for ( var i=0; i<7; i++ ) {
		myClassName = layer.childNodes[i].className;

		if ( myClassName.indexOf(extOver) != -1 ) {
			myClassName = myClassName.substring( 0, myClassName.length-(extOver.length));
		}
		if (state) {
			if ( myClassName.indexOf(extOn) != -1 ) {
				if ( type == extOver ) {
					return true;
				} else {
					myClassName = myClassName.substring( 0, myClassName.length-extOn.length);
				}
			}
			layer.childNodes[i].className = myClassName + type;
		} else {
			if ( myClassName.indexOf(extOn) != -1 ) {
				myClassName = myClassName.substring( 0, myClassName.length-extOn.length);
				myClassName += extOver;
			}
			layer.childNodes[i].className = myClassName;
		}
	}
}

function rollOver( layer, msg, state ) {
	changeCursor(layer,state);
	pushButton( layer, "-over", state );
	setWindowStatus(msg);
}

