--[[
		ChangeBar Layout v1.2
		
		By Arbi
		
		Change Log :
			v1.0 - Initial Release
			v1.1 - Added to reputation bar
			v1.2 - Fixed a bug that disappearing pet action bar in wind style
]]--

ChangeBar_PlayerName = nil;
local changebar_loaded = false;
local maxlevel = 70;
local cbl_petaction_init = false;

function ChangeBar_OnLoad()
	if ( not ChangeBar_Settings ) then
		ChangeBar_Settings = { };
	end
	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("PLAYER_XP_UPDATE");
	this:RegisterEvent("UPDATE_EXHAUSTION");
	this:RegisterEvent("UPDATE_FACTION");
	this:RegisterEvent("PLAYER_LEVEL_UP");
	this:RegisterEvent("PLAYER_UPDATE_RESTING");

	Sea.util.hook("MultiActionBar_Update", "ChangeBar_MultiActionBar_Update", replace);

	if ( Cosmos_RegisterConfiguration ) then
		Cosmos_RegisterConfiguration(
			"COS_CHANGEBAR",
			"SECTION",
			CHANGEBAR_CONFIG_HEADER,
			CHANGEBAR_CONFIG_HEADER_INFO
		);
		Cosmos_RegisterConfiguration(
			"COS_CHANGEBAR_HEADER",
			"SEPARATOR",
			CHANGEBAR_CONFIG_HEADER,
			CHANGEBAR_CONFIG_HEADER_INFO
		);
		Cosmos_RegisterConfiguration(
			"COS_CHANGEBAR_DEFAULT",
			"BUTTON",
			CHANGEBAR_CONFIG_DEFAULT, 
			CHANGEBAR_CONFIG_DEFAULT_INFO,
			ChangeBar_Default,
			0,
			0,
			0,
			0,
			CHANGEBAR_CONFIG_BUTTONTEXT
		);
		Cosmos_RegisterConfiguration(
			"COS_CHANGEBAR_WIND",
			"BUTTON",
			CHANGEBAR_CONFIG_WIND, 
			CHANGEBAR_CONFIG_WIND_INFO,
			ChangeBar_Wind,
			0,
			0,
			0,
			0,
			CHANGEBAR_CONFIG_BUTTONTEXT
		);
		Cosmos_RegisterConfiguration(
			"COS_CHANGEBAR_BARTENDER",
			"BUTTON",
			CHANGEBAR_CONFIG_BARTENDER, 
			CHANGEBAR_CONFIG_BARTENDER_INFO,
			ChangeBar_Bartender,
			0,
			0,
			0,
			0,
			CHANGEBAR_CONFIG_BUTTONTEXT
		);
	end
end

function ChangeBar_OnEvent()
	if ( event == "VARIABLES_LOADED" ) then
		if ( UnitName("player") ~= nil and UnitName("player") ~= UNKNOWNOBJECT and UnitName("player") ~= UKNOWNBEING ) then
			ChangeBar_PlayerName = GetCVar("realmName").."|"..UnitName("player");
			if ( not ChangeBar_Settings[ChangeBar_PlayerName] ) then
				ChangeBar_Settings[ChangeBar_PlayerName] = {};
				ChangeBar_Settings[ChangeBar_PlayerName].Style = 0;
				ChangeBar_Settings[ChangeBar_PlayerName].ShowXP = 1;
				ChangeBar_Settings[ChangeBar_PlayerName].XPWidth = 420;
			end
		end
		local style = ChangeBar_Settings[ChangeBar_PlayerName].Style;
		local showxp = ChangeBar_Settings[ChangeBar_PlayerName].ShowXP;
		ChangeBar_SetStyle(style);
		ChangeBar_InitialXP(style, showxp);
		ChangeBar_InitialRep(style);
		ChangeBar_InitialLatency(style);
	elseif (event == "PLAYER_ENTERING_WORLD") then
		if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 0 ) then
			return;
		end
		if ( changebar_loaded ) then
			return;
		end
		if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 1 ) then
			PetActionButton1:ClearAllPoints();
			if ( tonumber(GetNumShapeshiftForms()) > 0 ) then
				PetActionButton1:SetPoint("BOTTOMLEFT", "MainMenuBar", "BOTTOMLEFT", 240, 95);
			else
				PetActionButton1:SetPoint("BOTTOMLEFT", "MainMenuBar", "BOTTOMLEFT", 30, 95);
			end
		end
		if ( UnitLevel("player") == maxlevel ) then
			ChangeBar_Settings[ChangeBar_PlayerName].ShowXP = 0;
			if ChangeBar_ExpBar then ChangeBar_ExpBar:Hide(); end
			ChangeBar_UpdateRepData();
		else
			ChangeBar_Settings[ChangeBar_PlayerName].ShowXP = 1;
			ChangeBar_UpdateExpData();
			ChangeBar_UpdateRepData();
		end
		if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 2 ) then
			ChangeBar_Settings[ChangeBar_PlayerName].XPWidth = MultiBarBottomLeftButton1:GetLeft() - 3;
		end
		changebar_loaded = true;
	elseif ((event == "PLAYER_XP_UPDATE") or (event == "PLAYER_UPDATE_RESTING") or (event == "PLAYER_LEVEL_UP")) then
		ChangeBar_UpdateExpData();
	elseif (event == "UPDATE_EXHAUSTION") then
		ChangeBar_UpdateExpBar();
	elseif (event == "UPDATE_FACTION") then
		ChangeBar_UpdateRepData();
	end
end

function ChangeBar_HideNormalTexture(type)
	if ( type == 2 ) then
		ExhaustionTick:SetAlpha(0);
		MainMenuExpBar:SetScale(0.01);
		MainMenuExpBar:SetAlpha(0);
		ReputationWatchBar:SetAlpha(0);
	end
	for i = 0, 3 do
		getglobal("MainMenuBarTexture"..i):SetAlpha(0);
		getglobal("MainMenuMaxLevelBar"..i):SetAlpha(0);
	end
	MainMenuBarLeftEndCap:SetAlpha(0);
	MainMenuBarRightEndCap:SetAlpha(0);
	BonusActionBarTexture0:SetAlpha(0);
	BonusActionBarTexture1:SetAlpha(0);
	SlidingActionBarTexture0:SetAlpha(0);
	SlidingActionBarTexture1:SetAlpha(0);
end

function ChangeBar_HideShapeshiftTexture()
	ShapeshiftBarLeft:SetAlpha(0);
	ShapeshiftBarRight:SetAlpha(0);
	ShapeshiftBarMiddle:SetAlpha(0);

	for i=1, GetNumShapeshiftForms() do
		getglobal("ShapeshiftButton"..i.."NormalTexture"):SetAlpha(0);
	end
end

function ChangeBar_SetupWindBar(bar)
	if ( not bar ) then
		return;
	end
	frame = getglobal(bar);
	if ( bar == "MultiBarBottomLeft" ) then
		CreateFrame("Frame", "ChangeBar_BottomLeftBar", UIParent);
		MultiBarBottomLeft:ClearAllPoints();
		MultiBarBottomLeft:SetParent("ChangeBar_BottomLeftBar");
		MultiBarBottomLeft:SetFrameStrata("MEDIUM");
		frame = getglobal("ChangeBar_BottomLeftBar");
	elseif ( bar == "MultiBarRight" ) then
		CreateFrame("Frame", "ChangeBar_RightBar", UIParent);
		MultiBarRight:ClearAllPoints();
		MultiBarRight:SetParent("ChangeBar_RightBar");
		MultiBarRight:SetFrameStrata("MEDIUM");
		frame = getglobal("ChangeBar_RightBar");
	elseif ( bar == "PetAction" ) then
		CreateFrame("Frame", "ChangeBar_PetActionBar", UIParent);
		for i=1, NUM_PET_ACTION_SLOTS, 1 do
			local button = getglobal("PetActionButton"..i);
			button:SetParent("ChangeBar_PetActionBar");
			button.normalTexture = getglobal("PetActionButton"..i.."NormalTexture2");
			button.normalTexture:Hide();
			
			button.icon = getglobal("PetActionButton"..i.."Icon");
			button:SetFrameLevel(2);
		end
		ChangeBar_PetActionBar_Init();
	end
	if ( bar ~= "PetAction" ) then
		frame:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 16, edgeSize = 16, insets = { left = 5, right = 5, top = 5, bottom = 5 }});
		frame:SetBackdropColor(0,0,0,0.8);
		frame:SetBackdropBorderColor(1,0.82,0);
		frame:SetHeight(53);
		frame:SetWidth(518);
		frame:SetFrameStrata("LOW");
	end
	if ( bar ~= "MainMenuBar" and bar ~= "PetAction" ) then
		SetMultiBars_SetupBars(bar, 1, 36);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARBOTTOMLEFT_X", 1);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARBOTTOMRIGHT_X", 1);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARLEFT_X", 1);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARRIGHT_X", 1);
	end
end

function ChangeBar_SetupBartenderBar(bar)
	if ( not bar ) then
		return;
	end
	if ( bar == "Action" ) then
		getglobal(bar.."Button1"):ClearAllPoints();
		getglobal(bar.."Button1"):SetPoint("BOTTOMLEFT", "UIParent", "BOTTOM", -50, 5);
		BonusActionButton1:ClearAllPoints();
		BonusActionButton1:SetPoint("BOTTOMLEFT", "ActionButton1", "BOTTOMLEFT", 0, 0);
	elseif ( bar == "MultiBarBottomLeft" ) then
		getglobal(bar.."Button1"):ClearAllPoints();
		getglobal(bar.."Button1"):SetPoint("BOTTOMLEFT", "ActionButton1", "BOTTOMLEFT", -126, 0);
	elseif ( bar == "MultiBarBottomRight" ) then
		getglobal(bar.."Button1"):ClearAllPoints();
		getglobal(bar.."Button1"):SetPoint("BOTTOMLEFT", "ActionButton3", "BOTTOMRIGHT", 6, 0);
	elseif ( bar == "MultiBarRight" ) then
		getglobal(bar.."Button1"):ClearAllPoints();
		getglobal(bar.."Button1"):SetPoint("BOTTOMLEFT", "MultiBarBottomRightButton3", "BOTTOMRIGHT", 6, 0);
		getglobal(bar):SetFrameStrata("MEDIUM");
	elseif ( bar == "MultiBarLeft" ) then
		getglobal(bar.."Button1"):ClearAllPoints();
		getglobal(bar.."Button1"):SetPoint("BOTTOMLEFT", "MultiBarRightButton3", "BOTTOMRIGHT", 6, 0);
		getglobal(bar):SetFrameStrata("MEDIUM");
	end
	if ( bar == "MicroButton" ) then
		MicroButton1 = CharacterMicroButton;
		MicroButton2 = SpellbookMicroButton;
		MicroButton3 = TalentMicroButton;
		MicroButton4 = QuestLogMicroButton;
		MicroButton5 = SocialsMicroButton;
		MicroButton6 = LFGMicroButton;
		MicroButton7 = MainMenuMicroButton;
		MicroButton8 = HelpMicroButton;
		CharacterMicroButton:ClearAllPoints();
		CharacterMicroButton:SetPoint("BOTTOMLEFT", "MultiBarLeftButton3", "BOTTOMRIGHT", 6, -3);
		for i=2,8 do
			getglobal(bar..i):ClearAllPoints();
			if ( i == 3 or i == 5 or i == 7 ) then
				getglobal(bar..i):SetPoint("BOTTOM", bar..i-2, "TOP", 0, -22);
			else
				getglobal(bar..i):SetPoint("BOTTOMLEFT", bar..i-1, "BOTTOMRIGHT", -2, 0);
			end
		end
	elseif ( bar == "BagButton" ) then
		BagButton1 = CharacterBag0Slot;
		BagButton2 = CharacterBag1Slot;
		BagButton3 = CharacterBag2Slot;
		BagButton4 = CharacterBag3Slot;
		MainMenuBarBackpackButton:ClearAllPoints();
		MainMenuBarBackpackButton:SetPoint("BOTTOMLEFT", "SpellbookMicroButton", "BOTTOMRIGHT", 3, 3);
		CharacterBag0Slot:ClearAllPoints();
		CharacterBag0Slot:SetPoint("BOTTOM", "MainMenuBarBackpackButton", "TOP", 0, 6);
		for i=2,4 do
			getglobal(bar..i):ClearAllPoints();
			getglobal(bar..i):SetPoint("BOTTOM", bar..i-1, "TOP", 0, 6);
		end
	else
		for i=2,12 do
			getglobal(bar.."Button"..i):ClearAllPoints();
			if ( i == 4 or i == 7 or i == 10 ) then
				getglobal(bar.."Button"..i):SetPoint("BOTTOM", bar.."Button"..i-3, "TOP", 0, 6);
			else
				getglobal(bar.."Button"..i):SetPoint("LEFT", bar.."Button"..i-1, "RIGHT", 6, 0);
			end
		end
	end
end

function ChangeBar_SetScale(style, scale)
	if ( style == 1 ) then
		MultiBarBottomRight:SetScale(scale);
		MainMenuBar:SetScale(scale);
		ChangeBar_BottomLeftBar:SetScale(scale);
		ChangeBar_RightBar:SetScale(scale);
		MultiBarLeft:SetScale(scale);
	elseif ( style == 2 ) then
	end
end

function ChangeBar_SetStyle(style)
	if ( style == 0 ) then
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARBOTTOMLEFT_X", 1);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARBOTTOMRIGHT_X", 1);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARLEFT_X", 0);
		Cosmos_SetCVar("COS_SETMULTI_MULTIBARRIGHT_X", 0);
		SetMultiBars_SetupBars("MultiBarBottomRight", 1, 36);
		SetMultiBars_SetupBars("MultiBarBottomLeft", 1, 36);
		SetMultiBars_SetupBars("MultiBarRight", 0, 36);
		SetMultiBars_SetupBars("MultiBarLeft", 0, 36);
	end
	if ( style == 1 ) then
		ChangeBar_WindStyle();
		MultiBarRight:SetFrameStrata("MEDIUM");
		MainMenuBarBackpackButton:SetFrameStrata("BACKGROUND");
		for i = 0, 3 do
			getglobal("CharacterBag"..i.."Slot"):SetFrameStrata("LOW");
		end
		if ( BSM_BarLabels ) then
			BSM_BarLabels:SetFrameStrata("HIGH");
		end
	elseif ( style == 2 ) then
		ChangeBar_BartenderStyle();
		for i = 0, 3 do
			getglobal("CharacterBag"..i.."Slot"):SetFrameStrata("LOW");
		end
		if ( BSM_BarLabels ) then
			BSM_BarLabels:SetFrameStrata("HIGH");
		end
	end
end

function ChangeBar_PopupWindow()
	if ( CosmosMasterFrame ) then
		CosmosMaster_OnOkayClick();
	end
	
	ChangeBar_NoteText:SetText(CHANGEBAR_CONFIG_RELOADUI);
	ChangeBar_Note:SetAlpha(1);
	ChangeBar_Note:Show();
	UIFrameFadeOut(ChangeBar_Note, 15);
	PlaySound("PVPTHROUGHQUEUE");
end

function ChangeBar_Default()
	ChangeBar_Settings[ChangeBar_PlayerName].Style = 0;
	ChangeBar_PopupWindow();
	ChatFrame1:ClearAllPoints();
	ChatFrame1:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 30, 155);
end

function ChangeBar_Wind()
	ChangeBar_Settings[ChangeBar_PlayerName].Style = 1;
	ChangeBar_PopupWindow();
	ChatFrame1:ClearAllPoints();
	ChatFrame1:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 30, 155);
end

function ChangeBar_Bartender()
	ChangeBar_Settings[ChangeBar_PlayerName].Style = 2;
	ChangeBar_PopupWindow();
	ChatFrame1:ClearAllPoints();
	ChatFrame1:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", 30, 45);
end

function ChangeBar_PetActionBar_Init()
	if cbl_petaction_init then return; end
	cbl_petaction_init = true;
	hooksecurefunc("PetActionBar_ShowGrid", function() ChangeBar_PetActionBar_ShowGrid(); end);
	hooksecurefunc("PetActionBar_HideGrid", function() ChangeBar_PetActionBar_HideGrid(); end);
	hooksecurefunc("PetActionBar_Update", function() ChangeBar_PetActionBar_Update(); end);
end

function ChangeBar_PetActionBar_Update()
	for i=1, NUM_PET_ACTION_SLOTS, 1 do
		local _, _, texture = GetPetActionInfo(i);
		local button = getglobal("PetActionButton"..i);
		if ( texture ) then
			button.normalTexture:SetTexCoord(0,0,0,0)
		else
			button.normalTexture:SetTexCoord(-0.1,1.1,-0.1,1.12)
		end
	end
end

function ChangeBar_PetActionBar_ShowGrid()
	local name;
	for i=1, NUM_PET_ACTION_SLOTS, 1 do
		name = GetPetActionInfo(i);
		local button = getglobal("PetActionButton"..i);
		if ( not name ) then
			button.normalTexture:Show()
		end
	end
end

function ChangeBar_PetActionBar_HideGrid()
	for i=1, NUM_PET_ACTION_SLOTS, 1 do
		local button = getglobal("PetActionButton"..i);
		button.normalTexture:Hide()
	end
end

function ChangeBar_InitialXP(style, showxp)
	if ( not style or showxp == 0 ) then
		return;
	end
	local ExpBar = CreateFrame("Frame", "ChangeBar_ExpBar", UIParent);
	ExpBar:SetScale(1);

	ExpBar:SetHeight(20);
	if ( style == 1 ) then
		ExpBar:SetWidth(200);
	elseif ( style == 2 ) then
		local w = ChangeBar_Settings[ChangeBar_PlayerName].XPWidth;
		if not w then w = 420; end
		ExpBar:SetWidth(w);
	end

	local ExpBarBack = CreateFrame("StatusBar", "ChangeBar_ExpBarBack", ExpBar);
	ExpBarBack:SetPoint("CENTER", ExpBar, "CENTER", 0, 0);
	ExpBarBack:SetWidth(ExpBar:GetWidth() - 4);
	ExpBarBack:SetHeight(ExpBar:GetHeight() - 8);

	local ExpBarXP = CreateFrame("StatusBar", "ChangeBar_ExpBarXP", ExpBar);
	ExpBarXP:SetPoint("CENTER", ExpBar, "CENTER", 0, 0);
	ExpBarXP:SetWidth(ExpBar:GetWidth() - 4);
	ExpBarXP:SetHeight(ExpBar:GetHeight() - 8);

	local RestedXP = CreateFrame("StatusBar", "ChangeBar_ExpBarRestedXP", ExpBar);
	RestedXP:SetPoint("CENTER", ExpBar, "CENTER", 0, 0);
	RestedXP:SetWidth(ExpBar:GetWidth() - 4);
	RestedXP:SetHeight(ExpBar:GetHeight() - 8);

	local ExpBarText = ExpBar:CreateFontString("ChangeBar_ExpBarText", "ARTWORK");
	local font, _, _ = NumberFontNormal:GetFont();
	ExpBarText:SetFont(font, 14, "OUTLINE");
	ExpBarText:SetShadowOffset(1, -1);
	ExpBarText:SetPoint("CENTER", ExpBar, "CENTER");
	ExpBarText:SetTextColor(1,1,1,1);
	--[[
	if ( style == 1 ) then
		ExpBarText:SetPoint("CENTER", ExpBar, "CENTER");
	elseif ( style == 2 ) then
		ExpBarText:SetPoint("CENTER", ExpBar, "CENTER");
	end
	]]--

	ExpBarBack:SetFrameLevel(1);
	RestedXP:SetFrameLevel(ExpBarBack:GetFrameLevel() + 1);
	ExpBarXP:SetFrameLevel(RestedXP:GetFrameLevel() + 1);
	ExpBar:SetFrameLevel(ExpBarXP:GetFrameLevel() + 1);

	ExpBarBack:SetStatusBarTexture("Interface\\AddOns\\ChangeBar_Layout\\Textures\\smooth.tga");
	ExpBarXP:SetStatusBarTexture("Interface\\AddOns\\ChangeBar_Layout\\Textures\\smooth.tga");
	RestedXP:SetStatusBarTexture("Interface\\AddOns\\ChangeBar_Layout\\Textures\\smooth.tga");
	ExpBarBack:SetStatusBarColor(0.5, 0.5, 0.5, 0.5);

	ExpBar:ClearAllPoints();
	if ( style == 1 ) then
		ExpBar:SetPoint("BOTTOM", "CharacterBag3Slot", "TOP", 70, 0);
	elseif ( style == 2 ) then
		ExpBar:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", 0, 0);
	end
end

function ChangeBar_UpdateExpData()
	if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 0 or UnitLevel("player") == maxlevel ) then
		return;
	end
	prevXP = 0;
	if ChangeBar_playerCurrXP then
		prevXP = ChangeBar_playerCurrXP;
	end
	playerCurrXP = UnitXP("player");
	ChangeBar_playerCurrXP = playerCurrXP;
	playerMaxXP = UnitXPMax("player");
	remainXP = playerMaxXP - playerCurrXP;
	percentXP = playerCurrXP / playerMaxXP * 100;
	differentXP = playerCurrXP - prevXP;

	ChangeBar_UpdateExpBar();
end

function ChangeBar_UpdateExpBar()
	if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 0 ) then
		return;
	end
	local exhaustionThreshold = GetXPExhaustion();
	local xptext;

	ChangeBar_ExpBarXP:Show();

	ChangeBar_ExpBarBack:SetStatusBarColor(0.58, 0.6, 0.55);
	if ( exhaustionThreshold == nil ) then
		if ChangeBar_ExpBarRestedXP:IsVisible() then ChangeBar_ExpBarRestedXP:Hide(); end
		ChangeBar_ExpBarXP:SetStatusBarColor(0.8, 0.0, 1);
	else
		ChangeBar_ExpBarXP:SetStatusBarColor(0, 0.4, 1);
		ChangeBar_ExpBarRestedXP:SetMinMaxValues(min(0, playerCurrXP), playerMaxXP)
		ChangeBar_ExpBarRestedXP:SetValue(playerCurrXP + exhaustionThreshold);
		ChangeBar_ExpBarRestedXP:SetStatusBarColor(0.82, 0, 0);
		if IsResting() then
			ChangeBar_ExpBarText:SetTextColor(1, 0.82, 0.25, 1);
		else
			ChangeBar_ExpBarText:SetTextColor(1, 1, 1, 1);
		end
		ChangeBar_ExpBarRestedXP:Show();
	end

	xptext = string.format("XP %d/%d (%.1f%%) -%d", playerCurrXP, playerMaxXP, percentXP, remainXP);

	ChangeBar_ExpBarXP:SetMinMaxValues(min(0, playerCurrXP), playerMaxXP);
	ChangeBar_ExpBarXP:SetValue(playerCurrXP);
	ChangeBar_ExpBarText:SetText(xptext);
end

function ChangeBar_InitialRep(style)
	if ( not style or style == 0 ) then
		return;
	end
	local RepBar = CreateFrame("Frame", "ChangeBar_RepBar", UIParent);
	RepBar:SetScale(1);

	RepBar:SetHeight(20);
	if ( style == 1 ) then
		RepBar:SetWidth(200);
	elseif ( style == 2 ) then
		local w = ChangeBar_Settings[ChangeBar_PlayerName].XPWidth;
		if not w then w = 420; end
		RepBar:SetWidth(w);
	end

	local RepBarBack = CreateFrame("StatusBar", "ChangeBar_RepBarBack", RepBar);
	RepBarBack:SetPoint("CENTER", RepBar, "CENTER", 0, 0);
	RepBarBack:SetWidth(RepBar:GetWidth() - 4);
	RepBarBack:SetHeight(RepBar:GetHeight() - 8);

	local RepBarRep = CreateFrame("StatusBar", "ChangeBar_RepBarRep", RepBar);
	RepBarRep:SetPoint("CENTER", RepBar, "CENTER", 0, 0);
	RepBarRep:SetWidth(RepBar:GetWidth() - 4);
	RepBarRep:SetHeight(RepBar:GetHeight() - 8);

	local RepBarText1 = RepBar:CreateFontString("ChangeBar_RepBarText1", "ARTWORK");
	local font1, _, _ = GameFontNormal:GetFont();
	RepBarText1:SetFont(font1, 12, "OUTLINE");
	RepBarText1:SetShadowOffset(1, -1);
	RepBarText1:SetTextColor(1,1,1,1);

	local RepBarText2;
	local font2;

	if ( style == 1 ) then
		RepBarText2 = RepBar:CreateFontString("ChangeBar_RepBarText2", "ARTWORK");
		font2, _, _ = NumberFontNormal:GetFont();
		RepBarText2:SetFont(font2, 14, "OUTLINE");
		RepBarText2:SetShadowOffset(1, -1);
		RepBarText1:SetPoint("BOTTOM", RepBar, "TOP");
		RepBarText2:SetPoint("CENTER", RepBar, "CENTER");
		RepBarText2:SetTextColor(1,1,1,1);
	elseif ( style == 2 ) then
		RepBarText1:SetPoint("CENTER", RepBar, "CENTER");
	end

	RepBarBack:SetFrameLevel(1);
	RepBarRep:SetFrameLevel(RepBarBack:GetFrameLevel() + 1);
	RepBar:SetFrameLevel(RepBarRep:GetFrameLevel() + 1);

	RepBarBack:SetStatusBarTexture("Interface\\AddOns\\ChangeBar_Layout\\Textures\\smooth.tga");
	RepBarRep:SetStatusBarTexture("Interface\\AddOns\\ChangeBar_Layout\\Textures\\smooth.tga");
	RepBarBack:SetStatusBarColor(0.5, 0.5, 0.5, 0.5);

	RepBar:ClearAllPoints();
	if ( ChangeBar_ExpBar ) then
		if ( style == 1 ) then
			RepBar:SetPoint("BOTTOM", "ChangeBar_ExpBar", "TOP", 0, -6);
		elseif ( style == 2 ) then
			RepBar:SetPoint("BOTTOM", "ChangeBar_ExpBar", "TOP", 0, -6);
		end
	else
		if ( style == 1 ) then
			RepBar:SetPoint("BOTTOM", "CharacterBag3Slot", "TOP", 70, 0);
		elseif ( style == 2 ) then
			RepBar:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", 0, 0);
		end
	end
end

function ChangeBar_UpdateRepData()
	if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 0 ) then
		return;
	end
	local repName, repStanding, repMin, repMax, repValue = GetWatchedFactionInfo();

	if (repName == nil) then
		if ( ChangeBar_RepBar:IsVisible() ) then
			ChangeBar_RepBar:Hide();
		end
		return;
	end

	repMax = repMax - repMin;
	repValue = repValue - repMin;
	repMin = 0;
	repPercent = repValue / repMax * 100;
	local repStandingText = getglobal("FACTION_STANDING_LABEL"..repStanding);
	local reptext1;
	local reptext2;
	if ( ChangeBar_Settings[ChangeBar_PlayerName].Style == 1 ) then
		reptext1 = string.format("%s(%s)", repName, repStandingText);
		reptext2 = string.format("%d/%d (%.1f%%)", repValue, repMax, repPercent);
		ChangeBar_RepBarText1:SetText(reptext1);
		ChangeBar_RepBarText2:SetText(reptext2);
	else
		reptext1 = string.format("%s(%s): %d/%d (%.1f%%)", repName, repStandingText, repValue, repMax, repPercent);
		ChangeBar_RepBarText1:SetText(reptext1);
	end

	if ( not ChangeBar_RepBarRep:IsVisible() ) then
		ChangeBar_RepBar:Show();
		ChangeBar_RepBarRep:Show();
	end

	ChangeBar_RepBarBack:SetStatusBarColor(0.58, 0.6, 0.55);
	ChangeBar_RepBarRep:SetMinMaxValues(min(0, repValue), repMax);
	ChangeBar_RepBarRep:SetValue(repValue);
	ChangeBar_RepBarRep:SetStatusBarColor(FACTION_BAR_COLORS[repStanding].r, FACTION_BAR_COLORS[repStanding].g, FACTION_BAR_COLORS[repStanding].b);
end

function ChangeBar_InitialLatency(style)
	if ( style == 0 ) then
		return;
	end
	local latbar = CreateFrame("Frame", nil, UIParent);
	latbar:SetFrameStrata("LOW");
	latbar:SetWidth(20);
	latbar:SetHeight(60);
	latbar.hover = nil;
	latbar.updateInterval = 0;
	local latbart = latbar:CreateTexture(nil, "BACKGROUND");
	latbart:SetTexture("Interface\\MainMenuBar\\UI-MainMenuBar-PerformanceBar");
	latbart:SetAllPoints(latbar);
	latbar.texture = latbart;

	latbar:SetScript("OnUpdate", function() if (latbar.updateInterval > 0) then
		latbar.updateInterval = latbar.updateInterval - arg1;
	else
		latbar.updateInterval = PERFORMANCEBAR_UPDATE_INTERVAL;
		local bandwidthIn, bandwidthOut, latency = GetNetStats();
		if (latency > PERFORMANCEBAR_MEDIUM_LATENCY) then
			latbart:SetVertexColor(1, 0, 0);									
		elseif (latency > PERFORMANCEBAR_LOW_LATENCY) then
			latbart:SetVertexColor(1, 1, 0);
		else
			latbart:SetVertexColor(0, 1, 0);
		end
		if (this.hover) then
			MainMenuBarPerformanceBarFrame_OnEnter();
		end
	end end);

	local latbutton = CreateFrame("Button", nil, latbar);	
	latbutton:SetAllPoints(latbar);
	latbutton:SetScript("OnEnter", function()
		latbar.hover = 1;
		MainMenuBarPerformanceBarFrame_OnEnter();
	end);
	latbutton:SetScript("OnLeave", function()
		latbar.hover = nil;
		GameTooltip:Hide();
	end);

	latbar:ClearAllPoints();
	if ( style == 1 ) then
		latbar:SetPoint("CENTER", "CharacterMicroButton", "BOTTOMLEFT", -4, 15);
	elseif ( style == 2 ) then
		latbar:SetPoint("CENTER", "HelpMicroButton", "CENTER", 6, 25);
	end
	latbar:Show();
end

function ChangeBar_WindStyle()
	UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_X"].rightLeft = 6;
	UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_X"].rightRight = 3;
	UIPARENT_MANAGED_FRAME_POSITIONS["ShapeshiftBarFrame"].reputation = 0;
	UIPARENT_MANAGED_FRAME_POSITIONS["ShapeshiftBarFrame"].maxLevel = 0;

	ChangeBar_HideNormalTexture(2);

	ChangeBar_SetupWindBar("MultiBarBottomRight");
	ChangeBar_SetupWindBar("MainMenuBar");
	ChangeBar_SetupWindBar("MultiBarBottomLeft");
	ChangeBar_SetupWindBar("MultiBarRight");
	ChangeBar_SetupWindBar("MultiBarLeft");
	ChangeBar_SetupWindBar("PetAction");

	MultiBarBottomRight:ClearAllPoints();
	MultiBarBottomRight:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", 0, 0);
	MultiBarBottomRightButton1:ClearAllPoints();
	MultiBarBottomRightButton1:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", 10, 8);

	MainMenuBar:ClearAllPoints();
	MainMenuBar:SetPoint("BOTTOMLEFT", "MultiBarBottomRightButton12", "BOTTOMRIGHT", 5, -8);
	ActionButton1:ClearAllPoints();
	ActionButton1:SetPoint("BOTTOMLEFT", "MultiBarBottomRightButton12", "BOTTOMLEFT", 51, 0);
	BonusActionButton1:ClearAllPoints();
	BonusActionButton1:SetPoint("BOTTOMLEFT", "ActionButton1", "BOTTOMLEFT", 0, 0);
	
	ChangeBar_BottomLeftBar:ClearAllPoints();
	ChangeBar_BottomLeftBar:SetPoint("BOTTOMLEFT", "MultiBarRightButton12", "BOTTOMLEFT", 42, -10);
	MultiBarBottomLeftButton1:ClearAllPoints();
	MultiBarBottomLeftButton1:SetPoint("BOTTOMLEFT", "ChangeBar_BottomLeftBar", "BOTTOMLEFT", 10, 10);

	ChangeBar_RightBar:ClearAllPoints();
	ChangeBar_RightBar:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", 0, 50);
	MultiBarRightButton1:ClearAllPoints();
	MultiBarRightButton1:SetPoint("BOTTOMLEFT", "ChangeBar_RightBar", "BOTTOMLEFT", 10, 10);

	MultiBarLeft:ClearAllPoints();
	MultiBarLeft:SetPoint("BOTTOMLEFT", "ChangeBar_RightBar", "BOTTOMLEFT", 0, 50);
	MultiBarLeftButton1:ClearAllPoints();
	MultiBarLeftButton1:SetPoint("BOTTOMLEFT", "MultiBarLeft", "BOTTOMLEFT", 10, 8);

	ActionBarUpButton:ClearAllPoints();
	ActionBarUpButton:SetPoint("BOTTOMLEFT", "ActionButton12", "BOTTOMLEFT", 40, 18);
	ActionBarDownButton:ClearAllPoints();
	ActionBarDownButton:SetPoint("BOTTOMLEFT", "ActionButton12", "BOTTOMLEFT", 40, -15);
	MainMenuBarPageNumber:ClearAllPoints();
	MainMenuBarPageNumber:SetPoint("BOTTOMLEFT", "ActionButton12", "BOTTOMLEFT", 51, 13);

	CharacterMicroButton:ClearAllPoints();
	CharacterMicroButton:SetPoint("BOTTOMLEFT", "ActionButton12", "BOTTOMRIGHT", 40, 0);
	MainMenuBarBackpackButton:ClearAllPoints();
	MainMenuBarBackpackButton:SetPoint("BOTTOMLEFT", "CharacterMicroButton", "BOTTOMRIGHT", 143, 48);
	
	MainMenuBarPerformanceBarFrame:ClearAllPoints();
	MainMenuBarPerformanceBarFrame:SetPoint("BOTTOM", "UIParent", "BOTTOMRIGHT", 0, -100);

	if ( ( tonumber(GetCVar("uiScale")) > 0.85 and tonumber(GetCVar("uiScale")) < 0.9 ) or tonumber(GetCVar("useUIScale")) == 0 ) then
		ChangeBar_SetScale(1, 0.85);
	elseif ( tonumber(GetCVar("uiScale")) > 0.9 ) then
		ChangeBar_SetScale(1, 0.8);
	elseif ( tonumber(GetCVar("uiScale")) < 0.76 ) then
		ChangeBar_SetScale(1, 1);
	else
		ChangeBar_SetScale(1, 0.93);
	end
end

function ChangeBar_BartenderStyle()
	UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_X"].rightLeft = 6;
	UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_X"].rightRight = 3;
	UIPARENT_MANAGED_FRAME_POSITIONS["ShapeshiftBarFrame"] = {baseY = 2, bottomLeft = 0, reputation = 0, maxLevel = 0, anchorTo = "ActionButton10", point = "BOTTOMLEFT", rpoint = "TOP", xOffset = -30};
	UIPARENT_MANAGED_FRAME_POSITIONS["FramerateLabel"] = {baseY = 100, bottomEither = 90, pet = 1, reputation = 1};
	UIPARENT_MANAGED_FRAME_POSITIONS["CastingBarFrame"] = {baseY = 135, bottomEither = 85, pet = 1, reputation = 1};

	ChangeBar_HideNormalTexture(2);

	ChangeBar_SetupBartenderBar("Action");
	ChangeBar_SetupBartenderBar("BonusAction");
	ChangeBar_SetupBartenderBar("MultiBarBottomLeft");
	ChangeBar_SetupBartenderBar("MultiBarBottomRight");
	ChangeBar_SetupBartenderBar("MultiBarRight");
	ChangeBar_SetupBartenderBar("MultiBarLeft");
	ChangeBar_SetupBartenderBar("MicroButton");
	ChangeBar_SetupBartenderBar("BagButton");

	PetActionButton1:ClearAllPoints();
	PetActionButton1:SetPoint("TOPLEFT", "ActionButton10", "TOPLEFT", 0, 40);

	ActionBarUpButton:SetAlpha(0);
	ActionBarDownButton:SetAlpha(0);
	MainMenuBarPageNumber:SetAlpha(0);

	MainMenuBarPerformanceBarFrame:SetParent("UIParent");
	MainMenuBarPerformanceBarFrame:ClearAllPoints();
	MainMenuBarPerformanceBarFrame:SetPoint("BOTTOM", "UIParent", "BOTTOMRIGHT", 0, -100);
end

function ChangeBar_MultiActionBar_Update()
	if ( ChangeBar_Settings[ChangeBar_PlayerName].Style ~= 0 ) then
		ChangeBar_HideShapeshiftTexture();
	end
	if ( SHOW_MULTI_ACTIONBAR_1 ) then
		if ( ChangeBar_BottomLeftBar ) then
			ChangeBar_BottomLeftBar:Show();
		else
			MultiBarBottomLeft:Show();
		end
		MultiBarBottomLeft.isShowing = 1;
		VIEWABLE_ACTION_BAR_PAGES[BOTTOMLEFT_ACTIONBAR_PAGE] = nil;
	else
		if ( ChangeBar_BottomLeftBar ) then
			ChangeBar_BottomLeftBar:Hide();
		else
			MultiBarBottomLeft:Hide();
		end
		MultiBarBottomLeft.isShowing = nil;
		VIEWABLE_ACTION_BAR_PAGES[BOTTOMLEFT_ACTIONBAR_PAGE] = 1;
	end
	if ( SHOW_MULTI_ACTIONBAR_2 ) then
		MultiBarBottomRight:Show();
		VIEWABLE_ACTION_BAR_PAGES[BOTTOMRIGHT_ACTIONBAR_PAGE] = nil;
	else
		MultiBarBottomRight:Hide();
		VIEWABLE_ACTION_BAR_PAGES[BOTTOMRIGHT_ACTIONBAR_PAGE] = 1;
	end
	if ( SHOW_MULTI_ACTIONBAR_3 ) then
		if ( ChangeBar_RightBar ) then
			ChangeBar_RightBar:Show();
		else
			MultiBarRight:Show();
		end
		VIEWABLE_ACTION_BAR_PAGES[RIGHT_ACTIONBAR_PAGE] = nil;
	else
		if ( ChangeBar_RightBar ) then
			ChangeBar_RightBar:Hide();
		else
			MultiBarRight:Hide();
		end
		VIEWABLE_ACTION_BAR_PAGES[RIGHT_ACTIONBAR_PAGE] = 1;
	end
	if ( SHOW_MULTI_ACTIONBAR_3 and SHOW_MULTI_ACTIONBAR_4 ) then
		MultiBarLeft:Show();
		VIEWABLE_ACTION_BAR_PAGES[LEFT_ACTIONBAR_PAGE] = nil;
	else
		MultiBarLeft:Hide();
		VIEWABLE_ACTION_BAR_PAGES[LEFT_ACTIONBAR_PAGE] = 1;
	end
end