ButtonControl.prototype.toString = function() { return "[Button Control]"; }
ButtonControl.Inherits(WebControl);

function ButtonControl(sNameOrConstructorArguments, iWidth, iHeight, iLeft, iTop, sPosition, oParent) {
    /// <summary>Creates a new button control</summary>
    /// <param name="sNameOrConstructorArguments" type="Object">Arguments object or button name</param>
    /// <param name="iWidth" type="Number"></param>
    /// <param name="iHeight" type="Number"></param>
    /// <param name="iLeft" type="Number"></param>
    /// <param name="iTop" type="Number"></param>
    /// <param name="sPosition" type="String">Relative or Absolute</param>
    /// <param name="oParent" type="Object" domElement="true">HTML element to attach to</param>
    /// <field name="Control" type="Object" domElement="true">HTML UI</field>
    /// <field name="Width" type="Number"></field>
    /// <field name="Height" type="Number"></field>
    /// <field name="Style" type="Number">available from WebControl.Styles</field>
    /// <field name="Caption" type="String"></field>
    /// <field name="Image" type="String"></field>
    /// <field name="ImageOver" type="String"></field>
    /// <field name="Events" type="Events"></field>
    

	this.Inherits(WebControl);

	var sName = null;
	
	var onClickEventHandler = undefined;
	var eInitialStyle = undefined;
	var bRender = false;
	var sCaption = '';
	
	if (typeof(sNameOrConstructorArguments) == "object")
	{
	    var args = sNameOrConstructorArguments;
	    
        iHeight = args.Height;
        iWidth = args.Width;
        onClickEventHandler = args.OnClick;
        iLeft = args.Left;
        iTop = args.Top;
        sPosition = args.Position;
        oParent = args.Parent;
        eInitialStyle = args.Style;
        bRender = args.Render;
        sCaption = args.Caption;
	    sName = args.Name;
	} 
	else
	{
	    sName = sNameOrConstructorArguments;
	}
	
	if (sName == undefined) {
		alert("No name defined for new ButtonControl()");
		return;
	}
	/* Constructor */

	if (iWidth == undefined || iWidth == null || isNaN(iWidth)) iWidth = 100;
	if (iHeight == undefined || iHeight == null || isNaN(iHeight)) iHeight = 20;
	if (iLeft == undefined || iLeft == null || isNaN(iLeft)) iLeft = 0;
	if (iTop == undefined || iTop == null || isNaN(iTop)) iTop = 0;
	if (sPosition == undefined || sPosition == null || (sPosition.toUpperCase() == "ABSOLUTE" && sPosition.toUpperCase() == "RELATIVE")) sPosition = "absolute";
	if (oParent == undefined || oParent == null || typeof(oParent) != 'object') oParent = document.body;
	
	if (eInitialStyle == undefined || eInitialStyle == null) eInitialStyle = WebControl.Styles.XP;
	if (sCaption == undefined || sCaption == null) sCaption = '';
	
	if (sName.substr(0,3) != "ctl") sName = 'ctl' + sName;
	
	var oCtl = document.createElement("div");
	oCtl.setAttribute("id",sName);
	oCtl.setAttribute("name", sName);
	oCtl.setAttribute("tabIndex", 0);
	oCtl.style.position = sPosition;
	oCtl.style.display = "block";
	oCtl.style.top = iTop;
	oCtl.style.left = iLeft;
	oCtl.style.width = '0px';
	oCtl.style.cursor = "hand";

	oCtl.style.position = sPosition;
	oCtl.unselectable = "on";
	oCtl.ObjectType = "BUTTONCONTROL_2";
	oParent.appendChild(oCtl);

	oCtl.onfocus	=	function() { __BT_MouseOver(this);	}
	oCtl.onblur		=	function() { __BT_MouseOut(this);	}
	oCtl.onkeydown	=	function() { if (window.event.keyCode != 13) return; EventCollector(this, "CLICK");	}
	oCtl.onmouseover =	function() { __BT_MouseOver(this);	}
	oCtl.onmouseout =	function() { __BT_MouseOut(this);	}
	oCtl.onmousedown =	function() { __BT_MouseDown(this);	}
	oCtl.onmouseup =	function() { __BT_MouseUp(this);	}
	oCtl.onclick =		function() { EventCollector(this, "CLICK"); }
    oCtl.ondblclick =	function() { EventCollector(this, "DOUBLECLICK"); }

	/* End Constructor */
	
	this.Control = oCtl;
	this.Control.JSControl = this;
	this.Width  = iWidth;
	this.Height = iHeight;
	this.Style  = eInitialStyle;
	
	this.Caption = sCaption;
	
	this.Image      = "";
	this.ImageOver  = "";
	
	
	this.Render = function() 
	{
		__BT_Render(this);
	}
	
	this.__Dispose = function() {
		this.Control.onmouseover = null;
		this.Control.onmouseout = null;
		this.Control.onmousedown = null;
		this.Control.onmouseup = null;
		this.Control.onclick = null;
	}
	
	this.Hide = function() { __BT_Hide(this); }
	this.Show = function() { __BT_Show(this); }
	
	if (bRender == true)
	    this.Render();
	    
	if (onClickEventHandler != undefined)
	    this.Events.Add("CLICK", onClickEventHandler);
}


function __BT_Hide(oJSCtl) {
	oJSCtl.Control.style.visibility = "hidden";
}

function __BT_Show(oJSCtl) {
	oJSCtl.Control.style.visibility = "visible";
}


function __BT_MouseOver(oCtl) {
	__BT_MouseMovement(oCtl, "O");
}

function __BT_MouseOut(oCtl) {
	__BT_MouseMovement(oCtl, "N");
}


function __BT_MouseMovement(oCtl, Move) {
	var s = oCtl.ButtonStyle;	
	var t = oCtl.children[0].children[0];
	
	t.children[0].children[0].className = "B_" + s + "_TL" + Move;
	t.children[0].children[1].className = "B_" + s + "_TC" + Move;
	t.children[0].children[2].className = "B_" + s + "_TR" + Move;
	
	t.children[1].children[0].className = "B_" + s + "_L" + Move;
	t.children[1].children[1].className = "B_" + s + "_C" + Move;
		t.children[1].children[1].children[0].children[0].className = "B_" + s + "_Image" + Move;
		t.children[1].children[1].children[0].children[1].className = "B_" + s + "_ButtonText" + Move;
	t.children[1].children[2].className = "B_" + s + "_R" + Move;
	
	t.children[2].children[0].className = "B_" + s + "_BL" + Move;
	t.children[2].children[1].className = "B_" + s + "_BC" + Move;
	t.children[2].children[2].className = "B_" + s + "_BR" + Move;

}

function __BT_Disabled(oCtl, Val) {
	if (Val == undefined) Val = true;

	for (var i = 0; i < oCtl.children.length; i++)
		oCtl.children[i].disabled = Val;
}


function __BT_MouseDown(oCtl) {
	var s = oCtl.ButtonStyle;	
	var t = oCtl.children[0].children[0];
	
	t.children[1].children[1].className = "B_" + s + "_CD";
	t.children[1].children[1].children[0].children[1].className = "B_" + s + "_ButtonTextD";
}

function __BT_MouseUp(oCtl) {
	__BT_MouseMovement(oCtl, "O");
	
	var oJSCtl = GetParentJSControl(oCtl);
	oJSCtl.Events.RaiseEvent(ButtonPressed() + "CLICK");
}	



function __BT_Render(oJSCtl) {
    var sButtonStyle = oJSCtl.Style;
    if (typeof(sButtonStyle) == 'string') sButtonStyle = sButtonStyle.toUpperCase();
    
    switch (sButtonStyle)
    {
        case WebControl.Styles.Windows:
        case WebControl.Styles.Classic:
        case WebControl.Styles.XP:
        case "XP":
            sButtonStyle = "XP";    
            break;
        
        case "VISTA":
        case WebControl.Styles.Glass:
            sButtonStyle = "4";
            break;
    }

	oJSCtl.Control.ButtonStyle = sButtonStyle;

	var sOut = ''
	+	'<table cellpadding=0 cellspacing=0>'
	+		'<tr>'
	+			'<td nowrap></td>'
	+			'<td nowrap></td>'
	+			'<td nowrap></td>'
	+		'</tr>'
	+		'<tr>'
	+			'<td nowrap></td>'
	+			'<td style="height: ' + oJSCtl.Height + ';  vertical-align: middle;"><div style="width: ' + oJSCtl.Width + '" unselectable="on" style="overflow: hidden;"><span unselectable="on" width=*></span><span unselectable="on" style="white-space: nowrap; width: *; overflow: hidden;">' + ((oJSCtl.Image.length != 0) ? '<img style="vertical-align: middle; margin-right: 2px;" src="' + oJSCtl.Image + '" />' : '') + oJSCtl.Caption + '</span></div>'
	+			'<td nowrap></td>'
	+		'</tr>'
	+		'<tr>'
	+			'<td nowrap></td>'
	+			'<td nowrap></td>'
	+			'<td nowrap></td>'
	+		'</tr>'
	+	'</table>'
	+	'';
	
	oJSCtl.Control.innerHTML = sOut;
	
	__BT_MouseOut(oJSCtl.Control);
}