SMART_SETSCALE_ENABLE = true;

function CreateSmartUnitFrameSlider(name, parent, text)
	frame = CreateFrame("SLIDER", name, parent, "SmartSetScaleSliderTemplate")
	
	frame.text = text

	frame:SetMinMaxValues(50, 200)
	frame:SetValueStep(1)

	frame:SetScript("OnShow", SmartUnitFrameSlider_OnShow);
	frame:SetScript("OnMouseDown", function() getglobal(this:GetName().."Thumb"):SetVertexColor(1,0.82,0) end);
	frame:SetScript("OnMouseUp", function() getglobal(this:GetName().."Thumb"):SetVertexColor(1,1,1) end);
	return frame
end

function SmartUnitFrameSlider_OnShow()
	local sliderMin, sliderMax = this:GetMinMaxValues();

	getglobal(this:GetName().."Low"):SetText(sliderMin.."%");
	getglobal(this:GetName().."High"):SetText(sliderMax.."%");

	getglobal(this:GetName().."Text"):SetText(this.text)
end

function SmartSetScale_OnLoad()
	local frame

	frame = CreateSmartUnitFrameSlider("SmartPlayerScaleSlider", SmartSetScaleFrame, "자신")
	frame:SetPoint("TOP", SmartSetScaleFrame, "TOP", 25, -50)

	frame = CreateSmartUnitFrameSlider("SmartPetScaleSlider", SmartSetScaleFrame, "소환수")
	frame:SetPoint("TOP", SmartPlayerScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartTargetScaleSlider", SmartSetScaleFrame, "대상")
	frame:SetPoint("TOP", SmartPetScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartFocusScaleSlider", SmartSetScaleFrame, "주시대상")
	frame:SetPoint("TOP", SmartTargetScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartPartyScaleSlider", SmartSetScaleFrame, "파티")
	frame:SetPoint("TOP", SmartFocusScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartPartyPetScaleSlider", SmartSetScaleFrame, "파티소환수")
	frame:SetPoint("TOP", SmartPartyScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartToTScaleSlider", SmartSetScaleFrame, "대상의 대상")
	frame:SetPoint("TOP", SmartPartyPetScaleSlider, "BOTTOM", 0, -25)

	frame = CreateSmartUnitFrameSlider("SmartPartyTargetScaleSlider", SmartSetScaleFrame, "파티원의 대상")
	frame:SetPoint("TOP", SmartToTScaleSlider, "BOTTOM", 0, -25)
	
	SmartPlayerScaleSlider:SetScript("OnValueChanged", SmartPlayerScaleSlider_OnValueChanged)
	SmartPetScaleSlider:SetScript("OnValueChanged", SmartPetScaleSlider_OnValueChanged)
	SmartTargetScaleSlider:SetScript("OnValueChanged", SmartTargetScaleSlider_OnValueChanged)
	SmartFocusScaleSlider:SetScript("OnValueChanged", SmartFocusScaleSlider_OnValueChanged)
	SmartPartyScaleSlider:SetScript("OnValueChanged", SmartPartyScaleSlider_OnValueChanged)
	SmartPartyPetScaleSlider:SetScript("OnValueChanged", SmartPartyPetScaleSlider_OnValueChanged)
	SmartToTScaleSlider:SetScript("OnValueChanged", SmartToTScaleSlider_OnValueChanged)
	SmartPartyTargetScaleSlider:SetScript("OnValueChanged", SmartPartyTargetScaleSlider_OnValueChanged)

	if SCONFIG_ENABLE then
		frame = CreateFrame("BUTTON", "SmartSetScaleButton", SmartConfigUIFrame, "SmartConfigUIButtonTemplate")
		SmartSetScaleButton:SetPoint("TOPLEFT", SmartConfigUIFrame, "BOTTOMLEFT", 15, 120)
		SmartSetScaleButton:SetWidth(160)
		SmartSetScaleButton:SetScript("OnClick",function() SmartSetScale_ToggleMenu() end)
		SmartSetScaleButton.tooltip = "프레임들의 크기 설정창 열기/닫기"
		SmartSetScaleButtonText:SetText("프레임 크기 조절")
	end

	SmartSetScaleResetButtonText:SetText("초기화")
	SmartSetScaleCloseButtonText:SetText("닫기")

	SmartSetScale_Initialize()
end

function SmartSetScale_Initialize(reset)
	if (not SUF_SetScale or SUF_SetScale.partytarget == nil) then
		SUF_SetScale = { player=100,pet=100,target=100,focus=100,party=100,partypet=100,tot=100,partytarget=100 };
	elseif reset ~= nil then
		SUF_SetScale = { player=100,pet=100,target=100,focus=100,party=100,partypet=100,tot=100,partytarget=100 };
	end

	SmartPlayerScaleSlider:SetValue(SUF_SetScale.player)
	SmartPetScaleSlider:SetValue(SUF_SetScale.pet)
	SmartTargetScaleSlider:SetValue(SUF_SetScale.target)
	SmartFocusScaleSlider:SetValue(SUF_SetScale.focus)
	SmartPartyScaleSlider:SetValue(SUF_SetScale.party)
	SmartPartyPetScaleSlider:SetValue(SUF_SetScale.partypet)
	SmartToTScaleSlider:SetValue(SUF_SetScale.tot)
	SmartPartyTargetScaleSlider:SetValue(SUF_SetScale.partytarget)
end

function SmartSetScale_ToggleMenu()
	if SmartSetScaleFrame:IsShown() then
		SmartSetScaleFrame:Hide()
	else
		SmartSetScaleFrame:ClearAllPoints()
		SmartSetScaleFrame:SetPoint("CENTER", UIParent, "CENTER", -500, 0)
		SmartSetScaleFrame:Show()
	end
end

------------------------------------------------------------------------------------------
function SmartPlayerScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.player = this:GetValue()

	if SUF_SetScale.player == "" then
		this:SetValue(100)
	end

	PlayerFrame:SetScale(SUF_SetScale.player / 100)
end

function SmartPetScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.pet = this:GetValue()

	if SUF_SetScale.pet == "" then
		this:SetValue(100)
	end

	PetFrame:SetScale(SUF_SetScale.pet / 100)
end

function SmartTargetScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.target = this:GetValue()

	if SUF_SetScale.target == "" then
		this:SetValue(100)
	end

	TargetFrame:SetScale(SUF_SetScale.target / 100)
end

function SmartFocusScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.focus = this:GetValue()

	if SUF_SetScale.focus == "" then
		this:SetValue(100)
	end

	if SMART_FOCUS_ENABLE then
		SmartFocusFrame:SetScale(SUF_SetScale.focus / 100)
	end
end

function SmartPartyScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.party = this:GetValue()

	if SUF_SetScale.party == "" then
		this:SetValue(100)
	end

	local i
	for i=1, MAX_PARTY_MEMBERS do
		getglobal("PartyMemberFrame"..i):SetScale(SUF_SetScale.party / 100)
	end	
end

function SmartPartyPetScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.partypet = this:GetValue()

	if SUF_SetScale.partypet == "" then
		this:SetValue(100)
	end

	local i
	for i=1, MAX_PARTY_MEMBERS do
		getglobal("PartyMemberFrame"..i.."PetFrame"):SetScale(SUF_SetScale.partypet / 100)
	end	
end

function SmartToTScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.tot = this:GetValue()

	if SUF_SetScale.tot == "" then
		this:SetValue(100)
	end

	SmartToTFrame:SetScale(SUF_SetScale.tot / 100)
end

function SmartPartyTargetScaleSlider_OnValueChanged()
	getglobal(this:GetName().."EditBox"):SetText(this:GetValue())
	SUF_SetScale.partytarget = this:GetValue()

	if SUF_SetScale.partytarget == "" then
		this:SetValue(100)
	end

	local i
	for i=1, MAX_PARTY_MEMBERS do
		getglobal("PartyTargetFrame"..i):SetScale(SUF_SetScale.partytarget / 100)
	end
end

