local gui_button = 1;
local gui_list = 2;
local gui_slider = 3;
local gui_checkbox = 4;
local gui_edit = 5;
local gui_menu = 6;
local gui_text = 7;

function clone(t)            -- return a copy of the table t
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    if type(v)=="table" then v=clone(v) end;
	new[i] = v;
    i, v = next(t, i);       -- get next index
  end
  return new;
end

function update_table( old_table, new_table )	-- copy (and owerwrite)  all fields in new_table to old_table
  local i, v = next(new_table, nil);
  while i do
	old_table[i] = v;
    i, v = next(new_table, i);       -- get next index
  end
end

function	FXButton( data )

	if GUI == nil then
		GUI = {};
	end
	
	
	if type(data.name) ~= "string" then
		error("button name not specified");
	end
	
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_button then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy button [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_button;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;
	
end

function	List( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("list name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_list then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy list [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_list;
		GUI[data.name] = data;
	end

	-- returning name for using in menu
	return data.name;	
	
end

function	Slider( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("slider name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_slider then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy slider [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_slider;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;
	
end

function	CheckBox( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("checkbox name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_checkbox then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy checkbox [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_checkbox;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;
	
end

function	Edit( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("edit name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_edit then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy edit [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_edit;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;
	
end


function	Text( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("text name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_text then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy text [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_text;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;	
	
end


function	Menu( data )
	
	if GUI == nil then
		GUI = {};
	end
	
	if type(data.name) ~= "string" then
		error("menu name not specified");
	end
	
	if type(data.copy_from) == "string" then
		if GUI[data.copy_from] ~= nil and GUI[data.copy_from].type == gui_menu then
			GUI[data.name] = clone(GUI[data.copy_from]);
			update_table( GUI[data.name], data );
		else
			error( "cannot copy menu [" .. data.copy_from .. "]" );
		end
	else
		data.type = gui_menu;
		GUI[data.name] = data;
	end
	
	-- returning name for using in menu
	return data.name;	
	
end
