﻿$(function () {

    var initSilentAuctionButtons = function () {
        // Submit via AJAX the PrizeEntry counts the user has entered 
        // But delay a number of seconds, so they may press the +/- button a few times before final submission. ie: 'debounce' the submission
        $('#update-prize-entry-form').live('submit', function (e) {
            var form = $(this);

            $.doTimeout("prize-entry-form", 1000, function () {
                $.post(form.attr('action'), form.serialize());
            });

            return false;
        });

        $('button.subtract-entry').click(function (e) {
            var prizeID = $(this).attr("id").replace("subtract-entry-", "");
            var entryCountField = $('#entry-count-' + prizeID);
            var entryCount = $(entryCountField).val();
            var scoreField = $('.scoreboard-text');
            var score = parseInt($(scoreField).text());

            if (entryCount > 0) {
                entryCount--;
                $(entryCountField).val(entryCount);
            }

            updatePrizeEntryScoreboard();
        });

        $('button.add-entry').click(function (e) {
            var prizeID = $(this).attr("id").replace("add-entry-", "");
            var entryCountField = $('#entry-count-' + prizeID);
            var entryCount = $(entryCountField).val();
            var score = parseInt($('.scoreboard-text').text());

            if (score > 0) {
                entryCount++;
                $(entryCountField).val(entryCount);
            }

            updatePrizeEntryScoreboard();
        });


        $('.entry-count').blur(function (e) {
            updatePrizeEntryScoreboard();
            $(this).closest('form').submit();
        });

        // When I click the entry-count, select all text
        $('.entry-count').focus(function () {
            $(this).select();
        });

        $('#loading')
            .css("height", $(document).height())
            .hide()
            .ajaxStart(function () {
                $(this).center();
                $(this).fadeIn(200);
            })
            .ajaxStop(function () {
                $(this).delay(600);
                $(this).fadeOut(200);
            });

    };

    var updatePrizeEntryScoreboard = function () {
        var entrySum = 0;

        $('.entry-count').each(function () {
            entrySum += parseInt($(this).val());
        })

        var scoreField = $('.scoreboard-text');
        var allEntryPoints = parseInt($('#all-entry-points').val());

        /*
        alert("Scorefield value: " + $(scoreField).text());
        alert("Total entry points: " + allEntryPoints);
        alert("allocation sum: " + entrySum);
        */

        $(scoreField).text(allEntryPoints - entrySum);
    };


    initSilentAuctionButtons();

    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
        return this;
    }


});


