
local builded = false;

local realLocale = GetLocale()

local function GetLocale()
 if realLocale ~= "deDE" and realLocale ~= "enUS" then 
  return "enUS";
 else
  return realLocale;
 end
end

local DISABLED = { -- disabled string fail loading a optional dep or something else
 ["deDE"] = "|cff888888deaktiviert|r",
 ["enUS"] = "|cff888888disabled|r",
};

local RegistryWnd = CreateFrame("Frame","RegistryWnd",UIParent);

local function crc_GetManyAttributes(index, dataSource)
 crc_elementSelected = dataSource[index]["elementName"];
 for eid, v in pairs(commandstructure) do

  
  if v["type"] ~= nil then
       if v["type"] == "string" then 
    getglobal("cmd"..eid.."_control"):SetText(tostring(crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],true)))); 
   elseif v["type"] == "integer" then
    getglobal("cmd"..eid.."_control1"):SetText(tostring(crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],true)))); 
	getglobal("cmd"..eid.."_control2"):SetValue(tonumber(crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"]),true)));
   elseif v["type"] == "select" then 
    getglobal("cmd"..eid.."_controlText"):SetText(tostring(crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],true)))); 
   elseif v["type"] == "boolean" then
    if crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],false)) == 1 or
	   crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],false)) == "true" or
	   crt_getAttribute(dataSource[tonumber(index)]["elementName"],tostring(v["path"],false)) == true then
    getglobal("cmd"..eid.."_control"):SetChecked(true);
	end
   end
   end
 end
end
 
local function RGBToHex(r, g, b)
	r = r <= 255 and r >= 0 and r or 0
	g = g <= 255 and g >= 0 and g or 0
	b = b <= 255 and b >= 0 and b or 0
	return string.format("%02x%02x%02x", r, g, b)
end
 
local function crc_Builder()
local selectForDelete;
-- create Profile and Element Selectors
local SMLEnabled, SML = pcall(AceLibrary,"SharedMedia-1.0");  -- check if SharedMedia can be loaded an do it!

local WndIcon  = RegistryWnd:CreateTexture("crcWndIcon","BACKGROUND"); -- Window Icon Symbol
WndIcon:SetWidth(64);
WndIcon:SetHeight(64);
WndIcon:SetPoint("TOPLEFT",RegistryWnd,"TOPLEFT",5,-3);
WndIcon:SetTexture("interface\\macroframe\\macroframe-icon"); -- Window Graphic

local texture1 = RegistryWnd:CreateTexture("crcTopLeft","BORDER"); -- Window Graphic Top Left
texture1:SetWidth(256);
texture1:SetHeight(256);
texture1:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topleft");
texture1:SetPoint("TOPLEFT",RegistryWnd);

local texture2 = RegistryWnd:CreateTexture("crcBottomLeft","BORDER"); -- Window Graphic Bottom Left
texture2:SetWidth(256);
texture2:SetHeight(256);
texture2:SetTexture("interface\\paperdollinfoframe\\ui-character-general-bottomleft");
texture2:SetPoint("BOTTOMLEFT",RegistryWnd,"BOTTOMLEFT",0,0);

local texture3 = RegistryWnd:CreateTexture("crcTopRight","BORDER"); -- Window Graphic Top Right
texture3:SetWidth(128);
texture3:SetHeight(256);
texture3:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topright");
texture3:SetPoint("TOPRIGHT",RegistryWnd,"TOPRIGHT",0,0);

local texture4 = RegistryWnd:CreateTexture("crcBottomRight","BORDER"); -- Window Graphic Bottom Right
texture4:SetWidth(128);
texture4:SetHeight(256);
texture4:SetTexture("interface\\paperdollinfoframe\\ui-character-general-bottomright");
texture4:SetPoint("BOTTOMRIGHT",RegistryWnd,"BOTTOMRIGHT",0,0);

local texture5 = RegistryWnd:CreateTexture("crcBottom","BORDER"); -- Window Graphic Bottom spacer
texture5:SetWidth(128);
texture5:SetHeight(256);
texture5:SetTexture("interface\\paperdollinfoframe\\ui-character-general-bottomright");
--texture5:SetTexCoordModifiesRect(false);
texture5:SetTexCoord(0,0.5,0,1);
texture5:SetPoint("TOPRIGHT",texture4,"TOPLEFT",0,0);
texture5:SetPoint("BOTTOMLEFT",texture2,"BOTTOMRIGHT",0,0);

local texture6 = RegistryWnd:CreateTexture("crcTop","BORDER"); -- Window Graphic Top spacer
texture6:SetWidth(128);
texture6:SetHeight(256);
texture6:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topright");
--texture6:SetTexCoordModifiesRect(false);
texture6:SetTexCoord(0,0.5,0,1)
texture6:SetPoint("TOPRIGHT",texture3,"TOPLEFT",0,0);
texture6:SetPoint("BOTTOMLEFT",texture1,"BOTTOMRIGHT",0,0);

local texture7 = RegistryWnd:CreateTexture("crcLeft","BORDER"); -- Window Graphic Top spacer
texture7:SetWidth(128);
texture7:SetHeight(256);
texture7:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topleft");
--texture7:SetTexCoordModifiesRect(false);
texture7:SetTexCoord(0,1,0.4,1)
texture7:SetPoint("TOPRIGHT",texture1,"BOTTOMRIGHT",0,0);
texture7:SetPoint("BOTTOMLEFT",texture2,"TOPLEFT",0,0);

local texture8 = RegistryWnd:CreateTexture("crcRight","BORDER"); -- Window Graphic Top spacer
texture8:SetWidth(128);
texture8:SetHeight(256);
texture8:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topright");
--texture8:SetTexCoordModifiesRect(false);
texture8:SetTexCoord(0,1,0.4,1)
texture8:SetPoint("TOPRIGHT",texture3,"BOTTOMRIGHT",0,0);
texture8:SetPoint("BOTTOMLEFT",texture4,"TOPLEFT",0,0);

local texture9 = RegistryWnd:CreateTexture("crcRight","BORDER"); -- Window Graphic Top spacer
texture9:SetWidth(128);
texture9:SetHeight(256);
texture9:SetTexture("interface\\paperdollinfoframe\\ui-character-general-topleft");
--texture9:SetTexCoordModifiesRect(false);
texture9:SetTexCoord(0.4,1,0.4,1)
texture9:SetPoint("TOPLEFT",texture1,"BOTTOMRIGHT",0,0);
texture9:SetPoint("BOTTOMRIGHT",texture4,"TOPLEFT",0,0);


RegistryWnd:SetWidth(384);
RegistryWnd:SetHeight(512);
RegistryWnd:SetPoint("CENTER",UIParent);
RegistryWnd:SetMinResize(384, 512);
local RegistryWndCaption = RegistryWnd:CreateFontString("RegistryWndCaption","ARTWORK");
RegistryWndCaption:SetPoint("TOPLEFT",RegistryWnd,"TOPLEFT",73, -15);
RegistryWndCaption:SetPoint("TOPRIGHT",RegistryWnd,"TOPRIGHT",-64,-15);
RegistryWndCaption:SetFontObject("GameFontNormal");
RegistryWndCaption:SetText("CRT "..crt_version);
RegistryWndCaption:SetWidth(253);
RegistryWndCaption:SetHeight(17);
RegistryWndCaption:SetJustifyV("CENTER");

local RegistryWndCloseBtn = CreateFrame("Button","RegistryWndCloseBtn",RegistryWnd,"UIPanelCloseButton"); -- button to close
RegistryWndCloseBtn:SetPoint("CENTER","RegistryWnd","TOPRIGHT",-46,-24);
RegistryWndCloseBtn:SetScript("OnClick",function() RegistryWnd:Hide() end);

local ScrollFrame = CreateFrame("ScrollFrame","ClientWnd",RegistryWnd,"UIPanelScrollFrameTemplate"); -- Client Window
local ScrollRackTop = ScrollFrame:CreateTexture("ScrollTop","ARTWORK");
local ScrollRackBottom = ScrollFrame:CreateTexture("ScrollBottom","ARTWORK");
local ScrollRackMiddle = ScrollFrame:CreateTexture("ScrollMiddle","ARTWORK");

ScrollRackTop:SetTexture("interface\\paperdollinfoframe\\ui-character-scrollbar");
ScrollRackTop:SetTexCoord(0,0.5,0,1);
ScrollRackTop:SetWidth(32);
ScrollRackTop:SetHeight(256);
ScrollRackTop:SetPoint("TOPRIGHT",RegistryWnd,"TOPRIGHT",-37,-72);

ScrollRackBottom:SetTexture("interface\\paperdollinfoframe\\ui-character-scrollbar");
ScrollRackBottom:SetTexCoord(0.5,1,0,0.5);
ScrollRackBottom:SetWidth(32);
ScrollRackBottom:SetHeight(128);
ScrollRackBottom:SetPoint("BOTTOMRIGHT",RegistryWnd,"BOTTOMRIGHT",-38,60);

ScrollRackMiddle:SetTexture("interface\\paperdollinfoframe\\ui-character-scrollbar");
ScrollRackMiddle:SetTexCoord(0,0.5,0.4,0.6);
ScrollRackMiddle:SetWidth(32);
ScrollRackMiddle:SetPoint("TOPRIGHT",ScrollRackTop,"BOTTOMRIGHT",0,0);
ScrollRackMiddle:SetPoint("BOTTOMLEFT",ScrollRackBottom,"TOPLEFT",1,0);

ScrollFrame:SetPoint("TOPLEFT",RegistryWnd,"TOPLEFT",22,-76);
 
local Registry = CreateFrame("Frame","Registry",ScrollFrame); -- Client Panel
Registry:SetWidth(294);
Registry:SetHeight(24+(#(commandstructure)*24)+48); -- adjust height to the given commandstructure
Registry:SetPoint("TOPLEFT",ClientWnd);
ScrollFrame:SetWidth(295);
ScrollFrame:SetHeight(352);
ScrollFrame:SetScrollChild(Registry);
ScrollFrame:UpdateScrollChildRect();
RegistryWnd:SetMovable(true);
RegistryWnd:EnableMouse(true);
RegistryWnd:SetResizable(true);
RegistryWnd:RegisterForDrag("RightButton"); -- allow draggin

local sizer = CreateFrame("Button","sizer",RegistryWnd);
sizer:SetAllPoints(texture4);
sizer:RegisterForDrag("RightButton"); -- allow sizing
sizer:SetScript("OnDragStart",function()
									RegistryWnd:StartSizing("BOTTOMRIGHT")
									Registry:SetWidth(RegistryWnd:GetWidth()-(384-295))
									ScrollFrame:SetWidth(RegistryWnd:GetWidth()-89)
									ScrollFrame:SetHeight(RegistryWnd:GetHeight()-160)
									end);
sizer:SetScript("OnDragStop",function()
									RegistryWnd:StopMovingOrSizing()
									Registry:SetWidth(RegistryWnd:GetWidth()-(384-295))
									ScrollFrame:SetWidth(RegistryWnd:GetWidth()-89)
									ScrollFrame:SetHeight(RegistryWnd:GetHeight()-160)
									end);

RegistryWnd:SetScript("OnDragStart",function()RegistryWnd:StartMoving()end); -- draggin method 1
RegistryWnd:SetScript("OnDragStop",function()RegistryWnd:StopMovingOrSizing()end); -- draggin method 2


 local elementSelector = CreateFrame("Button","ElementSelector",RegistryWnd,"UIDropDownMenuTemplate"); -- Elementselection Combobox 
 elementSelector:SetPoint("TOPLEFT",RegistryWnd,"TOPLEFT",56,-41);
 elementSelector:SetParent("RegistryWnd");
 UIDropDownMenu_SetWidth(elementSelector, 128);
 UIDropDownMenu_Initialize(elementSelector, function() -- it works!
   for i,v in pairs(crt[crt_activeProfile]) do
    if i ~= "ProfileName" then
  level = 1;
  local info = UIDropDownMenu_CreateInfo();
    info.text = v["elementName"]; 
    info.value = i; 
    info.owner = getglobal(elementSelector); 
	
    info.func = function(this) selectForDelete = v["elementName"]; UIDropDownMenu_SetSelectedValue(elementSelector, this.value); crc_GetManyAttributes(i, crt[crt_activeProfile]) end;
    info.checked = nil; 
    info.icon = nil; 
    UIDropDownMenu_AddButton(info, level);
 end
  end end);   

local AddBtn = CreateFrame("Button","RegistryAddBtn",RegistryWnd); -- new Element Button (plus button)
AddBtn:SetWidth(16);
AddBtn:SetHeight(16);
AddBtn:SetScript("OnClick",function()
						   StaticPopupDialogs["NewPanel"] = {
						    text = NEW_ELEMENT[GetLocale()];
							button1 = TEXT(ACCEPT),
							button2 = TEXT(CANCEL),
							OnAccept = function()
							 crt_newElement(getglobal(this:GetParent():GetName().."EditBox"):GetText());
						     end,
							OnCancel = function()
							 getglobal(this:GetParent():GetName().."EditBox"):SetText("");
							 end,
							timeout = 0,
							whileDead = 1,
							hideOnEscape = 1,
							hasEditBox = true
						   }
						   StaticPopup_Show("NewPanel");
                           end);
AddBtn:SetPoint("LEFT",elementSelector,"RIGHT",0,0);
AddBtn:SetDisabledTexture("interface\\buttons\\ui-plusbutton-disabled");
AddBtn:SetPushedTexture("interface\\buttons\\ui-plusbutton-down");
AddBtn:SetHighlightTexture("interface\\buttons\\ui-plusbutton-hilight");
AddBtn:SetNormalTexture("interface\\buttons\\ui-plusbutton-up");


local DelBtn = CreateFrame("Button","RegistryDelBtn",RegistryWnd); -- delete Element Button (minus button)
DelBtn:SetWidth(16);
DelBtn:SetHeight(16);
DelBtn:SetScript("OnClick",function()
                            DEFAULT_CHAT_FRAME:AddMessage(elementSelector);
						    if elementSelector ~= nil then
							 crt_deleteByName(selectForDelete);
							end
                           end);
DelBtn:SetPoint("LEFT",AddBtn,"RIGHT",0,0);
DelBtn:SetDisabledTexture("interface\\buttons\\ui-minusbutton-disabled");
DelBtn:SetPushedTexture("interface\\buttons\\ui-minusbutton-down");
DelBtn:SetHighlightTexture("interface\\buttons\\ui-plusbutton-hilight");
DelBtn:SetNormalTexture("interface\\buttons\\ui-minusbutton-up");
  
local NewBtn = CreateFrame("CheckButton","RegistryNewBtn",RegistryWnd); -- confusing name - it will expand the profilselector frame (crt_profilSelector.lua)
NewBtn:SetWidth(16);
NewBtn:SetHeight(16);
NewBtn:SetScript("OnClick",function()
                            cr_ProfileSelector()
							if this:GetButtonState() == "NORMAL" then -- it doesnt work :-(
							 this:SetButtonState("PUSHED");
							else
							 this:SetButtonState("NORMAL");
							end
                           end);
NewBtn:SetPoint("LEFT",DelBtn,"RIGHT",48,0);
NewBtn:SetDisabledTexture("interface\\buttons\\ui-plusbutton-disabled");
NewBtn:SetPushedTexture("interface\\buttons\\ui-attributebutton-encourage-down");
NewBtn:SetHighlightTexture("interface\\buttons\\ui-attributebutton-encourage-hilight");
NewBtn:SetNormalTexture("interface\\buttons\\ui-attributebutton-encourage-up");
local NewBtnLabel = NewBtn:CreateFontString("SelectProfileLabel","OVERLAY");
NewBtnLabel:SetWidth(48);
NewBtnLabel:SetHeight(17);
NewBtnLabel:SetPoint("RIGHT",NewBtn,"LEFT",0,0);
NewBtnLabel:SetFontObject("GameFontNormal");
NewBtnLabel:SetJustifyV("RIGHT");
NewBtnLabel:SetText(PROFILE_LABEL[GetLocale()]);
  
-- create control elements based on seeded Array
-- this is the funny part of this file!
for i,v in pairs(commandstructure) do
 local cmd = CreateFrame("Frame","cmd"..i,Registry);
 
 
 cmd:SetHeight(36);
 cmd:SetWidth(Registry:GetWidth());
 if i > 0 then
 cmd:SetPoint("TOPLEFT",getglobal("cmd"..i-1),"TOPLEFT",0,0); -- attach to last element
 else
 cmd:SetPoint("TOPLEFT",Registry,"TOPLEFT",0,0); -- attach to last element
 end
 cmd.parent = v["Parent"]; -- set Parenthesis
 if cmd.parent == nil then -- make it valid
  cmd.parent = "Registry";
 end
 if v["parenthesis"] == nil then v["parenthesis"] = tonumber(0) end
 if i>1 then
 cmd:SetPoint("TOPLEFT",getglobal("cmd"..i-1),"BOTTOMLEFT",12*tonumber(v["parenthesis"]),0); -- no child
 else
 cmd:SetPoint("TOPLEFT","Registry","TOPLEFT",12*tonumber(v["parenthesis"]),0); -- no child
 end
 local caption = cmd:CreateFontString("cmd"..i.."_description","OVERLAY");
 if v["type"] == nil then
   caption:SetFontObject("GameFontNormal");
 else
   caption:SetFontObject("GameFontHighlight");
 end 
 caption:SetText(v[GetLocale()]);
 if v["type"] == nil then -- just a header
  caption:SetWidth(Registry:GetWidth());
 else
  caption:SetWidth(192-(12*tonumber(v["parenthesis"])));
 end
 caption:SetHeight(24);
 caption:SetParent("cmd"..i);
 caption:SetJustifyH("LEFT");
 caption:SetJustifyV("CENTER");

 caption:SetPoint("LEFT","cmd"..i,"LEFT",0,0);
 
     if v["type"] == "string" then -- create string specfic controls (editbox)
	 
  local control = CreateFrame("Editbox","cmd"..i.."_control",parent,"InputBoxTemplate");
  control:SetParent("cmd"..i);
  control:SetHeight(24);
  control:SetPoint("LEFT","cmd"..i,"LEFT",128,0);
  control:SetPoint("RIGHT",Registry,"RIGHT",0,0);
  control:SetFontObject("GameFontHighlight");
  control:EnableMouse(true);
  control:SetText(tostring(v["DefT"]));
  control:SetAutoFocus(false);
  control:SetScript("OnEnterPressed",function() crt_setAttribute(crc_elementSelected, v["path"],control:GetText(),true) loadstring(v["Exec"]) this:ClearFocus(); end);
  
 elseif v["type"] == "integer" then -- create integer specific controls (editbox + slider)
  local control1 = CreateFrame("Editbox","cmd"..i.."_control1",parent,"InputBoxTemplate");
  control1:SetParent("cmd"..i);
  control1:SetHeight(24);
  control1:SetWidth(48);
  control1:SetPoint("LEFT","cmd"..i,"LEFT",128,0);
  control1:SetFontObject("GameFontHighlight");
  control1:SetAutoFocus(false);
  control1:SetText(v["DefT"]);
  local control2 = CreateFrame("Slider","cmd"..i.."_control2",parent,"OptionsSliderTemplate")
  control2:SetFrameLevel(2);
  control2:SetFrameStrata("DIALOG");
  control2:SetParent("cmd"..i);
  getglobal(control2:GetName().."Low"):SetText(v["min"]);
  getglobal(control2:GetName().."High"):SetText(v["max"]);
  control2:SetMinMaxValues(tonumber(v["min"]),tonumber(v["max"]));
  control2:SetHeight(18);
  control2:SetValue(tonumber(v["DefT"]));
  control2:SetPoint("TOPLEFT",control1,"TOPRIGHT",0,0);
  control2:SetPoint("RIGHT",Registry,"RIGHT",0,0);
  control2:SetScript("OnValueChanged",function() control1:SetText(math.floor(tonumber(control2:GetValue()) +0.5)); crt_setAttribute(crc_elementSelected, v["path"],control2:GetValue(),true) loadstring(v["Exec"]) end);
  control1:SetScript("OnEnterPressed",function()
  
                                      if tonumber(control1:GetText()) < tonumber(v["max"]) and tonumber(control1:GetText()) > tonumber(v["min"]) then 
									  control2:SetValue(tonumber(control1:GetText())); 
									  crt_setAttribute(crc_elementSelected, v["path"],control1:GetText(),true)
									  this:ClearFocus();
									  end
									  end
					);
 elseif v["type"] == "button" then -- create button to click
  local control = CreateFrame("Button","cmd"..i.."_control",parent,"UIPanelButtonTemplate")
  control:SetParent("cmd"..i);
  control:SetPoint("RIGHT","cmd"..i,"RIGHT",-24,0);
  control:SetHeight(24);
  control:SetWidth(96);
  control:SetText(v["DefT"])
  control:SetScript("OnClick",function()RunScript(v["Exec"])end);
 elseif v["type"] == "parserdropdown" then -- create a dropdown for parser selection
   local control = CreateFrame("Button", "cmd"..i.."_control", parent, "UIDropDownMenuTemplate");
  
  control:SetPoint("RIGHT","cmd"..i,"RIGHT",-24,0);
  control:SetParent(Registry);
  getglobal(control:GetName().."Button"):SetFrameLevel(5);
  UIDropDownMenu_SetWidth(control, Registry:GetWidth()-(128+48));
   UIDropDownMenu_Initialize(control, function() 
   for k,Value in pairs(info_) do
   if k ~= 1 then
  level = 1;
  local info = UIDropDownMenu_CreateInfo();
    k = string.gsub(k,"%%%[","[")
    k = string.gsub(k,"%(.-%)",":parameter")
    k = string.gsub(k,"%%%]","]")
    info.text = k;
    info.value = k; 
	info.owner = getglobal("cmd"..i.."_control"); 
	
    info.func = function() UIDropDownMenu_SetSelectedValue(this.owner, this.value); 
	            if getglobal("cmd"..v["href"].."_control"):GetObjectType() == "EditBox" then 
				getglobal("cmd"..v["href"].."_control"):Insert(UIDropDownMenu_GetSelectedValue(control))end info.checked = nil; end;
    info.checked = nil; 
    info.icon = nil; 
    UIDropDownMenu_AddButton(info, level);
	end
 end
  end);
  
 elseif v["type"] == "hexcolor" then -- create hexcolor combound
  local control1 = CreateFrame("EditBox","cmd"..i.."_control1",parent,"InputBoxTemplate")
  control1:SetParent("cmd"..i);
  control1:SetHeight(24);
  control1:SetWidth(96);
  control1:SetPoint("LEFT","cmd"..i,"LEFT",128,0);
  control1:SetFontObject("GameFontHighlight");
  control1:SetAutoFocus(false);
  control1:SetText(v["DefT"]);
  local control2 = CreateFrame("Button","cmd"..i.."_control2",parent,"UIPanelButtonTemplate");
  control2:SetHeight(24);
  control2:SetWidth(64);
  control2:SetParent("cmd"..i);
  control2:SetPoint("TOPLEFT",control1,"TOPRIGHT",0,0);
  control2:SetPoint("RIGHT",Registry,"RIGHT",0,0);
  control2:SetText(PICK_COLOR[GetLocale()]);
  control2:SetScript("OnClick",function()
                               if crc_elementSelected ~= nil then
								ColorPickerFrame:Show();
								ColorPickerFrame.func = function()
							   	                        local R,G,B = ColorPickerFrame:GetColorRGB();
														control1:SetText("[color:FF"..string.upper(RGBToHex(R*255,G*255,B*255)).."]");
														end
								ColorPickerFrame.cancelFunc = nil;
                               end
					end);							   
 elseif v["type"] == "fullcolor" then -- create colorpicker button
  local control = CreateFrame("Button","cmd"..i.."_control",parent,"UIPanelButtonTemplate");
  control:SetScript("OnClick",function()
                              if crc_elementSelected ~= nil then
                              ColorPickerFrame:Show();
							  ColorPickerFrame.hasOpacity = 1;
							  ColorPickerFrame.setOpacity = tonumber(crt_getAttribute(crc_elementSelected,v["array"]["pathA"], true));
							  ColorPickerFrame.previousValues = {tonumber(crt_getAttribute(crc_elementSelected,v["array"]["pathR"],true)),
							                                     tonumber(crt_getAttribute(crc_elementSelected,v["array"]["pathG"],true)),
																 tonumber(crt_getAttribute(crc_elementSelected,v["array"]["pathB"],true)),
																};
							  ColorPickerFrame:SetColorRGB(v["array"]["pathR"], v["array"]["pathG"], v["array"]["pathB"]);
                              ColorPickerFrame.func = function()
							                          local R,G,B = ColorPickerFrame:GetColorRGB();
													  local A = OpacitySliderFrame:GetValue();
													  crt_setAttribute(crc_elementSelected,v["array"]["pathR"],R,true);
													  crt_setAttribute(crc_elementSelected,v["array"]["pathG"],G,true);
													  crt_setAttribute(crc_elementSelected,v["array"]["pathB"],B,true);
													  crt_setAttribute(crc_elementSelected,v["array"]["pathA"],A,true);
							                          end;
                              ColorPickerFrame.cancelFunc = nil;
							  end
                    end);
  control:SetParent("cmd"..i);
  control:SetPoint("RIGHT","cmd"..i,"RIGHT",-24,0);
  control:SetHeight(24);
  control:SetWidth(96);
  control:SetText(PICK_COLOR[GetLocale()]);
 elseif v["type"] == "color" then -- create color specific controls (hex-editbox + colorpicker button)
  local control1 = CreateFrame("Editbox","cmd"..i.."_control1",parent,"InputBoxTemplate");
  control1:SetParent("cmd"..i);
  control1:SetHeight(24);
  control1:SetWidth(48);
  control1:SetPoint("LEFT","cmd"..i,"LEFT",128,0);
  control1:SetFontObject("GameFontHighlight");
  control1:SetAutoFocus(false);
  control1:SetText(v["DefT"]);
  local control2 = CreateFrame("Slider","cmd"..i.."_control2",parent,"OptionsSliderTemplate")
  control2:SetFrameLevel(2);
  control2:SetFrameStrata("DIALOG");
  getglobal(control2:GetName().."Low"):SetText(v["min"]);
  getglobal(control2:GetName().."High"):SetText(v["max"]);
  control2:SetParent("cmd"..i);
  control2:SetMinMaxValues(tonumber(v["min"]),tonumber(v["max"]));
  control2:SetHeight(18);
  control2:SetValue(tonumber(v["DefT"]));
  control2:SetPoint("TOPLEFT",control1,"TOPRIGHT",0,0);
  control2:SetPoint("RIGHT",Registry,"RIGHT",0,0);
  control2:SetScript("OnValueChanged",function() control1:SetText(math.floor(tonumber(control2:GetValue())+0.5)); crt_setAttribute(crc_elementSelected, v["path"],control2:GetValue()/255,true) loadstring(v["Exec"]) end);
  control1:SetScript("OnEnterPressed",function()
  
                                      if tonumber(control1:GetText()) < tonumber(v["max"]) and tonumber(control1:GetText()) > tonumber(v["min"]) then 
									  control2:SetValue(tonumber(control1:GetText())); 
									  crt_setAttribute(crc_elementSelected, v["path"],tonumber(control1:GetText())/v["max"],true)
									  this:ClearFocus();
									  end
									  end
					); 
 elseif v["type"] == "select" then -- create selector specific controls (combobox with entries)
  local control = CreateFrame("Button", "cmd"..i.."_control", parent, "UIDropDownMenuTemplate");
  
  control:SetPoint("RIGHT","cmd"..i,"RIGHT",-24,0);
  control:SetParent(Registry);
  getglobal(control:GetName().."Button"):SetFrameLevel(5);
  UIDropDownMenu_SetWidth(control, Registry:GetWidth()-(128+48));
  local setuparray = v["array"];

  UIDropDownMenu_Initialize(control, function() 
   for k,Value in pairs(v["array"]) do
  level = 1;
  local info = UIDropDownMenu_CreateInfo();
    info.text = Value[GetLocale()]; 
    info.value = Value["value"]; 
	info.owner = getglobal("cmd"..i.."_control"); 
	
    info.func = function() UIDropDownMenu_SetSelectedValue(this.owner, this.value); crt_setAttribute(crc_elementSelected, v["path"],Value["value"],true) end;
    info.checked = nil; 
    info.icon = nil; 
    UIDropDownMenu_AddButton(info, level);
 end
  end);
 elseif v["type"] == "fontlister" then -- create a font listing based upon SharedMediaLib
  if SMLEnabled then
  local control = CreateFrame("Button", "cmd"..i.."_control", parent, "UIDropDownMenuTemplate");
  control:SetPoint("RIGHT","cmd"..i,"RIGHT",-24,0);
  control:SetParent(Registry);
  getglobal(control:GetName().."Button"):SetFrameLevel(5);
  UIDropDownMenu_SetWidth(control, Registry:GetWidth()-(128+48));
  UIDropDownMenu_Initialize(control, function() 
   for fontindex,fontfile in pairs(SML:List("font")) do
   local value = tostring(SML:Fetch("font",fontfile));
  level = 1;
  
  local info = UIDropDownMenu_CreateInfo();
    info.text = fontfile; 
	info.value = SML:Fetch("font",info.text);
	info.owner = getglobal("cmd"..i.."_control"); 
	
    info.func = function()
	 UIDropDownMenu_SetSelectedValue(this.owner, this.value); crt_setAttribute(crc_elementSelected, v["path"],UIDropDownMenu_GetSelectedValue(control),true) end;
    info.checked = nil; 
    info.icon = nil; 
    UIDropDownMenu_AddButton(info, level);
 end
  end);  
  else
   getglobal("cmd"..i).disabled = true;
   getglobal("cmd"..i):SetHeight(1);
   getglobal("cmd"..i):Hide();
  end
 elseif v["type"] == "boolean" then -- create boolean specific controls (checkbutton)
  local control = CreateFrame("Checkbutton","cmd"..i.."_control",parent,"UICheckButtonTemplate");
  control:SetParent("cmd"..i);
  
  control:SetHeight(24);
  control:SetWidth(24);
  control:SetPoint("LEFT","cmd"..i,"LEFT",128,0);
  control:EnableMouse(true);
  if v["DefT"] == "true" then control:SetChecked(true) else control:SetChecked(false); end
  control:SetScript("OnClick",function() crt_setAttribute(crc_elementSelected, v["path"],control:GetChecked(),true) loadstring(v["Exec"]) end);
 end
end 
 RegistryWnd:Hide();
end

function crc_toggle()
 if RegistryWnd:IsShown() == true then 
  RegistryWnd:Hide(); 
 else 
  if builded ~= true then
   crc_Builder();
   builded = true;
  end
  
  RegistryWnd:Show(); 
 end
end

RegistryWnd:Hide();