﻿$(function() {
    $('.bar *, #footer *, .titled').not('input.watermark').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " * ",
        fade: 250
    });

    $('input.watermark').each(function() {
        var el = $(this);
        var title = el.attr('title');

        el.click(function() {
            if (el.val() == title) {
                el.val('');
                el.removeClass('inputMask');
            }
        }).focus(function() {
            if (el.val() == title) {
                el.val('');
                el.removeClass('inputMask');
            }
        }).blur(function() {
            if (el.val() == '') {
                el.val(title);
            }
            if (el.val() == title) {
                el.addClass('inputMask');
            }
        });
        if (el.val() == '') {
            el.val(title);
        }
        if (el.val() != title) {
            el.removeClass('inputMask');
        }
        else {
            el.addClass('inputMask');
        }
    });

    $('form.ajaxForm').submit(function() {
        var form = $(this);

        form.find('.loading').show();
        form.find('.formFields, .message').hide();

        $.ajax({
            type: "POST",
            dataType: "json",
            url: form.attr('action'),
            cache: false,
            data: form.serialize(),
            error: function() {
                alert("Something unknown happened.  Please refresh the page.");
            },
            success: function(response) {
                var loading = form.find('.loading');
                var fields = form.find('.formFields');
                var message = form.find('.message');

                message.text(response.message);
                message.show();
                loading.hide();
                if (response.result != 'success') {
                    fields.show();
                }
            }
        });

        return false;
    });
    $('form.ajaxVoteForm').submit(function() {
        var form = $(this);

        form.find('.loading').show();
        form.find('.formFields, .message').hide();
        var id = form.find("#id").val();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: form.attr('action'),
            cache: false,
            data: form.serialize(),
            error: function() {
                alert("Something unknown happened.  Please refresh the page.");
            },
            success: function(response) {
                var loading = form.find('.loading');
                var message = form.find('.message');

                message.text(response.message);
                message.show();
                loading.hide();
                if (response.result == "success") {
                    $("#voteTotal").text(response.votes);
                    if ($("#leader" + id).length != 0)
                        $("#leader" + id).text(response.votes);
                }
            }
        });

        return false;
    });
    $('form .btn_submit').click(function() {
        $(this).parents('form').submit();
        return false;
    });
    $('form input.enterSubmit').keydown(function(e) {
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});
