// Wildcard's aka Marc's comment:
// Weird looking code, i know...trust me..i have my reasons

#include ww2x\_drm;

init()
{
	level.voteMenuDel = [[level.drm_cvardef]]("wwx_vote_menu_delay", 0, 0, 9999, "int");

        level.voteMenusDel = level.voteMenuDel/60;

	level.voteLimit	= [[level.drm_cvardef]]("wwx_vote_limiter", 0, 0, 2, "int");

	if(level.voteLimit)
	{
		level.voteEnable	= [[level.drm_cvardef]]("wwx_vote_enable_time", 120, 0, 9999, "int");
		level.voteEnableMsg	= level.voteEnable/60;
		level.voteDisable	= [[level.drm_cvardef]]("wwx_vote_disable_time", 240, 0, 9999, "int");
		level.voteDisableMsg	= level.voteDisable/60;

		thread voteLimiter();
	}

	if(level.voteMenuDel)
	{
		level.voteMenuMsg = level.voteMenusDel;
                if(level.voteLimit == 2)
                thread voteMenuChk();
                else
		thread voteMenuTimer();
	}

}

voteMenuChk()
{
      level.voteMsg = [[level.drm_cvardef]]("wwx_vote_msg", 1, 0, 1, "int");

	if(level.voteMenuDel && !isDefined(level.voteTimerDone))
	{
		if(level.voteMenuMsg)
                {
                  if(level.voteMsg)
	          self iprintln(&"TEMPVOTE_DELAY", level.voteMenuMsg, &"TEMPVOTE_MINUTES");
               }
        }
}

voteMenuTimer()
{
      level.voteMsg = [[level.drm_cvardef]]("wwx_vote_msg", 1, 0, 1, "int");

	level.voteTimerDone = undefined;
        setCvar("g_allowvote", "0");

        if(level.voteLimit == 2)
         {      
           setCvar("g_allowvote", "1");
	   level.voteTimerDone = true;
         }
        else
	wait level.voteMenuDel;

           if(level.voteMsg)
           iprintln(&"TEMPVOTE_ENABLED", level.voteEnableMsg, &"TEMPVOTE_MINUTES");
             
        setCvar("g_allowvote", "1");
	level.voteTimerDone = true;

}

voteLimiter()
{
	thread setVote(true, true);

	if(level.voteMenuDel)
	{
		if(level.voteLimit == 1)
		{
			while (!isDefined(level.voteTimerDone))
				wait 1;
		}
		else
		{
			level.voteTimerDone = true;
		}
	}

	for (;;)
	{ 
		wait level.voteEnable;
		thread setVote(false, false);

		wait level.voteDisable;
	        thread setVote(true, false);

		wait 1;
	}
}

setVote(enable, init)
{
      level.voteMsg = [[level.drm_cvardef]]("wwx_vote_msg", 1, 0, 1, "int");

		if(enable)
			setCvar("g_allowvote", "1");
		else
			setCvar("g_allowvote", "0");
		wait 1;

	if(enable)
	{
		wait 1.5;

		if(level.voteEnableMsg && !init)
                     {
                       if(level.voteMsg)
		       iprintln(&"TEMPVOTE_ENABLED", level.voteEnableMsg, &"TEMPVOTE_MINUTES");
                     }  
	}
	else
	{
		wait 1.5;

	        if(level.voteDisableMsg)
                     {
                      if(level.voteMsg) 
		      iprintln(&"TEMPVOTE_DISABLED", level.voteDisableMsg, &"TEMPVOTE_MINUTES");
                     }
	}
}