CBHViewportOverlay = WorldFrame:CreateTexture(nil, "BACKGROUND");
CBHViewportOverlay:SetTexture(0, 0, 0, 1);
CBHViewportOverlay:SetPoint("TOPLEFT", UIParent, "TOPLEFT", -1, 1);
CBHViewportOverlay:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", 1, -1);

CBH_VOptions = {};
CBH_VOptions.top = 0;
CBH_VOptions.bottom = 180;
CBH_VOptions.left = 0;
CBH_VOptions.right = 0;
CBH_VOptions.currentYResolution = tonumber(string.sub(GetCVar("gxResolution"), 1, string.find(GetCVar("gxResolution"), "x")-1));
CBH_VOptions.scaling = 768 / CBH_VOptions.currentYResolution;

function CBH_Viewport_UpdatePort()
	CBH_VOptions.currentYResolution = tonumber(string.sub(GetCVar("gxResolution"), 1, string.find(GetCVar("gxResolution"), "x")-1));
	CBH_VOptions.scaling = 768 / CBH_VOptions.currentYResolution;
	WorldFrame:SetPoint("TOPLEFT", (CBH_VOptions.left * CBH_VOptions.scaling), -(CBH_VOptions.top * CBH_VOptions.scaling));
	WorldFrame:SetPoint("BOTTOMRIGHT", -(CBH_VOptions.right * CBH_VOptions.scaling), (CBH_VOptions.bottom * CBH_VOptions.scaling));
	WorldFrame:SetPoint("TOPLEFT", 0, -(CBH_VOptions.top * 0.75));
	WorldFrame:SetPoint("BOTTOMRIGHT", 0, (CBH_VOptions.bottom * 0.75));
end

local function CBH_SetupSlider(self, text, myMin, myMax, step)
	getglobal(self:GetName().."Text"):SetText(text);
	getglobal(self:GetName().."Low"):SetText(myMin);
	getglobal(self:GetName().."High"):SetText(myMax);
	self:SetMinMaxValues(myMin, myMax);
	self:SetValueStep(step);
end

local mainFrame = CreateFrame("Frame", "CBH_ViewportOptions");

--Default Options Menu
local slider = CreateFrame("Slider", "CBH_TopSlider", mainFrame, "OptionsSliderTemplate");
slider:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -80);
slider:SetScript("OnShow", function() CBH_SetupSlider(CBH_TopSlider, "Top Size", 0, 400, 10); CBH_TopSlider:SetValue(CBH_VOptions.top); end);
slider:SetScript("OnValueChanged", function() CBH_VOptions.top = CBH_TopSlider:GetValue(); CBH_Viewport_UpdatePort(); end );

local slider = CreateFrame("Slider", "CBH_BottomSlider", mainFrame, "OptionsSliderTemplate");
slider:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -120);
slider:SetScript("OnShow", function() CBH_SetupSlider(CBH_BottomSlider, "Bottom Size", 0, 400, 10); CBH_BottomSlider:SetValue(CBH_VOptions.bottom); end);
slider:SetScript("OnValueChanged", function() CBH_VOptions.bottom = CBH_BottomSlider:GetValue(); CBH_Viewport_UpdatePort(); end );

local button = CreateFrame("Button", "CBH_ShowAdv", mainFrame, "OptionsButtonTemplate");
button:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -300);
button:SetText("Advanced");
button:SetScript("OnClick", function() 
	CBH_TopSlider:Hide(); CBH_BottomSlider:Hide(); CBH_ShowAdv:Hide(); 
	CBH_TopEdit:Show(); CBH_BottomEdit:Show(); CBH_ShowBasic:Show();
end);

--Advanced Options Menu
local editBox = CreateFrame("EditBox", "CBH_TopEdit", mainFrame, "InputBoxTemplate");
editBox:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -80);
editBox:SetWidth(75);
editBox:SetHeight(20);
editBox:SetAutoFocus(false);
editBox:SetMaxLetters(3);
editBox:SetScript("OnShow", function() CBH_TopEdit:SetText(CBH_VOptions.top); end);
editBox:SetScript("OnCursorChanged", function() CBH_VOptions.top = CBH_TopEdit:GetText(); CBH_Viewport_UpdatePort(); end);
editBox:Hide();
	
local editBox = CreateFrame("EditBox", "CBH_BottomEdit", mainFrame, "InputBoxTemplate");
editBox:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -120);
editBox:SetWidth(75);
editBox:SetHeight(20);
editBox:SetAutoFocus(false);
editBox:SetMaxLetters(3);
editBox:SetScript("OnShow", function() CBH_BottomEdit:SetText(CBH_VOptions.bottom); end);
editBox:SetScript("OnCursorChanged", function() CBH_VOptions.bottom = CBH_BottomEdit:GetText(); CBH_Viewport_UpdatePort(); end);
editBox:Hide();

local button = CreateFrame("Button", "CBH_ShowBasic", mainFrame, "OptionsButtonTemplate");
button:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 20, -300);
button:SetText("Basaic");
button:SetScript("OnClick", function() 
	CBH_TopSlider:Show(); CBH_BottomSlider:Show(); CBH_ShowAdv:Show(); 
	CBH_TopEdit:Hide(); CBH_BottomEdit:Hide(); CBH_ShowBasic:Hide();
end);
button:Hide();
	

--Credits
local fontString = mainFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
fontString:SetText("CBH Viewport By:\n _Chloe @ WoWInterface.com");
fontString:SetPoint("CENTER", mainFrame, "TOP", 0, -20);
	
mainFrame.name = "CBH Viewport";
InterfaceOptions_AddCategory(mainFrame);

local mainFrame = CreateFrame("Frame", "CBH_Viewport");
mainFrame:SetScript("OnEvent", function() CBH_Viewport_UpdatePort(); end );
mainFrame:RegisterEvent("PLAYER_ENTERING_WORLD");