function common.serverList(window)
	if window then
		window:clear()
		window:insert("Fetching server list...")
		
		fetch_server_list(function(list)
			window:clear()
			if list then
				for _, server in ipairs (list) do
					local n = window:insert(server.title, server.mod, server.map)
					n:data().ip = server.ip
				end
			else
				window:insert("Couldn't fetch list from server")
			end
		end)
	end
end



function common.initScoreboard()
	if not DEDSERV then
		gui_load_gss("scoreboard")
		local scoreboardw = gui_load_xml("scoreboard")
		
		scoreboardw:set_visibility(false)
		--scoreboardw:add_column("Name", 0.33)
		--scoreboardw:add_column("Kills", 0.33)
		--scoreboardw:add_column("Deaths", 0.33)
		
		local scoreFields = {}

		function addScoreField(name, size, func)
			scoreboardw:add_column(name, size)
			table.insert(scoreFields, func)
		end
		
		addScoreField("Name", 1, function(p)
			return p:name()
		end)
		
		addScoreField("Kills", 1, function(p)
			return p:kills()
		end)
		
		addScoreField("Deaths", 1, function(p)
			return p:deaths()
		end)
		
		function fillScoreboard(p)
			local l = {}
			for i, v in ipairs(scoreFields) do
				l[i] = v(p)
			end
			local item = scoreboardw:insert(unpack(l))
			item:data().p = p
		end
		
		function scoreboardComparer(a, b)
			return a:data().p:kills() > b:data().p:kills() 
		end
				
		console_register_command("+SCORES", function()
			scoreboardw:clear()
			for p in game_players() do
				--scoreboardw:insert(p:name(), p:kills(), p:deaths())
				fillScoreboard(p)
			end
			
			--scoreboardw:sort(scoreboardSortField)
			scoreboardw:sort(scoreboardComparer)
			
			scoreboardw:set_visibility(true)
		end)
		
		console_register_command("-SCORES", function()
			scoreboardw:set_visibility(false)
		end)
	end
end

function common.initChat()
	if not DEDSERV then
		gui_load_gss("chat")
		local chatboxw = gui_load_xml("chat")
		
		chatboxw:set_visibility(false)
			
		function chatboxw:onKeyDown(k)
			if k == Keys.ESC then
				self:set_visibility(false)
				return true
			end
		end
		
		local chattextw = chatboxw:child("chattext")
		
		function chattextw:onAction()
			local p = game_local_player(0)
			if p then
				p:say(self:get_text())
				chatboxw:set_visibility(false)
			end
		end
		
		console_register_command("SHOWCHAT", function()
			if game_local_player(0) and not mainm.isShown() then
				chatboxw:set_visibility(true)
				local w = chatboxw:child("chattext")
				w:set_text("")
				w:focus()
				w:activate()
				clear_keybuf()
			end
		end)
	end
end

function common.initHUD(options)
	if not DEDSERV then
		if not options then
			options = {}
		end
		
		local testCrosshair = sprites_load("crosshair")
		local killsMeter = sprites_load("kills")

		local hudFont = fonts.liero

		function bindings.wormRender(x, y, worm, viewport, ownerPlayer)

			local ownViewport = ( ownerPlayer == worm:get_player() )

			local bitmap = viewport:get_bitmap()

			if not ownViewport then
				if not options.hideEnemyHealth then
					gfx_set_alpha(90)
					gfx_draw_box(bitmap, x - 6, y - 17, x + 6, y - 16, 255, 255, 255)
					gfx_reset_blending()
					local filled = (worm:get_health() / 100)
					gfx_draw_box(bitmap, x - 6, y - 17, x - 6 + filled * 12, y - 16, hudtools.greenRedInterpol(1-filled))
				end
				
				if not options.hideNames then
					fonts.minifont:render( bitmap, worm:get_player():name(), x, y - 10, 255, 255, 255, Font.Formatting + Font.CenterH )
					--fonts.minifont:render( bitmap, worm:get_player():team(), x, y - 30, 0, 255, 0, Font.CenterH )
				end
				
			else
				local dx, dy = angle_vector(worm:get_angle(), 25)
				testCrosshair:render(bitmap, 0, x + dx, y + dy)
			end
		end
		
		function bindings.viewportRender(viewport, worm)
			local bitmap = viewport:get_bitmap()
			local filled = (worm:get_health() / 100)
			local limit = 200-filled*2

			killsMeter:render(bitmap, 0, bitmap:w()-140, 6)
			fonts.minifont:render(bitmap, worm:get_player():kills(), bitmap:w()-135, 4, 255, 255, 255, 0)
			
			if filled <= 0 then
				hudFont:render( bitmap, "You are dead... Press [JUMP] to respawn!", bitmap:w()/2, 12, 255, 255, 255, Font.Shadow + Font.CenterH )
			end

			hudtools.drawHBar( bitmap, filled, bitmap:w()-54, 5, 50, 4 )
			
			local w = worm:current_weapon()
			if w:is_reloading() then
				filled = 1 - w:reload_time() / w:type():reload_time()
			else
				filled = w:ammo() / w:type():ammo()
			end
			hudtools.drawHBar( bitmap, filled, bitmap:w()-109, 5, 50, 4 )
		end
	end
end

function common.initWeaponSelection()
	if not DEDSERV then
		local maxSelectableWeapons = 5
		local selectWeaponStart = maxSelectableWeapons+1
		local selectWeaponCount = selectWeaponStart+1
		
		function bindings.viewportRender(viewport, worm)
			local bitmap = viewport:get_bitmap()
			local player = worm:get_player()
			if player and player:data().weaponSelection then
				local o = player:data().weaponSelection
				local y = (bitmap:h() - selectWeaponCount*10) / 2
				local x = (bitmap:w() - 60) / 2
				local i = 0
				
				local function drawRow(name)
					if i == o.cur then
						fonts.liero:render(
							bitmap,
							name,
							x, y,
							50, 190, 50, Font.Shadow)
					else
						fonts.liero:render(
							bitmap,
							name,
							x, y,
							255, 255, 255, Font.Shadow)
					end
					y = y + 10
					i = i + 1
				end
				
				drawRow("Randomize")
				
				while i <= maxSelectableWeapons do
					drawRow(o.list[i]:name())
				end
				
				drawRow("Start!")
			end
		end
		
		local function randomWeapons()
			local list = {}
			
			for i = 1, maxSelectableWeapons do
				local ok
				
				repeat
					list[i] = weapon_random()
					
					if weapon_count() < maxSelectableWeapons then
						break
					end
					
					ok = true
					for j = 1, i-1 do
						if list[j] == list[i] then
							ok = false
						end
					end
				until ok
			end
			
			return list
		end
		
		function bindings.localplayerDown(player, state)
			local o = player:data().weaponSelection
			if o and state then
				o.cur = o.cur + 1
				if o.cur >= selectWeaponCount then
					o.cur = 0
				end
			end
						
			if o then
				return true
			end
		end
		
		function bindings.localplayerUp(player, state)
			local o = player:data().weaponSelection
			if o and state then
				o.cur = o.cur - 1
				if o.cur < 0 then
					o.cur = selectWeaponCount - 1
				end
			end
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerLeft(player, state)
			local o = player:data().weaponSelection
			if o and state and o.list[o.cur] then
				o.list[o.cur] = o.list[o.cur]:prev()
			end
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerRight(player, state)
			local o = player:data().weaponSelection
			if o and state and o.list[o.cur] then
				o.list[o.cur] = o.list[o.cur]:next()
			end
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerFire(player, state)
			local o = player:data().weaponSelection
			if o and state then
				if o.cur == 0 then
					o.list = randomWeapons()
				elseif o.cur == selectWeaponStart then
					player:select_weapons(o.list)
					player:data().weaponSelection = nil
				end
			end
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerJump(player, state)
			local o = player:data().weaponSelection
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerChange(player, state)
			local o = player:data().weaponSelection
			
			if o then
				return true
			end
		end
		
		function bindings.localplayerInit(player)
			player:data().weaponSelection =
			{
				list = randomWeapons(),
				cur = 1
			}
		end
	end
end

function common.init(options)
	common.initHUD(options)
	common.initChat(options)
	common.initScoreboard(options)
	common.initWeaponSelection(options)
	
	console_register_command("SHOWMENU", function()
		mainm.show()
	end)

	if not DEDSERV then
		mainm.init()
	end

end