--lua文件必须是UTF-8编码的(最好无BOM头)
function SetState(self,newState)
	local attr = self:GetAttribute()
	if newState ~= attr.NowState then
		local ownerTree = self:GetOwner()
		local oldBkg = self:GetControlObject("oldBkg")
		local bkg = self:GetControlObject("bkg")
		
		oldBkg:SetResID(bkg:GetResID())
		oldBkg:SetAlpha(255)
		if attr.Tick == 1 and attr.TwoState then
			if newState == 0 then
				bkg:SetResID(attr.NormalBkgIDEx)
			elseif newState == 1 then
				bkg:SetResID(attr.DownBkgIDEx)
			elseif newState == 2 then
				bkg:SetResID(attr.DisableBkgIDEx)
			elseif newState == 3 then
				bkg:SetResID(attr.HoverBkgIDEx)
			end
		else
			if newState == 0 then
				bkg:SetResID(attr.NormalBkgID)
			elseif newState == 1 then
				bkg:SetResID(attr.DownBkgID)
			elseif newState == 2 then
				bkg:SetResID(attr.DisableBkgID)
			elseif newState == 3 then
				bkg:SetResID(attr.HoverBkgID)
			end
		end
		local aniFactory = XLGetObject("Xunlei.UIEngine.AnimationFactory")	
		local aniAlpha = aniFactory:CreateAnimation("AlphaChangeAnimation")
		aniAlpha:BindRenderObj(oldBkg)
		aniAlpha:SetTotalTime(500)
		aniAlpha:SetKeyFrameAlpha(255,0)
		ownerTree:AddAnimation(aniAlpha)
		aniAlpha:Resume()
		attr.NowState = newState
	end
end

function ChangeState(self,newState,tick)
	local attr = self:GetAttribute()
	attr.Tick = tick
	self:SetState(newState)
end

function GetTick(self) 		
	local attr = self:GetAttribute()
	return attr.Tick
end

function SetText(self,newText)
	local textObj = self:GetControlObject("text")
	textObj:SetText(newText)
end

function OnLButtonDown(self)
	local attr = self:GetAttribute()
	if attr.Enable then
		self:SetState(1)
		self:SetCaptureMouse(true)
	end
end

function OnLButtonUp(self)
	local attr = self:GetAttribute()
	if attr.Enable then
		if attr.NowState==1 then
			self:FireExtEvent("OnClick")
			attr.Tick = 0 - attr.Tick
			self:SetState(0)
		end
		self:SetCaptureMouse(false)
	end
end

function OnMouseMove(self)
	local attr = self:GetAttribute()
	if attr.Enable then
		if attr.NowState==0 then
			self:SetState(3)
		end
	end
end

function OnMouseLeave(self)
	local attr = self:GetAttribute()
	self:SetState(0)
end

function Click(self)
	OnLButtonDown(self)
	OnLButtonUp(self)
end

function OnBind(self)
	local attr = self:GetAttribute()
	self:SetText(attr.Text)
	attr.NowState=0
	local bkg = self:GetControlObject("bkg")
	bkg:SetResID(attr.NormalBkgID)
end