Vote = {}
Vote.Voted = {}
for i=1, _MaxPlayers() do
	Vote.Voted[i] = false
end
Vote.VoteEnabled = false
Vote.VoteTopic = "New Admin!"
Vote.VoteSubTopic = "Vote for your new GMod9 admin!"
Vote.Choices = {
"All of these people are pricks!",
"Code (That guy who made the NoX logo)",
"Alaskanbullworm (Gimp)",
"Overload (That guy who made the FW map)",
"Justice In-A-Can (Gimp)",
"PainKiller (?)",
"Neko Mimi Mode (Hentai fag)",
"Kenny (This guy is stupid, trust me)",
"No_Eggs (Wears a hat or something)",
"jeremyy (Wastes money a lot)",
"Testament-Doom (Thinks he's good at racing)",
"Rambo_Sechs (Dumbshit)"
}

Vote.Vote = function(userid)
	if not Vote.VoteEnabled then _PrintMessage(userid, 3, "There is no poll going on right now.") return end
	if _file.Exists("noxiousprofiles/vote_"..Vote.VoteTopic.."_"..ConvertNet(_PlayerInfo(userid, "networkid"))..".txt") then _PrintMessage(userid, 3, "You have already voted.") return end
	local menu = JBMenu.CreateMenu(false, Vote.VoteTopic, Vote.VoteSubTopic, "255 255 255", "255 0 0")
	for i in Vote.Choices do
		JBMenu.AddChoice(menu, Vote.Choices[i], "", "255 255 255", Vote.SendVote, userid, Vote.Choices[i])
	end
	JBMenu.SendMenu(userid, menu)
end

Vote.CompileVotes = function(topic, delete)
	_PrintMessageAll(3, "Compiling all votes for topic: "..topic)
	local finalresults = {}
	local finalstring = topic..[[
]]
	local result = _file.Find( "noxiousprofiles/*.txt" )
	for i=1, table.getn(result) do
		if result[i] ~= "." and result[i] ~= ".." then
			if string.find(result[i], "vote_"..topic) ~= nil then
				if _file.Exists( "noxiousprofiles/"..result[i]) then
					local str = _file.Read("noxiousprofiles/"..result[i])
					local already = false
					for x=1, table.getn(finalresults) do
						if finalresults[x].name == str then
							finalresults[x].votes = finalresults[x].votes + 1
							already = true
						end
					end
					if not already then
							finalresults[table.getn(finalresults)+1] = {}
							finalresults[table.getn(finalresults)+1].name = str
							finalresults[table.getn(finalresults)+1].votes = 1
					end
					if delete then
						_file.Delete( "noxiousprofiles/"..result[i])
					end
				end
			end
		end
	end
	for y=1, table.getn(finalresults) do
		finalstring = finalstring..[[
]]..finalresults[y].name..[[: ]]..finalresults[y].votes
	end
	_file.Write("voteresults_"..topic..".txt", finalstring)
	_PrintMessageAll(3, "Done.")
end

if not Vote.VoteEnabled then return end

Vote.SendVote = function(userid, str)
	_file.Write("noxiousprofiles/vote_"..Vote.VoteTopic.."_"..ConvertNet(_PlayerInfo(userid, "networkid")..".txt"), str)
	_PrintMessage(userid, 3, "You have placed your vote.")
	Vote.Voted[userid] = true
end

Vote.Reminder = function()
	local str = "Server poll: "..Vote.VoteTopic..". Say 'vote' to vote on this."
	for i=1, _MaxPlayers() do
		if IsPlayerOnline(i) then
			if not Vote.Voted[i] then
				_PrintMessage(i, 3, str)
			end
		end
	end
end
Vote.Timer = AddTimer(75, 0, Vote.Reminder)
