var items =  [];
var found = 0;
var seconds = 0;
var allowInput = 0;
var foundItems = [];
var banana = 0;

var current_game = "";

var games = new Object();

games["shakespeare"] = [
"Name all of Shakespeare's plays in 5 minutes",
300,
[
["All's Well That Ends Well", 0],
["Antony and Cleopatra", 0],
["As You Like It", 0],
["The Comedy of Errors", 0],
["Coriolanus", 0],
["Cymbeline", 0],
["Hamlet", 0],
["Henry IV, Part I", 0],
["Henry IV, Part II", 0],
["Henry V", 0],
["Henry VI, Part I", 0],
["Henry VI, Part II", 0],
["Henry VI, Part III", 0],
["Henry VIII", 0],
["Julius Caesar", 0],
["King John", 0],
["King Lear", 0],
["Love's Labour's Lost", 0],
["Macbeth", 0],
["Measure for Measure", 0],
["Merchant of Venice", 0],
["Merry Wives of Windsor", 0],
["A Midsummer Night's Dream", 0],
["Much Ado about Nothing", 0],
["Othello", 0],
["Pericles", 0],
["Prince of Tyre", 0],
["Richard II", 0],
["Richard III", 0],
["Romeo and Juliet", 0],
["The Taming of the Shrew", 0],
["The Tempest", 0],
["Timon of Athens", 0],
["Titus Andronicus", 0],
["Troilus and Cressida", 0],
["Twelfth Night", 0],
["The Two Gentlemen of Verona", 0],
["The Two Noble Kinsmen", 0],
["The Winter's Tale", 0]
]];

games["amino"] = [
"Name the 20 Amino Acids in 3 minutes",
180,
[
["Alanine", 0],
["Arginine", 0],
["Asparagine", 0],
["Aspartic acid", 0],
["Cysteine", 0],
["Glutamic acid", 0],
["Glutamine", 0],
["Glycine", 0],
["Histidine", 0],
["Isoleucine", 0],
["Leucine", 0],
["Lysine", 0],
["Methionine", 0],
["Phenylalanine", 0],
["Proline", 0],
["Serine", 0],
["Threonine", 0],
["Tryptophan", 0],
["Tyrosine", 0],
["Valine", 0]
]];

games["c++"] = [
"Name 80 C++ Reserved words in 5 minutes",
300,
[
    ["abstract", 0],
    ["auto", 0],
    ["bool", 0],
    ["break", 0],
    ["case", 0],
    ["catch", 0],
    ["char", 0],
    ["class", 0],
    ["const", 0],
    ["const_cast", 0],
    ["continue", 0],
    ["default", 0],
    ["delegate", 0],
    ["delete", 0],
    ["do", 0],
    ["double", 0],
    ["dynamic_cast", 0],
    ["each", 0],
    ["else", 0],
    ["enum", 0],
    ["explicit", 0],
    ["extern", 0],
    ["event", 0],
    ["false", 0],
    ["finally", 0],
    ["float", 0],
    ["for", 0],
    ["friend", 0],
    ["gcnew", 0],
    ["generic", 0],
    ["goto", 0],
    ["if", 0],
    ["in", 0],
    ["inline", 0],
    ["int", 0],
    ["internal", 0],
    ["initonly", 0],
    ["interface", 0],
    ["literal", 0],
    ["long", 0],
    ["mutable", 0],
    ["namespace", 0],
    ["new", 0],
    ["nullptr", 0],
    ["operator", 0],
    ["override", 0],
    ["private", 0],
    ["property", 0],
    ["protected", 0],
    ["public", 0],
    ["ref", 0],
    ["register", 0],
    ["reinterpret_cast", 0],
    ["return", 0],
    ["sealed", 0],
    ["signed", 0],
    ["sizeof", 0],
    ["short", 0],
    ["static", 0],
    ["static_cast", 0],
    ["struct", 0],
    ["switch", 0],
    ["template", 0],
    ["this", 0],
    ["true", 0],
    ["try", 0],
    ["typedef", 0],
    ["typeid", 0],
    ["throw", 0],
    ["typename", 0],
    ["union", 0],
    ["unsigned", 0],
    ["using", 0],
    ["value", 0],
    ["virtual", 0],
    ["void", 0],
    ["volatile", 0],
    ["wchar_t", 0],
    ["while", 0],
    ["where", 0] 	 	
]];

games["countries"] = [
"Name all 194 UN-recognized countries in 10 minutes",
600,
[
    ["Afghanistan", 0],
    ["Albania", 0],
    ["Algeria", 0],
    ["Andorra", 0],
    ["Angola", 0],
    ["Antigua and Barbuda", 0],
    ["Argentina", 0],
    ["Armenia", 0],
    ["Australia", 0],
    ["Austria", 0],
    ["Azerbaijan", 0],
    ["Bahamas", 0],
    ["Bahrain", 0],
    ["Bangladesh", 0],
    ["Barbados", 0],
    ["Belarus", 0],
    ["Belgium", 0],
    ["Belize", 0],
    ["Benin", 0],
    ["Bhutan", 0],
    ["Bolivia", 0],
    ["Bosnia and Herzegovina", 0],
    ["Botswana", 0],
    ["Brunei", 0],
    ["Bulgaria", 0],
    ["Burkina Faso", 0],
    ["Burundi", 0],
    ["Brazil", 0],
    ["Cambodia", 0],
    ["Cameroon", 0],
    ["Canada", 0],
    ["Cape Verde", 0],
    ["Central African Republic", 0],
    ["Chad", 0],
    ["Chile", 0],
    ["China", 0], 
    ["Colombia", 0],
    ["Comoros", 0],
    ["Costa Rica", 0],
    ["Croatia", 0],
    ["Cuba", 0],
    ["Cyprus", 0],
    ["Czech Republic", 0],
    ["Democratic Republic of the Congo", 0],
    ["Denmark", 0],
    ["Djibouti", 0],
    ["Dominica", 0],
    ["Dominican Republic", 0],
    ["East Timor", 0],
    ["Ecuador", 0],
    ["Egypt", 0],
    ["El Salvador", 0],
    ["Equatorial Guinea", 0],
    ["Eritrea", 0],
    ["Estonia", 0],
    ["Ethiopia", 0],
    ["Fiji", 0],
    ["Finland", 0],
    ["France", 0],
    ["Gabon", 0],
    ["Gambia", 0],    
    ["Georgia", 0],
    ["Germany", 0],
    ["Ghana", 0],
    ["Greece", 0],
    ["Grenada", 0],
    ["Guatemala", 0],
    ["Guinea", 0],
    ["Guinea-Bissau", 0],
    ["Guyana", 0],
    ["Haiti", 0],
    ["Honduras", 0],
    ["Hungary", 0],
    ["Iceland", 0],
    ["India", 0],
    ["Indonesia", 0],
    ["Iran", 0],
    ["Iraq", 0],
    ["Ireland", 0],
    ["Israel", 0],
    ["Italy", 0],
    ["Ivory Coast", 0],
    ["Jamaica", 0],
    ["Japan", 0],
    ["Jordan", 0],
    ["Kazakhstan", 0],
    ["Kenya", 0],
    ["Kiribati", 0],
    ["Kosovo", 0],
    ["Kuwait", 0],
    ["Kyrgyzstan", 0],
    ["Laos", 0],
    ["Latvia", 0],
    ["Lebanon", 0],
    ["Lesotho", 0],
    ["Liberia", 0],
    ["Libya", 0],
    ["Liechtenstein", 0],
    ["Lithuania", 0],
    ["Luxembourg", 0],
    ["Macedonia", 0],
    ["Madagascar", 0],
    ["Malawi", 0],
    ["Malaysia", 0],
    ["Maldives", 0],
    ["Mali", 0],
    ["Malta", 0],
    ["Marshall Islands", 0],
    ["Mauritania", 0],
    ["Mauritius", 0],
    ["Mexico", 0],
    ["Micronesia", 0],
    ["Moldova", 0],
    ["Monaco", 0],
    ["Mongolia", 0],
    ["Montenegro", 0],
    ["Morocco", 0],
    ["Mozambique", 0],
    ["Myanmar", 0],
    ["Namibia", 0],
    ["Nauru", 0],
    ["Nepal", 0],
    ["Netherlands", 0],
    ["New Zealand", 0],
    ["Nicaragua", 0],
    ["Niger", 0],
    ["Nigeria", 0],
    ["North Korea", 0],
    ["Norway", 0],
    ["Oman", 0],
    ["Pakistan", 0],
    ["Palau", 0],
    ["Papua New Guinea", 0],
    ["Paraguay", 0],
    ["Panama", 0],
    ["Peru", 0],
    ["Philippines", 0],
    ["Poland", 0],
    ["Portugal", 0],
    ["Qatar", 0],
    ["Republic of the Congo", 0],
    ["Romania", 0],
    ["Russia", 0],
    ["Rwanda", 0],
    ["Saint Lucia", 0],
    ["Saint Kitts and Nevis", 0],
    ["Saint Vincent and the Grenadines", 0],
    ["Samoa", 0],
    ["San Marino", 0],
    ["Sao Tome and Principe", 0],
    ["Saudi Arabia", 0],
    ["Senegal", 0],
    ["Serbia", 0],
    ["Seychelles", 0],
    ["Sierra Leone", 0],
    ["Singapore", 0],
    ["Slovakia", 0],
    ["Slovenia", 0],
    ["Solomon Islands", 0],
    ["Somalia", 0],
    ["South Africa", 0],
    ["South Korea", 0],
    ["Southern Sudan", 0],
    ["Spain", 0],
    ["Sri Lanka", 0],
    ["Sudan", 0],
    ["Suriname", 0],
    ["Swaziland", 0],
    ["Sweden", 0],
    ["Switzerland", 0],
    ["Syria", 0],
    ["Tajikistan", 0],
    ["Tanzania", 0],
    ["Thailand", 0],
    ["Togo", 0],
    ["Tonga", 0],
    ["Trinidad and Tobago", 0],
    ["Tunisia", 0],
    ["Turkey", 0],
    ["Turkmenistan", 0],
    ["Tuvalu", 0],
    ["Uganda", 0],
    ["Ukraine", 0],
    ["United Arab Emirates", 0],
    ["United Kingdom", 0],
    ["United States", 0],
    ["Uruguay", 0],
    ["Uzbekistan", 0],
    ["Vanuatu", 0],
    ["Venezuela", 0],
    ["Vietnam", 0],
    ["Yemen", 0],
    ["Zambia", 0],
    ["Zimbabwe", 0]
]];

games["state_capitals"] = [
"Name all 50 US capitals in 5 minutes",
300,
[
    ["Atlanta", 0],
    ["Olympia", 0]
]];

games["states"] = [
"Name all 50 US states in 5 minutes",
300,
[
    ["Alabama", 0],
    ["Alaska", 0],
    ["Arizona", 0],
    ["Arkansas", 0],
    ["California", 0],
    ["Colorado", 0],
    ["Connecticut", 0],
    ["Delaware", 0],
    ["Florida", 0],
    ["Georgia", 0],
    ["Hawaii", 0],
    ["Idaho", 0],
    ["Illinois", 0],
    ["Indiana", 0],
    ["Iowa", 0],
    ["Kansas", 0],
    ["Kentucky", 0],
    ["Louisiana", 0],
    ["Maine", 0],
    ["Maryland", 0],
    ["Massachusetts", 0],
    ["Michigan", 0],
    ["Minnesota", 0],
    ["Mississippi", 0],
    ["Missouri", 0],
    ["Montana", 0],
    ["Nebraska", 0],
    ["Nevada", 0],
    ["New Hampshire", 0],
    ["New Jersey", 0],
    ["New Mexico", 0],
    ["New York", 0],
    ["North Carolina", 0],
    ["North Dakota", 0],
    ["Ohio", 0],
    ["Oklahoma", 0],
    ["Oregon", 0],
    ["Pennsylvania", 0],
    ["Rhode Island", 0],
    ["South Carolina", 0],
    ["South Dakota", 0],
    ["Tennessee", 0],
    ["Texas", 0],
    ["Utah", 0],
    ["Vermont", 0],
    ["Virginia", 0],
    ["Washington", 0],
    ["West Virginia", 0],
    ["Wisconsin", 0],
    ["Wyoming", 0]
]];

games["presidents"] = [
"Name all US Presidents (The FULL name he was given when he was born including \"Jr.\" if applicable) in 10 minutes",
600,
[
    ["George Washington", 0],
    ["John Adams Jr.", 0],
    ["Thomas Jefferson", 0],
    ["James Madison Jr.", 0],
    ["James Monroe", 0],
    ["John Quincy Adams", 0],
    ["Andrew Jackson", 0],
    ["Martin Van Buren", 0], 
    ["William Henry Harrison", 0],
    ["John Tyler Jr.", 0],
    ["James Knox Polk", 0],
    ["Zachary Taylor", 0],
    ["Millard Fillmore", 0],
    ["Franklin Pierce", 0],
    ["James Buchanan Jr.", 0],
    ["Abraham Lincoln", 0],
    ["Andrew Johnson", 0],
    ["Hiram Ulysses Grant", 0],
    ["Rutherford Birchard Hayes", 0],
    ["James Abram Garfield", 0],
    ["Chester Alan Arthur", 0],
    ["Stephen Grover Cleveland", 0],
    ["Benjamin Harrison", 0],
    ["William McKinley Jr.", 0],
    ["Theodore Roosevelt Jr.", 0],
    ["William Howard Taft", 0],
    ["Thomas Woodrow Wilson", 0],
    ["Warren Gamaliel Harding", 0],
    ["John Calvin Coolidge Jr.", 0],
    ["Herbert Clark Hoover", 0],
    ["Franklin Delano Roosevelt", 0],
    ["Harry S Truman", 0],
    ["Dwight David Eisenhower", 0],
    ["John Fitzgerald Kennedy", 0],
    ["Lyndon Baines Johnson", 0],
    ["Richard Milhous Nixon", 0],
    ["Gerald Rudolph Ford Jr.", 0],
    ["James Earl Carter Jr.", 0],
    ["Ronald Wilson Reagan", 0],
    ["George Herbert Walker Bush", 0],
    ["William Jefferson Clinton", 0],
    ["George Walker Bush", 0]
]];

games["elements"] = [
"Name the first 110 chemical elements in 10 minutes",
600,
[
	["Hydrogen", 0],
	["Helium", 0],
	["Lithium", 0],
	["Beryllium", 0],
	["Boron", 0],
	["Carbon", 0],
	["Nitrogen", 0],
	["Oxygen", 0],
	["Fluorine", 0],
	["Neon", 0],
	["Sodium", 0],
	["Magnesium", 0],
	["Aluminum", 0],
	["Silicon", 0],
	["Phosphorus", 0],
	["Sulfur", 0],
	["Chlorine", 0],
	["Argon", 0],
	["Potassium", 0],
	["Calcium", 0],
	["Scandium", 0],
	["Titanium", 0],
	["Vanadium", 0],
	["Chromium", 0],
	["Manganese", 0],
	["Iron", 0],
	["Cobalt", 0],
	["Nickel", 0],
	["Copper", 0],
	["Zinc", 0],
	["Gallium", 0],
	["Germanium", 0],
	["Arsenic", 0],
	["Selenium", 0],
	["Bromine", 0],
	["Krypton", 0],
	["Rubidium", 0],
	["Strontium", 0],
	["Yttrium", 0],
	["Zirconium", 0],
	["Niobium", 0],
	["Molybdenum", 0],
	["Technetium", 0],
	["Ruthenium", 0],
	["Rhodium", 0],
	["Palladium", 0],
	["Silver", 0],
	["Cadmium", 0],
	["Indium", 0],
	["Tin", 0],
	["Antimony", 0],
	["Tellurium", 0],
	["Iodine", 0],
	["Xenon", 0],
	["Cesium", 0],
	["Barium", 0],
	["Lanthanum", 0],
	["Cerium", 0],
	["Praseodymium", 0],
	["Neodymium", 0],
	["Promethium", 0],
	["Samarium", 0],
	["Europium", 0],
	["Gadolinium", 0],
	["Terbium", 0],
	["Dysprosium", 0],
	["Holmium", 0],
	["Erbium", 0],
	["Thulium", 0],
	["Ytterbium", 0],
	["Lutetium", 0],
	["Hafnium", 0],
	["Tantalum", 0],
	["Tungsten", 0],
	["Rhenium", 0],
	["Osmium", 0],
	["Iridium", 0],
	["Platinum", 0],
	["Gold", 0],
	["Mercury", 0],
	["Thallium", 0],
	["Lead", 0],
	["Bismuth", 0],
	["Polonium", 0],
	["Astatine", 0],
	["Radon", 0],
	["Francium", 0],
	["Radium", 0],
	["Actinium", 0],
	["Thorium", 0],
	["Protactinium", 0],
	["Uranium", 0],
	["Neptunium", 0],
	["Plutonium", 0],
	["Americium", 0],
	["Curium", 0],
	["Berkelium", 0],
	["Californium", 0],
	["Einsteinium", 0],
	["Fermium", 0],
	["Mendelevium", 0],
	["Nobelium", 0],
	["Lawrencium", 0],
	["Rutherfordium", 0],
	["Dubnium", 0],
	["Seaborgium", 0],
	["Bohrium", 0],
	["Hassium", 0],
	["Meitnerium", 0],
	["Darmstadtium", 0]
]];

String.prototype.trim = function ()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function setGame(gameID)
{
    banana++;
    current_game = gameID + banana;
    var game = games[gameID];
    foundItems = [];
    items = game[2];
    found = 0;
    seconds = game[1];
    allowInput = 1;
    
    for (var i = 0; i < items.length; ++i)
    {
        items[i][1] = 0;
    }
    document.getElementById("text_input").value = "";
    document.getElementById("so_far").innerHTML = "";
    document.getElementById("description").innerHTML = game[0];
    document.getElementById("remaining").innerHTML = "";
    window.setTimeout("play('" + gameID + banana + "')", 0);
}

function play(this_game)
{
    if (current_game != this_game) {
    //window.alert("works!");
    return;
    }
    
    if (allowInput == 0) return;
    
    var disp_minutes = Math.floor(seconds / 60);
    var disp_seconds = seconds % 60;
    if (seconds % 60 < 10) disp_seconds = "0" + disp_seconds;
    document.getElementById("timer").innerHTML = disp_minutes + ":" + disp_seconds;
    document.getElementById("num_left").innerHTML = (items.length - found) + " left";
    if (seconds == 0)
    {
        window.alert("Time's up! Let's see what you missed...");
        allowInput = 0;
        
        var left = "";
        var keke = 0;
        for (var i = 0; i < items.length; ++i)
        {
	    if (items[i][1] == 0)
	    {
		if (keke > 0) left += ", ";
		left += items[i][0];
		keke = 1;
	    }
        }
        document.getElementById("remaining").innerHTML = left;
        
    }
    else
    {
        --seconds;
        window.setTimeout("play('" + this_game + "')", 1000);
    }
}

function check_input()
{
    var check_this = document.getElementById("text_input").value.toLowerCase().trim();
    
    if (allowInput == 0)
    {
        return;
    }
    
    for (var i = 0; i < items.length; ++i)
    {
        if (items[i][1] == 0)
        {
            if (items[i][0].toLowerCase() == check_this)
            {
                items[i][1] = 1;
                ++found;
                foundItems[foundItems.length] = items[i][0];
                foundItems.sort();
                document.getElementById("so_far").innerHTML = foundItems.join(", ");
                document.getElementById("text_input").value = "";
                document.getElementById("num_left").innerHTML = (items.length - found) + " left";
                break;
            }
        }
    }
    
    if (found == items.length)
    {
        allowInput = 0;
        window.alert("You did it!");
    }
}

