
var SmartButton = {
	
	ApplyPermissions: function(btn) {
		if(btn.getAttribute("permissao") != null) {
			if((Number(btn.getAttribute("permissao")) & _usutipo) > 0) {
			} else {
				SmartButton.Disable(btn);
			}
		}
	},
	
	ApplyProperties: function(btn) {
		if(btn.onmousedown == null) btn.onmousedown = function(){SmartButton.Swap(this, "down");};
		if(btn.onmouseout == null) btn.onmouseout = function(){SmartButton.Swap(this, "out");};
		if(btn.onmouseover == null) btn.onmouseover = function(){SmartButton.Swap(this, "over");};
		if(btn.onmouseup == null) btn.onmouseup = function(){SmartButton.Swap(this, "over");};
		btn.backgroundPosition = "top left";
	},
	
	SetProperties: function(target) {
		var btns = $C("smartbutton", target);
		if(btns.length > 0) {
			for(var i=0; i<btns.length; i++) {
				this.ApplyProperties(btns[i]);
				if(btns[i].getAttribute("enabled")!=null && btns[i].getAttribute("enabled").toString()=="false") {
					this.Disable(btns[i]);
				}
				this.ApplyPermissions(btns[i]);
			}
		}
	},
	
	Disable: function(obj) {
		this.Swap(obj, "disabled");
		$(obj).setAttribute("enabled", false);
		if($(obj).onclick!= null) {
			$(obj).setAttribute("fonclick", $(obj).onclick);
			$(obj).AddClass("smartbutton_disabled");
			$(obj).onclick = null;
		}
	},
	
	Enable: function(obj) {
		$(obj).setAttribute("enabled", true);
		var state = ($(obj).getAttribute("currentstate")!=null) ? $(obj).getAttribute("currentstate") : "out";
		this.Swap(obj, state);
		if($(obj).getAttribute("fonclick")!=null) {
			$(obj).onclick = $(obj).getAttribute("fonclick");
			$(obj).RemoveClass("smartbutton_disabled");
			$(obj).setAttribute("fonclick", null);
		}
	},
	
	Swap: function(obj, action) {
		obj = $(obj);
		if(((obj.getAttribute("enabled")==null) || (obj.getAttribute("enabled").toString() != "false")) || action=="disabled") {
			switch(action) {
				case "out":
					if(obj.getAttribute("state")!=null && obj.getAttribute("state")!="out") {
						this.Swap(obj, obj.getAttribute("state"));
					} else {
						$(obj).style.backgroundPosition = "top left";
						$(obj).setAttribute("currentstate", "out");
					}
				break;
				case "over":
					$(obj).style.backgroundPosition = "top right";
					$(obj).setAttribute("currentstate", "over");
				break;
				case "down":
					$(obj).style.backgroundPosition = "bottom right";
					$(obj).setAttribute("currentstate", "down");
				break;
				case "disabled":
					$(obj).style.backgroundPosition = "bottom left";
				break;
			}
		}
	}
}
