local addon, ns = ...;

local function createInputPrompt(parent, index, x, y, variable)
	local eb = CreateFrame("EditBox", nil, parent, "InputBoxTemplate");
	eb.variable = variable
	eb:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y);
	eb:SetEnabled(true);
	eb:SetWidth(100);
	eb:SetHeight(35);	
	eb:SetNumeric(true);	
	eb:SetText(variable);
	eb:SetCursorPosition(0);
	eb:HighlightText(0,0);
	eb:SetEnabled(false);	
	local t = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal");
	t:SetPoint("TOPLEFT", eb, "TOPRIGHT", 10, -10);
	eb:SetScript("OnShow", function(self) 
		self:SetEnabled(false);
		local min = WarbandMiserDB.Settings.Range[index].low;
		local max = WarbandMiserDB.Settings.Range[index].high;
		if min == max then t:SetText("Level " .. min);
		else t:SetText("Level " .. min .. " to " .. max); end;
		eb:SetText(WarbandMiserDB.Settings[eb.variable]);
	end);
	eb:SetScript("OnMouseUp", function(self) 
		self:SetEnabled(true); 
		eb:SetCursorPosition(0);
		eb:HighlightText();
	end);
	eb:SetScript("OnEnterPressed", function(self) 
		WarbandMiserDB.Settings[eb.variable] = tonumber(eb:GetText());
		self:SetEnabled(false);
	end);
	eb:SetScript("OnEscapePressed", function(self) 
		self:SetEnabled(false);
		eb:SetText(WarbandMiserDB.Settings[eb.variable]);
	end);
	eb:SetScript("OnEditFocusLost", function(self)		
		WarbandMiserDB.Settings[eb.variable] = tonumber(eb:GetText());
		self:SetEnabled(false);
	end);
		t:SetText("Bracket" .. index)
end;          

local function PopulateTree(DataProvider)
	if #WarbandMiserDB.Toons < 0 then return; end;
	DataProvider:Flush();
	local servers = {};
	for k,v in pairs(WarbandMiserDB.Toons) do
		tinsert(servers, k);	--load servers into a table
	end;
	sort(servers);				--Sort them
	local topLevel = {};
	local i = 1;
	for _,s in ipairs(servers) do	--Add a button for each server
		local topLevelData = {
			ButtonText = s,
			Level = 1,
		}
		topLevel[i] = DataProvider:Insert(topLevelData)		
		local toons = {};
		for k,v in pairs(WarbandMiserDB.Toons[s]) do
			tinsert(toons,k);		--Get toons on this server
		end;
		sort(toons)					--sort them
		for _,t in ipairs(toons) do	--Add a child button for each toon on the server
			local nestedData = {
				ButtonText = t,
				Level = 2,
				Server = s,
			}		
			topLevel[i]:Insert(nestedData);				
		end;		
		topLevel[i]:ToggleCollapsed();	
		i = i + 1;			
	end;
end;

function ns:LoadOptionsPage()		--Build the options frame
	if ns.OptionsLoaded then return; end;	
	local f = CreateFrame("Frame");
	ns.category = Settings.RegisterCanvasLayoutCategory(f, "Warband Miser");
	Settings.RegisterAddOnCategory(ns.category);
	do	-- Title
		local title = f:CreateFontString();
		title:SetFont("Fonts\\FRIZQT__.TTF", 20);
		title:SetPoint("TOPLEFT", 10, -20);
		title:SetText("Warband Miser");	
		local line = f:CreateTexture();
		line:SetSize(5,1);
		line:ClearAllPoints();
		line:SetPoint("TOPLEFT", f, "TOPLEFT", 40, -50);
		line:SetPoint("TOPRIGHT", f, "TOPRIGHT", -60, 0);
		line:SetColorTexture(1, 1, 1, 0.4);
	end;
	local xPos = 10;
	local yPos = -60;	
	do	-- Use defaults checkbox
		if WarbandMiserDB.Settings.IncludeNew == nil then WarbandMiserDB.Settings.IncludeNew = true; end;
		local cb = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
		cb:SetPoint("TOPLEFT", xPos, yPos);
		cb:SetSize(32, 32);	
		cb:SetChecked(WarbandMiserDB.Settings.IncludeNew);
		cb:SetScript("PostClick", function(self)
			WarbandMiserDB.Settings.IncludeNew = self:GetChecked();
		end);		
		cb.Text:SetFontObject("GameFontNormal");
		cb.Text:SetText("Include new characters using default settings.");
	end;
	do	--Edit Ranges Button
		local b = CreateFrame("Button", nil, f, "GameMenuButtonTemplate");		
		b:SetSize(200, 30);
		b:SetText("Edit Bracket Ranges");
		b:SetNormalFontObject("GameFontNormal");
		b:SetHighlightFontObject("GameFontHighlight");
		b:SetPoint("TOPLEFT", 350, yPos);
		--Button function
		b:SetScript("OnClick", function(self, button, down)
			Settings.OpenToCategory(ns.category2:GetID());
		end)		
	end;
	yPos = yPos - 40;
	-- Default gold levels
	createInputPrompt(f, 1, 20, yPos, "Bracket1");
	createInputPrompt(f, 2, 325, yPos, "Bracket2");
	yPos = yPos - 40;
	createInputPrompt(f, 3, 20, yPos, "Bracket3");
	createInputPrompt(f, 4, 325, yPos, "Bracket4");
	yPos = yPos - 40;
	createInputPrompt(f, 5, 20, yPos, "Bracket5");
	createInputPrompt(f, 6, 325, yPos, "Bracket6");
	yPos = yPos - 40;	
	do	-- Description text
		local t = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		t:SetSize(600, 40);
		t:SetJustifyH("LEFT");
		t:SetPoint("TOPLEFT", f, "TOPLEFT", 20, yPos);
		local text = "Above value is the amount of gold to be left on characters set as default. " ..
			"If you don't have sufficient funds in your bank to top up a character all available " ..
			"funds will be transferred. If you reach gold cap for your Warband Bank any excess " ..
			"gold will be left on your character."
		t:SetText(text);
		yPos = yPos - 70;
	end;

	do	-- Toons list
		local sbx = CreateFrame("Frame", nil, f, "WowScrollBoxList");
		sbx:SetPoint("TOPLEFT", xPos, yPos);
		sbx:SetSize(200, 300);
		local ScrollBar = CreateFrame("EventFrame", nil, f, "MinimalScrollBar");
		ScrollBar:SetPoint("TOPLEFT", sbx, "TOPRIGHT");
		ScrollBar:SetPoint("BOTTOMLEFT", sbx, "BOTTOMRIGHT");
		local DataProvider = CreateTreeDataProvider();
		local ScrollView = CreateScrollBoxListTreeListView();
		ScrollView:SetDataProvider(DataProvider);
		ScrollUtil.InitScrollBoxListWithScrollBar(sbx, ScrollBar, ScrollView);
		local function Initializer(button, node)
			local data = node:GetData(); 	-- get our data from the node with :GetData()
			local text = data.ButtonText;
			button:SetText(text);
			button:SetScript("OnClick", function()
				if data.Level == 1 then 
					node:ToggleCollapsed(); 
					ns:SetServer(text);
				end;
				if data.Level == 2 then
					ns:SetChar(data.ButtonText, data.Server);
				end;
			end);
		end;
		ScrollView:SetElementInitializer("UIPanelButtonTemplate", Initializer);
		if ns.OptionsLoaded == false then PopulateTree(DataProvider); end;
		sbx:SetScript("OnShow", function(self) PopulateTree(DataProvider); end);
		function ns:RefreshTree() PopulateTree(DataProvider); end;
	end;
	
	do	-- Detail Panel
		local data = {};
		data.server = ""; data.name = ""; data.variable = "";
		local character = f:CreateFontString();
		character:SetFont("Fonts\\FRIZQT__.TTF", 20);
		character:SetPoint("TOPLEFT", 275, yPos );	
		character:SetText("ToonName" .. "   " .. "Server");		
		local tLevel = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		tLevel:SetPoint("TOPLEFT", character, "TOPLEFT", 0, -70);		
		local tGold = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		tGold:SetPoint("TOPLEFT", tLevel, "TOPLEFT", 0, -25);		
		local tDefGold = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		tDefGold:SetPoint("TOPLEFT", tGold, "TOPLEFT", 0, -25);	
		local tSetGold = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		tSetGold:SetPoint("TOPLEFT", tDefGold, "TOPLEFT", 0, -25);			
		local cbInclude = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
		cbInclude:SetPoint("TOPLEFT", tSetGold, "TOPLEFT", 0, -20);
		cbInclude:SetSize(32, 32);			
		cbInclude.Text:SetFontObject("GameFontNormal")
		cbInclude.Text:SetText("Include this character in currency sharing");	
		local cbDefaults = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
		local cbServerDefault = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
		local cbAllGold = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
		local ebKeepGold = CreateFrame("EditBox", nil, f, "InputBoxTemplate");
		local ebWBLevel = CreateFrame("EditBox", nil, f, "InputBoxTemplate");

		cbDefaults:SetPoint("TOPLEFT", cbInclude, "TOPLEFT", 0, -25);
		cbDefaults:SetSize(32, 32);			
		cbDefaults.Text:SetFontObject("GameFontNormal")
		cbDefaults.Text:SetText("Use default gold value");	
		cbServerDefault:SetPoint("TOPLEFT", cbDefaults, "TOPLEFT", 0, -25);
		cbServerDefault:SetSize(32, 32);			
		cbServerDefault.Text:SetFontObject("GameFontNormal")
		cbServerDefault.Text:SetText("Use server default gold value");		
		cbAllGold:SetPoint("TOPLEFT", cbServerDefault, "TOPLEFT", 0, -25);
		cbAllGold:SetSize(32, 32);		
		cbAllGold.Text:SetFontObject("GameFontNormal")
		cbAllGold.Text:SetText("Draw all gold leaving the minimum value");		
		
		ebKeepGold:SetPoint("TOPLEFT", cbAllGold, "TOPLEFT", 10, -30);
		ebKeepGold:SetEnabled(true);
		ebKeepGold:SetSize(75, 35);	
		ebKeepGold:SetNumeric(true);
		ebKeepGold:SetCursorPosition(0);
		ebKeepGold:HighlightText(0,0);
		ebKeepGold:SetEnabled(false);	
		ebKeepGold:SetScript("OnMouseUp", function(self) 
			if data.variable == "" or data.variable == nil then return; end;
			self:SetEnabled(true); 
			ebKeepGold:SetCursorPosition(0);
			ebKeepGold:HighlightText();
		end);
		ebKeepGold:SetScript("OnEnterPressed", function(self)
			if data.variable == "" or data.variable == nil then return; end;
			data.variable.GoldCap = tonumber(self:GetText());
			tSetGold:SetText("Set gold cap is " .. ns:GoldSilverCopper(data.variable.GoldCap * 10000));
			self:SetEnabled(false);
		end);
		ebKeepGold:SetScript("OnEscapePressed", function(self) 
			if data.variable == "" or data.variable == nil then return; end;
			self:SetEnabled(false);
			ebKeepGold:SetText(data.variable.GoldCap);
			tSetGold:SetText("Set gold cap is " .. ns:GoldSilverCopper(data.variable.GoldCap));
		end);
		ebKeepGold:SetScript("OnEditFocusLost", function(self)
			if data.variable == "" or data.variable == nil then return; end;		
			data.variable.GoldCap = tonumber(self:GetText());
			tSetGold:SetText("Set gold cap is " .. ns:GoldSilverCopper(data.variable.GoldCap * 10000));
			self:SetEnabled(false);
		end);
		ebKeepGold.t = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		ebKeepGold.t:SetPoint("TOPLEFT", ebKeepGold, "TOPRIGHT", 10, -10);
		ebKeepGold.t:SetText("Gold to keep on hand for this character.");		
				
		cbInclude:SetScript("OnClick", function(self)
			if data.variable == "" or data.variable == nil then return; end;
			data.variable.Include = self:GetChecked();
			if data.variable.Include == true then cbDefaults:Show()	else  cbDefaults:Hide(); end;
		end);
		ebKeepGold:SetScript("OnShow", function(self) 
			self:SetEnabled(false);
			if data.variable == "" or data.variable == nil then return; end;
			self:SetText(data.variable.GoldCap);
			self.t:Show();
		end);
		ebKeepGold:SetScript("OnHide", function(self) self.t:Hide(); end);
		
		ebWBLevel:SetPoint("TOPLEFT", cbAllGold, "TOPLEFT", 10, -30);
		ebWBLevel:SetEnabled(true);
		ebWBLevel:SetSize(75, 35);	
		ebWBLevel:SetNumeric(true);
		ebWBLevel:SetCursorPosition(0);
		ebWBLevel:HighlightText(0,0);
		ebWBLevel:SetEnabled(false);	
		ebWBLevel:SetScript("OnMouseUp", function(self) 
			self:SetEnabled(true); 
			ebWBLevel:SetCursorPosition(0);
			ebWBLevel:HighlightText();
		end);
		ebWBLevel:SetScript("OnEnterPressed", function(self)
			WarbandMiserDB.wbMinumum = tonumber(ebWBLevel:GetText());
			tSetGold:SetText("Set gold cap is " .. ns:GoldSilverCopper(WarbandMiserDB.wbMinumum * 10000));
			self:SetEnabled(false);
		end);
		ebWBLevel:SetScript("OnEscapePressed", function(self) 
			self:SetEnabled(false);
			ebWBLevel:SetText(WarbandMiserDB.wbMinumum);
		end);
		ebWBLevel:SetScript("OnEditFocusLost", function(self)		
			WarbandMiserDB.wbMinumum = tonumber(ebWBLevel:GetText());
			tSetGold:SetText("Set gold cap is " .. ns:GoldSilverCopper(WarbandMiserDB.wbMinumum * 10000));
			self:SetEnabled(false);
		end);
		ebWBLevel.t = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
		ebWBLevel.t:SetPoint("TOPLEFT", ebWBLevel, "TOPRIGHT", 10, -10);
		ebWBLevel.t:SetText("Warband Minimum");		
		ebWBLevel:SetScript("OnHide", function(self) self.t:Hide(); end);
		 ebWBLevel:SetScript("OnShow", function(self) 	
			self.t:Show();				
		end);
		
		-- Delete button
		local bDelete = CreateFrame("Button",  nil, f, "GameMenuButtonTemplate");
		bDelete:SetSize(300, 30);
		bDelete:SetText("Delete This Character")
		bDelete:SetNormalFontObject("GameFontNormalLarge");
		bDelete:SetHighlightFontObject("GameFontHighlightLarge");
		bDelete:SetPoint("TOPLEFT", character, "TOPLEFT", 0, -30);
		bDelete:SetScript("OnEnter", function(self)
			GameTooltip:SetOwner(self, "LEFT");
			GameTooltip:AddLine("Remove this character from the data file.");
			GameTooltip:AddLine("This cannot be undone");
			GameTooltip:Show();
		end);
		bDelete:SetScript("OnLeave", function() GameTooltip:Hide(); end);
		bDelete:SetScript("OnClick", function(self, button, down)
			WarbandMiserDB.Toons[data.server][data.name] = nil;
			ns:RefreshTree();
			ns:SetChar("", "");
		end)	
		
		function ns:SetChar(t, s)
			data.name = t; data.server = s;	
			if t ~= "" and s ~= "" then 		
				ns:VerifyData(s, t);
				data.variable = WarbandMiserDB.Toons[s][t];
				--Update strings
				character:SetText(t .. " - " .. s);
				tLevel:SetText("Player Level: " .. data.variable.Level);
				tGold:SetText("Current Gold: " .. ns:GoldSilverCopper(data.variable.Gold));
				tDefGold:SetText("Default gold for this level is " .. 
					ns:GoldSilverCopper(ns:GetDefaultGoldForLevel(data.variable.Level)*10000));
				local cap = data.variable.GoldCap or 0;
				if cap == 0 then cap = "--" else cap = ns:GoldSilverCopper(cap * 10000); end;
				tSetGold:SetText("Set gold cap is " .. cap);
				--Check Checkboxes
				cbInclude:SetChecked(data.variable.Include);
				if data.variable.Include then cbDefaults:Show() else cbDefaults:Hide(); end;
				cbDefaults:SetChecked(data.variable.Defaults);
				cbServerDefault:SetChecked(data.variable.ServerDefault);
				cbAllGold:SetChecked(data.variable.AllGold);
				--Show/Hide elements				
				character:Show(); tLevel:Show(); tGold:Show(); tDefGold:Show(); tSetGold:Show(); 
				cbInclude:Show(); bDelete:Show();
				--Show/Hide Editboxes
				cbDefaults:Hide(); ebKeepGold:Hide(); ebWBLevel:Hide(); --Hide all 3
				if data.variable.Include == true then
					cbDefaults:Show();
					if (data.variable.Defaults == false and data.variable.ServerDefault == false 
							and data.variable.AllGold == false) then 
						ebKeepGold:Show();
					elseif (data.variable.Defaults == false and data.variable.ServerDefault == false and data.variable.AllGold == true) then 
						ebWBLevel:Show();
					end;
				end;
				if ns.ServerDefaultValue then ns:ServerDefaultValue(false); end;
			else
				--Empty strings so s server is selected
				character:SetText("Select a server and character");	
				tLevel:Hide(); tGold:Hide(); tDefGold:Hide(); tSetGold:Hide(); cbInclude:Hide(); 
				ebKeepGold:Hide(); cbDefaults:Hide(); cbServerDefault:Hide(); bDelete:Hide(); ebWBLevel:Hide();
				if ns.ServerDefaultValue then ns:ServerDefaultValue(false); end;
			end;
		end;
		
		function ns:SetServer(s)
			character:SetText(s .. " - Server Options");	
			tLevel:Hide(); tGold:Hide(); tDefGold:Hide(); tSetGold:Hide(); cbInclude:Hide(); ebWBLevel:Hide()
			ebKeepGold:Hide(); ebKeepGold.t:Hide(); cbDefaults:Hide(); cbServerDefault:Hide(); bDelete:Hide();
			ns:ServerDefaultValue(true, s)
		end;
		local function RadioButtonClick(button)
			if data.variable == "" or data.variable == nil then return; end;
			--turn off other 2 and set variable if button is checked
			local flag = button:GetChecked();	
			if button == cbDefaults then
				data.variable.Defaults = flag;
				cbServerDefault:SetChecked(false);
				data.variable.ServerDefault = false;
				cbAllGold:SetChecked(false);
				data.variable.AllGold = false;	
				data.variable.GoldCap = ns:GetDefaultGoldForLevel(data.variable.Level);				
			elseif button == cbServerDefault then 
				data.variable.ServerDefault = flag;
				cbDefaults:SetChecked(false);
				data.variable.Defaults = false;
				cbAllGold:SetChecked(false);
				data.variable.AllGold = false;	
				data.variable.GoldCap = WarbandMiserDB.Settings.Servers[data.server].Default;
			elseif button == cbAllGold then 
				data.variable.AllGold = flag;
				cbServerDefault:SetChecked(false);
				data.variable.ServerDefault = false;
				cbDefaults:SetChecked(false);
				data.variable.Defaults = false;
				data.variable.GoldCap = 0;
			else
				data.variable.GoldCap = tonumber(ebKeepGold:GetText());
			end;						
			--see what is checked and set Page
			ns:SetChar(data.name, data.server);
		end;
		
		cbDefaults:SetScript("OnClick", function(self) RadioButtonClick(self); end);
		cbServerDefault:SetScript("OnClick", function(self) RadioButtonClick(self); end);
		cbAllGold:SetScript("OnClick", function(self) RadioButtonClick(self); end);
		
		cbDefaults:SetScript("OnShow", function(self) cbServerDefault:Show(); cbAllGold:Show(); end);	
		cbDefaults:SetScript("OnHide", function(self) cbServerDefault:Hide(); cbAllGold:Hide(); end);		
		
		--ns:SetChar("", "");
		--function ns:GetValue() return WarbandMiserDB.Toons[s][t].GoldCap; end;
		--function ns:SetValue(v) WarbandMiserDB.Toons[s][t].GoldCap = v; end;

		--Server options		
		do
			local eb = CreateFrame("EditBox", nil, f, "InputBoxTemplate");
			local b = CreateFrame("Button", nil, f, "GameMenuButtonTemplate");
			local b2 = CreateFrame("Button", nil, f, "GameMenuButtonTemplate");	
			local cb = CreateFrame("CheckButton", nil, f, "ChatConfigCheckButtonTemplate");
			
			eb.variable = "";
			yPos = -320
			eb:SetPoint("TOPLEFT", f, "TOPLEFT", 285, yPos );			
			eb:SetEnabled(true);
			eb:SetWidth(100);
			eb:SetHeight(35);	
			eb:SetNumeric(true);	
			eb:SetText(0);
			eb:SetCursorPosition(0);
			eb:HighlightText(0,0);
			eb:SetEnabled(false);	
			local t = f:CreateFontString(nil, "OVERLAY", "GameFontNormal");
			t:SetPoint("TOPLEFT", eb, "TOPRIGHT", 10, -10);
			
			function ns:ServerDefaultValue(vis, s)
				if vis == true then
					if s ~= "" then 
						eb.variable = s;
						cb.variable = s;
						WarbandMiserDB.Settings.Servers[s] = WarbandMiserDB.Settings.Servers[s] or {};
						WarbandMiserDB.Settings.Servers[s].Default = WarbandMiserDB.Settings.Servers[s].Default or 0;
						WarbandMiserDB.Settings.Servers[s].Include = WarbandMiserDB.Settings.Servers[s].Include or false;
						eb:SetText(WarbandMiserDB.Settings.Servers[s].Default);
						cb:SetChecked(WarbandMiserDB.Settings.Servers[s].Include);
						eb:Show();
					end;
				else
					eb:Hide();
				end;
			end;
			eb:SetScript("OnShow", function(self) 	
				self:SetText(WarbandMiserDB.Settings.Servers[self.variable].Default);
				t:Show(); b:Show(); b2:Show(); cb:Show();
			end);
			eb:SetScript("OnHide", function(self) t:Hide(); b:Hide(); b2:Hide(); cb:Hide(); end);
			eb:SetScript("OnMouseUp", function(self) 
				self:SetEnabled(true); 
				self:SetCursorPosition(0);
				self:HighlightText();
			end);
			eb:SetScript("OnEnterPressed", function(self) 
				WarbandMiserDB.Settings.Servers[self.variable].Default = tonumber(self:GetText());
				self:SetEnabled(false);
			end);
			eb:SetScript("OnEscapePressed", function(self) 
				self:SetEnabled(false);
				eb:SetText(WarbandMiserDB.Settings.Servers[self.variable].Default);
			end);
			eb:SetScript("OnEditFocusLost", function(self)		
				WarbandMiserDB.Settings.Servers[self.variable].Default = tonumber(self:GetText());
				self:SetEnabled(false);
			end);			
			t:SetText("Server Default" )
			eb:Hide(); t:Hide();
			yPos = yPos - 40;
			-- Set All button	
			b:SetSize(300, 30);
			b:SetText("Include all characters on this server");
			b:SetNormalFontObject("GameFontNormal");
			b:SetHighlightFontObject("GameFontHighlight");
			b:SetPoint("TOPLEFT", 275, yPos);
			b:Hide();
			--Button function
			b:SetScript("OnClick", function(self, button, down)
				local sDef = WarbandMiserDB.Settings.Servers[eb.variable].Default;
				for k,v in pairs (WarbandMiserDB.Toons[eb.variable]) do
					WarbandMiserDB.Toons[eb.variable][k].GoldCap = sDef;
					WarbandMiserDB.Toons[eb.variable][k].Include = true;
					WarbandMiserDB.Toons[eb.variable][k].ServerDefault = true;
					WarbandMiserDB.Toons[eb.variable][k].Defaults = false;
					WarbandMiserDB.Toons[eb.variable][k].AllGold = false;
				end;				
			end);
			yPos = yPos - 40;
			-- Set All button	
			b2:SetSize(300, 30);
			b2:SetText("Remove server default from this server");
			b2:SetNormalFontObject("GameFontNormal");
			b2:SetHighlightFontObject("GameFontHighlight");
			b2:SetPoint("TOPLEFT", 275, yPos);
			b2:Hide();
			--Button function
			b2:SetScript("OnClick", function(self, button, down)
				for k,v in pairs (WarbandMiserDB.Toons[eb.variable]) do
					WarbandMiserDB.Toons[eb.variable][k].GoldCap = ns:GetDefaultGoldForLevel(v.Level);
					WarbandMiserDB.Toons[eb.variable][k].Include = true;
					WarbandMiserDB.Toons[eb.variable][k].ServerDefault = false;
					WarbandMiserDB.Toons[eb.variable][k].Defaults = true;
					WarbandMiserDB.Toons[eb.variable][k].AllGold = false;
				end;
			end);
			yPos = yPos - 40;
			--Include new toons in server minimum cb
			cb:SetPoint("TOPLEFT", 275, yPos);
			cb:SetSize(32, 32);			
			cb.Text:SetFontObject("GameFontNormal")
			cb.Text:SetText("Include new characters on this server\nat server default value");
			cb.variable = "";
			cb:Hide();
			cb:SetScript("OnClick", function(self)
				if self.variable == "" then return; end;
				WarbandMiserDB.Settings.Servers[self.variable].Include = self:GetChecked();
			end);
			
			ns:SetChar("", "");
		end;
		
		
	end;
	
	do	--Edit bracket ranges Page
		local sf = CreateFrame("Frame");
		ns.category2 = Settings.RegisterCanvasLayoutSubcategory(ns.category, sf, "Bracket Ranges...");
		Settings.RegisterAddOnCategory(ns.category2);
		do	-- Title
			local title = sf:CreateFontString();
			title:SetFont("Fonts\\FRIZQT__.TTF", 20);
			title:SetPoint("TOPLEFT", 10, -20);
			title:SetText("Edit Bracket Ranges");	
			local line = sf:CreateTexture();
			line:SetSize(5,1);
			line:ClearAllPoints();
			line:SetPoint("TOPLEFT", sf, "TOPLEFT", 40, -50);
			line:SetPoint("TOPRIGHT", sf, "TOPRIGHT", -60, 0);
			line:SetColorTexture(1, 1, 1, 0.4);
		end;
		xPos = 30;
		yPos = -60;	
		
		--Table for bracket Sliders		
		--local min = 1;			
		local sliders = {};
		for i=1, 6 do 
			sliders[i] = {};
			sliders[i].header = sf:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge");
			sliders[i].header:SetSize(250, 40);
			sliders[i].header:SetJustifyH("LEFT");
			if i == 1 or i == 3 or i == 5 then xPos = 30 else xPos = 350; end;
			if i == 3 or i == 5 then yPos = yPos - 120; end;
			sliders[i].header:SetPoint("TOPLEFT", sf, "TOPLEFT", xPos, yPos);
			sliders[i].header:SetText("Bracket " .. i .. "  -  " .. WarbandMiserDB.Settings.Range[i].low .. " to " .. WarbandMiserDB.Settings.Range[i].high);	
			sliders[i].slider = CreateFrame("Slider", nil, sf, "UISliderTemplateWithLabels");
			sliders[i].slider:SetSize(230, 17);
			sliders[i].slider:SetPoint("TOPLEFT", sliders[i].header, "BOTTOMLEFT", 0, -10);		
			sliders[i].slider:SetMinMaxValues(WarbandMiserDB.Settings.Range[i].low, ns.maxLevel);
			sliders[i].slider.Low:SetText(WarbandMiserDB.Settings.Range[i].low);
			sliders[i].slider.High:SetText(ns.maxLevel);
			sliders[i].slider:SetValue(WarbandMiserDB.Settings.Range[i].high); 
			sliders[i].slider:SetValueStep(1);
			sliders[i].slider:SetObeyStepOnDrag(true);
			sliders[i].slider.Text:SetFont("Fonts\\FRIZQT__.TTF", 12);
			sliders[i].slider.Text:SetText(WarbandMiserDB.Settings.Range[i].high);
	
			sliders[i].slider:SetScript("OnValueChanged", function(self, value)
				self.Text:SetText( value );
				WarbandMiserDB.Settings.Range[i].high = value;
				sliders[i].header:SetText("Bracket " .. i .. "  -  " .. WarbandMiserDB.Settings.Range[i].low .. " to " .. WarbandMiserDB.Settings.Range[i].high);
				if i == 6 then return; end;
				if value >= WarbandMiserDB.Settings.Range[i+1].low then
					local x = value + 1;
					if x > 80 then x = 80; end;
					sliders[i+1].slider:SetMinMaxValues(x, ns.maxLevel);
					sliders[i+1].slider.SetValue = x;
				else
					sliders[i+1].slider:SetMinMaxValues(value + 1, ns.maxLevel)
				end;
			end)
			sliders[i].slider:SetScript("OnMinMaxChanged", function (self, min, max)	
				self.Low:SetText(min);
				self.High:SetText(max);
				if self:GetValue() < min then self.SetValue = min; end;
				WarbandMiserDB.Settings.Range[i].low = min;
				WarbandMiserDB.Settings.Range[i].high = self:GetValue();			
			end);
		end;
		 yPos = yPos - 120;
		do	-- Description text
			local t = sf:CreateFontString(nil, "OVERLAY", "GameFontNormal");
			t:SetSize(600, 60);
			t:SetJustifyH("LEFT");
			t:SetPoint("TOPLEFT", sf, "TOPLEFT", 20, yPos);
			local text = "Move the sliders to set the maximum level for each range. " ..
				"Adjacent ranges will be adjusted if needed.\n\n" ..
				"The first range(lowest) that includes the maximum level will be used ignoring all others."
			t:SetText(text);
		end;
	end;	
	ns.OptionsLoaded = true;
end





