﻿/// <reference path="jquery-1.7.1.min.js" />
/// <reference path="jquery.jcountdown1.3.3.min.js" />
/// <reference path="jquery-ui-1.8.11.min.js" />

var blinds = new Array();
var currentRound = 0;
var minsLeft = 0;
var secondsLeft = 0;

$(function () {

    $("#start").click(function () {

        var minutes = parseInt($("#minutes").val());

        //get the blinds
        $(".blindRow").each(function (index) {
            blinds[index] = $(".smallB", $(this)).val() + ":" + $(".largeB", $(this)).val();
        });

        startClock(minutes);
        $("#pause").css("display", "inline");
        $("#start").css("display", "none");
    });

    $("#pause").toggle(function () {
        $("#time").countdown("pause");
        $(this).html("Start");
    },
    function () {
        $("#time").countdown("resume");
        $(this).html("Pause");
    });


    $("#go").click(function () {

        var count = parseInt($("#rounds").val());

        $("#step2, #step2Ready").css("display", "inline");

        var table = $(document.createElement("table"));
        table.attr("cellpadding", "2");
        table.attr("cellspacing", "2");
        table.append("<tr><td style='text-align:left;'>Round #</td><td style='text-align:left;'>Small Blind</td><td style='text-align:left;'>Big Blind</td></tr>");

        var smallBlind = 25;
        var bigBlind = 50;

        for (var i = 0; i < count; i++) {
            var tr = $(document.createElement("tr"));
            var rowTd = $(document.createElement("td"));
            var smallTd = $(document.createElement("td"));
            var largeTd = $(document.createElement("td"));
            var smallInput = $(document.createElement("input"));
            var largeInput = $(document.createElement("input"));

            tr.attr("class", "blindRow");

            smallInput.attr("type", "text");
            largeInput.attr("type", "text");

            smallInput.attr("id", "small" + i);
            smallInput.attr("class", "smallB");
            largeInput.attr("id", "large" + i);
            largeInput.attr("class", "largeB");
            smallInput.val(smallBlind);
            largeInput.val(bigBlind);

            if (smallBlind == 400) {
                smallBlind = 500;
                bigBlind = 1000;
            }
            else if (smallBlind == 4000) {
                smallBlind = 5000;
                bigBlind = 10000;
            }
            else if (smallBlind == 40000) {
                smallBlind = 50000;
                bigBlind = 100000;
            }
            else {
                smallBlind = bigBlind;
                bigBlind = bigBlind * 2;
                
            }

            smallTd.append(smallInput);
            largeTd.append(largeInput);

            rowTd.html(i + 1);

            tr.append(rowTd);
            tr.append(smallTd);
            tr.append(largeTd);
            table.append(tr);
        }

        $("#blinds").append(table);

        $("#ready").css("display", "inline");
        $("#go1").css("display", "none");
    });

    $("#ready").click(function () {
        $("#time").html($("#minutes").val() + ":00");
        $("#blindDisplay").html("Blinds: " + $("#small0").val() + " - " + $("#large0").val());
        $("#roundNumber").html("Round: 1");
        $("#game").css("display", "block");
        $("#setup").css("display", "none");
    });
});

function startClock(minutes) {   

    var T1 = new Date();
    var T2 = new Date(T1);
    T2.setMinutes(T1.getMinutes() + minutes);
    T2.setMilliseconds(T1.getMilliseconds() + 100);        

    var parts = blinds[currentRound].toString().split(':');
    $("#blindDisplay").html("Blinds: " + parts[0] + " - " + parts[1]);
    currentRound++;

    $("#time").countdown({
        "date": T2,
        "htmlTemplate": "%{m}:%{s}",
        "leadingZero": true,
        "onComplete": function (event) {
            $(this).countdown("destroy");
            $("#game, #time, #blindDisplay, #controls, #pause, #roundNumber").effect("highlight", { "color": "#fff" }, 1500);
            $("#roundNumber").html("Round: " + (currentRound + 1));
            var sound = document.getElementById("endOfRound");
            sound.Play();
            startClock(minutes);
        },
        "onChange": function () {
            if ($(this).countdown("getSettings", "minsLeft") == 1 && $(this).countdown("getSettings", "secLeft") == 0) {
                $("#game, #time, #blindDisplay, #controls, #pause, #roundNumber").effect("highlight", { "color": "#505050" }, 1500);
            }
        },
        "onPause": function () {
            minsLeft = $(this).countdown("getSettings", "minsLeft");
            secondsLeft = $(this).countdown("getSettings", "secLeft");
        },
        "onResume": function () {
            var d1 = new Date();
            var d2 = new Date(d1);
            d2.setMinutes(d1.getMinutes() + minsLeft);
            d2.setSeconds(d1.getSeconds() + secondsLeft);
            d2.setMilliseconds(d1.getMilliseconds() + 100);
            $(this).countdown("changeSettings", { "date": d2 });
        }
    });
}
