var YzButton=function(dom)
{
	var doc=Yz.util.getElementDocument(dom);
	Yz.util.importStyleSheet(doc,"__YzButton.css");
	//if(Yz.button.cssStyleAdded==false)
	//{
	//	Yz.button.cssStyleAdded=true;
	//}
	if(Yz.isGecko)
	{
		var node=dom.previousSibling;
		while(node && node.nodeType==3)
		{
			var tmpNode=node.previousSibling;
			if(node.textContent.replace(/\s/gi,"")=="")
				node.parentNode.removeChild(node);
			node=tmpNode;
		}
	}
	
	this.label=dom.innerHTML.replace(/\s/g,"&nbsp;");
	var img=dom.getAttribute("img");
	if(img)
	{
		img=Yz.config.skin+"Images/"+img;
		this.label="<img src='"+img+"' align='absmiddle' border='0' /> "+this.label;
	}
	this.width=parseInt(dom.getAttribute("width")) || 0;
	this.id=dom.id || this.handle;
	dom.style.display="none";
	
	var span=doc.createElement("span");
		span.innerHTML="<span class='first-child'><a"+(this.width>0 ? " style='width:"+this.width+"px;'" : "")+">"+this.label+"</a></span>";
		span.className="YzButton";
		
		span.onmouseover=YzButton_onmouseover;
		span.onmouseout=YzButton_onmouseout;
		span.onmousedown=YzButton_onmousedown;
		span.onmouseup=YzButton_onmouseup;
		span.onclick=dom.onclick;
		
		span.firstChild.onfocus=YzButton_onfocus;
		span.firstChild.onblur=YzButton_onblur;
						
		dom.parentNode.insertBefore(span,dom);
		dom.onclick=null; 
		Yz.util.removeNode(dom);//.outerHTML="";
		
		span.id=this.id;
		this.dom=span;
		
		Yz.util.disableSelection(span);
				
		Yz.button.buttons.push(this);
}
Yz.button=YzButton;
Yz.button.buttons=[];
Yz.button.cssStyleAdded=false;
Yz.button.render=function(dom)
{
	var arr=dom ? [dom] : document.getElementsByTagName("BUTTON");
	
	if((arr instanceof Array)==false)
	{
		var a=arr; arr=[];
		for(var i=0;i<a.length;i++)
			arr.push(a[i]);
	}
	
	while(dom=arr.pop())
	{
		var btn=new YzButton(dom);
			btn=null;
	}
}
Yz.button.dispose=function()
{
	var btn=null;
	while(btn=Yz.button.buttons.pop())
	{
		btn.dispose();
		btn=null;
	}
}
Yz.cleanup.add(Yz.button);

Yz.button.setOnclickHandle=function(id,fn)
{
	if(!$(id))return;
	Yz.button.getById(id).setOnclickHandle(fn);
}
Yz.button.setText=function(id,txt)
{
	Yz.button.getById(id).setText(txt);
}
Yz.button.show=function(id,bShow)
{
	Yz.button.getById(id).setVisible(bShow);
}
Yz.button.getById=function(id)
{
	for(var i=0;i<Yz.button.buttons.length;i++)
	{
		if(Yz.button.buttons[i].id==id)return Yz.button.buttons[i];
	}
	return null;
}
Yz.button.setMouseEventProxy=function(id,proxy)
{
	proxy.buttonId=id;
	proxy.onmouseover=YzButton_eventHandler;
	proxy.onmouseout=YzButton_eventHandler;
	proxy.onmousedown=YzButton_eventHandler;
	proxy.onmouseup=YzButton_eventHandler;
}
Yz.button.setAppearance=function(id,eventType)
{
	switch(eventType)
	{
		case 'mouseover': Yz.util.addRemoveClass(Yz.button.getById(id).dom,"hover"); break;
		case 'mouseout': Yz.util.addRemoveClass(Yz.button.getById(id).dom,"-active -hover"); break;
	}
}
YzButton.prototype=
{
	getType : function(){return "YzButton";},
	setText : function(txt)
	{
		this.label=txt;
		this.dom.firstChild.firstChild.innerHTML=txt;
	},
	setOnclickHandle : function(fn)
	{
		this.dom.onclick=fn;
	},
	dispose : function()
	{
		this.dom.onmouseover=null;
		this.dom.onmouseout=null;
		this.dom.onmousedown=null;
		this.dom.onmouseup=null;
		this.dom.onclick=null;
		this.dom.firstChild.onfocus=null;
		this.dom.firstChild.onblur=null;
		
		Yz.util.removeNode(this.dom);
		this.dom=null;
	},
	setVisible : function(bShow){this.dom.style.display=bShow ? "" : "none";}
}
//--------------------------------------------------------------------
function YzButton_onblur()
{
	Yz.util.addRemoveClass(this.parentNode,"-focus -active");
}
function YzButton_onfocus()
{
	Yz.util.addRemoveClass(this.parentNode,"focus");
}
function YzButton_onmousedown(e)
{
	Yz.util.addRemoveClass(this,"active");
}
function YzButton_onmouseup()
{
	Yz.util.addRemoveClass(this,"hover -active");
}
function YzButton_onmouseover()
{
	Yz.util.addRemoveClass(this,"hover");
}
function YzButton_onmouseout()
{
	Yz.util.addRemoveClass(this,"-active -hover");
}
function YzButton_eventHandler(ev)
{
	var e=ev || window.event;
	var type=e.type.toLowerCase();
	
	var dom=Yz.button.getById(this.buttonId).dom;
	
	switch(type)
	{
		case 'mouseover': 	Yz.util.addRemoveClass(dom,"hover"); break;
		case 'mouseout':	Yz.util.addRemoveClass(dom,"-active -hover"); break;
		case 'mousedown': 	Yz.util.addRemoveClass(dom,"active");	break;
		case 'mouseup':		Yz.util.addRemoveClass(dom,"hover -active");	break;
	}
}
//--------------------------------------------------------------------