var xgAllButtons = new Array();

function xgButton(){
				this.id								= "button_"+GenerateID();
				this.dLeft							=0;
				this.dTop							=0;
				this.dWidth						=100;
				this.dHeight						=30;         
				this.dPosition						="relative";
				this.Type							="xgButton";
				this.disable						=false;				

				this.showFocus						=xgbutton_showFocus;
				this.removeFocus				=xgbutton_removeFocus;
				this.click							=xgbutton_click;
				this.rightclick					=xgbutton_rightclick;
				this.setDisable					=xgbutton_setDisable;
				this.setVisible						=xgbutton_setVisible;
				this.getVisible						=xgbutton_getVisible;


				this.Multiple							=1;
				this.Label								="";
				this.icon										=null;
				this.iconOrientation					=0;
				this.textAlign							="center";
				this.ClassName						="xg_button_label";
				this.ClassNameOnClick			="xg_button_label_onclick";
				this.ClassNameOver				="xg_button_label_onmouseover";
				this.ClassNameOnFocus			="xg_button_label_onfocus";
				this.iPicture							=xg.coreLocation+"imgs/buttons/xg-button.png";
				this.iPictureOnClick					=xg.coreLocation+"imgs/buttons/xg-button-on.png";
				this.iPictureOver						=xg.coreLocation+"imgs/buttons/xg-button-over.png";		 
				this.iPictureOnFocus				=null;
				this.setLabel							=xgbutton_setLabel;
				this.setIcon							=xgbutton_setIcon;
				this.move								=xgbutton_move;
				this.setFocus							=xgbutton_setFocus;
				this._isOn								=false;
				this.isOn								=xgbutton_isOn;
				this.setOn								=xgbutton_setOn;
				this.skinOn							=false;
				this.Title									="";
				this.MultipleLabel					=new Array();
				this.zIndex								=0;
				this._type								="button";

				this.onclick;
				this.onrightclick;
				this.onmouseover;
				this.onmouseout;
				this.onmousemove;
				this.onmousedown;
				this.onmouseup;

				this.Draw							=xgbutton_draw;
				this.ButtonIndex					=xgAllButtons.length;
				this.tabIndex						=-1;
				this.ParentObject				=document.body;

				xgAllButtons[xgAllButtons.length]=this;
				xgDocument.currentObject = this;
				xg.tabs.add(this);
}

function xgbutton_draw(dLeft,dTop,dWidth,dHeight){
	if (dLeft) this.dLeft = dLeft;
	if (dTop) this.dTop = dTop;
	if (dWidth) {
		this.dWidth = dWidth;
	}else{
		this.dWidth = this.ParentObject.offsetWidth;
	}
	if (dHeight) {
		this.dHeight = dHeight;
	}else{
		this.dHeight = this.ParentObject.offsetHeight;
	}

	var  _xgbutton_surface = document.createElement("DIV");
	var _xgbutton_image = document.createElement("IMG");
	var _xgbutton_leftimage = document.createElement("IMG");
	var _xgbutton_rightimage = document.createElement("IMG");
	var _xgbutton_label = document.createElement("DIV");
	var _xgbutton_focus = document.createElement("INPUT");
	var _xgfocus = document.createElement("DIV");
	
	_xgbutton_surface.id = "xg_button_surface_"+this.id;
	_xgbutton_label.id = "_xg_button_label_"+this.id;
	_xgbutton_focus.id = "_xg_focus_"+this.id;
	_xgfocus.id = "_focus_"+this.id;
	_xgbutton_focus.tabIndex = this.tabIndex;

	_xgbutton_image.border = 0;
	

	this._xgbtn_image = _xgbutton_image;
	this._xgbtn_label = _xgbutton_label;

	if (Browser.sName == "ie"){
		this.ParentObject.insertBefore(_xgbutton_surface);
		if (this.iPicture.length>0) _xgbutton_surface.insertBefore(_xgbutton_image);
		_xgbutton_surface.insertBefore(_xgbutton_label);
		_xgbutton_surface.insertBefore(_xgbutton_focus);
		_xgbutton_surface.insertBefore(_xgfocus);
	}else{
		this.ParentObject.appendChild(_xgbutton_surface);
		_xgbutton_surface.appendChild(_xgbutton_image);
		_xgbutton_surface.appendChild(_xgbutton_label);
		_xgbutton_surface.appendChild(_xgbutton_focus);
		_xgbutton_surface.appendChild(_xgfocus);
	}
	
	_xgbutton_surface.style.position = "absolute";
	_xgbutton_surface.style.zIndex = 200-this.ButtonIndex;
	_xgbutton_surface.style.left=this.dLeft;
	_xgbutton_surface.style.top = this.dTop;
	_xgbutton_surface.style.width = this.dWidth;
	_xgbutton_surface.style.height = this.dHeight;
	_xgbutton_surface.style.border="none";
	_xgbutton_surface.title = this.Title;
	if (this.zIndex!=0){
		_xgbutton_surface.style.zIndex=200+this.zIndex;
	}

	this._surface = _xgbutton_surface;
	if (this.iPicture.length>0){
		_xgbutton_image.src=this.iPicture;
	}
	_xgbutton_image.style.position = "absolute";
	_xgbutton_image.style.left = 0;
	_xgbutton_image.style.top = 0
	_xgbutton_image.style.width = this.dWidth;
	_xgbutton_image.style.height = this.dHeight;

	_xgbutton_label.style.position = "absolute";
	_xgbutton_label.style.left = 0;
	_xgbutton_label.style.top = 0;
	_xgbutton_label.style.width  = this.dWidth;
	_xgbutton_label.style.height = this.dHeight;
	
	_xgbutton_focus.type="text";
	_xgbutton_focus.style.position = "absolute";
	_xgbutton_focus.style.left = -5;
	_xgbutton_focus.style.top = -5;
	_xgbutton_focus.readOnly = true;
	_xgbutton_focus.style.border = "none";
	this.ParentObject.style.overflow = "hidden";

	_xgbutton_focus.style.width = 0;
	_xgbutton_focus.style.height = 0;

	_xgfocus.style.position = "absolute";
	_xgfocus.style.left =0;
	_xgfocus.style.top= 0;
	_xgfocus.style.overflow = "hidden";
	_xgfocus.className = this.ClassNameOnFocus;

	var _width = this.dWidth;
	var _height = this.dHeight;

	if (xg.browser.sName != "ie")	{
		_width = this.dWidth - 2;
		_height = this.dHeight -2;
	}
	if (xg.browser.sName == "opera"){
		_width = this.dWidth;
		_height = this.dHeight;
	}
	_xgfocus.style.width = _width;
	_xgfocus.style.height = _height;
	_xgfocus.style.visibility = "hidden";

	this.setLabel(this.Label);

	var _xgbutton = this;

	_xgbutton_surface.onmouseover = function(){
		
		if (_xgbutton._isOn == true) return;

		if (_xgbutton.disable == false){
			if (_xgbutton.iPictureOver.length>0){
			_xgbutton_image.src = _xgbutton.iPictureOver;
			}
		var _xg_label = document.getElementById("_xg_span_label_"+_xgbutton.id);
		_xg_label.className = _xgbutton.ClassNameOver;
		try{
			eval(_xgbutton.onmouseover());
		}catch(e){}
		}

	}

	_xgbutton_surface.onmouseout = function(){
		if (_xgbutton._isOn == true) return;
		if (_xgbutton.disable == false){
			if (_xgbutton.iPicture.length>0)	{
			_xgbutton_image.src = _xgbutton.iPicture;
			}
		var _xg_label = document.getElementById("_xg_span_label_"+_xgbutton.id);
		_xg_label.className =_xgbutton.ClassName;
		try{
			eval(_xgbutton.onmouseout());
		}catch(e){}
		}
	}

	_xgbutton_surface.onmousedown = function(){
		if (_xgbutton.disable == false)
		{
			if (_xgbutton.iPictureOnClick.length>0){
					_xgbutton_image.src = _xgbutton.iPictureOnClick;
			}
				try{
			eval(_xgbutton.onmousedown());
		}catch(e){}
		}
	}

	_xgbutton_surface.onmouseup = function(evt){
		if (xg.browser.sName != 'ie'){ event = evt; }
		if (_xgbutton.disable == false){
		_xgbutton.setFocus();
		if ((event.button == 1) || (event.button == 0)){
				_xgbutton.click();
		}else{
				_xgbutton.rightclick();
		}

		if (_xgbutton.iPicture.lenght>0){
				_xgbutton_image.src = _xgbutton.iPicture;
		}
		try{
			eval(_xgbutton.onmouseup());
		}catch(e){}
		}
	}
	_xgbutton_focus.onfocus = function(){
		_xgbutton.setFocus();
		try{
			eval(_xgbutton.onfocus());
		}catch(e){}
	}

	_xgbutton_focus.onblur = function(){
		_xgbutton.removeFocus();
		try{
			eval(_xgbutton.onblur());
		}catch(e){}
	}

	_xgbutton_focus.onkeypress = function(){
		if (_xgbutton.disable == false){
		if ((xg.event.keyCode == 13) || (xg.event.keyCode == 32)){
		_xgbutton_surface.onmousedown();
		_xgbutton_surface.onmouseup();
		}
		}
	}

	_xgbutton_surface.onselectstart = function(){return false};

	_xgbutton_surface.onkeypress = function(){
		if (_xgbutton.disable == false){
				if ((xg.event.keyCode == 13) || (xg.event.keyCode == 32)){
				_xgbutton_surface.onmouseover();
				_xgbutton.click();
				_xgbutton_surface.onmouseout();
				}
		}
	}

}//draw function

function xgbutton_move(newleft,newtop){
	this._surface.style.left = newleft;
	this._surface.style.top = newtop;
	alert(newleft+":"+newtop);
}
function xgbutton_setFocus(){
	if (this.disable == false){
		try{
			var _focus = document.getElementById("_xg_focus_"+this.id);
			_focus.focus();
			}
			catch(e){}
		var _xg_label = document.getElementById("_focus_"+this.id);
		_xg_label.style.visibility="visible";
		var _xg_lbl = document.getElementById("_xg_span_label_"+this.id);
		_xg_lbl.className =this.ClassNameOnFocus;
		_xg_lbl.style.border="none";
	}
}

function xgbutton_setOn(bool){
	if (bool == true){
		this._tempPicture = this.iPicture;
		this._tempClassName = this.ClassName;
		this.iPicture = this.iPictureOnClick;
		this.ClassName =this.ClassNameOnClick;
		if (this.iPicture.length>0) this._xgbtn_image.src = this.iPicture;
		this.setLabel(this.Label);
		this._isOn = true;
	}else{
		this.iPicture = this._tempPicture;
		this.ClassName = this._tempClassName;
		if (this.iPicture.length >0 ) this._xgbtn_image.src = this.iPicture;
		this.setLabel(this.Label);
		this._isOn = false;
	}
}

function xgbutton_isOn(){
	return this._isOn;
}

function xgbutton_removeFocus(){
	if (this.disable == false)
	{
		var _xg_label = document.getElementById("_focus_"+this.id);
		_xg_label.style.visibility="hidden";

		var _xg_lbl = document.getElementById("_xg_span_label_"+this.id);
		_xg_lbl.className = this.ClassName;
	}
}

function xgbutton_click(){
	try{
		eval(this.onclick());
		return false;
	}catch(e){
		var desc = e;
		if (xg.browser.sName == "ie") {
			desc = (e.number & 0xFFFF)+"  / "+e.description + "\n\n";
		}
		alert("ERROR!\n\n"+desc+"\n\n"+e.lineNumber+":"+e.fileName);
	}
}

function xgbutton_rightclick(){
	try{
		eval(this.onrightclick());
	}catch(e){
	}
}

function xgbutton_setIcon(newIcon){
	this.icon = newIcon;
	this.setLabel(this.Label);
}

function xgbutton_setLabel(newLabel){
	this.Label=newLabel;
	var _xgbutton_label = document.getElementById("_xg_button_label_"+this.id);
	var temphtml = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\""+this.dWidth+"\" height=\""+this.dHeight+"\" onselectstart=\"return false;\">";
		temphtml += "<tr>";
		temphtml += "<td align=\""+this.textAlign+"\" valign=\"middle\" onselectstart=\"return false;\">";
		temphtml+="<table border='0' cellspacing='0' cellpadding='0' >";
		temphtml+="<tr>";
		if (this.icon != null){
			if (this.iconOrientation == "1"){
				temphtml+="<td align='center' valign='middle'>";
				temphtml +="<img src=\""+this.icon+"\" border=\"0\" style=\"floating:left\"/>";
				temphtml+="</td></tr><tr>";
			}else
			if (this.iconOrientation == "0"){
				temphtml+="<td align='center' valign='middle'>";
				temphtml +="<img src=\""+this.icon+"\" border=\"0\" style=\"floating:left\"/>";
				temphtml+="</td>";
		
			}
		}
		temphtml+="<td valign='middle'>";
		temphtml +="<span class=\""+this.ClassName+"\" style=\"cursor:default;text-align:center\" id=\"_xg_span_label_"+this.id+"\" onselectstart=\"return false;\">";
			temphtml +=unescape(this.Label);
		temphtml +="</span>";
		temphtml+="</td></tr></table>";
		temphtml +="</td>";
		temphtml+="</tr>";
		temphtml+="</table>";
	_xgbutton_label.innerHTML=temphtml;
//	_xgbutton_label.title=_xgbutton_label.offsetWidth;
}

function xgbutton_setDisable(disable){
	_surface = document.getElementById("xg_button_surface_"+this.id);		
	_focus = document.getElementById("_xg_focus_"+this.id);

	if (disable == true){
			this.disable = disable;
			_focus.style.visibility = "hidden";
			this._xgbtn_image.src = this.iPictureDisable;
			this._xgbtn_label.className = "xg_button_label_disable";
			this.className = "xg_button_label_disable";		
}else{
			this.disable = false;
			this._xgbtn_image.src = this.iPicture;
			_focus.style.visibility = "visible";
	}
}

function xgbutton_getVisible(){
	var _surface = document.getElementById("xg_button_surface_"+this.id);		
	if (_surface.style.visibility == "visible"){
		return true;
	}else{
		return false;
	}
}

function xgbutton_setVisible(vis){
		_surface = document.getElementById("xg_button_surface_"+this.id);		
	if (vis == true){
		_surface.style.visibility = "visible";
	}else{
		_surface.style.visibility = "hidden";
	}
}

function xgbutton_showFocus(boolfocus){
	var _focus = document.getElementById("_xg_focus_"+this.id);
	if (boolfocus == true){
		_focus.style.visibility="visible";
	}else{
			_focus.style.visibility="hidden";
	}
}
