var game = {}
game.init = function () {
    if (game.gamedata == null) return;

    $('#game').fadeIn(1000);
    $('#options').hide();
    $('#stats').show();

    game.teams = [];
    $(".team").each(function () {
        game.teams.push({
            "name": $(this).val(),
            "points": 0
        });
    });
    game.createScoreboard()
    game.current_points = 0;
}

game.createScoreboard = function () {
    var content = "<table cellspacing=10><tbody><tr>";
    for (var i = 0; i < game.teams.length; i++) {
        content += "<th><h3>" + game.teams[i]["name"] + "</h3></th>";
    }
    content += "</tr><tr>";
    for (var i = 0; i < game.teams.length; i++) {
        content += "<td><h3 id='team" + i + "'>0</h3><input class='add-points' onclick='game.addPoints(" + i + ")' value='+' type='button' /><input class='subtract-points' onclick='game.subtractPoints(" + i + ")' type='button' value='-' /></td>";
    }
    content += "</tr></tbody></table>";
    $('#stats').html(content);
}

game.addPoints = function (team) {
    game.updatePoints(team, game.current_points);
}

game.subtractPoints = function (team) {
    var points = 0 - game.current_points;
    game.updatePoints(team, points);
}

game.updatePoints = function (team, diff) {
    game.teams[team]['points'] += diff;

    $('#team' + team).html(game.teams[team]['points']);
    $('#' + game.current_questionID).addClass("dirty")
        .unbind('mouseover')
        .unbind('mouseout');

    game.current_points = 0;
}

game.loadFile = function (onload) {
    if (typeof window.FileReader !== 'function') {
        return;
    }

    var input = document.getElementById('fileinput');
    if (input && input.files && input.files[0]) {
        var fr = new FileReader();
        fr.onload = onload;
        fr.readAsText(input.files[0]);
    }
}

$(document).ready(function () {
    $('tbody tr td').each(function () {
        $(this).addClass('cell')
            .addClass('clean')
            .click(function () {
                prompt.show($(this));
            });
    });

    $('textarea.edit').focus(function () {
        $(this).addClass('active');
        var val = $(this).val();
        if (val == "Enter Category" || val == "Enter Title")
            this.select();
    }).blur(function () {
        $(this).removeClass('active');
    }).autogrow();

    $('.clean').mouseover(function () {
        $(this).addClass('ie-hack')
    }).mouseout(function () {
        $(this).removeClass('ie-hack')
    })

    $('#fileinput').change(function () {
        game.loadFile(function (e) {
            var data = $.parseJSON(e.target.result);
            $('#title').html(data["name"]);
            $('#gametitle').html(data["name"]);
			var i = 0;
            $('thead').find('th:not(#gametitle)').each(function () {
                var id = $(this).attr("id");
                var label = data["data"][i++]["name"];
                $(this).html(label)
            });
            game.gamedata = data;
        });
    });

    $("#add").click(function () {
        $("<input type=\"text\" name=\"team[]\" class=\"team\" />").appendTo("#teams");
    });
});

game.extractFromId = function(identifier, index) {
	var regexp = /c([0-9])r([0-9])/g;
	var retval = regexp.exec(identifier);
	return retval[index];
}

var prompt = {}
prompt.show = function (field) {
    game.current_points = parseInt(field.find('h3').text());
    game.current_questionID = field.attr("id");
    var column = game.extractFromId(field.attr("id"), 1) -1;
    var row = game.extractFromId(field.attr("id"), 2) -1;
    var dataset = game.gamedata["data"][column]["data"][row];

    $('#game').hide();

    if (dataset.daily) {
        if (game.points_bet) {
            game.current_points = parseInt(game.points_bet);
            game.points_bet = null;
        } else {
            daily.show(field);
            return;
        }
    }

    var question = prompt.loadImage(dataset['question']);
    var answer = prompt.loadImage(dataset['answer']);

    $('#question').hide();
    $('#prompt').fadeIn(1000);
    $('#answer').html(question);
    $('#question').html(answer);
    if ($('#question').html().length == 0)
        $('#correct-response').hide();
    else
        $('#correct-response').show();
}

prompt.loadImage = function (image) {
    var prefix = game.gamedata['directory'];
    if (image.substring(0, 6) == "image:") {
        return "<img src=\"data/" + prefix + "/" + image.substring(6) + "\">";
    }
    return image;
}

prompt.hide = function () {
    $('#prompt').hide();
    $('#game').show();
}

prompt.showQuestion = function () {
    $('#question').fadeIn(1000)
}

var daily = {}
daily.show = function (field) {
    $('#prompt').hide();
    $('#daily').show();

    $('#bet_submit').click(function () {
        game.points_bet = $("#bet").val();
        game.current_points = game.points_bet;
        prompt.show(field);
        $('#daily').hide();
    });
}
@@ -3789,21 +3789,26 @@
 if (
+%22daily%22 in 
 dataset
-.daily
 ) %7B%0D
@@ -5106,20 +5106,24 @@
 .hide();%0D%0A    %7D);%0D%0A%7D
+%0D%0A%0D%0A

improve code style.