good="Good";
evil="Evil";
neutral="Neutral";

function roleToString() {
	return this.name;
}

function Role(name) {
	this.name=name;
	this.align=null;
	this.exec=null;
	//edible = able to be chosen by wolf
	this.edible=true;
	this.choose=null;
	this.toString=roleToString;
	this.detectsAsWerewolf=false;
	this.protectable=true;
}

function villagerFunc() {
	setInfo("<p>You're a <b>villager</b>!</p>"
		+ "<p>You win if all the werewolves die.</p>");
	setButton("End Turn");
	alert("Click here to look busy and make it less obvious you don't have anything to do at night.");
}
villager=new Role("Villager");
villager.align=good;
villager.exec=villagerFunc;

function werewolfFunc() {
	players[currentPlayer].chosen=-1;
	wolves=new Array();
	wolfText="<p>You're a <b>werewolf</b>!</p>"
		+ "<p>You win if the number of villagers equals the number of werewolves</p>";
	candidates=new Array();
	for (i=0; i<players.length; i++) {
		person=players[i];
		if (person.role.name=="Werewolf" && person.alive)
			wolves[wolves.length]=person.name;
		if (person.role.edible && person.alive)
			candidates[candidates.length]=i;
	}
	if (wolves.length>1)
		wolfText+="<p>Active werewolves: "+wolves.toString()+"</p>"
			+ "<p>Only one werewolf, chosen at random, will succeed in killing someone tonight</p>";
	else
		wolfText+="<p>You're the only active werewolf.</p>";
	wolfText+="</p>Choose someone below to kill.</p>";
	setInfo(wolfText);
	setCandidates(candidates);
	setButton("End Turn");
}
function werewolfChoose(chosen) {
	players[currentPlayer].chosen=chosen;
	n=players[chosen];
	clearCandidates(n+" chosen to be killed!");
}
werewolf=new Role("Werewolf");
werewolf.align=evil;
werewolf.edible=false;
werewolf.detectsAsWerewolf=true;
werewolf.exec=werewolfFunc;
werewolf.choose=werewolfChoose;

function seerFunc() {
	toWrite="<p>You're a <b>seer</b>!</p>"
		+ "<p>You win if all the werewolves die.</p>"
		+ "<p>";
	k=players[currentPlayer].chosen;
	players[currentPlayer].priorChosen[k]=true;
	players[currentPlayer].chosen=-1;
	candidates=new Array();
	for (i=0; i<players.length; i++) {
		person=players[i];
		if (person.alive && i!=currentPlayer) {
			if (players[currentPlayer].priorChosen[i]) {
				toWrite+="<b>"+person.name+"</b> ";
				if (person.role.detectsAsWerewolf)
					toWrite+="IS a werewolf.";
				else
					toWrite+="is NOT a werewolf.";
				toWrite+="<br>";
			}
			else
				candidates[candidates.length]=i;
		}
	}
	toWrite+="</p>";
	if (candidates.length>0) {
		toWrite+="<p>Choose a player to find out if that player is a werewolf!</p>";
		setCandidates(candidates);
	} else
		clearCandidates();

	setInfo(toWrite);
	setButton("End Turn");
}
function seerChoose(chosen) {
	players[currentPlayer].chosen=chosen;
	n=players[chosen].name;
	if (players[chosen].role.detectsAsWerewolf)
		clearCandidates("<b>Yes!</b> "+n+" IS a werewolf");
	else
		clearCandidates("<b>No!</b> "+n+" is NOT a werewolf");
}
seer=new Role("Seer");
seer.align=good;
seer.exec=seerFunc;
seer.choose=seerChoose;

function twinFunc() {
	otherTwin="Your twin is dead!";
	for (i=0; i<players.length; i++)
		if (players[i].role==twin && i!=currentPlayer)
			otherTwin="Your twin is <b>"+players[i]+"</b> (He or she can't be a werewolf)";
	setInfo("<p>You're a <b>twin</b>!</p>"
		+ "<p>"+otherTwin+"</p>"
		+ "<p>You win if all the werewolves die.</p>");
	setButton("End Turn");
	alert("Click here to look busy and make it less obvious you don't have anything to do at night.");
}
twin=new Role("Twin");
twin.align=good;
twin.exec=twinFunc;

function lycanFunc() {
	setInfo("<p>You're a <b>lycan</b>!</p>"
		+ "<p>Even though you're a villager, the seer thinks you're a werewolf.</p>"
		+ "<p>You win if all the werewolves die.</p>");
	setButton("End Turn");
	alert("Click here to look busy and make it less obvious you don't have anything to do at night.");
}
lycan=new Role("Lycan");
lycan.align=good;
lycan.detectsAsWerewolf=true;
lycan.exec=lycanFunc;

function sorcererFunc() {
	toWrite="<p>You're a <b>sorcerer</b>!</p>"
		+ "<p>You win if the number of villagers equals the number of werewolves (you don't count as either a werewolf or a villager for purposes of victory).</p>"
		+ "<p>";
	k=players[currentPlayer].chosen;
	players[currentPlayer].priorChosen[k]=true;
	players[currentPlayer].chosen=-1;
	candidates=new Array();
	for (i=0; i<players.length; i++) {
		person=players[i];
		if (person.alive && i!=currentPlayer) {
			if (players[currentPlayer].priorChosen[i]) {
				toWrite+="<b>"+person.name+"</b> ";
				if (person.role==seer)
					toWrite+="IS a seer.";
				else
					toWrite+="is NOT a seer.";
				toWrite+="<br>";
			}
			else
				candidates[candidates.length]=i;
		}
	}
	toWrite+="</p>";
	if (candidates.length>0) {
		toWrite+="<p>Choose a player to find out if that player is a seer!</p>";
		setCandidates(candidates);
	} else
		clearCandidates();

	setInfo(toWrite);
	setButton("End Turn");
}
function sorcererChoose(chosen) {
	players[currentPlayer].chosen=chosen;
	n=players[chosen].name;
	if (players[chosen].role==seer)
		clearCandidates("<b>Yes!</b> "+n+" IS a seer");
	else
		clearCandidates("<b>No!</b> "+n+" is NOT a seer");
}
sorcerer=new Role("Sorcerer");
sorcerer.align=evil;
sorcerer.exec=sorcererFunc;
sorcerer.choose=sorcererChoose;

function protectorFunc() {
	players[currentPlayer].chosen=-1;
	wolves=new Array();
	toWrite="<p>You're a <b>protector</b>!</p>"
		+ "<p>You win if all the werewolves die.</p>";
	candidates=new Array();
	for (i=0; i<players.length; i++) {
		person=players[i];
		if (person.role.protectable && person.alive)
			candidates[candidates.length]=i;
	}
	if (candidates.length>0)
		toWrite+="</p>Choose someone to protect. They cannot be killed tonight, though they can still be lynched during the day.</p>";
	setInfo(toWrite);
	setCandidates(candidates);
	setButton("End Turn");
}
function protectorChoose(chosen) {
	players[currentPlayer].chosen=chosen;
	n=players[chosen];
	players[chosen].invincible=true;
	clearCandidates(n+" chosen to be protected!");
}
protector=new Role("Protector");
protector.align=good;
protector.protectable=false;
protector.exec=protectorFunc;
protector.choose=protectorChoose;